liveBook · Manning

Callback

As explained in Deep Learning with Python, Third Edition

A user-defined callback that runs automatically during training to perform extra actions such as monitoring outputs.

A callback is an object passed to model.fit() that observes training state and acts automatically. It can monitor metrics, save models, adjust parameters, or stop training instead of leaving a long run unattended. section 7.3.2

When built-in callbacks do not cover a needed action, subclass keras.callbacks.Callback and implement lifecycle methods. Each method receives logs containing recent training or validation information; batch and epoch methods also receive an index. section 7.3.3

Record and plot batch losses

A LossHistory callback initializes per_batch_losses at training start, appends logs.get("loss") after every batch, and plots the collected values at epoch end before clearing the list. Pass an instance through callbacks=[LossHistory()] in model.fit(). section 7.3.3

Choose a lifecycle hook according to when the action belongs:

  • Use on_epoch_begin or on_epoch_end for epoch-level evaluation, plotting, or generated-output inspection. section 17.2.6

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains callback objects, lifecycle hooks, logs, subclassing, and working implementations, but does not detail callback internals beyond these APIs.
Examples
Uses batch-loss plotting, MNIST training, checkpoint restoration, and epoch-end grids of generated images.
What it assumes
Assumes familiarity with Keras models, compile(), fit(), epochs, batches, and training metrics.
What you can do after
Build it from scratch by subclassing keras.callbacks.Callback, implementing hooks, reading logs, and registering the instance with model.fit().
How it connects
Connects callbacks with model.fit(), training metrics, early stopping, model checkpointing, learning-rate scheduling, and visualization.

Also explained in

Compare how 19 books explain callback →

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.