Function of model.eval()

What is the default mode (training or inference) for a Pytorch model if model.eval() or model.train() is not executed? I was recently working on a pet project and was loading a trained model using torch.load(). I was only using the loaded model for inference. Now if I don’t execute model.eval(), it’s behaving as if the weights are untrained and giving a very bad performance. But just setting the eval flag with model.eval() fixes the issue. What might be causing this behavior? Isn’t a model supposed to be in eval mode by default.

They are initialized with train=True by default.

However, the difference only lies on a few things: batch nom layers and dropout layers. It has nothing to do with the weights being trained or not… So if you have batch norm or dropout layers, setting the eval flag will modify them accordingly, so that you can have correct values in the output! The weights of the model are unchanged, though.

1 Like