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: object

Base 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: Callback

Parent 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: LogCallback

Saves 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: LogCallback

Creates 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: Callback

Saves 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: Callback

Creates 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

class qewton.optim.trainer.callbacks.stoper_callback.NaNStoppingCallback(priority=0)[source]

Bases: Callback

Callback that stops training if the loss becomes NaN.

on_train_step_end(state: TrainerState)[source]

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: Callback

Callback 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.

graphs

The graphs to run.

Type:

set of Graph

evaluation_phase

The phase of the evaluation.

Type:

EvaluationPhase

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: Callback

Callback 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:

EvaluationPhase

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: Callback

Callback that caches the data of data nodes from a given phase on.

When phase_to_start_cache starts, cache is 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.

data_nodes

The data nodes to cache.

Type:

list of DataNode

phase_to_start_cache

The phase at whose start the data is cached.

Type:

OptimizationPhase

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.