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

 ##########
 File path: tests/python/unittest/test_higher_order_grad.py
 ##########
 @@ -210,6 +217,168 @@ def check_second_order_unary(x, op, grad_grad_op, 
rtol=None, atol=None):
                         x.grad.asnumpy(), rtol=rtol, atol=atol)
 
 
+def arange_shape_like(y):
+    shape = y.shape
+    nelems = reduce(mul, shape)
+    x = nd.arange(nelems).reshape(shape)
+    return x
+
+
+class NDArrayGenerator(object):
+    def __init__(self, dim, startdim=1):
+        self.dim = dim
+        self.curdim = startdim
+
+    def __iter__(self):
+        return self
+
+    @staticmethod
+    def gen(dimensions):
+        shape = rand_shape_nd(dimensions, 4)
+        nelems = reduce(mul, shape)
+        x = nd.arange(nelems).reshape(shape)
+        return x
+
+    def next(self):
+        return self.__next__()
+
+    def __next__(self):
+        if self.curdim > self.dim:
+            raise StopIteration
+        x = NDArrayGenerator.gen(self.curdim)
+        self.curdim += 1
+        return x
+
+
+def flatten2d_right(x):
+    s_0 = x.shape[0]
+    s_1 = reduce(mul, x.shape[1:])
+    return x.reshape((s_0, s_1))
+
+
+def flatten2d_left(x):
+    s_0 = reduce(mul, x.shape[:-1])
+    s_1 = x.shape[-1]
+    return x.reshape((s_0, s_1))
+
+
+@with_seed()
+def test_dense_backward_flatten():
+    print("2nd order gradient for Fully Connected, flatten=True")
+    for x in NDArrayGenerator(4,2):
+        hidden = random.randrange(1, 4)
+        net = gluon.nn.Sequential()
+        with net.name_scope():
+            net.add(gluon.nn.Dense(hidden, flatten=True))
+        net.initialize(mxnet.initializer.Constant(.5))
+        x.attach_grad()
+        with autograd.record():
+            y = net.forward(x)
+            o_y = arange_shape_like(y)  # head gradient of y
+            params = [p.data() for p in net.collect_params().values()]
+            w = params[0]
+            b = params[1]
+            print("Checking y ({}) = x({}) * w^T({}) + b({})".format(y.shape, 
x.shape, w.shape, b.shape))
 
 Review comment:
   > Output is ignored by nosetests unless specifically enabled. It's ok to 
leave print in unit tests. Check nosetest for more detail.
   
   Reference? For most mxnet python test, not adding print statement has been a 
general practice.

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


With regards,
Apache Git Services

Reply via email to