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/06/26 17:21:10 UTC

[GitHub] [incubator-mxnet] sxjscience commented on a change in pull request #14779: Fully connected, higher order grad

sxjscience commented on a change in pull request #14779: Fully connected, higher order grad
URL: https://github.com/apache/incubator-mxnet/pull/14779#discussion_r297779647
 
 

 ##########
 File path: tests/python/unittest/test_higher_order_grad.py
 ##########
 @@ -129,6 +131,44 @@ def check_second_order_unary(x, op, grad_grad_op):
     # Validate the gradients.
     assert_almost_equal(expected_grad_grad, x.grad.asnumpy())
 
+class RandomShapes(object):
+    def __init__(self, dim):
+        self.dim = dim
+        self.curdim = 1
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        return self.__next__()
+
+    def __next__(self):
+        if self.curdim > self.dim:
+            raise StopIteration
+        shape = rand_shape_nd(self.curdim)
+        print(shape)
+        x = nd.random.normal(shape=shape)
+        self.curdim += 1
+        return x
+
+
+@with_seed()
+def test_dense_backward():
+    import mxnet.autograd as ag
+    import mxnet.ndarray as nd
+    for x in RandomShapes(5):
+        net = gluon.nn.Sequential()
+        with net.name_scope():
+            #net.add(gluon.nn.Dense(1, in_units=x.shape[1]))
+            net.add(gluon.nn.Dense(1))
+        net.initialize(mxnet.initializer.Constant(.5))
+        x.attach_grad()
+        with ag.record():
+            y = net.forward(x)
+            x_grad = ag.grad(y, x, create_graph=True, retain_graph=True)[0]
 
 Review comment:
   We'd better do something like the following to check the correctness.
   ```python
           with ag.record():
               y = net.forward(x)
               x_grad = ag.grad(y, x, create_graph=True, retain_graph=True)[0]
               z = (random_multiplier * x_grad).sum()
           z.backward()
   ```
   Then we check whether `x.grad` is correct.

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