0.4.0pytorch运行过程中对0-dim和volatile提示UserWarning的解决方法

将pytorch更新到0.4.0最新版后对0.3.1版本代码会有如下警告,它在提醒用户下个版本这将成为一个错误
1.
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
  train_loss += loss.data[0]

修改方法如下:

#原语句:
train_loss+=loss.data[0]
#修改后:
train_loss+=loss.item()
#bingo

2.
UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
  label = Variable(label.cuda(), volatile=True)

修改方法如下:
#原语句
label = Variable(label.cuda(), volatile=True)
#修改后语句
  label = Variable(label.cuda())

猜你喜欢

转载自blog.csdn.net/weixin_41797117/article/details/80237179
今日推荐