๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #125

Early Stopping

Early stopping halts training automatically once validation performance stops improving โ€” a simple, remarkably effective form of regularization that directly prevents the model from continuing to overfit past its best generalization point.

The Core Idea

Revisit the training/validation loss diagram from Validation Loop: training loss keeps decreasing, but validation loss eventually turns upward as the model starts memorizing training-specific noise. Early stopping watches for exactly this turning point and stops training there โ€” rather than continuing for a pre-fixed number of epochs regardless of what validation performance is doing.

The Algorithm โ€” Patience-Based Early Stopping

  1. Track the best validation loss seen so far, and how many epochs it's been since that best value improved (the "patience counter").
  2. After each epoch, if validation loss improved, reset the patience counter to 0 and save a checkpoint (see Checkpointing).
  3. If it didn't improve, increment the patience counter.
  4. If the patience counter exceeds a threshold (e.g. 5 or 10 epochs with no improvement), stop training and restore the best checkpoint.

Code

best_val_loss = float('inf')
patience = 5
epochs_without_improvement = 0

for epoch in range(num_epochs):
    # ... training loop for this epoch ...
    # ... validation loop for this epoch, producing avg_val_loss ...

    if avg_val_loss < best_val_loss:
        best_val_loss = avg_val_loss
        epochs_without_improvement = 0
        torch.save(model.state_dict(), 'best_model.pt')
    else:
        epochs_without_improvement += 1

    if epochs_without_improvement >= patience:
        print(f"Early stopping at epoch {epoch} -- no improvement for {patience} epochs")
        break

model.load_state_dict(torch.load('best_model.pt'))   # restore the best version, not the last one

Why "Patience," Not Stopping at the First Non-Improvement

Validation loss is itself somewhat noisy from epoch to epoch (it's still computed on a finite sample) โ€” stopping the very first time it fails to improve would trigger prematurely on normal noise, not genuine overfitting. A patience window of several epochs distinguishes a real, sustained upward trend from ordinary epoch-to-epoch fluctuation.

Choosing the Patience Value

PatienceTradeoff
Too small (e.g. 1)Stops prematurely on normal validation noise, before the model has fully converged
Too large (e.g. 50)Wastes significant compute time continuing to train well past the point of genuine improvement
Moderate (5โ€“15, task-dependent)Reasonable balance โ€” tolerates normal noise while still stopping promptly once overfitting is genuinely underway

Common Mistakes

  • Restoring the last epoch's weights instead of the best checkpoint after early stopping triggers โ€” the whole point of early stopping is to use the best-performing version, not whatever happened to be current when the patience threshold was hit.
  • Using training loss instead of validation loss to decide when to stop โ€” training loss will keep improving even as the model overfits, making it useless as an early-stopping signal.

Interview Relevance

Q: "Why does early stopping need a 'patience' parameter instead of stopping immediately when validation loss fails to improve?" Validation loss fluctuates somewhat from epoch to epoch due to normal noise, even when the model is still genuinely improving overall. Stopping at the very first non-improvement would trigger on this noise prematurely; a patience window requires several consecutive non-improving epochs before concluding that overfitting has genuinely begun, distinguishing signal from noise.

Practice Question

A model's validation loss improves for 8 epochs, then stays flat (neither improving nor worsening significantly) for the next 6 epochs, with patience set to 5. At which epoch does early stopping trigger?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Early Stopping โ€“ FAQs

Quick answers about learning Early Stopping in Deep Learning.

This free note from CodingNow 2.0 explains Early Stopping in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Early Stopping, is 100% free with no signup required.
With focused practice, most students grasp Early Stopping in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now