An Introduction to PyTorch model.eval() for Beginners – PyTorch Tutorial

By | April 27, 2022

PyTorch model.eval() is often used in pytorch scripts. In this tutorial, we will introduce why and how to use it when building a ai model.

model.eval() explain

Here model is a pytorch torch.nn.Module instance. The method .eval() will make this model in evaluation mode.

What is evaluation mode?

A torch moduel usually contains training/evaluation model.

In a training model, we can use some ways to improve moding training, such as batch normalization, dropout et al.

Understand Batch Normalization: A Beginner Explain – Machine Learning Tutorial

Understand Dropout – Place it Before or After Activation Function in Dense Layer?

However, when we plant to test or evaluate the performance of our model, we should set batch normalization not be updated or dropout rate = 0.

tf.layers.dropout() vs tf.nn.dropout(): A Difference Introduction – TensorFlow Tutorial

In pytorch, we can use a simple code model.eval() to implement, which is much easier than tensorflow.

As to us, we usually use model.eval() as follow in evaluation mode.

model.eval()
with torch.no_grad():
    #your code

To understand torch.no_grad(), you can read:

Understand with torch.no_grad() with Examples – PyTorch Tutorial

Leave a Reply