peal.training.trainers.ModelTrainer¶
- class peal.training.trainers.ModelTrainer[source]¶
Bases:
objectTrain a predictor from a
PredictorConfigwith early stopping.On construction the model, dataloaders (via
create_dataloaders_from_datasource), optimizer (sgdwith a 0.95 exponential schedule,adamoradamw), criterions andLoggerare built from the config.training.class_balancedwraps the train loader in aDataloaderMixerand splits every validation loader into one class-restricted copy per class. Training happens infit().- Parameters:
config (
str,dictorPredictorConfig) – Loaded withload_yaml_config; consumesmodel_path,only_last_layer,tracking_leveland thetraining,task,dataandarchitecturesections.model_path (
str, optional) – Output directory; overridesconfig.model_path.model (
torch.nn.Module, optional) – Pre-built model; otherwiseget_predictor()is used.datasource (optional) – Datasets or dataloaders handed to
create_dataloaders_from_datasource.optimizer (
torch.optim.Optimizer, optional) – Overrides the optimizer built fromtraining.optimizer.criterions (
dict, optional) – Overrides the criterions built fromtask.criterions.logger (
Logger, optional) – Overrides the defaultLogger.only_last_layer (
bool, optional) – Train only the final linear layer (model.fcor the last weight and bias). Defaults toconfig.only_last_layer.unit_test_train_loop (
bool, optional) – Stop every epoch after two batches.unit_test_single_sample (
bool, optional) – Replace every batch by the logger’s fixed test sample.log_frequency (
int, optional) – Stored on the instance; not used by the loop itself.val_dataloader_weights (
listoffloat, optional) – Weights for averaging validation accuracies across loaders.
- Variables:
Methods
Run
_fit()with every parameter the optimizer does not update frozen.Run one pass over
dataloaderin train or validation mode.- __init__(config, model_path=None, model=None, datasource=None, optimizer=None, criterions=None, logger=None, only_last_layer=None, unit_test_train_loop=False, unit_test_single_sample=False, log_frequency=1000, val_dataloader_weights=[1.0])[source]¶
Build model, dataloaders, optimizer, criterions and logger.
See the class docstring for the parameters.
- run_epoch(dataloader, mode='train', pbar=None, debug=False)[source]¶
Run one pass over
dataloaderin train or validation mode.Per batch: optional diffusion augmentation, label smoothing, mixup and PGD adversarial attack (train mode only), forward pass (with latents when the
lccriterion is configured), the weighted sum oftask.criterions(regularisers scaled byregularization_level),loss.backward()and, in train mode, an optimizer step. Every step is reported toself.loggerand the progress bar; the loop stops aftertraining.steps_per_epochbatches when that is set.- Parameters:
dataloader (
torch.utils.data.DataLoaderorDataloaderMixer) – Batches of(X, y); when the loader hasreturn_srcthe source distribution is tracked for the progress bar.mode (
str, optional) –"train"or"validation_<i>"; passed to the logger.pbar (
tqdm) – Progress bar with astored_valuesdict (required).debug (
bool, optional) – Unused.
- Returns:
(last_batch_loss, accuracy)where the accuracy comes fromlogger.log_epoch; the loss is 0.0 when no batch was yielded.- Return type:
- fit(continue_training=False, is_initialized=False)[source]¶
Run
_fit()with every parameter the optimizer does not update frozen.only_last_layerhands the optimizer just the head, but the backbone keptrequires_grad=True, so every step stored its activations and computed gradients that were thrown away: a DINOv3 ViT-L linear probe needed a full backward pass (and ran out of memory next to other jobs on 2026-09-28). The flags are restored afterwards because the same model object can later be fine-tuned in full (CFKD), where permanently frozen parameters would silently stop training.