qewton.optim.trainer.callbacks package¶
Module contents¶
Submodules¶
qewton.optim.trainer.callbacks.base_callback module¶
- class qewton.optim.trainer.callbacks.base_callback.Callback(priority=0)[source]¶
Bases:
objectBase class for callbacks. Override the hooks you need.
- Parameters:
priority (int, optional) – Priority of this callback. Defaults to 0. If multiple callbacks are called at the same time, they will be ordered accordingly to their priority.
- add_callback_info_to_state(state: TrainerState)[source]¶
Adds this callback to the state.
- property saves_data: bool¶
- on_training_start(state: TrainerState)[source]¶
- on_optimization_phases_start(state: TrainerState)[source]¶
- training_step(phase_idx: int, state: TrainerState)[source]¶
- on_train_step_end(state: TrainerState)[source]¶
- on_optimization_phases_end(state: TrainerState)[source]¶
- on_training_end(state: TrainerState)[source]¶
qewton.optim.trainer.callbacks.log_callback module¶
- class qewton.optim.trainer.callbacks.log_callback.LogCallback(file_name: str = 'training_logs', log_phase: EvaluationPhase = EvaluationPhase.ALWAYS, log_interval=-1, priority=-100)[source]¶
Bases:
CallbackParent class for all logging callbacks.
- Parameters:
file_name (str, optional) – The file where to save the logs to. Defaults to “training_logs”.
log_phase (EvaluationPhase, optional) – What data to log. Defaults to EvaluationPhase.ALWAYS, saving all data.
log_interval (int, optional) – How often to save the logs to disk. Defaults to -1, leading to saving whenever the trainer state is updating its internal logs.
priority (int, optional) – Priority of this callback. Defaults to -100.
- property saves_data: bool¶
- on_training_start(state: TrainerState)[source]¶
- on_train_step_end(state: TrainerState)[source]¶
- flatten_log_dict(state: TrainerState) dict[str, float | None][source]¶
- class qewton.optim.trainer.callbacks.log_callback.CSVLogger(file_name: str = 'training_logs', log_phase: EvaluationPhase = EvaluationPhase.ALWAYS, log_interval=-1, priority=-100)[source]¶
Bases:
LogCallbackSaves the log to a CSV file
- on_training_start(state: TrainerState)[source]¶
- on_train_step_end(state: TrainerState)[source]¶
- class qewton.optim.trainer.callbacks.log_callback.TensorboardLogger(file_name: str = 'training_logs', log_phase: EvaluationPhase = EvaluationPhase.ALWAYS, log_interval=-1, priority=-100)[source]¶
Bases:
LogCallbackCreates a tensorboard logging file.
- on_training_start(state: TrainerState)[source]¶
- on_train_step_end(state: TrainerState)[source]¶
- on_training_end(state: TrainerState)[source]¶
qewton.optim.trainer.callbacks.memory_callback module¶
- class qewton.optim.trainer.callbacks.memory_callback.ModelParameterSizeCallback(parameter_nodes: list[Node], memory_format: Literal['KB', 'MB', 'GB'] = 'MB', priority=0)[source]¶
Bases:
CallbackSaves the size of the model in at the end of the training process.
- Parameters:
parameter_nodes (list[Node]) – List of model parameter nodes to calculate the size of.
memory_format (Literal["KB", "MB", "GB"], optional) – Memory format to use for the size. Defaults to “MB”.
priority (int, optional) – Priority of the callback. Callbacks with higher priority are executed first. Defaults to 0.
- add_callback_info_to_state(state: TrainerState)[source]¶
Adds this callback to the state.
- on_training_start(state: TrainerState)[source]¶
qewton.optim.trainer.callbacks.progressbar_callback module¶
- class qewton.optim.trainer.callbacks.progressbar_callback.ProgressBarCallback(priority=-100)[source]¶
Bases:
CallbackCreates a progress bar for showing the training progress.
- on_optimization_phases_start(state: TrainerState)[source]¶
- on_train_step_end(state: TrainerState)[source]¶
- on_optimization_phases_end(state: TrainerState)[source]¶
qewton.optim.trainer.callbacks.stoper_callback module¶
qewton.optim.trainer.callbacks.training_callbacks module¶
Callbacks that evaluate graphs and functions or cache data during training.
- class qewton.optim.trainer.callbacks.training_callbacks.GraphEvalCallback(graphs: set[Graph], evaluation_phase: EvaluationPhase, constraints: list[Constraint] | set[Constraint], evaluation_interval=1, priority=0)[source]¶
Bases:
CallbackCallback that runs graphs and records the losses of constraints.
Every evaluation_interval steps, all graphs are run in evaluation_phase and the loss of each constraint is stored in
state.losses[evaluation_phase]under the constraint name. In the training phase the losses include the constraint weights.- Parameters:
graphs – The graphs to run.
evaluation_phase – The phase the graphs are run in and the losses are recorded for.
constraints – The constraints whose losses are recorded.
evaluation_interval (int, optional) – The number of steps between evaluations. Defaults to 1.
priority (int, optional) – The priority of this callback. Defaults to 0.
- evaluation_phase¶
The phase of the evaluation.
- Type:
- evaluation_interval¶
The number of steps between evaluations.
- Type:
int
- constraints¶
The constraints whose losses are recorded.
- Type:
list or set of Constraint
- training_step(phase_idx: int, state: TrainerState)[source]¶
Run the graphs and record the losses, if the step is due.
- Parameters:
phase_idx – The step index within the current optimization phase.
state – The trainer state the losses are written to.
- Raises:
ValueError – If two different constraints have the same name.
- class qewton.optim.trainer.callbacks.training_callbacks.FunctionEvalCallback(functions: list[Callable[[int, TrainerState], Any]], evaluation_phase: EvaluationPhase, evaluation_interval=1, priority=0)[source]¶
Bases:
CallbackCallback that records the values of functions as losses.
Every evaluation_interval steps, each function is called with the step index and the trainer state, and its result is stored in
state.losses[evaluation_phase]under the function’s name.- Parameters:
functions – The functions to evaluate.
evaluation_phase – The phase the values are recorded for.
evaluation_interval (int, optional) – The number of steps between evaluations. Defaults to 1.
priority (int, optional) – The priority of this callback. Defaults to 0.
- functions¶
The functions to evaluate.
- Type:
list of callable
- evaluation_phase¶
The phase the values are recorded for.
- Type:
- evaluation_interval¶
The number of steps between evaluations.
- Type:
int
- training_step(phase_idx: int, state: TrainerState)[source]¶
Evaluate the functions and record their values, if the step is due.
- Parameters:
phase_idx – The step index within the current optimization phase.
state – The trainer state the values are written to.
- Raises:
ValueError – If two different functions have the same name.
- class qewton.optim.trainer.callbacks.training_callbacks.CacheDataCallback(data_nodes: list[DataNode], phase_to_start_cache: OptimizationPhase, cache_batches: int = 1, priority=0)[source]¶
Bases:
CallbackCallback that caches the data of data nodes from a given phase on.
When phase_to_start_cache starts,
cacheis called on every data node, so they provide the same data from then on, e.g. for an LBFGS phase.- Parameters:
data_nodes – The data nodes to cache.
phase_to_start_cache – The optimization phase at whose start the data is cached.
cache_batches – The number of batches to cache.
priority (int, optional) – The priority of this callback. Defaults to 0.
- phase_to_start_cache¶
The phase at whose start the data is cached.
- Type:
- cache_batches¶
The number of batches to cache.
- Type:
int
- on_optimization_phases_start(state: TrainerState)[source]¶
Cache the data if phase_to_start_cache is starting.
- Parameters:
state – The trainer state.