When should I use
.eval()? I understand it is supposed to allow me to "evaluate my model". How do I turn it back off for training?
Example training code using
model.eval() is a kind of switch for some specific layers/parts of the model that behave differently during training and inference (evaluating) time. For example, Dropouts Layers, BatchNorm Layers etc. You need to turn them off during model evaluation, and
.eval() will do it for you. In addition, the common practice for evaluating/validation is using
torch.no_grad() in pair with
model.eval() to turn off gradients computation:
# evaluate model: model.eval() with torch.no_grad(): ... out_data = model(data) ...
BUT, don’t forget to turn back to
training mode after eval step:
# training step ... model.train() ...
Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
This is equivalent with
The opposite method is
model.train explained nicely by Umang Gupta.
An extra addition to the above answers:
I recently started working with Pytorch-lightning, which wraps much of the boilerplate in the training-validation-testing pipelines.
Among other things, it makes
model.train() near redundant by allowing the
validation_step callbacks which wrap the
train so you never forget to.
|Sets model in training mode:
• normalisation layers1 use per-batch statistics
|Sets model in evaluation (inference) mode:
• normalisation layers use running statistics
You can turn off evaluation mode by running
model.train(). You should use it when running your model as an inference engine – i.e. when testing, validating, and predicting (though practically it will make no difference if your model does not include any of the differently behaving layers).