liveBook · Manning

Early stopping

As explained in Deep Learning with Python, Third Edition

A training strategy that stops optimization when validation performance stops improving.

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

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains callback configuration, patience, validation monitoring, and best-weight restoration without detailing the callback implementation.
Examples
Uses a Keras bag-of-words sentiment classifier with validation monitoring, patience of two epochs, plots, and final test evaluation.
What it assumes
Assumes a validation dataset, a monitored metric, and familiarity with passing callbacks to model.fit().
What you can do after
Apply it via Keras to stop training automatically and retain the weights associated with the best validation epoch.
How it connects
Connects validation loss, overfitting, regularization, callbacks, model checkpointing, and generalization.

Also explained in

Compare how 9 books explain early stopping →

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.