PS: I checked my previous post and the code I wrote looks correct:
> import numpy as np
> from sympy import *
>
> n, d, n2, d2 = 5, 7, 4, 3
>
> x = np.random.randn(n, d)
> y = np.random.randn(n, d2)
>
> w1 = MatrixSymbol("l", 7, 4)
> w1 = Matrix(w1)
>
> w2 = MatrixSymbol("p", 4, 3)
> w2 = Matrix(w2)
>
> h2 = x * w1
> predicted = h2 * w2
>
> loss2 = Matrix(np.square(predicted - y))
On Sunday, May 31, 2020 at 6:56:06 PM UTC+2, Giuseppe G. A. Celano wrote:
>
> Hi Lee,
>
> Yes, it is a mistake. I meant:
>
> x = np.random.randn(n, d)
> y = np.random.randn(n, d2)
>
>
>
> On Sunday, May 31, 2020 at 3:18:56 PM UTC+2, S.Y. Lee wrote:
>>
>> It's better work on matrix expressions
>> I also don't think that x, y should be numeric matrices if they are
>> random matrices.
>>
>> Now, the problem is the matrix derivative is computed wrong when it's
>> derived with it's own elements
>> But when I tried with https://github.com/sympy/sympy/pull/17232 and
>> symbolized all the stuff
>>
>> import numpy as np
>> from sympy import *
>>
>> n, d, n2, d2 = 5, 7, 4, 3
>>
>> x = MatrixSymbol('x', n, d)
>> y = MatrixSymbol('y', n, d2)
>>
>> w1 = MatrixSymbol("l", 7, 4)
>> w2 = MatrixSymbol("p", 4, 3)
>>
>> h2 = x * w1
>> predicted = h2 * w2
>> HadamardPower(predicted - y, 2).diff(x[0, 0])
>> I see it gives consistent result with computations with explicit matrix.
>> Although I can't easily read the formula.
>>
>>
>> On Sunday, May 31, 2020 at 8:12:11 PM UTC+9, Oscar wrote:
>>>
>>> On Sun, 31 May 2020 at 03:42, Giuseppe G. A. Celano
>>> <[email protected]> wrote:
>>> >
>>> > I am trying to use very small matrices. Is there any way to calculate
>>> the partial derivatives of "loss2" below?
>>> >
>>> > import numpy as np
>>> > from sympy import *
>>> >
>>> > n, d, n2, d2 = 5, 7, 4, 3
>>> >
>>> > x = np.random.randn(n, d)
>>> > y = np.random.randn(n, d2)
>>> >
>>> > w1 = MatrixSymbol("l", 7, 4)
>>> > w1 = Matrix(w1)
>>> >
>>> > w2 = MatrixSymbol("p", 4, 3)
>>> > w2 = Matrix(w2)
>>> >
>>> > h2 = x * w1
>>> > predicted = h2 * w2
>>> >
>>> > loss2 = Matrix(np.square(predicted - y))
>>>
>>> What do you want to differentiate with respect to?
>>>
>>> You can use loss2.diff(w1[0, 0]) to differentiate with respect to the
>>> upper left entry of w1.
>>>
>>> --
>>> Oscar
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/0b94756b-773d-4821-bf4f-408ece4b735e%40googlegroups.com.