🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

callbacks.ModelCheckpoint()

AI & DATA SCIENCE // callbacks-modelcheckpoint

tf.keras.callbacks.ModelCheckpoint() automatically saves a model, or its weights, during training, typically whenever a monitored metric improves.

Syntax

tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', save_best_only=False)

Deep Dive Course

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.

editor.html
import tensorflow as tf

callback = tf.keras.callbacks.ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)
print(callback.save_best_only)
localhost:3000

2Practical Example

Here is a real-world application of callbacks.ModelCheckpoint() showing how it is used in production TensorFlow code.

editor.html
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'))
localhost:3000

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.

editor.html
import tensorflow as tf

callback = tf.keras.callbacks.ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)
print(callback.save_best_only)
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

callback = tf.keras.callbacks.ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)
print(callback.save_best_only)
Example 02Advanced Example
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'))

Best Practices

  • Set save_best_only=True to automatically retain only the single best-performing checkpoint, rather than accumulating one file per epoch
  • Monitor a validation metric, like val_loss, rather than a training metric, so the saved checkpoint reflects genuine generalization improvement
  • Combine ModelCheckpoint with EarlyStopping so training both saves its best result along the way and stops automatically once that result stops improving

Interview Question

Why does ModelCheckpoint with save_best_only=True need a monitor metric, while save_best_only=False doesn't require one to function?

Hint: Think about what decision ModelCheckpoint actually needs to make in each of the two modes.

With save_best_only=False, ModelCheckpoint's job is unconditional: simply save the current model after every single epoch, regardless of how it's performing, which requires no comparison against anything and therefore no monitored metric at all. With save_best_only=True, ModelCheckpoint instead needs to make an actual decision at the end of each epoch, comparing the current epoch's performance against the best performance seen in any previous epoch, and only overwriting the saved file if the current epoch is genuinely better — that comparison is impossible without knowing which specific metric to compare, hence the required monitor argument. This is exactly why the monitor argument becomes meaningful, and necessary, only once you're asking ModelCheckpoint to be selective about what it saves rather than saving everything indiscriminately.

Exercises

MediumPractice using callbacks.ModelCheckpoint() in a real scenario.
View Solution
import tensorflow as tf

callback = tf.keras.callbacks.ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True)
print(callback.save_best_only)

Frequently Asked Questions

Why does ModelCheckpoint with save_best_only=True need a monitor metric, while save_best_only=False doesn't require one to function?

With save_best_only=False, ModelCheckpoint's job is unconditional: simply save the current model after every single epoch, regardless of how it's performing, which requires no comparison against anything and therefore no monitored metric at all. With save_best_only=True, ModelCheckpoint instead needs to make an actual decision at the end of each epoch, comparing the current epoch's performance against the best performance seen in any previous epoch, and only overwriting the saved file if the current epoch is genuinely better — that comparison is impossible without knowing which specific metric to compare, hence the required monitor argument. This is exactly why the monitor argument becomes meaningful, and necessary, only once you're asking ModelCheckpoint to be selective about what it saves rather than saving everything indiscriminately.

Related Functions

callbacks-earlystoppingmodel-savemodel-fit