Slicing axis in NDArray


#1

Hello,

How can I mimic Numpy behavior for slicing the axis fully in MXNet?

# Numpy
import numpy as np
data_np = np.array([[1,2,3],[4,5,6],[7,8,9]])
data_np.shape
(3,3)
data_np[:,-1]
array([3, 6, 9])

# MXNet
import mxnet as mx
data_mx = mx.nd.array([[1,2,3],[4,5,6],[7,8,9]])
data_mx.shape
(3,3)
data_mx[:, -1] # Does not work

How can I achieve that behavior?

I tried various options using slice and slice_axis, couldn’t come up with a solution that can slice the full axis. Any suggestions?

Thanks in advance.


#2

@skm can you precise what result you are expecting? And what do you mean by “does not work”?

data_mx = mx.nd.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

data_mx.shape
(3, 3)

data_mx[:, -1]
[ 3.  6.  9.]
<NDArray 3 @cpu(0)>
``

#3

Hi Thomas,
Thanks for your reply. Below is the error I get:

>>> data_mx = mx.nd.array([[1,2,3],[4,5,6],[7,8,9]])
>>> data_mx[:, -1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/skm/anaconda3/lib/python3.6/site-packages/mxnet/ndarray/ndarray.py", line 506, in __getitem__
    return self._get_nd_basic_indexing(key)
  File "/Users/skm/anaconda3/lib/python3.6/site-packages/mxnet/ndarray/ndarray.py", line 814, in _get_nd_basic_indexing
    sliced_nd = op.slice(self, begin, end, step)
  File "<string>", line 86, in slice
  File "/Users/skm/anaconda3/lib/python3.6/site-packages/mxnet/_ctypes/ndarray.py", line 92, in _imperative_invoke
    ctypes.byref(out_stypes)))
  File "/Users/skm/anaconda3/lib/python3.6/site-packages/mxnet/base.py", line 149, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [08:23:42] src/operator/tensor/./matrix_op-inl.h:677: Check failed: b < e (2 vs. 0) slicing with begin=[1]=2, end[1]=0, and step[1]=1 is invalid

Stack trace returned 9 entries:
[bt] (0) 0   libmxnet.so                         0x0000000102ba6fdb libmxnet.so + 65499
[bt] (1) 1   libmxnet.so                         0x0000000102ba6d6f libmxnet.so + 64879
[bt] (2) 2   libmxnet.so                         0x0000000103dc849e libmxnet.so + 19076254
[bt] (3) 3   libmxnet.so                         0x0000000103d8fbd1 libmxnet.so + 18844625
[bt] (4) 4   libmxnet.so                         0x0000000103f0699a MXNDListFree + 432010
[bt] (5) 5   libmxnet.so                         0x0000000103f05494 MXNDListFree + 426628
[bt] (6) 6   libmxnet.so                         0x0000000103e74e5e MXCustomFunctionRecord + 17886
[bt] (7) 7   libmxnet.so                         0x0000000103e75d10 MXImperativeInvokeEx + 176
[bt] (8) 8   _ctypes.cpython-36m-darwin.so       0x0000000101bfa2b7 ffi_call_unix64 + 79

The expectation is a single dimension array like you showed in your example similar to numpy behavior.

I am trying to do with Symbols. So I was trying to mimic this behavior via slice / slice_axis APIs.


#4

What version of MXNet do you have installed?

I am running your code, but it works fine. The output of my NDArray is what you mentioned you want it to be:

[3. 6. 9.]
<NDArray 3 @cpu(0)>

I am using 1.2.0 version.


#5

It was an older version of MXNet. Works as expected.