WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Intermediate · question 30 of 100

How can you apply early stopping during model training in PyTorch?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

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.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics