Developing callbacks#
The callback class#
To be compatible with scikit-learn estimators, callbacks must inherit from the
FitCallback class.
The following 4 methods provided by that class are referred to as callback hooks. They will be called at specific steps during the fitting process of the estimator the callback is registered on. A callback class has to overwrite the ones it needs.
-
These hooks are only called once, respectively at the start and end of
fit. They take care of setting up and tearing down the callback, like allocating and freeing resources for instance. on_fit_task_beginandon_fit_task_endThese hooks are called at the beginning and end of each task during
fit.In concrete implementations of callbacks, only the optional keyword-only arguments actually used by the hook should be explicitly declared in the hook signature. The presence of an argument in the signature signals that the hook requires that argument, which allows the callback framework to avoid computing values that are not used by any registered callback.
Warning
These arguments must be defined as keyword only. If the kwargs are not keyword only, the values will not be provided to the hooks.
Even if requested, the optional arguments might or might not be provided by the estimator, depending on its ability to produce them at this task. Thus the implementation of the hooks should not expect to always receive a value for each of them and adapt their behavior accordingly.
Interrupting
fit#The
on_fit_task_endhook returns a boolean, which when set toTrue, requests the estimator to stop thefitprocess at this task. Note that estimators that don’t aim to be interruptible will ignore this request and continue with the next task.
All the hooks receive, as mandatory arguments, the estimator instance calling the
callback and the CallbackContext object holding the contextual information
that allows unique identification of the task that is being processed as public
attributes. See CallbackContext for more details.
The estimator argument#
The estimator instance received by the hooks, as a mandatory argument, is in the
same state as it was when calling the hook during fit. Therefore it is not
expected to be fully fitted (except for the teardown hook).
Callbacks should not rely on it to predict, transform, etc … but
rather use the fitted_estimator when available.
Requesting metadata#
In addition to values produced by the estimator (such as X, y, and
fitted_estimator), callbacks can receive routed metadata (e.g. sample_weight). To be
requestable, parameters must be declared as keyword-only parameters of the hook. The
routed metadata reach a hook only when metadata routing is
enabled and the callback requested it for that hook. Commonly used metadata such as
sample_weight, X_val, y_val and sample_weight_val are expected to be
automatically requested by the callback, see auto-requested metadata. For example, the sample_weight metadata is
automatically requested for the on_fit_task_end hook in the following example:
from sklearn.callback import FitCallback
class MyCallback(FitCallback):
def on_fit_task_end(self, estimator, context, *, sample_weight=None):
...
def get_metadata_routing(self):
requests = super().get_metadata_routing()
requests.on_fit_task_end.add_auto_request("sample_weight")
return requests
Manually requesting metadata#
When auto-requests are disabled or if the metadata must be requested under
different names, manual requests can be made using the
set_on_fit_task_begin_request or set_on_fit_task_end_request methods.
For example:
from sklearn.callback import FitCallback
class MyCallback(FitCallback):
def on_fit_task_end(self, estimator, context, *, sample_weight=None):
...
callback = MyCallback().set_on_fit_task_end_request(sample_weight=True)
True requests the parameter under its own name. A string requests a fit argument
under a different name, for example
set_on_fit_task_end_request(sample_weight="sw_val").
Forwarding sample_weight while metadata routing is disabled#
Until metadata routing is always enabled, sample_weight can still be forwarded
while routing is disabled. Override _accept_sample_weight and return True for
each hook that should receive it. For example, to have a callback accepting
sample_weight only for the on_fit_task_end hook while routing is disabled, the
_accept_sample_weight method should look like this:
class MyCallback(FitCallback):
def _accept_sample_weight(self, hook_name):
"""Whether the callback accepts sample_weight for a given hook."""
if hook_name == "on_fit_task_end":
return True
return False
Auto-propagated callbacks#
Auto-propagated callbacks, i.e. callbacks that are expected to
be propagated from meta-estimators to their sub-estimators, must inherit from the
AutoPropagatedCallbackMixin class in addition to the FitCallback
class. They must also set the max_propagation_depth property to indicate the maximum
depth of the propagation. For example:
class MyAutopropagatedCallback(AutoPropagatedCallbackMixin, FitCallback):
def __init__(self, max_propagation_depth=2):
self.max_propagation_depth = max_propagation_depth
def on_fit_task_begin(self, estimator, context, *, X=None, y=None):
...
def on_fit_task_end(self, estimator, context, *, X=None, y=None):
...
By contrast with regular callbacks that are only invoked at the tasks of the estimator
on which they are registered, auto-propagated callbacks are invoked at the tasks of
all the estimators in estimator compositions, up to the maximum propagation depth. If
set to 0, the callback is not propagated to sub-estimators and only invoked at the tasks
of the top-level estimator. If set to None, the callback is propagated to
sub-estimators at all nesting levels.
Auto-propagated callbacks should be registered on the top-level estimator. If the top-level estimator does not support callbacks, they can be registered on sub-estimators and are expected to work, though possibly not at full capacity.
Minimal example#
Here is an example implementation of a simple custom callback that prints a message every time it is invoked:
from sklearn.callback import FitCallback
class MyCallback(FitCallback):
def setup(self, estimator, context):
print(f"Setup hook is being called in the {context.task_name} task.")
def teardown(self, estimator, context):
print(f"Teardown hook is being called in the {context.task_name} task.")
def on_fit_task_begin(self, estimator, context, *, X=None):
msg = f"{context.task_name} task is starting."
if X is not None:
msg += f" With training data of shape {X.shape}."
print(msg)
def on_fit_task_end(
self, estimator, context, *, X=None, y=None, fitted_estimator=None
):
msg = f"{context.task_name} task is ending."
if y is not None and X is not None and fitted_estimator is not None:
mean_squared_error = ((y - fitted_estimator.predict(X))**2).mean()
msg += f" With a mean squared error of {mean_squared_error}."
print(msg)