peal.training.loggers.Logger¶
- class peal.training.loggers.Logger[source]¶
Bases:
objectAccumulate step results of one epoch and write TensorBoard summaries.
The logger reads
config.task.criterionsto decide which metrics apply:ce/bceruns collect predicted classes and per-sample correctness (accuracy),mixed/focal_mixed/focalruns treat the firstconfig.data.output_splitoutputs as multi-label logits and the rest as regression targets (micro / macro F1, precision, recall, MAE, MSE, focal loss, balanced accuracy).InvertibleGeneratormodels only log the loss. Step countersconfig.training.global_<mode>_stepare incremented as a side effect oflog_step.- Parameters:
config (
PredictorConfig) – Run config;task,dataandtrainingare 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 underbase_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 astest_X/test_yand its dataset’sattributesname 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_channelsor, 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_epochreduces and resets.
Methods
Reduce the buffered steps, write the epoch summaries and reset buffers.
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_logsas a3_step_<mode>_<name>scalar atconfig.training.global_train_step, buffers the loss, targets and predictions needed for the epoch metrics and incrementsconfig.training.global_<mode>_stepwhen 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 force, 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 under2_epoch_<mode>_*. Selected values are also stored inpbar.stored_valuesfor the progress bar.- Parameters:
mode (
str) – Same mode string as passed tolog_step; modes starting with"validation"use the0_prefix, others1_.pbar (optional) – Progress bar with a
stored_valuesdict to update.
- Returns:
The epoch score early stopping compares: accuracy (
ce/bce), micro F1 (mixed criterions) orexp(-mean loss)when no classification metric applies. An epoch with no collected steps yieldsexp(0) = 1.- Return type: