【问题标题】:volatile was removed and now has no effect. Use with torch.no_grad(): insteadvolatile 已被删除,现在没有效果。与 torch.no_grad(): 一起使用:
【发布时间】:2020-07-07 21:48:08
【问题描述】:

我遇到了这个错误,你能帮帮我吗? 这是我在 utils.py 文件中的错误

 content/gdrive/My Drive/DeepFakeDetection/utils.py:21: UserWarning:
 volatile was removed and now has no effect. Use with torch.no_grad():
 instead. return Variable(x, volatile=volatile)

这是我在 utils.py 文件中的代码

 def to_var(x, volatile=False):
 
     if torch.cuda.is_available(): 
        x = x.cuda() 
     return Variable(x, volatile=volatile)

谢谢

【问题讨论】:

  • Variablevolatile 都已弃用,取而代之的是 torch.Tensor 和上下文管理器 torch.no_grad()。无论如何,这只是一个警告而不是错误,因此如果代码运行正确,您可以选择忽略它。

标签: pytorch volatile


【解决方案1】:

我改成这样了:

def to_var(x, volatile=False):
>     
>     with torch.no_grad():
>         
>         if torch.cuda.is_available():
>             x = x.cuda()
>     return x

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2021-05-03
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2012-03-18
    • 1970-01-01
    • 2021-03-29
    相关资源
    最近更新 更多