Early stopping ends training when a monitored validation metric stops improving, avoiding a wasteful run that continues into overfitting. As a Keras callback, it can also restore the best weights. section 7.3.2 section 14.4.1
A practical Keras setup
Configure EarlyStopping with monitor="val_loss", restore_best_weights=True, and patience=2, then pass it to model.fit() with validation data and a maximum of 10 epochs. Training may finish earlier while retaining the weights from the best epoch. section 14.4.1
Use early stopping when:
- The required number of epochs is uncertain and a preliminary overfitting run would otherwise be used to choose it. section 7.3.2
- Validation performance has leveled off; combine patience with best-weight restoration rather than stopping at the first imperfect epoch. section 14.4.1
- Keep a separate test set for final evaluation because validation information can leak into model decisions. section 5.5
Listing 7.19 Using the callbacks argument in the fit() method
callbacks_list = [ #1 keras.callbacks.EarlyStopping( #2 monitor="accuracy", #3 patience=1, #4 ), keras.callbacks.ModelCheckpoint( #5 filepath="checkpoint_path.keras", #6 monitor="val_loss", #7 save_best_only=True, #7 ), ] model = get_mnist_model() model.compile( optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"], #8 ) model.fit( #9 train_images, #9 train_labels, #9 epochs=10, #9 callbacks=callbacks_list, #9 validation_data=(val_images, val_labels), #9 ) #9
Inspect the callback configuration alongside ModelCheckpoint to see how stopping and saving the best model can work together. section 7.3.2








