MXNet NDArray索引报错求助:numpy.int64类型索引不支持
Hey there! I’ve run into this exact indexing quirk with older MXNet versions before, so let’s break down what’s happening and how to fix it.
The Root Cause
MXNet 1.0.0 (a fairly early release in the 1.x line) has limited compatibility with numpy scalar types like numpy.int64 when indexing multi-dimensional NDArrays. Unlike newer MXNet versions that automatically handle numpy types, this older release expects index values to be either Python native integers (int) or MXNet NDArray objects.
Practical Solutions
Here are three actionable fixes you can try, depending on your code context:
Convert numpy scalars to Python integers: Wrap any numpy.int64 index values with
int()to cast them to native Python integers. For example, if your code has something like:idx = np.int64(0) result = nd_array[idx]Change it to:
idx = np.int64(0) result = nd_array[int(idx)]Use MXNet NDArrays for indexing: If you’re working with batch indices or multi-dimensional indexing, convert your numpy index array to an MXNet NDArray first:
import mxnet as mx numpy_indices = np.array([0, 2, 4], dtype=np.int64) mx_indices = mx.nd.array(numpy_indices) result = nd_array[mx_indices]Replace numpy index arrays with Python lists: For small index sets, converting numpy arrays to Python lists can also resolve the issue:
numpy_indices = np.array([0, 2, 4], dtype=np.int64) result = nd_array[list(numpy_indices)]
Bonus Tip
If your project allows it, upgrading to a newer stable MXNet version (like 1.8.x) will eliminate this and many other compatibility headaches with numpy types. That said, the fixes above should work perfectly with your current 1.0.0 setup.
内容的提问来源于stack exchange,提问作者John Watts

