tingying2020 commented on a change in pull request #15838: [numpy] nonzero
URL: https://github.com/apache/incubator-mxnet/pull/15838#discussion_r314590829
 
 

 ##########
 File path: tests/python/gpu/test_operator_gpu.py
 ##########
 @@ -2331,6 +2331,47 @@ def test_math():
             for op in ops:
                 run_math(op, shape, dtype, check_value=check_value)
 
+@with_seed()
+def test_np_nonzero():
+    class TestNonzero(HybridBlock):
+        def __init__(self):
+            super(TestNonzero, self).__init__()
+            
+        def hybrid_forward(self, F, x):
+            return F.npx.nonzero(x)
+
+    types = ['int32', 'int64', 'float64', 'float32', 'float16']
+    for hybridize in [True, False]:
+        for shape in [(),
+                      (1,),
+                      (1, 1),
+                      (1, 2, 3),
+                      (1, 0),
+                      (2, 0, 3)
+                      ]:
+            for oneType in types:
+                if oneType is 'float16':
+                    rtol=1e-2
+                    atol=1e-2
+                else:
+                    rtol=1e-3
+                    atol=1e-5
+                test_nonzero = TestNonzero()
+                if hybridize:
+                    test_nonzero.hybridize()
+                x = rand_ndarray(shape, dtype=oneType).as_np_ndarray()
+                np_out = np.nonzero(x.asnumpy())
+                np_out = np.transpose(np_out)
+                mx_out = test_nonzero(x)
+                assert mx_out.shape == np_out.shape
+                assert_almost_equal(mx_out.asnumpy(), np_out, rtol, atol)
+
+                # Test imperative once again
+                mx_out = mx.npx.nonzero(x)
+                np_out = np.nonzero(x.asnumpy())
+                np_out = np.transpose(np_out)
+                assert_almost_equal(mx_out.asnumpy(), np_out, rtol, atol)
+
 
 Review comment:
   Done.

----------------------------------------------------------------
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