Create restartable training checkpoints
Recovering from interruptions
Keras · Class
ModelCheckpoint saves a Keras model or its weights during fit() training. It can save every epoch or after a batch interval, optionally keeping only checkpoints that improve a monitored metric.
ModelCheckpoint is a training callback that periodically saves either the complete Keras model or only its weights. It can save after every epoch or after a specified number of batches, and can retain only checkpoints that improve a monitored metric.
keras.callbacks.ModelCheckpoint(filepath, monitor="val_loss", verbose=0, save_best_only=False, save_weights_only=False, mode="auto", save_freq="epoch", initial_value_threshold=None)| Name | Type | Description |
|---|---|---|
filepath | string or PathLike | Destination filepath for the saved model or weights; formatting tokens such as {epoch} and metric names may be included. |
monitor | string | Metric name used to decide which checkpoint is best; defaults to val_loss. |
verbose | integer | Verbosity level for checkpoint messages. |
save_best_only | boolean | Whether to save only when the monitored quantity improves. |
save_weights_only | boolean | Whether to save only weights instead of the complete model. |
mode | string | One of "auto", "min", or "max"; controls whether lower or higher monitored values are better. |
save_freq | `"epoch"` or integer | "epoch" or an integer number of batches between saves. |
initial_value_threshold | float or None | Optional initial monitored value used as the baseline for deciding whether a save is worthwhile. |
A Keras callback object that saves the model or weights during training.
model.fit().save_best_only when older, worse checkpoints should be replaced.monitor and, when needed, mode to define how improvement is evaluated.save_freq="epoch" for epoch checkpoints or an integer for batch-based saving.save_weights_only..weights.h5 filename for weights-only checkpoints.import keras
model = keras.Sequential([
keras.layers.Input(shape=(4,)),
keras.layers.Dense(1)
])
model.compile(optimizer="adam", loss="mse")
checkpoint = keras.callbacks.ModelCheckpoint(
"best_model.keras",
monitor="val_loss",
save_best_only=True
)
model.fit(
[[0, 0, 0, 0], [1, 1, 1, 1]],
[[0], [1]],
validation_split=0.5,
epochs=2,
callbacks=[checkpoint]
)Trains a small model and keeps the checkpoint only when validation loss improves.
Recovering from interruptions
Model selection
Large or distributed training runs
Deployment artifacts
ModelCheckpoint is a callback used with model.fit() to save a model or its weights at intervals. It supports saving at each epoch or after a fixed number of batches.
Follow-up: How would you save only improving checkpoints? Set save_best_only=True and choose the metric with monitor; use mode when the direction cannot be inferred.
save_best_only=True compares the monitored quantity and overwrites the checkpoint only when the current result is better. The monitored metric is selected with monitor, whose default is val_loss.
Follow-up: What must be present for a validation metric to be monitored? Training must produce that metric, commonly by supplying validation data to fit().
save_weights_only=False saves the complete model, while True saves only its weights. The corresponding filepath extension must match the kind of artifact being saved.
Follow-up: Why might weights-only saving be preferable? It produces a smaller artifact when the model architecture and configuration are maintained separately.
keras.callbacks.Callbackkeras.callbacks.BackupAndRestorekeras.callbacks.EarlyStoppingkeras.Model.fitkeras.models.load_modelkeras.Model.load_weights