How to get the best model when using EarlyStopping callback in Keras?
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
EarlyStopping is a powerful regularization technique for training neural networks using Keras. It works by monitoring a model's performance on a validation set and halting training when the performance ceases to show improvement, thereby preventing overfitting and reducing unnecessary computation. This article provides an in-depth exploration of how to effectively leverage the EarlyStopping callback in Keras to train highly performant models.
Understanding EarlyStopping
EarlyStopping in Keras monitors a specified metric, such as validation loss or validation accuracy, during the training process. When this metric stops improving, training is halted. By preventing further updates to the model that may lead to overfitting, EarlyStopping can ensure a model retains its ability to generalize well to new data.
Key `Parameters` of EarlyStopping
The EarlyStopping callback in Keras can be customized through several parameters:
- monitor: Specifies the metric to monitor, e.g., 'val_loss' for validation loss.
- min_delta: Defines the minimum change in the monitored quantity to qualify as an improvement. This value helps in avoiding stopping training for trivial improvements.
- patience: Denotes the number of epochs with no improvement after which training will be stopped. The higher the patience, the longer training will continue without improvement.
- mode: Determines whether the training halts when the monitored metric stops increasing ('max') or decreasing ('min'). For validation accuracy, 'max' is used, while for validation loss, 'min' is appropriate.
- restore_best_weights: If set to `True`, the model weights are restored to those of the epoch that yielded the best monitored metric.
Here's a summary of these key parameters:
| Parameter | Description |
monitor | Metric to observe, e.g., 'val_loss'. |
min\_delta | Minimal change to define improvement. |
patience | Epochs to wait for improvement before stopping. |
mode | Metric trend to stop training ('min' or 'max'). |
restore\_best\_weights | Restore the model weights to those at the best-achieving epoch if True. |
Implementing EarlyStopping
Implementing EarlyStopping in Keras is straightforward. Below is an example using a simple neural network model on the MNIST dataset:
Related reading
- How to get the CUDA version?
- How to get the dimensions of a tensor in TensorFlow at graph construction time?
- How to get top n arg max/min in tensorflow?
- How to get weight matrix of one layer at every epoch in LSTM model based on Keras?
- How to get the count of an element in a tensor in TensorFlow?
- How to get the global_step when restoring checkpoints in Tensorflow?
- How to get the cpu usage per thread on windows win32
- How to have a processor intensive function update every tick of the metronome and draw to canvas using web workers?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the 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.