peal.adaptors.group_dro_v2.GroupDROv2¶
- class peal.adaptors.group_dro_v2.GroupDROv2[source]¶
Bases:
AdaptorAdaptor that retrains a classifier with the group-robust objective.
- Parameters:
adaptor_config (
GroupDROv2Config) – SeeGroupDROv2Config.- Variables:
original_model (
torch.nn.Module) – The loaded classifier, kept untouched.model (
torch.nn.Module) – The deep copy that is actually retrained.test_group_counts (train_group_counts, val_group_counts,) – Samples per group, counted once by
compute_group_sizes().test_data_unpoisoned (
DataLoaderorNone) – Test loader built fromconfig.unpoisoned_data.
Methods
Entry point of the adaptor interface; runs
train().Run one pass over
loader, training or evaluating.Retrain the model with the group-robust objective and evaluate it.
- __init__(adaptor_config)[source]¶
Prepare the run directory, load the model and count the group sizes.
Seeds torch, moves an existing
base_diraside with a timestamp suffix, writesconfig.yamlinto the fresh one, loads the classifier, builds the train/validation dataloaders with groups enabled and dict-style batches, and optionally the unpoisoned test loader. Counting the group sizes walks each loader once.- Parameters:
adaptor_config (GroupDROv2Config)
- train()[source]¶
Retrain the model with the group-robust objective and evaluate it.
Runs one validation (and, if configured, test) pass at epoch
-1as a baseline, thentraining.max_epochstrain/validation rounds with SGD (momentum 0.9,config.weight_decay). The epoch’s validation score is the worst or the average group accuracy depending ontraining.early_stopping_goal; a new best writesmodel.cplintobase_dir. Withautomatic_adjustmentthe generalization adjustment is re-estimated each epoch as the train/validation gap scaled bysqrt(group_counts). Finally the best checkpoint is scored on the unpoisoned test split and its average and worst group accuracy are logged astest_final_*.Side effects: TensorBoard logs under
<base_dir>/logs, per-epoch checkpoints under<base_dir>/checkpointswhenconfig.save_intermediate.- Raises:
NotImplementedError – If
training.early_stopping_goalis neither"worst_group_accuracy"nor"average_group_accuracy".
- run_epoch(epoch, model, optimizer, loader, loss_computer, log_writer=None, mode='train')[source]¶
Run one pass over
loader, training or evaluating.Each batch is expected as a dict with
x,yandhas_confounder; the group index is2 * y + has_confounder. Gradients are only enabled, and the optimizer only steps, in"train"mode. After the pass the loss computer’s statistics are written to TensorBoard under the<mode>_prefix, and reset for the next training epoch (the evaluation computers are short-lived and discarded by the caller instead).- Parameters:
epoch (
int) – Step the statistics are logged at;-1for the baseline pass.model (
torch.nn.Module) – Model to train or evaluate.optimizer (
torch.optim.Optimizer) – Only used in"train"mode.loader (
torch.utils.data.DataLoader) – Loader yielding dict batches with group information.loss_computer (
GroupDroLossComputer) – Computes the loss and accumulates the statistics.log_writer (
torch.utils.tensorboard.SummaryWriter, optional) – WhenNone, nothing is logged.mode (
str, optional) –"train","val"or"test".