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

