IndexError: invalid index of a 0-dim tensor. Use tensor.item()
in Python or tensor.item<T>()
in C++ to convert a 0-dim tensor to a number
`
plt.scatter(x.data.numpy(),y.data.numpy())
plt.plot(x.data.numpy(),prediction.data.numpy(),'r-',lw=5)
plt.text(0.5,0,'Loss=%.4f' % loss.data[0],fontdict={'size':20,'color': 'red'})
plt.pause(0.1)
`
修改建议:
去掉loss.data[0]后面的[0]。
在pytorch0.4版本之后,Variable和Tensor进行的合并。loss.data直接输出tensor值,不输出tensor的梯度信息``