This guide demonstrates how to use scikit-learn’s callbacks on compatible
estimators. For information about how to implement the callback API, you can
refer to the following sections of the developer’s guide:
In scikit-learn, callbacks are objects from the callback module that can
be registered on an estimator to insert custom logic like monitoring progress or
metrics, without modifying the underlying learning algorithm. The registered callbacks
are invoked at specific steps of the fitting process.
7.1. Registering callbacks
Estimators that support callbacks expose a set_callbacks
method to register callbacks on them. The following example shows how to register a
ProgressBar callback on a LogisticRegression:
During fit, the callbacks are invoked at the start and end of each task, where tasks are arbitrary units of work defined by the estimator. Usually, tasks
correspond to iterations of the estimator’s learning algorithm, but they can also
correspond to more abstract operations like fitting an estimator, steps of a pipeline,
cross-validation folds, etc. Within fit, tasks are divided into subtasks, which can
themselves be divided and so on, giving them a natural tree structure where fitting the estimator is the root task.
This tree structure will usually be reflected in a callback’s generated objects and
tasks will be identified by their name, id, and a reference to their parent task.
For tasks that have a natural ordering, like the iterations of a learning algorithm, the
ids are consecutive integers starting from 0. Some callbacks may provide additional
contextual information about the tasks. Here’s an example of the logs of the
ScoringMonitor:
When using callbacks in estimator compositions, involving estimators and
meta-estimators, we distinguish two types of callbacks: regular and
auto-propagated. They serve different purposes and are meant to be registered on
different estimators or meta-estimators in the composition.
7.3.1. Regular callbacks
Regular callbacks are meant to be invoked within the fit of a given estimator. Their
goal is usually to track the learning process of that estimator.
ScoringMonitor, for example, records the scores at each iteration of a model.
A regular callback can be registered on an estimator at any level of a composition. If a
regular callback is registered on an estimator that is cloned by a
meta-estimator, possibly multiple times, that callback will be invoked in each one of
the fit executions of the clones.
For example, when tuning the hyperparameters of a
LogisticRegression using a
GridSearchCV, a ScoringMonitor can be
registered on the LogisticRegression to monitor the
scores of the logistic regression model for each parameter combination and each fold of
the grid search:
Auto-propagated callbacks are meant to be invoked within the
fit of all (meta-)estimators in an estimator composition. Their goal is usually to
report more general information about the status at each step of the composition.
ProgressBar for instance displays nested progress bars for the
meta-estimators, their sub-estimators and so on. When registered on a meta-estimator, an
auto-propagated callback will automatically be registered on all its sub-estimators that
support callbacks.
registration restrictions
Auto-propagated callbacks are designed to be registered on the top-level
meta-estimator of an estimator composition. If some of its sub-estimators already
have auto-propagated callbacks registered on them, an error will be raised.
If the top-level meta-estimator doesn’t itself support callbacks, then its
sub-estimators are allowed to have auto-propagated callbacks registered on them.
However, be aware that this is not optimal and that callbacks might not be able to
deliver their full capabilities.
Let’s add progress bars to the example of the previous section, tuning the
hyperparameters of a . The
callback needs to be registered on the grid search:
7.4. Scikit-learn’s built-in callbacks
The built-in callbacks currently available in scikit-learn are the following:
Auto-propagated callbacks can be distinguished from regular callbacks by the fact
that they have a max_propagation_depth attribute. This attribute indicates the
maximum depth of nested estimators at which the callback will be propagated. In the
previous example, the grid search is at depth 0 and the logistic regression is at
depth 1 for instance.
If max_propagation_depth is set to 0, the callback will not be propagated to any
sub-estimators and will only be invoked for the (meta-)estimator it is registered
on. If it is set to None, it will be propagated to all nested levels of the
estimator composition.