Tensorflow save the model with smallest validation error
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
If you want TensorFlow or Keras to keep the model from the epoch with the lowest validation error, the standard solution is ModelCheckpoint with monitor='val_loss' and save_best_only=True. That tells Keras to overwrite the saved model only when the validation loss improves.
The important detail is that the model left in memory at the end of training is not automatically the best one unless you also restore the best weights. Saving the best checkpoint and ending training are separate concerns.
Save the Best Model During Training
Here is the basic pattern:
Because val_loss is something you want to minimize, mode='min' is the correct setting.
Load the Best Model After Training
If you save the best model to disk, load it explicitly before evaluation or inference.
This matters because the final epoch may not be the best epoch. Overfitting often means later epochs are worse on validation data than earlier ones.
Combine with EarlyStopping
A very common pattern is to pair checkpointing with early stopping.
With restore_best_weights=True, the in-memory model is reset to the best validation-loss epoch when training stops. That means you may not need to reload from disk immediately, although many teams still keep the checkpoint for reproducibility.
Save Weights Only or the Whole Model
You can save either the whole model or only the weights.
Saving only weights is smaller and faster, but then you must recreate the model architecture before loading them. Saving the full model is often simpler when you want an immediately reusable artifact.
Monitoring a Different Validation Metric
The same callback works for other validation metrics too. The only rule is to choose the right mode.
Use mode='max' for metrics that should increase, such as accuracy or AUC. Use mode='min' for loss or error metrics that should decrease.
A Practical Mental Model
Think of ModelCheckpoint as "preserve the best artifact so far" and EarlyStopping as "decide when to stop wasting epochs." They are complementary tools, not substitutes for each other.
That distinction helps avoid confusion when training logs say the best model was saved at epoch 12, but the training loop actually ends at epoch 20.
Common Pitfalls
- Forgetting
save_best_only=True, which causes every epoch to be saved instead of only the best one. - Using
mode='max'while monitoringval_loss, which would keep worse models instead of better ones. - Assuming the final in-memory model is the best validation model when
restore_best_weightswas not enabled. - Monitoring a metric name that does not actually exist in the training logs.
- Saving only weights and then forgetting that the model architecture must be rebuilt before loading them.
Summary
- Use
ModelCheckpointwithmonitor='val_loss',mode='min', andsave_best_only=Trueto save the model with the smallest validation error. - Reload the saved model after training unless you also restore the best weights in memory.
- Pair checkpointing with
EarlyStoppingwhen you want training to stop after validation performance stalls. - Choose
mode='min'for losses andmode='max'for metrics that should increase. - Decide early whether you want to save the full model or only the weights.
Related reading
- TensorFlow Saver has 5 models limit
- TensorFlow Saver has 5 models limit
- TensorFlow saving into/loading a graph from a file
- tensorflow scalar summary tags name exception
- Tensorflow seq2seq multidimensional regression
- Tensorflow Sequence to sequence model using the seq2seq API ver 1.1 and above
- TensorFlow slow performance when getting gradients at inputs
- Tensorflow startup time?

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.