ModelCheckpoint saves the model, or just its weights if save_weights_only=True, after every epoch by default, writing to filepath, which can include placeholders like {epoch} to save a separate file per epoch. Setting save_best_only=True instead saves only when the monitored metric, like val_loss, has improved compared to every previous epoch, overwriting the same file each time, which is the most common configuration since it avoids accumulating dozens of checkpoint files while still guaranteeing you always have the best-performing version saved.
1Understanding callbacks.ModelCheckpoint()
ModelCheckpoint saves the model, or just its weights if save_weights_only=True, after every epoch by default, writing to filepath, which can include placeholders like {epoch} to save a separate file per epoch. Setting save_best_only=True instead saves only when the monitored metric, like val_loss, has improved compared to every previous epoch, overwriting the same file each time, which is the most common configuration since it avoids accumulating dozens of checkpoint files while still guaranteeing you always have the best-performing version saved.
Set save_best_only=True in almost every case — it keeps just a single file containing the best model seen so far, avoiding the disk-space overhead of saving every single epoch while still guaranteeing you never lose the best result to a later, worse epoch.
import tensorflow as tf
callback = tf.keras.callbacks.ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)
print(callback.save_best_only)2Practical Example
Here is a real-world application of callbacks.ModelCheckpoint() showing how it is used in production TensorFlow code.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import numpy as np
import os
model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
callback = tf.keras.callbacks.ModelCheckpoint('checkpoint.keras', save_best_only=True, monitor='loss')
x, y = np.array([1, 2, 3, 4]), np.array([2, 4, 6, 8])
model.fit(x, y, epochs=5, callbacks=[callback], verbose=0)
print(os.path.exists('checkpoint.keras'))3Best Practices
Follow these guidelines when working with callbacks.ModelCheckpoint():
1. Set save_best_only=True to automatically retain only the single best-performing checkpoint, rather than accumulating one file per epoch
2. Monitor a validation metric, like val_loss, rather than a training metric, so the saved checkpoint reflects genuine generalization improvement
3. Combine ModelCheckpoint with EarlyStopping so training both saves its best result along the way and stops automatically once that result stops improving
Tip: Set save_best_only=True in almost every case — it keeps just a single file containing the best model seen so far, avoiding the disk-space overhead of saving every single epoch while still guaranteeing you never lose the best result to a later, worse epoch.
import tensorflow as tf
callback = tf.keras.callbacks.ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)
print(callback.save_best_only)