Callbacks¶
Callbacks module contains classes and functions for handling callback functions during an event-driven process. This makes it easier to customize the behavior of the training loop and add additional functionality to the training process without modifying the core code.
To use a callback, create a class that inherits from the Callback class and implement the necessary methods. Callbacks can be used to perform actions at different stages of the training process, such as at the beginning or end of an epoch, batch, or fitting process. Then pass the callback object to the Trainer.
AvgStats
¶
AvgStats class is used to track and accumulate average statistics (like loss and other metrics) during training and validation phases.
Attributes: metrics (list): A list of metric functions to be tracked. in_train (bool): A flag to indicate if the statistics are for the training phase.
Methods: init(metrics, in_train): Initializes the AvgStats with metrics and in_train flag.
reset():
Resets the accumulated statistics.
all_stats:
Property that returns all accumulated statistics including loss and metrics.
avg_stats:
Property that returns the average of the accumulated statistics.
accumulate(run):
Accumulates the statistics using the data from the given run.
__repr__():
Returns a string representation of the average statistics.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
AvgStatsCallBack
¶
Bases: Callback
AvgStatsCallBack class is a custom callback used to track and print average statistics for training and validation phases during the training loop.
Arguments: metrics: A list of metric functions to evaluate during training and validation.
Methods: init: Initializes the callback with given metrics and sets up AvgStats objects for both training and validation phases. begin_epoch: Resets the statistics at the beginning of each epoch. after_loss: Accumulates the metrics after computing the loss, differentiating between training and validation phases. after_epoch: Prints the accumulated statistics for both training and validation phases after each epoch.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
Callback
¶
Callback class is a base class designed for handling different callback functions during an event-driven process. It provides functionality to set a runner, retrieve the class name in snake_case format, directly call callback methods, and delegate attribute access to the runner if the attribute does not exist in the Callback class.
The _order is used to decide the order of Callbacks.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
ParamScheduler
¶
Bases: Callback
Manages scheduling of parameter adjustments over the course of training.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
begin_batch()
¶
Apply parameter adjustments at the beginning of each batch if in training mode.
begin_fit()
¶
Prepare the scheduler at the start of the fitting process. This method ensures that sched_funcs is a list with one function per parameter group.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
set_param()
¶
Adjust the parameter value for each parameter group based on the scheduling function. Ensures the number of scheduling functions matches the number of parameter groups.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
Recorder
¶
Bases: Callback
Recorder is a callback class used to record learning rates and losses during the training process.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
after_batch()
¶
Handles operations to execute after each training batch.
Modifies the learning rate for each parameter group in the optimizer and appends the current learning rate and loss to the corresponding lists.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
begin_fit()
¶
Initializes attributes necessary for the fitting process.
Sets up learning rates and losses storage.
Attributes: self.lrs (list): A list of lists, where each inner list will hold learning rates for a parameter group. self.losses (list): An empty list to store loss values during the fitting process.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
plot(skip_last=0, pgid=-1)
¶
Generates a plot of the loss values against the learning rates.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
plot_loss(skip_last=0)
¶
TrainEvalCallback
¶
Bases: Callback
TrainEvalCallback class is a custom callback used during the training and validation phases of a machine learning model to perform specific actions at the beginning and after certain events.
Methods:
begin_fit(): Initialize the number of epochs and iteration counts at the start of the fitting process.
after_batch(): Update the epoch and iteration counts after each batch during training.
begin_epoch(): Set the current epoch, switch the model to training mode, and indicate that the model is in training.
begin_validate(): Switch the model to evaluation mode and indicate that the model is in validation.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
annealer(f)
¶
A decorator function for creating a partially applied function with predefined start and end arguments.
The inner function _inner
captures the start
and end
parameters and returns a partial
object that fixes these parameters for the decorated function f
.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
camel2snake(name)
¶
Convert name
from camel case to snake case.
combine_scheds(pcts, scheds)
¶
Combine multiple scheduling functions.
Source code in temp_dir/pytorch/openml_pytorch/callbacks.py
listify(o=None)
¶
Convert o
to list. If o
is None, return empty list.