Does model.eval() placement matter in the code?

Question:

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 …

Asked By: TSRAI

||

Answers:

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.

Answered By: candied_orange

both of them are correct, you just need to use the 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

Answered By: Mohamed Fathallah