🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #201

Early Stopping

Early stopping halts iterative training the moment validation performance stops improving — a simple, effective way to prevent overfitting for any model trained step by step, without needing to tune a separate regularization strength.

The Core Idea

training iteration → training loss validation loss stop training here

Training loss will happily keep falling forever — early stopping halts training right when validation loss stops improving, before overfitting sets in.

The Algorithm

StepWhat Happens
1After each training iteration/epoch, evaluate on a held-out validation set
2Track the best validation score seen so far
3If validation performance hasn't improved for a set number of iterations ("patience"), stop
4Use the model weights from the best iteration, not necessarily the last one

Python Implementation — XGBoost

import xgboost as xgb

model = xgb.XGBClassifier(
    n_estimators=1000,           # set generously high -- early stopping decides the real number
    learning_rate=0.05,
    early_stopping_rounds=20,     # stop if no improvement for 20 rounds
    eval_metric="logloss",
)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)
print("Stopped at iteration:", model.best_iteration)

Python Implementation — Neural Networks (Keras-style)

# Conceptual pattern shared across most deep learning frameworks
from tensorflow.keras.callbacks import EarlyStopping

early_stop = EarlyStopping(monitor="val_loss", patience=10, restore_best_weights=True)
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=200, callbacks=[early_stop])

restore_best_weights=True ensures the final model uses the weights from the best-performing epoch, not simply the epoch where training happened to stop.

Why "Patience" Matters

Validation loss doesn't always improve monotonically — it can plateau or briefly worsen before continuing to improve. Too little patience stops training prematurely, right before a genuine improvement; too much patience wastes computation and risks the very overfitting early stopping is meant to prevent. Patience is itself a tunable hyperparameter, usually set based on how noisy the validation curve typically looks for a given problem.

Practical Use Cases

  • Training neural networks, where the number of epochs is otherwise a hard-to-guess hyperparameter
  • Boosting methods (XGBoost, LightGBM), where it directly determines the number of estimators without manual guessing

Common Mistakes

  • Monitoring training loss instead of validation loss for the stopping decision — this defeats the entire purpose, since training loss keeps improving even as the model overfits.
  • Setting patience too low, stopping right as the model was about to improve past a temporary plateau.
  • Forgetting restore_best_weights (or its equivalent), ending up with the last epoch's weights instead of the best one.

Interview Relevance

Q: "Why should early stopping monitor validation loss, not training loss?" Training loss will essentially always keep decreasing the longer you train, regardless of whether the model is still learning genuine patterns or has started overfitting noise — validation loss is the only signal that actually reveals when generalization starts to degrade, which is exactly the moment training should stop.

Practice Question

You set patience=2 and notice training stops very early, well before training loss has flattened out. What might be happening, and what would you try adjusting?

Want to build robust, well-regularized models on real projects? CodingNow 2.0's Data Science course covers these techniques with hands-on practice.

Want to go beyond the notes?

Join CodingNow 2.0's Machine 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 Machine Learning.

This free note from CodingNow 2.0 explains Early Stopping in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine 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