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

 ##########
 File path: tests/python/unittest/test_higher_order_grad.py
 ##########
 @@ -129,6 +135,83 @@ 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, startdim=1):
+        self.dim = dim
+        self.curdim = startdim
+
+    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)
+        x = nd.random.normal(shape=shape)
+        self.curdim += 1
+        return x
+
+
+@with_seed()
+def test_dense_backward():
+    for x in RandomShapes(4,2):
+        net = gluon.nn.Sequential()
+        with net.name_scope():
+            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(heads=y, variables=x, create_graph=True, 
retain_graph=True)[0]
+        x_grad.backward()
+        same(x.grad, nd.zeros(4))
+
+        with ag.record():
+            y = net.forward(x)
+            x_grad = ag.grad(heads=y, variables=x, create_graph=True, 
retain_graph=True)[0]
+            random_multiplier = nd.random.uniform_like(x_grad)
+            z = (random_multiplier * x_grad).sum()
+        z.backward()
+        same(x.grad, nd.zeros(4))
+
+        with ag.record():
+            y = net.forward(x)
+            x_grad_0 = ag.grad(heads=y, variables=x, create_graph=True, 
retain_graph=True)[0]
+        x_grad_grad_0 = x.grad
+
+        w_0 = list(net.collect_params().values())[0].data()
+        h_w = nd.ones_like(w_0) * 0.01
+        net.initialize(mxnet.initializer.Constant(w_0 + h_w), 
force_reinit=True)
+        w_1 = list(net.collect_params().values())[0].data()
+        with ag.record():
+            y = net.forward(x)
+            x_grad_1 = ag.grad(heads=y, variables=x, create_graph=True, 
retain_graph=True)[0]
+        x_grad_1.backward()
+        x_grad_grad_1 = x.grad
+        ok_(not np.array_equal(x_grad_0, x_grad_1))
+        ok_(np.array_equal(x_grad_grad_0, x_grad_grad_1))
+
+        w = list(net.collect_params().values())[0].data()
+        with ag.record():
+            y = net.forward(x)
+            w_grad_0 = ag.grad(heads=y, variables=w, create_graph=True, 
retain_graph=True)[0]
+        w_grad_0.backward()
+        w_grad_grad_0 = w.grad
+
+        x = x + nd.ones_like(x) * 0.01
+        with ag.record():
 
 Review comment:
   What are you trying to test here? Maybe break the `with` blocks in this 
method with multiple methods so we know the purpose of each test?

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


With regards,
Apache Git Services

Reply via email to