Does EarlyStopping in Keras save the best model?
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
Keras EarlyStopping stops training when a monitored metric stops improving, but that does not automatically mean your best model is saved anywhere. The exact behavior depends on one callback setting for in-memory weights and a different callback if you want the best model written to disk.
What EarlyStopping Actually Does
EarlyStopping monitors a metric such as val_loss or val_accuracy during model.fit(). When the metric stops improving for the configured number of epochs, training ends early.
The important default is this: restore_best_weights=False.
That means if you use EarlyStopping with its defaults, Keras stops training, but the model instance keeps the weights from the last epoch that ran, not necessarily the best epoch.
This callback can stop training early, but it does not by itself restore the best validation-loss weights.
Restoring the Best Weights in Memory
If you want the model object in memory to end training at the best epoch, set restore_best_weights=True.
With that setting, once training stops, Keras rolls the model weights back to the best observed epoch for the monitored quantity.
That solves an important part of the problem, but it still does not save a file to disk. It only changes the state of the model currently living in memory.
Saving the Best Model to Disk
If you want a durable saved artifact, use ModelCheckpoint, usually together with save_best_only=True.
This combination does two different jobs:
- '
EarlyStoppingdecides when to halt training' - '
ModelCheckpointdecides what gets written to disk'
If you only care about the final in-memory model, restore_best_weights=True may be enough. If you need to reload the best model later, use checkpointing.
Why This Distinction Matters
It is easy to assume "best model" means one thing, but there are really two separate questions:
- what weights should the live model object have after training ends
- what file should be persisted for later use
EarlyStopping answers the first question only when restore_best_weights=True. ModelCheckpoint answers the second question.
A Small Demonstration
Suppose validation loss reaches its minimum at epoch 8, then gets slightly worse, and training stops at epoch 11 because patience expires.
With default EarlyStopping, your model keeps epoch 11 weights.
With restore_best_weights=True, your in-memory model is rolled back to epoch 8 weights.
With ModelCheckpoint(save_best_only=True), the file on disk tracks the best epoch independently of whether the training loop later continues for a few more epochs.
That is why many training setups use both callbacks together.
Choosing the Right Monitored Metric
Make sure monitor and mode match your objective:
- use
monitor="val_loss"withmode="min"when lower is better - use
monitor="val_accuracy"withmode="max"when higher is better
If you monitor the wrong quantity, "best" will mean the wrong thing no matter how you configure the callbacks.
Common Pitfalls
The most common mistake is assuming EarlyStopping saves a checkpoint file. It does not.
Another mistake is forgetting that restore_best_weights defaults to False. Many people expect the best epoch to be restored automatically, but Keras only does that when you ask for it.
A third pitfall is using EarlyStopping without validation data while monitoring a validation metric such as val_loss. If the metric is not present in the training logs, the callback cannot work correctly.
Finally, keep your monitor consistent across callbacks. If EarlyStopping watches val_loss and ModelCheckpoint watches val_accuracy, the "best" model in memory and on disk may not match.
Summary
- '
EarlyStoppingdoes not automatically save the best model to disk' - By default, it also does not restore the best weights in memory
- Set
restore_best_weights=Trueif you want the final model object to use the best epoch's weights - Use
ModelCheckpoint(save_best_only=True)if you want the best model saved as a file - Many training pipelines use both callbacks together for clean stopping and reliable persistence
Related reading
- Does model.compile initialize all the weights and biases in Keras tensorflow backend?
- Does TensorFlow by default use all available GPUs in the machine?
- Does TensorFlow have cross validation implemented?
- Does tensorflow map_fn support taking more than one tensor?
- Does Google Tensorflow support OpenCL
- Does image size matter when training with TensorFlow?
- Does GridSearchCV in sklearn train the model with whole data set?
- Does GridSearchCV perform cross-validation?

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.