Incubator-mxnet: nd.slice errors out when end=-1 and step=-1

Created on 9 Feb 2019  路  7Comments  路  Source: apache/incubator-mxnet

Description

For mxnet.ndarray.slice(data, begin, end), if end=-1 and step=-1, it does not return a reversed tensor. Instead, it gives an invalid data error.

Minimum reproducible environment

>>> import mxnet.ndarray as nd
>>> a = nd.normal(shape=(4))
>>> a
[2.2122064 0.7740038 1.0434405 1.1839255]
<NDArray 4 @cpu(0)>a = nd.normal(shape=(4))
>>> a1 = nd.slice(a, begin=2, end=-1, step=-1)

Expected output: [1.0434405 0.7740038 2.2122064]
Outputs:

Traceback (most recent call last):
  File "test_slice.py", line 4, in <module>
    a1 = nd.slice(a, begin=2, end=-1, step=-1)
  File "<string>", line 86, in slice
  File "/home/ubuntu/workspace/mxnet0/python/mxnet/_ctypes/ndarray.py", line 92, in _imperative_invoke
    ctypes.byref(out_stypes)))
  File "/home/ubuntu/workspace/mxnet0/python/mxnet/base.py", line 252, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [13:57:39] /home/ubuntu/workspace/mxnet0/src/operator/tensor/./matrix_op-inl.h:716: Check failed: e < b (3 vs. 2) slicing with begin=[0]=2, end[0]=3, and step[0]=-1 is invalid

```

Bug NDArray Operator Python

Most helpful comment

According to numpy, this should return an empty array. Since MXNet does not support emtpy array in computation, it simply throws exception. So there is nothing wrong for now until we support empty arrays which has been planned.

>>> import numpy as np
>>> data = np.random.uniform(size=(4,))
>>> data
array([0.19584417, 0.05437645, 0.73802081, 0.20259332])
>>> data[2:-1:-1]
array([], dtype=float64)

All 7 comments

Hey, this is the MXNet Label Bot.
Thank you for submitting the issue! I will try and suggest some labels so that the appropriate MXNet community members can help resolve it.
Here are my recommended labels: Bug

@mxnet-label-bot add [Python, NDArray, Operator, Bug]

According to numpy, this should return an empty array. Since MXNet does not support emtpy array in computation, it simply throws exception. So there is nothing wrong for now until we support empty arrays which has been planned.

>>> import numpy as np
>>> data = np.random.uniform(size=(4,))
>>> data
array([0.19584417, 0.05437645, 0.73802081, 0.20259332])
>>> data[2:-1:-1]
array([], dtype=float64)

@reminisce numpy behavior seems to be inconsistent . For example

>>> data = np.random.uniform(size=(4,))
>>> data
array([0.6677262 , 0.29855998, 0.32659663, 0.15015646])
>>> data[2:0:-1]
array([0.32659663, 0.29855998])
>>> data[2:-1:-1]
array([], dtype=float64)
>>>

end = 0 returns the two element array till index 1. end = -1 instead of returning 3 element array returns empty array. Ideally it should return the three element array. Otherwise there is no way to include the 0th index with negative step.

Opened an issue with numpy for the same https://github.com/numpy/numpy/issues/13123

According to numpy, -1 corresponds to last element of array.
To include element 0, slice down to None, or just omit that bound.

>>> data = np.random.uniform(size=(4,))
>>> data
array([0.39041899, 0.65619514, 0.79147856, 0.71647746])
>>> data[2:None:-1]
array([0.79147856, 0.65619514, 0.39041899])
>>> data[2::-1]
array([0.79147856, 0.65619514, 0.39041899])

Closing this issue as the behavior is consistent with numpy.

Here is how we can get the element at 0-th index with MXNet NDArray.

>>> import mxnet.ndarray as nd
>>> a = nd.normal(shape=(4))
>>> a
[2.2122064 0.7740038 1.0434405 1.1839255]
<NDArray 4 @cpu(0)>
>>> nd.slice(a, begin=2, end=(None,), step=-1)
[1.0434405 0.7740038 2.2122064]
<NDArray 3 @cpu(0)>

Note: None needs to be given as part of a tuple.

Was this page helpful?
0 / 5 - 0 ratings