CallbackContext
-
class sklearn.callback.CallbackContext
Task level context for the callbacks.
This class is responsible for managing the callbacks and holding the tree structure
of an estimator’s tasks. Each instance corresponds to a task of the estimator.
This class should not be instantiated directly, but through the
_init_callback_context method of the estimator to create the root context or using
the subcontext method of this class to create sub-contexts.
These contexts are passed to the callback hooks to be able to keep track of the
position of a task in the task tree from within the callbacks.
- Attributes:
- task_name
: str
The name of the task this context is responsible for.
task_id : intThe identifier of the task this context is responsible for. It uniquely
identifies the task among its siblings.
max_subtasks : int or NoneThe maximum number of children tasks for this task. 0 means it’s a leaf.
None means the maximum number of subtasks is not known in advance.
sequential_subtasks : boolWhether this context’s subtasks are sequential. When True, children contexts’
have consecutive integer task_ids starting from 0.
estimator_name : strThe name of the estimator that holds this context.
parent : CallbackContext or NoneThe parent context of this context. None if this context is the root.
root_uuid : uuid.UUID instanceThe UUID of the root context. All contexts in the same task tree have the same
root UUID that is used to identify the task tree itself.
init_time : datetime.datetimeThe time when the context was initialised, in the UTC timezone.
source_estimator_name : str or NoneThe name of the estimator that holds the parent task this task was
merged with. None if it was not merged with another context.
source_task_name : str or NoneThe task name of the parent task this task was merged with. None if it
was not merged with another context.
-
call_on_fit_task_begin(*, estimator, X=None, y=None, metadata=None, reconstruction_attributes=None)
Call the on_fit_task_begin hook of the callbacks.
- Parameters:
- estimator : estimator instance
The estimator calling the callback hook.
- X : array-like or None, default=None
The training data of the current task.
- y : array-like or None, default=None
The training targets of the current task.
- metadata : dict or None, default=None
A dictionary containing training metadata for the current task.
- reconstruction_attributes : dict or None, default=None
A dictionary of the sufficient fitted attributes needed to construct a
fitted_estimator from the current state of the estimator, i.e. an
estimator instance ready to predict, transform, etc … as if the fit had
stopped at the beginning of this task. The fitted_estimator is the
object that will be passed to the callbacks, if required.
-
call_on_fit_task_end(*, estimator, X=None, y=None, metadata=None, reconstruction_attributes=None)
Call the on_fit_task_end hook of the callbacks.
- Parameters:
- estimator : estimator instance
The estimator calling the callback hook.
- X : array-like or None, default=None
The training data of the current task.
- y : array-like or None, default=None
The training targets of the current task.
- metadata : dict or None, default=None
A dictionary containing training metadata of the current task.
- reconstruction_attributes : dict or None, default=None
A dictionary of the sufficient fitted attributes needed to construct a
fitted_estimator from the current state of the estimator, i.e. an
estimator instance ready to predict, transform, etc … as if the fit had
stopped at the end of this task. The fitted_estimator is the object
that will be passed to the callbacks, if required.
- Returns:
- stop : bool
Whether or not to stop the current level of iterations at this end of this
task.
-
propagate_callback_context(sub_estimator)
Propagate the context and callbacks to a sub-estimator.
Clear the propagated callbacks from the sub-estimator on exit.
Only auto-propagated callbacks are propagated to the sub-estimator. An error is
raised if the sub-estimator already holds auto-propagated callbacks.
The sub-estimator receives this context as an attribute named
_parent_callback_ctx so that the meta-estimator’s task tree can be merged with
the sub-estimator’s one.
- Parameters:
- sub_estimator : estimator instance
The estimator to propagate the callbacks and context to.
-
subcontext(task_name='', task_id=None, max_subtasks=0, sequential_subtasks=True)
Create a context for a subtask of the current task.
- Parameters:
- task_name : str, default=””
The name of the subtask.
- task_id : int or None, default=None
An identifier of the subtask. It must be distinct from the task_ids of its
siblings. If None, task_id is automatically set to the next available
integer task_id.
- max_subtasks : int or None, default=0
The maximum number of tasks that can be children of the subtask. 0 means
it’s a leaf. None means the maximum number of subtasks is not known in
advance.
- sequential_subtasks : bool, default=True
Whether the new context’s subtasks are sequential. If True, children
contexts of the new context, created via subcontext, will have
automatically assigned consecutive integer task_ids starting from 0.