与 torch.no_grad:AttributeError:__enter__

Day*_*ala 7 machine-learning deep-learning torch pytorch

with torch.no_grad:AttributeError: __enter__
Run Code Online (Sandbox Code Playgroud)

我在运行 pytorch 代码时遇到此错误。

我有 torch==0.4.1 torchvision==0.3.0,我在 google colab 中运行代码。

trs*_*chn 10

torch.no_grad是一个上下文管理器,它确实有__enter____exit__

你应该将它与with语句一起使用,就像这样

with context_manager():
    pass
Run Code Online (Sandbox Code Playgroud)

因此,只需将with torch.no_grad:(访问属性)替换为with torch.no_grad():(调用方法)即可正确使用 contextmanager。