haojin2 commented on a change in pull request #10413: [MXNET-160] Fix for issue 9062 URL: https://github.com/apache/incubator-mxnet/pull/10413#discussion_r179348107
########## File path: tests/python/unittest/test_random.py ########## @@ -276,29 +276,34 @@ def test_parallel_random_seed_setting(): @with_seed() def test_sample_multinomial(): - x = mx.nd.array([[0,1,2,3,4],[4,3,2,1,0]])/10.0 - dx = mx.nd.ones_like(x) - mx.contrib.autograd.mark_variables([x], [dx]) - # Adding rtol and increasing samples needed to pass with seed 2951820647 - samples = 5000 - with mx.autograd.record(): - y, prob = mx.nd.random.multinomial(x, shape=samples, get_prob=True) - r = prob * 5 - r.backward() - - y = y.asnumpy() - x = x.asnumpy() - for i in range(x.shape[0]): - - freq = np.bincount(y[i], minlength=5)/np.float32(samples)*x[i].sum() - mx.test_utils.assert_almost_equal(freq, x[i], rtol=0.20) - rprob = x[i][y[i]]/x[i].sum() - mx.test_utils.assert_almost_equal(np.log(rprob), prob.asnumpy()[i]) - - real_dx = np.zeros((5,)) - for j in range(samples): - real_dx[y[i][j]] += 5.0 / rprob[j] - mx.test_utils.assert_almost_equal(real_dx, dx.asnumpy()[i], rtol=1e-4) + for x in [mx.nd.array([[0,1,2,3,4],[4,3,2,1,0]])/10.0, mx.nd.array([0,1,2,3,4])/10.0]: + dx = mx.nd.ones_like(x) + mx.contrib.autograd.mark_variables([x], [dx]) + # Adding rtol and increasing samples needed to pass with seed 2951820647 + samples = 5000 + with mx.autograd.record(): + y, prob = mx.nd.random.multinomial(x, shape=samples, get_prob=True) + r = prob * 5 + r.backward() + + y = y.asnumpy() + x = x.asnumpy() + dx = dx.asnumpy() + if len(x.shape) is 1: + x = x.reshape((1, x.shape[0])) + dx = dx.reshape(1, dx.shape[0]) + y = y.reshape((1, y.shape[0])) + prob = prob.reshape((1, prob.shape[0])) + for i in range(x.shape[0]): + freq = np.bincount(y[i,:], minlength=5)/np.float32(samples)*x[i,:].sum() + mx.test_utils.assert_almost_equal(freq, x[i], rtol=0.20) + rprob = x[i][y[i]]/x[i].sum() + mx.test_utils.assert_almost_equal(np.log(rprob), prob.asnumpy()[i]) Review comment: Is the default not 1e-20? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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