Reputation: 516
Might be a bit silly but I need to make sure this is correct. Does it matter if I place my code like this?:
model.eval()
with torch.no_grad():
Or can I get the same behaviour like this:
with torch.no_grad():
model.eval()
I'm just wondering because I have a function that has model.eval() inside of it which goes inside of a loop where with torch.no_grad():
is before it ...
Upvotes: 0
Views: 825
Reputation: 1484
model. eval()
Before you explore,you should put the model in eval mode, both in general and so that batch norm doesn't cause you issues and is using its eval statistics
Upvotes: 2
Reputation: 7334
If model.eval()
ever throws an exception the behavior won’t be the same. That’s because with
will attempt to close whatever was returned by torch.no_grad()
when an exception is thrown under its scope.
with
is a handy way to make sure you clean up resources even when things blow up.
Upvotes: 1