wkcn commented on issue #18539:
URL: 
https://github.com/apache/incubator-mxnet/issues/18539#issuecomment-643003657


   Another solution: add `mx.sym.MakeLoss`
   ```python
   import mxnet as mx
   
   a = mx.sym.Variable('a')
   b = mx.sym.Variable('b')
   # c is not a loss function symbol
   c = mx.sym.MakeLoss(2 * a + b)
   args = {'a': mx.nd.array([1,2]), 'b':mx.nd.array([2,3])}
   args_grad = {'a': mx.nd.zeros((2)), 'b': mx.nd.zeros((2))}
   texec = c.bind(ctx=mx.cpu(), args=args, args_grad=args_grad)
   out = texec.forward(is_train=True)[0].copy()
   print(out.asnumpy())
   # answer [ 4.  7.]
   texec.backward()
   print(texec.grad_arrays[0].asnumpy())
   # answer[ 2.  2.]
   print(texec.grad_arrays[1].asnumpy())
   # answer [ 1.  1.]
   ```


----------------------------------------------------------------
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