szha closed pull request #11661: [MXNET-670] shape_array and size_array operator is non-differentiable URL: https://github.com/apache/incubator-mxnet/pull/11661
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/src/operator/tensor/elemwise_unary_op_basic.cc b/src/operator/tensor/elemwise_unary_op_basic.cc index 8e64a7f6366..929bc7426d5 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 faaa45efdb1..2aa2fa4f11c 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -827,19 +827,37 @@ def fsigmoid(a): 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(): ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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