关于pytorch的invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python

在学习神经网络训练MINIST数据集时Pycharm爆出这个问题,问题代码如下:

train_loss+=loss.data[0]

分析发现原因在于PyTorch0.3.1版本会使用这种写法,但在更高版本里已经不再支持,所以需要修改如下:

train_loss+=loss.item()

版权声明:本文为qq_41911710原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。