Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

nd.slice errors out when end=-1 and step=-1 #14105

Closed
mseth10 opened this issue Feb 9, 2019 · 7 comments
Closed

nd.slice errors out when end=-1 and step=-1 #14105

mseth10 opened this issue Feb 9, 2019 · 7 comments

Comments

@mseth10
Copy link
Contributor

mseth10 commented Feb 9, 2019

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
@mxnet-label-bot
Copy link
Contributor

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

@mseth10
Copy link
Contributor Author

mseth10 commented Feb 9, 2019

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

@reminisce
Copy link
Contributor

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)

@anirudh2290
Copy link
Member

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

@mseth10
Copy link
Contributor Author

mseth10 commented Mar 14, 2019

Opened an issue with numpy for the same numpy/numpy#13123

@mseth10
Copy link
Contributor Author

mseth10 commented Mar 14, 2019

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

@mseth10
Copy link
Contributor Author

mseth10 commented Mar 14, 2019

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.

@mseth10 mseth10 closed this as completed Mar 14, 2019
mseth10 added a commit to mseth10/incubator-mxnet that referenced this issue Mar 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.