Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

callbacks.ModelCheckpoint

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
filepathstring or PathLikeDestination filepath for the saved model or weights; formatting tokens such as {epoch} and metric names may be included.
monitorstringMetric name used to decide which checkpoint is best; defaults to val_loss.
verboseintegerVerbosity level for checkpoint messages.
save_best_onlybooleanWhether to save only when the monitored quantity improves.
save_weights_onlybooleanWhether to save only weights instead of the complete model.
modestringOne 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_thresholdfloat or NoneOptional initial monitored value used as the baseline for deciding whether a save is worthwhile.

Returns

A Keras callback object that saves the model or weights during training.

Key points

  • Use the callback to create periodic model or weight checkpoints during training.
  • Register it in the callbacks list passed to model.fit().
  • Set save_best_only when older, worse checkpoints should be replaced.
  • Choose monitor and, when needed, mode to define how improvement is evaluated.
  • Use save_freq="epoch" for epoch checkpoints or an integer for batch-based saving.
  • Choose between a portable complete-model artifact and a weights-only artifact with save_weights_only.

Common mistakes

  • An integer save frequency counts batches, not epochs.
  • Use a .weights.h5 filename for weights-only checkpoints.

Example: Save the best validation model

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.

When to use it

Create restartable training checkpoints

Recovering from interruptions

Keep the best validation result

Model selection

Save at batch-based intervals

Large or distributed training runs

Persist the full model or only weights

Deployment artifacts

Interview questions

QWhat does ModelCheckpoint do, and when does it save?

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.

QHow does best-checkpoint selection work?

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().

QWhat is the difference between saving a model and saving weights?

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.Callback
  • keras.callbacks.BackupAndRestore
  • keras.callbacks.EarlyStopping
  • keras.Model.fit
  • keras.models.load_model
  • keras.Model.load_weights

Source: ModelCheckpoint. Checked 2026-10-15.