Early stopping is a technique used in machine learning to prevent overfitting of the model to the training data. With early stopping, we monitor the performance of the model on a validation set during training and stop training when the performance on the validation set does not improve over a certain number of epochs. This can save us time and resources that would have been wasted on further training.
In PyTorch, we can apply early stopping during model training using the ‘torch.nn.utils.early_stopping‘ utility. Here’s an example of how to use it:
from torch.nn.utils import early_stopping
# Initialize the early stopping object
early_stopping_obj = early_stopping.EarlyStopping(patience=10, verbose=True)
# Train the model
for epoch in range(num_epochs):
for i, (inputs, targets) in enumerate(train_loader):
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, targets)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Evaluate the model on the validation set
with torch.no_grad():
val_loss = 0
for inputs, targets in val_loader:
outputs = model(inputs)
val_loss += criterion(outputs, targets)
val_loss /= len(val_loader)
# Check if the validation loss has improved
early_stopping_obj(val_loss, model)
if early_stopping_obj.early_stop:
print("Early stopping")
break
In this code, we initialize the ‘early_stopping.EarlyStopping‘ object with a ‘patience‘ parameter, which determines how many epochs we should wait for an improvement in the validation loss. We also set ‘verbose‘ to ‘True‘ to enable printing out information about the early stopping process.
Inside the training loop, we evaluate the model on the validation set after each epoch and pass the validation loss to the ‘early_stopping_obj‘. The ‘early_stopping_obj‘ keeps track of the best validation loss seen so far and how long it has been since the best validation loss was improved. If the validation loss does not improve for ‘patience‘ epochs, the ‘early_stopping_obj‘ sets ‘early_stop‘ to ‘True‘, and we can break out of the training loop.
With this method, we can apply early stopping during model training in PyTorch to prevent overfitting and save time and resources.