xidulu commented on a change in pull request #17302: [numpy]add op
random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r369386680
##########
File path: tests/python/unittest/test_numpy_op.py
##########
@@ -3323,21 +3322,96 @@ def hybrid_forward(self, F, loc, scale):
(3, 4, 5)
]
for hybridize in [False, True]:
- for dtype in dtypes:
- for ((shape1, shape2), out_shape) in zip(param_shape,
output_shapes):
- test_normal_grad = TestNormalGrad(out_shape)
- if hybridize:
- test_normal_grad.hybridize()
- loc = np.zeros(shape1)
- loc.attach_grad()
- scale = np.ones(shape2)
- scale.attach_grad()
- with mx.autograd.record():
- samples = test_normal_grad(loc, scale)
- samples.backward()
- assert loc.grad.shape == shape1
- assert scale.grad.shape == shape2
- assert_almost_equal(loc.grad.asnumpy().sum(),
_np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5)
+ for ((shape1, shape2), out_shape) in zip(param_shape, output_shapes):
+ test_normal_grad = TestNormalGrad(out_shape)
+ if hybridize:
+ test_normal_grad.hybridize()
+ loc = np.zeros(shape1)
+ loc.attach_grad()
+ scale = np.ones(shape2)
+ scale.attach_grad()
+ with mx.autograd.record():
+ samples = test_normal_grad(loc, scale)
+ samples.backward()
+ assert loc.grad.shape == shape1
+ assert scale.grad.shape == shape2
+ assert_almost_equal(loc.grad.asnumpy().sum(),
_np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5)
+
+
+@with_seed()
+@use_np
+def test_np_logistic_grad():
+ class TestLogisticGrad(HybridBlock):
+ def __init__(self, shape):
+ super(TestLogisticGrad, self).__init__()
+ self._shape = shape
+
+ def hybrid_forward(self, F, loc, scale):
+ return F.np.random.logistic(loc, scale, self._shape)
+
+ param_shape = [
+ [(3, 2), (3, 2)],
+ [(3, 2, 2), (3, 2, 2)],
+ [(3, 4, 5), (4, 1)],
+ ]
+ output_shapes = [
+ (3, 2),
+ (4, 3, 2, 2),
+ (3, 4, 5)
+ ]
+ for hybridize in [False, True]:
+ for ((shape1, shape2), out_shape) in zip(param_shape, output_shapes):
+ test_logistic_grad = TestLogisticGrad(out_shape)
+ if hybridize:
+ test_logistic_grad.hybridize()
+ loc = np.zeros(shape1)
+ loc.attach_grad()
+ scale = np.ones(shape2)
+ scale.attach_grad()
+ with mx.autograd.record():
+ samples = test_logistic_grad(loc, scale)
+ samples.backward()
+ assert loc.grad.shape == shape1
+ assert scale.grad.shape == shape2
+ assert_almost_equal(loc.grad.asnumpy().sum(),
_np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5)
+
+
+@with_seed()
+@use_np
+def test_np_gumbel_grad():
Review comment:
You could merge `test_np_normal_grad`,`test_np_gumbel_grad` and
`test_np_logistic_grad` into one test case, and iterate the corresponding
methods through a list and `getattr` like
https://github.com/apache/incubator-mxnet/blob/master/tests/python/unittest/test_numpy_op.py#L3411
----------------------------------------------------------------
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