endvroy commented on a change in pull request #15468: Numpy compatible diagflat operator URL: https://github.com/apache/incubator-mxnet/pull/15468#discussion_r303277772
########## File path: tests/python/unittest/test_numpy_op.py ########## @@ -28,6 +28,59 @@ import random +@with_seed() [email protected]_np_shape +def test_np_diagflat(): + @npx.use_np_shape + class TestDiagflat(HybridBlock): + def __init__(self, k): + super(TestDiagflat, self).__init__() + self.k = k + + def hybrid_forward(self, F, a): + return F.np.diagflat(a, self.k) + + for hybridize in [True, False]: + for dtype in ['int32', 'float16', 'float32', 'float64']: + for shape_x in [(1,), # single element + (3, 3), # square + (), # scalar + (3, 0, 2), # zero-dim + (3, 4, 5), + ]: + for k in range(-5, 6): + if dtype == 'float16': + rtol = atol = 1e-2 + else: + rtol = atol = 1e-5 + + test_diagflat = TestDiagflat(k) + if hybridize: + test_diagflat.hybridize() + + x = rand_ndarray(shape_x, dtype=dtype).as_np_ndarray() + x.attach_grad() + + np_out = _np.diagflat(x.asnumpy(), k) + with mx.autograd.record(): + mx_out = test_diagflat(x) + assert mx_out.shape == np_out.shape + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, + atol=atol) + # test grad + mx_out.backward() + assert_almost_equal(x.grad.asnumpy(), _np.ones(x.shape), + rtol=1e-3, atol=1e-5) + + # test imperative once again + mx_out = np.diagflat(x, k) + np_out = _np.diagflat(x.asnumpy(), k) + assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, + atol=atol) + Review comment: fixed ---------------------------------------------------------------- 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
