Warning volatile was removed and now has no effect

将pytorch更新到0.4.0最新版后对0.3.1版本代码会有以下警告,它在提醒用户下个版本这将成为一个错误spa

 

  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  
  2.   train_loss += loss.data[0]  
  3. #原语句:  
  4. train_loss+=loss.data[0]  
  5. #修改后:  
  6. train_loss+=loss.item()   

 

  1. UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.  
  2. #原语句  
  3. label = Variable(label.cuda(), volatile=True)  
  4. #修改后语句  
  5.   label = Variable(label.cuda())  
相关文章
相关标签/搜索