peal.training.loggers.Logger

class peal.training.loggers.Logger[source]

Bases: object

Accumulate step results of one epoch and write TensorBoard summaries.

The logger reads config.task.criterions to decide which metrics apply: ce / bce runs collect predicted classes and per-sample correctness (accuracy), mixed / focal_mixed / focal runs treat the first config.data.output_split outputs as multi-label logits and the rest as regression targets (micro / macro F1, precision, recall, MAE, MSE, focal loss, balanced accuracy). InvertibleGenerator models only log the loss. Step counters config.training.global_<mode>_step are incremented as a side effect of log_step.

Parameters:
  • config (PredictorConfig) – Run config; task, data and training are consulted.

  • model (torch.nn.Module) – The model being trained; used for its device and to detect generators.

  • optimizer (torch.optim.Optimizer) – Kept for reference only.

  • base_dir (str) – Run directory (images would be written under base_dir/outputs).

  • criterions (dict) – Loss functions of the run; kept for reference only.

  • val_dataloader (torch.utils.data.DataLoader) – Validation loader; its first batch is stored as test_X / test_y and its dataset’s attributes name the per-attribute scalars.

  • attacker (optional) – Adversarial attacker of the trainer; kept for reference only.

  • writer (torch.utils.tensorboard.SummaryWriter) – Destination of all summaries.

Variables:
  • output_channels (int) – config.task.output_channels or, if unset, config.data.output_size[0]; the width used for one-hot encoding predictions.

  • correct (losses, predictions, targets, predicted_classes,) – Per-step buffers that log_epoch reduces and resets.

Methods

log_epoch

Reduce the buffered steps, write the epoch summaries and reset buffers.

log_step

Record one training / validation step.

__init__(config, model, optimizer, base_dir, criterions, val_dataloader, attacker=None, writer=None)[source]

Store the trainer’s objects and initialise the per-epoch buffers.

Parameters:
  • config – See the class docstring.

  • model – See the class docstring.

  • optimizer – See the class docstring.

  • base_dir – See the class docstring.

  • criterions – See the class docstring.

  • val_dataloader – See the class docstring.

  • attacker – See the class docstring.

  • writer – See the class docstring.

log_step(mode, pred, y, loss_logs)[source]

Record one training / validation step.

Writes every entry of loss_logs as a 3_step_<mode>_<name> scalar at config.training.global_train_step, buffers the loss, targets and predictions needed for the epoch metrics and increments config.training.global_<mode>_step when that attribute exists.

Parameters:
  • mode (str) – "train", "validation" or a variant such as "validation_cf".

  • pred (torch.Tensor) – Model outputs of the step, [B, output_channels].

  • y (torch.Tensor) – Targets of the step (class indices for ce, multi-hot / mixed vectors otherwise).

  • loss_logs (dict) – Named loss values; must contain "loss".

log_epoch(mode, pbar=None)[source]

Reduce the buffered steps, write the epoch summaries and reset buffers.

The mean loss is written as 3_epoch_<mode>_loss_accumulated; classifier runs additionally get <0_|1_>epoch_<mode>_accuracy (or F1 / MAE / MSE / focal loss / balanced accuracy for mixed runs), per-channel prediction rates, a correct-per-class histogram and, for validation modes, per-attribute F1 and MAE scalars under 2_epoch_<mode>_*. Selected values are also stored in pbar.stored_values for the progress bar.

Parameters:
  • mode (str) – Same mode string as passed to log_step; modes starting with "validation" use the 0_ prefix, others 1_.

  • pbar (optional) – Progress bar with a stored_values dict to update.

Returns:

The epoch score early stopping compares: accuracy (ce / bce), micro F1 (mixed criterions) or exp(-mean loss) when no classification metric applies. An epoch with no collected steps yields exp(0) = 1.

Return type:

float