liveBook · Manning

Callback

As explained in Deep Learning with R, Third Edition

A Keras callback is an object invoked at specified points during model training or evaluation to monitor state or perform actions.

A Keras callback is an object passed to fit() and invoked at defined points during training. It observes model state and performance, then performs actions such as stopping training, saving weights, changing learning rates, or logging metrics. section 7.3.2

callback_model_checkpoint()
callback_early_stopping()
callback_learning_rate_scheduler()
callback_reduce_lr_on_plateau()
callback_csv_logger()

Built-in callbacks cover checkpointing, early stopping, learning-rate scheduling, plateau reduction, and CSV logging. section 7.3.2

Stop early and keep the best model

Create a callback list with callback_early_stopping(monitor = "val_accuracy", patience = 1) and callback_model_checkpoint(filepath = "checkpoint_path.keras", monitor = "val_loss", save_best_only = TRUE). Pass it to fit() through callbacks = callbacks_list. EarlyStopping interrupts training when the monitored metric stops improving; ModelCheckpoint preserves the best epoch's model, which can later be restored with load_model(). section 7.3.3

When built-in behavior is insufficient, subclass Callback and implement lifecycle methods such as on_train_begin(), on_batch_end(), and on_epoch_end(). These methods receive logs containing metrics and related training information. A custom callback can collect per-batch losses and plot them after each epoch, or generate and display images for visual inspection. section 7.3.4 section 17.2.6

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains built-in uses, callback configuration, lifecycle methods, logs, and custom implementations without detailing framework internals.
Examples
Demonstrates MNIST checkpointing and early stopping, per-batch loss plotting, and generated-image grids displayed after epochs.
What it assumes
Assumes familiarity with compile(), fit(), validation data, metrics, and basic Keras models while teaching callback configuration from practical examples.
What you can do after
Apply it via Keras to monitor training, stop overfitting runs, preserve best models, adjust parameters, or create custom visualizations.
How it connects
Connects callbacks with fit(), validation metrics, model checkpointing, early stopping, learning-rate scheduling, and custom training 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 R, Third Edition. The section links point to the parts of the book it draws on.