Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Module

callbacks

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.

How it works

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.

Syntax

keras.callbacks
model.fit(..., callbacks=[callback1, callback2])

Parameters

NameTypeDescription
callbackslistA list of callback objects to invoke during model execution.

Returns

Callback classes and utilities for hooking behavior into Keras model execution.

Key points

  • Callbacks encapsulate actions that run at defined points in a model lifecycle.
  • Attach multiple callback instances to fit through its callbacks keyword.
  • TensorBoard callbacks can record metrics continuously for visualization.
  • ModelCheckpoint can persist the model or its weights at configured intervals.
  • EarlyStopping can stop training when the monitored behavior no longer improves.
  • Subclassing the base callback offers a route to customize training behavior.

Common mistakes

  • Callbacks are not limited to training: they can also be attached to evaluation and prediction.
  • A custom loop requires explicit callback orchestration rather than automatic invocation by fit().
  • When managing callbacks in a custom loop, call lifecycle methods yourself at the corresponding points.

Example: Using callbacks during training

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.

When to use it

Training observability

Monitor experiments with metrics and visualizations.

Checkpointing

Save recoverable or best-performing model states.

Adaptive training control

Stop or alter training based on observed metrics.

Custom training logic

Add application-specific lifecycle behavior.

Interview questions

QHow are callbacks applied to a Keras model?

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.

QWhat problems do callbacks solve?

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.

QHow do you create a custom callback?

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.Callback
  • keras.callbacks.CallbackList
  • keras.callbacks.EarlyStopping
  • keras.callbacks.ModelCheckpoint
  • keras.callbacks.TensorBoard
  • keras.callbacks.LambdaCallback

Source: Callbacks API. Checked 2026-10-15.