peal.adaptors.group_distributionally_robust_optimization.GroupDRO¶
- class peal.adaptors.group_distributionally_robust_optimization.GroupDRO[source]¶
Bases:
AdaptorAdaptor that (re)trains a predictor with the GroupDRO objective.
Groups are
y * output_size + confounderfor PEAL datasets and the dataset’s own group index for the replication datasets. Only SGD (momentum 0.9, weight decaytask.criterions["l2"]) and a singlece/bce/mse/maeobjective are supported.- Parameters:
adaptor_config (
dictorstrorPathorAdaptorConfig) – AGroupDROConfig(or yaml path);predictorsupplies the architecture, training, data and task configs.model_path (
str, optional) – Overridesadaptor_config.model_path; default is the predictor’smodel_pathwith aDRO_prefix on its last component.model (
torch.nn.Module, optional) – Model to adapt; built from the architecture config whenNone(SequentialModelfor replication runs,TorchvisionModelfortorchvision_*strings).datasource (
strorsequenceofDataset, optional) – Dataset root or(train, val[, test])datasets; defaults todata_config.dataset_path. Ignored for replication datasets.optimizer – Accepted for interface compatibility with
ModelTrainerbut unused.criterions – Accepted for interface compatibility with
ModelTrainerbut unused.logger – Accepted for interface compatibility with
ModelTrainerbut unused.only_last_layer – Accepted for interface compatibility with
ModelTrainerbut unused.unit_test_train_loop – Accepted for interface compatibility with
ModelTrainerbut unused.unit_test_single_sample – Accepted for interface compatibility with
ModelTrainerbut unused.log_frequency – Accepted for interface compatibility with
ModelTrainerbut unused.gigabyte_vram – Accepted for interface compatibility with
ModelTrainerbut unused.val_dataloader_weights – Accepted for interface compatibility with
ModelTrainerbut unused.
- Variables:
test_dataloader (train_dataloader, val_dataloaders,) – Loaders created by
_load_PEAL_datasetor_load_replication_dataset.objective (
torch.nn.Module) – Per-sample (reduction="none") loss selected from the task’scriterions.
- Raises:
NotImplementedError – If
replication_datasetis notNone,"celeba"or"waterbirds".Exception – If no supported architecture is found, or the task defines several non-
l2criteria or an unsupported one.
Methods
Train for
training.max_epochsepochs with the GroupDRO loss.Run one training or validation pass over
loader.- __init__(adaptor_config='<PEAL_BASE>/configs/adaptors/test_dro.yaml', model_path=None, model=None, datasource=None, optimizer=None, criterions=None, logger=None, only_last_layer=False, unit_test_train_loop=False, unit_test_single_sample=False, log_frequency=1000, gigabyte_vram=None, val_dataloader_weights=[1.0])[source]¶
Unpack the config, build the dataloaders, the model and the objective.
Resolves
model_path, loads either the PEAL dataset fromdatasourceor one of the replication datasets (celeba,waterbirds), instantiates the architecture whenmodelisNone(optionally resetting its weights), and picks the single per-sample objective fromtask.criterions. See the class docstring for the parameters and the exceptions raised.- Parameters:
adaptor_config (dict | str | Path | AdaptorConfig)
- run(continue_training=False, is_initialized=False)[source]¶
Train for
training.max_epochsepochs with the GroupDRO loss.Side effects: an existing
model_pathis renamed with an_old_timestamp suffix (unlessis_initialized),<model_path>/config.yamlis written,checkpoints/<epoch>.cplis saved after every epoch andcheckpoints/final.cplwhenever the validationavg_accof theLossComputerimproves.- Parameters:
- Raises:
ValueError – If
generalization_adjustmentis neither a scalar nor a list of length 1 orn_groups.NotImplementedError – If the training optimizer is not
"sgd".
Notes
A
ReduceLROnPlateauscheduler is created whenconfig.scheduleris set but never stepped.
- run_epoch(epoch, model, optimizer, loader, loss_computer, is_training, pbar=None)[source]¶
Run one training or validation pass over
loader.- Parameters:
epoch (
int) – Current epoch (only for bookkeeping).model (
torch.nn.Module) – Model to evaluate/update; switched to train or eval mode.optimizer (
torch.optim.Optimizer) – Stepped after every batch whenis_training.loader (
DataLoader) – Yields(x, (y, confounder))for PEAL datasets or(x, y, group)for replication datasets.loss_computer (
LossComputer) – Accumulates per-group statistics; itsavg_actual_lossandavg_accare printed at the end and then reset.is_training (
bool) – Whether gradients are enabled and the optimizer steps.pbar (
tqdm) – Progress bar advanced by one per batch; must not beNone.