Training observability
Monitor experiments with metrics and visualizations.
Keras · Module
Keras callbacks are objects that run actions at selected stages of model execution. Pass them as a list to model methods such as fit() to add logging, checkpointing, early stopping, or custom behavior.
The callbacks module provides objects that hook into stages of model training, evaluation, and inference. Supply one or more callback instances to a model method, and Keras invokes their lifecycle methods at events such as epoch or batch boundaries.
keras.callbacks
model.fit(..., callbacks=[callback1, callback2])| Name | Type | Description |
|---|---|---|
callbacks | list | A list of callback objects to invoke during model execution. |
Callback classes and utilities for hooking behavior into Keras model execution.
import keras
model = keras.Sequential([
keras.layers.Input(shape=(4,)),
keras.layers.Dense(1)
])
model.compile(optimizer="adam", loss="mse")
callbacks = [
keras.callbacks.EarlyStopping(patience=2),
keras.callbacks.ModelCheckpoint("model.keras", save_best_only=True),
]
model.fit(x_train, y_train, epochs=10, callbacks=callbacks)Pass callback instances in a list through the callbacks argument of fit; Keras invokes each callback at the relevant training stages.
Monitor experiments with metrics and visualizations.
Save recoverable or best-performing model states.
Stop or alter training based on observed metrics.
Add application-specific lifecycle behavior.
The callbacks argument accepts a list of callback objects. Keras then invokes each object's lifecycle methods at the relevant stages, such as epoch or batch boundaries.
Follow-up: How would a custom loop use callbacks? Pack them into a CallbackList and manually invoke the appropriate on_* methods.
Callbacks can observe and act during training, evaluation, and inference. Typical uses include logging, checkpointing, early stopping, and inspecting model state.
Follow-up: Which callback mechanism supports simple one-off behavior? LambdaCallback accepts functions for selected lifecycle events.
A custom callback subclasses keras.callbacks.Callback and overrides the method associated with the stage it needs to handle. This lets application-specific behavior run at selected lifecycle points.
Follow-up: What data can callback methods receive? The logs dictionary contains quantities relevant to the current batch or epoch.
keras.callbacks.Callbackkeras.callbacks.CallbackListkeras.callbacks.EarlyStoppingkeras.callbacks.ModelCheckpointkeras.callbacks.TensorBoardkeras.callbacks.LambdaCallback