You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/03/14 04:33:28 UTC

[GitHub] [incubator-mxnet] szha commented on a change in pull request #14427: Copy the gradient to the new context when as_in_context() is called

szha commented on a change in pull request #14427: Copy the gradient to the new context when as_in_context() is called
URL: https://github.com/apache/incubator-mxnet/pull/14427#discussion_r265416734
 
 

 ##########
 File path: python/mxnet/ndarray/ndarray.py
 ##########
 @@ -2086,12 +2088,17 @@ def copyto(self, other):
             if other.handle is self.handle:
                 warnings.warn('You are attempting to copy an array to itself', RuntimeWarning)
                 return False
-            return _internal._copyto(self, out=other)
+            data = _internal._copyto(self, out=other)
         elif isinstance(other, Context):
             hret = NDArray(_new_alloc_handle(self.shape, other, True, self.dtype))
-            return _internal._copyto(self, out=hret)
+            data = _internal._copyto(self, out=hret)
         else:
             raise TypeError('copyto does not support type ' + str(type(other)))
+        if copy_grad:
+            grad = self.grad
+            if grad is not None:
+                setattr(data, '_old_grad', grad.copyto(other, copy_grad=False))
 
 Review comment:
   This seems rather obscure as users aren't aware that this field exists. If user explicitly chose to do the copy, I think it's ok to set it in `.grad` attribute

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services