piiswrong closed pull request #10468: [MXNET-300] Fix ndarray assignment bug 
with index -1
URL: https://github.com/apache/incubator-mxnet/pull/10468
 
 
   

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/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py
index 4677189aaac..34aff0f7eff 100644
--- a/python/mxnet/ndarray/ndarray.py
+++ b/python/mxnet/ndarray/ndarray.py
@@ -733,7 +733,7 @@ def _set_nd_basic_indexing(self, key, value):
                 vshape.append(dim_size)
             elif isinstance(slice_i, integer_types):
                 begin.append(slice_i)
-                end.append(slice_i+1)
+                end.append(slice_i+1 if slice_i != -1 else self.shape[i])
                 steps.append(1)
             else:
                 raise ValueError("basic indexing does not support index=%s of 
type=%s"
diff --git a/tests/python/unittest/test_ndarray.py 
b/tests/python/unittest/test_ndarray.py
index 6f2bd450776..a7cb037dcf4 100644
--- a/tests/python/unittest/test_ndarray.py
+++ b/tests/python/unittest/test_ndarray.py
@@ -92,6 +92,9 @@ def test_ndarray_setitem():
     x_np = np.zeros(shape, dtype=x.dtype)
     x_np[1] = 1
     assert same(x.asnumpy(), x_np)
+    x[-1] = 1
+    x_np[-1] = 1
+    assert same(x.asnumpy(), x_np)
 
     # short all-dim indexing
     x = mx.nd.zeros(shape)
@@ -100,12 +103,18 @@ def test_ndarray_setitem():
     x_np = np.zeros(shape, dtype=x.dtype)
     x_np[:, 1:3, 1] = val.asnumpy()
     assert same(x.asnumpy(), x_np)
+    x[:, 1:3, -1] = val
+    x_np[:, 1:3, -1] = val.asnumpy()
+    assert same(x.asnumpy(), x_np)
 
     x = mx.nd.zeros(shape)
-    x[:, 1:3, 1] = 1
+    x[:, 1:3, 1:2] = 1
     x_np = np.zeros(shape, dtype=x.dtype)
     x_np[:, 1:3, 1:2] = 1
     assert same(x.asnumpy(), x_np)
+    x[:, -3:-1, -2:-1] = 1
+    x_np[:, -3:-1, -2:-1] = 1
+    assert same(x.asnumpy(), x_np)
 
 
 @with_seed(0)
@@ -1109,20 +1118,29 @@ def convert(num):
                   (slice(None, None, -2), False),
                   (np_int(slice(None, None, -2), np.int32), False), 
(np_int(slice(None, None, -2), np.int64), False),
                   ((slice(None), slice(None), 1, 8), False),
+                  ((slice(None), slice(None), -1, 8), False),
+                  ((slice(None), slice(None), 1, -8), False),
+                  ((slice(None), slice(None), -1, -8), False),
                   (np_int((slice(None), slice(None), 1, 8)), False),
                   (np_int((slice(None), slice(None), 1, 8), np.int64), False),
                   ((slice(None), slice(None), 1, 8), False),
-                  (np_int((slice(None), slice(None), 1, 8)), False),
-                  (np_int((slice(None), slice(None), 1, 8), np.int64), False),
+                  (np_int((slice(None), slice(None), -1, -8)), False),
+                  (np_int((slice(None), slice(None), -1, -8), np.int64), 
False),
                   ((slice(None), 2, slice(1, 5), 1), False),
                   (np_int((slice(None), 2, slice(1, 5), 1)), False),
                   (np_int((slice(None), 2, slice(1, 5), 1), np.int64), False),
                   ((1, 2, 3), False),
                   (np_int((1, 2, 3)), False),
                   (np_int((1, 2, 3), np.int64), False),
+                  ((-1, -2, -3), False),
+                  (np_int((-1, -2, -3)), False),
+                  (np_int((-1, -2, -3), np.int64), False),
                   ((1, 2, 3, 4), True),
                   (np_int((1, 2, 3, 4)), True),
                   (np_int((1, 2, 3, 4), np.int64), True),
+                  ((-4, -3, -2, -1), True),
+                  (np_int((-4, -3, -2, -1)), True),
+                  (np_int((-4, -3, -2, -1), np.int64), True),
                   ((slice(None, None, -1), 2, slice(1, 5), 1), False),
                   (np_int((slice(None, None, -1), 2, slice(1, 5), 1)), False),
                   (np_int((slice(None, None, -1), 2, slice(1, 5), 1), 
np.int64), False),


 

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

Reply via email to