liveBook · Manning

ModelCheckpoint

As explained in Deep Learning with Python, Third Edition

A Keras callback that saves a model or its best weights during training at specified points or when performance improves.

ModelCheckpoint is a Keras callback that preserves a model’s state during training. Instead of retraining after validation performance peaks and overfitting begins, save the best-performing state and reload it for evaluation. section 7.3.2 section 8.2.4

Save the best validation model

Configure the callback with a destination, the metric to monitor, and save_best_only=True: callbacks = [ keras.callbacks.ModelCheckpoint( filepath="convnet_from_scratch.keras", save_best_only=True, monitor="val_loss", ) ] history = model.fit( train_dataset, epochs=50, validation_data=validation_dataset, callbacks=callbacks, ) After training, reload the saved state with keras.models.load_model("convnet_from_scratch.keras") and evaluate that model on the test dataset. With monitor="val_loss", the file is overwritten only when validation loss reaches a new low. section 8.2.4

Use the configuration to make the checkpoint match the training decision you care about:

  • Set filepath to the file where Keras should save the model. section 8.2.4
  • Set monitor to the validation metric that determines improvement, such as val_loss. section 7.3.2 section 8.2.4
  • Set save_best_only=True when the goal is to retain the strongest validation result rather than every epoch. section 8.2.4
  • Pass the callback through model.fit() alongside the training and validation data. section 7.3.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines callback use and demonstrates filepath, monitoring, best-only saving, and reloading, but does not detail callback internals.
Examples
Uses an MNIST callback setup and a ConvNet image-classification run that reloads the saved model for test evaluation.
What it assumes
Assumes familiarity with model.fit(), validation data, and training metrics while demonstrating callback use in context.
What you can do after
Operate/configure ModelCheckpoint through model.fit(), then reload the retained model for evaluation.
How it connects
Connects model checkpointing with callbacks, validation loss, overfitting, EarlyStopping, and model evaluation.

Also explained in

Compare how 2 books explain ModelCheckpoint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.