IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

报错原因:数组是0维,但取了一维数据

报错代码:loss.data.cpu().numpy()[0]

寻找解决办法:利用 np.shape(loss.data.cpu().numpy()查看,发现输出为(),这里就发现“loss.data.cpu().numpy()”是0维的,但是这里却取了0维的第一个数“loss.data.cpu().numpy()[0]”,所以是错误的。

解决方法:(1)直接取该0维的数(通过该方法已解决)

loss.data.cpu().numpy()

(2)利用上述的reshape函数,将这个数组转为1维的

loss.data.cpu().numpy().reshape(1,-1)[0] 

本文转自IndexError: too many indices for array_HealthScience的博客-CSDN博客 

你可能感兴趣的:(Pytorch,numpy,python,机器学习,pytorch)