ValueError: setting an array element with a sequence.
详细:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pydev\pydevd.py", line 1448, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/projects/0Fea/mxnet_test/loss_test.py", line 11, in <module>
prob = pred[np.arange(num_examples, dtype=np.int64), np.int64(label)]
ValueError: setting an array element with a sequence.
代码:
from mxnet import nd
import numpy as np
num_examples = 1
pred = nd.array([[0, 0, 0, 0, 0, 0, 0, 0, 0]])
label = nd.array([6])
#### need fix here #####
prob = pred[np.arange(num_examples, dtype=np.int64), np.int64(label)]
解决:先转成numpy再索引。
### fix
label = label.asnumpy().ravel()