how to implement early stopping in tensorflow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Early stopping is one of the simplest ways to control overfitting in TensorFlow training loops. Instead of training for a fixed number of epochs and hoping you guessed well, you monitor a validation metric and stop once the model stops improving for long enough.
The standard Keras callback
In TensorFlow, the normal implementation is tf.keras.callbacks.EarlyStopping. You attach it to model.fit and tell it which metric to watch.
This stops training when val_loss fails to improve for three consecutive epochs and restores the weights from the best epoch.
Choosing the right metric
The monitored metric should match the real goal:
- use
val_lossfor most regression and classification problems - use
val_accuracyonly when accuracy is the metric you genuinely care about - use a custom validation metric if the default metrics do not reflect business quality
In many cases, val_loss is a better default than accuracy because it reacts earlier to overfitting.
Why restore_best_weights matters
Without restore_best_weights=True, training stops at the last epoch that exceeded patience, not at the best epoch. That often leaves the model slightly worse than the best point seen during training.
If you want the best checkpoint even when training runs longer than necessary, combine early stopping with model checkpointing:
This gives you both a good stopping rule and a saved copy of the best model.
What patience actually does
patience is not the number of bad batches or noisy updates. It is the number of epochs with no meaningful improvement. A patience of 0 is usually too aggressive because validation curves often bounce slightly. A small value such as 2 through 5 is a common starting point.
TensorFlow also supports min_delta, which defines how large an improvement must be before it counts:
This prevents tiny fluctuations from resetting the patience counter.
Early stopping in custom training loops
If you are not using model.fit, you can still implement the same idea manually: track the best validation metric, count epochs without improvement, and break once the counter exceeds your patience value. The logic is simple; EarlyStopping just packages it cleanly for Keras workflows.
Common Pitfalls
- Monitoring training loss instead of validation loss, which defeats the point of early stopping.
- Forgetting
restore_best_weights, then keeping weights from a later, worse epoch. - Using no validation data at all, which leaves nothing meaningful to monitor.
- Setting patience too low and stopping during normal metric noise.
- Treating early stopping as a substitute for good data splits and sensible model design.
Summary
- Use
tf.keras.callbacks.EarlyStoppingwith a validation metric. - '
val_lossis usually the safest default metric to monitor.' - '
restore_best_weights=Trueis often what you actually want.' - Tune
patienceandmin_deltabased on how noisy the validation curve is. - Combine early stopping with checkpointing when you want the best saved model as well as a clean stopping rule.
Related reading
- How to implement Grad-CAM on a trained network
- How to implement multi-class semantic segmentation?
- How to implement neural network pruning?
- How to implement pixel-wise classification for scene labeling in TensorFlow?
- How to implement PReLU activation in Tensorflow?
- How to implement sklearn's PolynomialFeatures in tensorflow?
- How to implement mini-batch gradient descent in python?
- How to implement tensorflow Estimator with multiple models for GAN?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.