with torch.no_grad: AttributeError: __enter__

Viewed 2581
with torch.no_grad:AttributeError: __enter__

I got this error while running pytorch code.

I have torch==0.4.1 torchvision==0.3.0, I run the code in google colab.

1 Answers

torch.no_grad is a contextmanager it really has __enter__ and __exit__.

You should use it with with statement, like this

with context_manager():
    pass

Thus, simply replace with torch.no_grad: (accessing the attribute) with with torch.no_grad(): (calling a method) to use contextmanager properly.

Related