Concepts / Saving and Loading Keras Models

Saving and Loading Keras Models

A Keras callback is an object passed to fit that is called at various points during training.

  • Programming

Training Without Guesswork

When training begins, you usually cannot know in advance how many epochs will be needed to reach the best validation loss. One simple approach is to train long enough for overfitting to begin, determine a suitable number of epochs, and then start another training run from scratch. That approach wastes time. Keras callbacks provide a way to observe training as it happens and respond without requiring a separate trial run.

training proceedsepoch completestraining completesTraining startcallback eventBatch endcallback eventEpoch endcallback eventTraining endcallback event
At which points during training can Keras invoke a callback?

A Keras callback is an object passed to fit that Keras calls at various points during training.

Following the Training State

A callback watches training as it unfolds. At a selected training event, it can examine the current training situation and perform the action for which it was designed. The important idea is that the callback reacts during the existing fit process. It is not a separate trial run used only after training has finished.

What do you think happens?

Suppose the validation loss has stopped improving. Which response matches the stated purpose of ReduceLROnPlateau?

  • Preserve model weights
  • Interrupt training
  • Change the learning rate
Reveal answer

Answer: Change the learning rate

ReduceLROnPlateau changes the learning rate when validation loss stops improving. ModelCheckpoint preserves weights, while EarlyStopping interrupts training when its monitored metric stops improving for the configured patience period.

next eventstill no improvementpatience is used upconfigured responseMetric improvespatience not usedNo improvementpatience period beginsNo improvementpatience continuesPatience period usedconfigured limit reachedCallback actionstop or change learningrate
How does a callback respond as a monitored metric improves and then fails to improve across successive training events?

A monitored metric is the training measure used to judge whether the desired progress is happening. ModelCheckpoint can keep only the best model according to a monitored metric. EarlyStopping watches a monitored metric and interrupts training when it stops improving for the configured patience period. ReduceLROnPlateau responds specifically when validation loss stops improving. Patience therefore controls how long a callback tolerates a lack of improvement before taking its configured action.

Selecting the Built-In Callback

The main selection question is what you want to change in response to training behavior. Choose ModelCheckpoint when preserving model weights matters. Choose EarlyStopping when continuing to train is no longer useful. Choose ReduceLROnPlateau when validation loss has stopped improving and you want to change the learning rate rather than immediately stop.

preservesinterruptschangesModelCheckpointsaves weightsModel weightsEarlyStoppinginterrupts trainingTraining processReduceLROnPlateauchanges learning rateLearning rate
What is different about preserving weights, stopping training, and changing the learning rate?
GoalCallbackResponse
Preserve model weightsModelCheckpointSaves weights during training and can keep only the best model according to a monitored metric.
Stop when more training is not usefulEarlyStoppingInterrupts training when a monitored metric stops improving for the configured patience period.
React to a validation-loss plateauReduceLROnPlateauChanges the learning rate when validation loss stops improving.

Select the callback according to the training behavior you want to change.

Choosing a Response to Training Behavior

A learner wants to respond to three different situations: preserve the best weights, stop when progress has ceased, and adjust the learning rate when validation loss stops improving.

Preserve weights: Choose ModelCheckpoint because its purpose is to save weights during training and it can keep only the best model according to a monitored metric.

Stop training: Choose EarlyStopping because it interrupts training after the monitored metric has stopped improving for the configured patience period.

Adjust learning rate: Choose ReduceLROnPlateau because it changes the learning rate when validation loss stops improving.

The correct choice depends on the desired response: preserve weights, interrupt training, or change the learning rate.

Checkpointing Model State

ModelCheckpoint is the callback to choose when preserving model weights matters. During training, it saves weights and can keep only the best model according to a monitored metric. This gives checkpointing a different role from EarlyStopping: checkpointing preserves a selected training result, while EarlyStopping controls whether training continues.

training produces metricbest model is savedstate is loadedActive modeltraining stateMonitored metricbest-model decisionSaved checkpointpreserved weightsLoaded modelcheckpoint state
How does model state move from the actively trained model into a saved checkpoint and then into a loaded model?

Custom Callback Logic

Built-in callbacks do not cover every possible training action. When a required response is not provided by the built-in choices, create a custom callback by subclassing keras.callbacks.Callback and implementing methods that run at selected training events. The custom callback then receives the event notifications selected by its implementation and can execute its own logic at those points.

notificationexecutesTraining eventselected eventCallback methodimplemented methodCustom logicrequested action
How does a custom callback receive training-event notifications and run different logic at selected events?

The essential design pattern is event response: identify the training event that matters, implement the corresponding callback method, and place the desired action in that method. The source material establishes that a custom callback can be built by subclassing keras.callbacks.Callback and implementing methods for selected training events. It does not specify a complete list of callback attributes or the exact model, log, metric, and training-information interfaces available inside every event method, so those details should not be assumed from this overview alone.

implementsrunsCustom callbacksubclass of CallbackEvent methodselected eventTraining logiccustom action
What relationship is explicitly established between a custom callback, its event methods, and its training logic?

Mistakes in Callback Selection

  • Treating every callback as a model-saving mechanism.

    EarlyStopping interrupts training; ModelCheckpoint is the callback described as saving weights and keeping the best model according to a monitored metric.

    Fix: Choose ModelCheckpoint when preserving model weights matters.

  • Using EarlyStopping when the desired response is a learning-rate change.

    ReduceLROnPlateau is the callback described as changing the learning rate when validation loss stops improving.

    Fix: Choose ReduceLROnPlateau when the intended response is to change the learning rate rather than immediately stop.

  • Ignoring the monitored metric.

    ModelCheckpoint selects the best model according to a monitored metric, and EarlyStopping reacts when a monitored metric stops improving.

    Fix: Interpret the callback together with the metric it monitors.

  • Assuming patience means that a callback never acts.

    Patience is the configured period tolerated before EarlyStopping interrupts training.

    Fix: Treat patience as the allowed period without improvement before the callback takes its configured action.

  • Writing a custom callback when a built-in callback already matches the required action.

    Keras already provides ModelCheckpoint, EarlyStopping, and ReduceLROnPlateau for these stated training-control tasks.

    Fix: Use a built-in callback when its purpose matches the desired response; create a custom callback for actions not covered by the built-ins.

Practice the Decision

MEDIUM

For each goal, choose ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, or a custom callback: preserve only the best model according to a monitored metric; interrupt training after a monitored metric stops improving for the configured patience period; change the learning rate when validation loss stops improving; perform an action that none of the built-in callbacks provides.

Hints
  • Separate preserving weights from controlling whether training continues.
  • Look for the callback whose stated response is a learning-rate change.
  • Use a custom callback when the required training action is not covered by the built-in callbacks.
  1. A Keras callback is an object passed to fit and invoked at selected points during training. ModelCheckpoint saves weights and can preserve the best model according to a monitored metric. EarlyStopping interrupts training after a monitored metric stops improving for its configured patience period. ReduceLROnPlateau changes the learning rate when validation loss stops improving. Custom callbacks extend this event-driven approach by subclassing keras.callbacks.Callback and implementing methods for selected training events.

Key Takeaways

  • Callbacks let Keras observe training and respond during the existing fit process.
  • ModelCheckpoint preserves weights, EarlyStopping interrupts training, and ReduceLROnPlateau changes the learning rate.
  • A monitored metric determines what progress a callback evaluates.
  • Patience defines how long a callback tolerates a lack of improvement before acting.
  • Custom callbacks handle training actions that built-in callbacks do not cover.