Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 428
Dayanand Dhumala Avatar asked Jul 25 '26 03:07

Dayanand Dhumala


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.

like image 200
trsvchn Avatar answered Jul 28 '26 00:07

trsvchn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!