From 1c1cc028a46bb27a2535ecc5adeccbd47307abc8 Mon Sep 17 00:00:00 2001 From: Hao Jin Date: Fri, 9 Aug 2019 21:37:25 +0000 Subject: [PATCH] add coverage for npx.relu and npx.sigmoid --- tests/python/unittest/test_numpy_op.py | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 818e0948dc65..574dd3b79d4a 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -224,6 +224,80 @@ def hybrid_forward(self, F, a, *args, **kwargs): check_unary_func(func, ref_grad, shape, low, high) +@with_seed() +@use_np +def test_npx_relu(): + def np_relu(x): + return _np.maximum(x, 0.0) + def np_relu_grad(x): + return 1.0 * (x > 0.0) + + class TestReLU(HybridBlock): + def __init__(self): + super(TestReLU, self).__init__() + + def hybrid_forward(self, F, a): + return F.npx.relu(a) + + shapes = [(), (2, 3, 4), (2, 0, 3), (1, 0, 0)] + for hybridize in [True, False]: + for shape in shapes: + test_relu = TestReLU() + if hybridize: + test_relu.hybridize() + x = rand_ndarray(shape).as_np_ndarray() + x.attach_grad() + np_out = np_relu(x.asnumpy()) + with mx.autograd.record(): + mx_out = test_relu(x) + assert mx_out.shape == np_out.shape + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5) + mx_out.backward() + np_backward = np_relu_grad(x.asnumpy()) + assert_almost_equal(x.grad.asnumpy(), np_backward, rtol=1e-3, atol=1e-5) + + mx_out = npx.relu(x) + np_out = np_relu(x.asnumpy()) + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5) + + +@with_seed() +@use_np +def test_npx_sigmoid(): + def np_sigmoid(x): + return _np.divide(1.0, (1.0 + _np.exp(-x))) + def np_sigmoid_grad(ya): + return ya * (1 - ya) + + class TestSigmoid(HybridBlock): + def __init__(self): + super(TestSigmoid, self).__init__() + + def hybrid_forward(self, F, a): + return F.npx.sigmoid(a) + + shapes = [(), (2, 3, 4), (2, 0, 3), (1, 0, 0)] + for hybridize in [True, False]: + for shape in shapes: + test_sigmoid = TestSigmoid() + if hybridize: + test_sigmoid.hybridize() + x = rand_ndarray(shape).as_np_ndarray() + x.attach_grad() + np_out = np_sigmoid(x.asnumpy()) + with mx.autograd.record(): + mx_out = test_sigmoid(x) + assert mx_out.shape == np_out.shape + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5) + mx_out.backward() + np_backward = np_sigmoid_grad(np_out) + assert_almost_equal(x.grad.asnumpy(), np_backward, rtol=1e-3, atol=1e-5) + + mx_out = npx.sigmoid(x) + np_out = np_sigmoid(x.asnumpy()) + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5) + + if __name__ == '__main__': import nose nose.runmodule()