Practice in Lab: Update a detached Group DRO adversary from group means →
Update a detached Group DRO adversary from group means
Average risk can hide a poorly served subgroup when that group is rare. Group DRO maintains an adversary over declared groups, increasing mass on groups with larger observed mean loss. The adversary state is an optimization state, not a differentiable shortcut through every previous batch. Normalizing by each group count prevents duplicated rows from becoming an unintended group weight. Observing only a subset of groups requires an explicit estimator; this exercise instead declares complete group coverage per batch.
Prepare offline
Choose Download entire course or Download entire category first. After completion, code and checks run locally on CPU. The independent example needs no account, remote service or API key.
import torch
log_q = torch.tensor([0., 2.])
print(log_q.log_softmax(0).exp())Review the evidence
Separate group definition, adversary optimization and final evaluation. Compare average and worst-group held-out results with an ERM baseline; training group weights alone are not evidence of robustness under unseen shifts.
Your task
Return (robust_loss, next_log_weights). All G declared groups must occur in this batch. Average losses within each group, update adversary log weights by eta times DETACHED group means, normalize in log space, then weight differentiable group means by detached updated probabilities. Do not backpropagate into old adversary state. Reject empty/missing/out-of-range groups, nonfinite losses/state and nonpositive eta. This deliberately balanced-batch contract avoids silently inventing loss for unobserved groups.
The explanation opens here once every check passes.