Dayanand Dhumala
Dayanand Dhumala

Reputation: 321

with torch.no_grad: AttributeError: __enter__

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.

Upvotes: 7

Views: 6000

Answers (1)

trsvchn
trsvchn

Reputation: 8981

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.

Upvotes: 11

Related Questions