This is an automated email from the ASF dual-hosted git repository.

zhasheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new d25fdb7  [MXNET-670] shape_array and size_array operator is 
non-differentiable (#11661)
d25fdb7 is described below

commit d25fdb7068a66944b658ca8a8e913904d03d8e4e
Author: Jian Guo <13162287+ijk...@users.noreply.github.com>
AuthorDate: Wed Jul 11 20:44:00 2018 -0700

    [MXNET-670] shape_array and size_array operator is non-differentiable 
(#11661)
    
    * zero gradient for size_array, shape_array
    
    * add op test for shape, size
---
 src/operator/tensor/elemwise_unary_op_basic.cc |  2 ++
 tests/python/unittest/test_operator.py         | 34 ++++++++++++++++++++------
 2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/operator/tensor/elemwise_unary_op_basic.cc 
b/src/operator/tensor/elemwise_unary_op_basic.cc
index 8e64a7f..929bc74 100644
--- a/src/operator/tensor/elemwise_unary_op_basic.cc
+++ b/src/operator/tensor/elemwise_unary_op_basic.cc
@@ -417,6 +417,7 @@ Example::
 .set_num_inputs(1)
 .set_num_outputs(1)
 .set_attr<FCompute>("FCompute<cpu>", ShapeComputeCPU)
+.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes)
 .set_attr<nnvm::FInferShape>("FInferShape",
   [](const nnvm::NodeAttrs& attrs,
      std::vector<TShape> *in_attrs,
@@ -466,6 +467,7 @@ Example::
 .set_num_inputs(1)
 .set_num_outputs(1)
 .set_attr<FCompute>("FCompute<cpu>", SizeComputeCPU)
+.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes)
 .set_attr<nnvm::FInferShape>("FInferShape",
   [](const nnvm::NodeAttrs& attrs,
      std::vector<TShape> *in_attrs,
diff --git a/tests/python/unittest/test_operator.py 
b/tests/python/unittest/test_operator.py
index 6529601..44fffdd 100644
--- a/tests/python/unittest/test_operator.py
+++ b/tests/python/unittest/test_operator.py
@@ -828,19 +828,37 @@ def test_sigmoid():
 def test_shape_array():
     for i in range(1,6):
         shape = rand_shape_nd(i)
-        x = np.random.ranf(shape)
-        y = mx.nd.shape_array(mx.nd.array(x))
-        expected_y = np.shape(x)
-        same(y.asnumpy(), expected_y)
+        x = mx.sym.var('x')
+        y = mx.sym.shape_array(x)
+        xa = mx.nd.array(np.random.ranf(shape))
+        xg = mx.nd.empty(xa.shape)
+        ya = np.shape(xa)
+        yg = mx.nd.ones(ya)
+        exe = y.bind(ctx=default_context(), args={'x': xa},
+                     args_grad={'x': xg})
+        exe.forward(is_train=True)
+        exe.backward([yg])
+        yo = exe.outputs[0].asnumpy()
+        same(yo, ya)
+        assert_almost_equal(xg.asnumpy(), np.zeros_like(xg.asnumpy()))
 
 @with_seed()
 def test_size_array():
     for i in range(1,6):
         shape = rand_shape_nd(i)
-        x = np.random.ranf(shape)
-        y = mx.nd.size_array(mx.nd.array(x))
-        expected_y = np.size(x)
-        same(y.asnumpy(), expected_y)
+        x = mx.sym.var('x')
+        y = mx.sym.size_array(x)
+        xa = mx.nd.array(np.random.ranf(shape))
+        xg = mx.nd.empty(xa.shape)
+        ya = np.size(xa)
+        yg = mx.nd.ones(ya)
+        exe = y.bind(ctx=default_context(), args={'x': xa},
+                     args_grad={'x': xg})
+        exe.forward(is_train=True)
+        exe.backward([yg])
+        yo = exe.outputs[0].asnumpy()
+        same(yo, ya)
+        assert_almost_equal(xg.asnumpy(), np.zeros_like(xg.asnumpy()))
 
 @with_seed()
 def test_hard_sigmoid():

Reply via email to