mathephysicist opened a new issue #19021:
URL: https://github.com/apache/incubator-mxnet/issues/19021


   ## Description
   Unexpected gradient results when using .backward() with an array which does 
not match the functions input.
   
   
   ## To Reproduce
   `
   # z= np.array([[ 6. 12.],[35. 48.]])
   x = np.array([[2.0,3.0],[5,6]])
   y = np.array([[3.0,4.0],[7,8]])
   x.attach_grad()
   y.attach_grad()
   
   #Expected Result
   with autograd.record():
       z = np.sum(x*y)
   z.backward()
   print(1.5*x.grad)
   print(1.5*y.grad)
   
   #Expected Result
   with autograd.record():
       z = x*y
   z.backward()
   print(1.5*x.grad)
   print(1.5*y.grad)
   
   
   #Expected Result
   with autograd.record():
       z = np.sum(x*y)
   z.backward(np.array([1.5]))
   print(x.grad)
   print(y.grad)
   
   # Unexpected Result
   with autograd.record():
       z = x*y
   z.backward(np.array([1.5]))
   print(x.grad)
   print(y.grad)
   
   #Unexpected Result
   with autograd.record():
       z = x*y
   z.backward(np.array([1.5,1.5]))
   print(x.grad)
   print(y.grad)
   
   #Unexpected Result
   with autograd.record():
       z = x*y
   z.backward(np.array([[1.5],[1.5]]))
   print(x.grad)
   print(y.grad)
   `
   
   ### Steps to reproduce
   Run the code above
   
   ## What have you tried to solve it?
   Make sure the the input to z.backward() has the same shape as z
   
   ## Environment
   I don't think this is relevant right now. I am using !pip install --pre 
mxnet-cu102 -f https://dist.mxnet.io/python -q to install my environment.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to