haojin2 commented on a change in pull request #17234: Op Quantile/Percentile
[Numpy]
URL: https://github.com/apache/incubator-mxnet/pull/17234#discussion_r364661074
##########
File path: tests/python/unittest/test_numpy_op.py
##########
@@ -5680,6 +5680,49 @@ def test_np_share_memory():
assert not op(np.ones((5, 0), dtype=dt), np.ones((0, 3, 0),
dtype=adt))
+@with_seed()
+@use_np
+def test_np_quantile():
+ class TestQuantile(HybridBlock):
+ def __init__(self, axis=None, interpolation='linear', keepdims=False):
+ super(TestQuantile, self).__init__()
+ self._axis = axis
+ self._interpolation = interpolation
+ self._keepdims = keepdims
+
+ def hybrid_forward(self, F, a, q):
+ return F.np.quantile(a, q, axis=self._axis,
interpolation=self._interpolation, keepdims=self._keepdims)
+
+ flags = [True, False]
+ interpolation_options = ['linear', 'lower', 'higher', 'nearest',
'midpoint']
+ dtypes = [np.int32, np.int64, np.float16, np.float32, np.float64]
+ qtypes = [np.float32, np.float64]
+ tensor_shapes = [
+ ((2, 3), (), None),
+ ((2, 3, 4, 5), (), 3),
+ ((2, 3, 4), (3,), (0, 2)),
+ ((2, 3, 4), (3,), 1)
+ ]
+ for hybridize, keepdims, (a_shape, q_shape, axis), interpolation, dtype in
\
+ itertools.product(flags, flags, tensor_shapes, interpolation_options,
dtypes):
+ atol = 3e-4 if dtype == np.float16 else 1e-4
+ rtol = 3e-2 if dtype == np.float16 else 1e-2
+ test_quantile = TestQuantile(axis=axis, interpolation=interpolation,
keepdims=keepdims)
+ if hybridize:
+ test_quantile.hybridize()
+ a = np.random.uniform(-10.0, 10.0, size=a_shape).astype(dtype)
+ qtype = random.choice(qtypes)
+ q = np.random.uniform(0, 1.0, size=q_shape).astype(qtype)
+ np_out = _np.quantile(a.asnumpy(), q.asnumpy(), axis=axis,
interpolation=interpolation, keepdims=keepdims)
+ mx_out = test_quantile(a, q)
+ assert mx_out.shape == np_out.shape
+ assert_almost_equal(mx_out.asnumpy(), np_out, atol=atol, rtol=rtol)
+
+ mx_out = np.quantile(a, q, axis=axis, interpolation=interpolation,
keepdims=keepdims)
+ np_out = _np.quantile(a.asnumpy(), q.asnumpy(), axis=axis,
interpolation=interpolation, keepdims=keepdims)
+ assert_almost_equal(mx_out.asnumpy(), np_out, atol=atol, rtol=rtol)
Review comment:
also add test for percentile, or make this one a compound unit test
----------------------------------------------------------------
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