You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/04/05 18:30:44 UTC

[GitHub] piiswrong closed pull request #10413: [MXNET-160] Fix for issue 9062

piiswrong closed pull request #10413: [MXNET-160] Fix for issue 9062
URL: https://github.com/apache/incubator-mxnet/pull/10413
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/operator/random/sample_multinomial_op.h b/src/operator/random/sample_multinomial_op.h
index 48b9897aa4c..898ca050891 100644
--- a/src/operator/random/sample_multinomial_op.h
+++ b/src/operator/random/sample_multinomial_op.h
@@ -70,10 +70,10 @@ inline bool SampleMultinomialOpShape(const nnvm::NodeAttrs& attrs,
   if (ishape.ndim() == 1) {
     if (param.shape.ndim()) {
       SHAPE_ASSIGN_CHECK(*out_attrs, 0, param.shape);
-      if (param.get_prob) SHAPE_ASSIGN_CHECK(*out_attrs, 0, param.shape);
+      if (param.get_prob) SHAPE_ASSIGN_CHECK(*out_attrs, 1, param.shape);
     } else {
       SHAPE_ASSIGN_CHECK(*out_attrs, 0, TShape(1));
-      if (param.get_prob) SHAPE_ASSIGN_CHECK(*out_attrs, 0, TShape(1));
+      if (param.get_prob) SHAPE_ASSIGN_CHECK(*out_attrs, 1, TShape(1));
     }
     return true;
   }
diff --git a/tests/python/unittest/test_random.py b/tests/python/unittest/test_random.py
index 5138728bfe6..03c9d5c1002 100644
--- a/tests/python/unittest/test_random.py
+++ b/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], atol=1e-5)
+
+            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[i, :], rtol=1e-4, atol=1e-5)
 
 # Test the generators with the chi-square testing
 @with_seed()


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services