最近空闲下来,终于有机会从繁忙的工作中脱身做些自己的事情。在新买的电脑上安装了Pytorch,跑了一下研究生期间写的一套程序。发现程序报了以前没有出现过的一个警告,
UserWarning:invalid index of a 0-dim tensor.This will be an error in Pytorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number.
经过查阅发现,在新发布的 0.4.0 版本中,loss 是一个 0维标量。 对于标量的索引是没有意义的,在version>0.4的Pytorch 中,语句应修改为:
# 原语句
losses[split] += loss.data[0]
# 修改为
losses[split] += loss.item()