peal.adaptors.group_dro_v2.GroupDroLossComputer¶
- class peal.adaptors.group_dro_v2.GroupDroLossComputer[source]¶
Bases:
objectGroup-robust loss plus the running per-group statistics it needs.
Wraps a per-sample cross-entropy: the batch is split into the four groups, per-group mean losses and accuracies are computed, and the batch loss is the dot product of the group losses with an adversarial weight vector
adv_probsthat is updated multiplicatively (exponentiated gradient ascent withstep_size) so that hard groups get more weight. The running statistics double as the epoch’s evaluation metrics, which is why the same class is also instantiated for the validation and test passes.- Parameters:
group_counts (
torch.Tensor) – Number of training samples per group, shape(4,); used for the group fractions and the generalization adjustment.alpha (
float) – Tail size of the “best-that-can-happen” (BTL) / greedy variant: the fraction of the data the worst-case average is taken over.gamma (
float, optional) – Step size of the exponential moving average of the group losses, which the BTL variant ranks groups by. Default 0.1.adj (
numpy.ndarray, optional) – Per-group generalization adjustmentC_g; added asadj / sqrt(group_counts)before the weight update. Default zeros.min_var_weight (
float, optional) – Mixes the greedy weights back towards the empirical group fractions.step_size (
float, optional) – Learning rate of the adversarial weight update. Default 0.01.normalize_loss (
bool, optional) – Normalise the adjusted group losses to sum to one before the update.btl (
bool, optional) – Use the BTL/greedy worst-case weighting instead of the exponentiated gradient weights.device (
str, optional) – Device all running statistics live on.
- Variables:
n_groups (
int) – Fixed to 4 (label x confounder).group_str (
listofstr) – Group names"<y>-<has_confounder>"used in the logged keys.adv_probs (
torch.Tensor) – Current adversarial distribution over the groups.exp_avg_loss (
torch.Tensor) – Exponential moving average of the per-group losses.avg_group_loss (avg_group_acc,) – Running per-group averages since the last
reset_stats().
Notes
adv_probsis never reset byreset_stats(); it is meant to persist across epochs. It is also only applied when every entry ofadjis positive, mirroring the reference implementation.Methods
Mean of a per-sample quantity within each group, plus the group counts.
Exponentiated-gradient update of
adv_probsand the weighted loss.Robust loss ranking groups by their historical (moving-average) loss.
Average the
alphaworst fraction of the data, ranked byref_loss.Add the squared parameter norm and the implied L2 penalty to
stats_dict.Collect the running statistics as a flat dict for TensorBoard.
Group-robust loss of one batch, updating all running statistics.
Zero the per-epoch running statistics.
Advance the moving average of the per-group losses by one batch.
Fold one batch into the running averages reported by
get_stats().- __init__(group_counts, alpha, gamma=0.1, adj=None, min_var_weight=0, step_size=0.01, normalize_loss=False, btl=False, device='cpu')[source]¶
Set up the per-sample criterion, the adjustment and the running stats.
- loss(yhat, y, group_idx)[source]¶
Group-robust loss of one batch, updating all running statistics.
- Parameters:
yhat (
torch.Tensor) – Logits of shape(N, C).y (
torch.Tensor) – Integer labels of shape(N,).group_idx (
torch.Tensor) – Group index in0..3per sample, shape(N,).
- Returns:
Scalar loss to backpropagate: the group losses weighted by the (updated) adversarial distribution.
- Return type:
- compute_robust_loss(group_loss)[source]¶
Exponentiated-gradient update of
adv_probsand the weighted loss.- Parameters:
group_loss (
torch.Tensor) – Mean loss per group, shape(4,).- Returns:
The scalar robust loss and the updated group weights.
- Return type:
- compute_robust_loss_btl(group_loss)[source]¶
Robust loss ranking groups by their historical (moving-average) loss.
- Parameters:
group_loss (
torch.Tensor) – Mean loss per group of the current batch, shape(4,).- Returns:
The scalar robust loss and the weights used, in group order.
- Return type:
- compute_robust_loss_greedy(group_loss, ref_loss)[source]¶
Average the
alphaworst fraction of the data, ranked byref_loss.Groups are sorted by
ref_lossdescending and taken until their cumulative data fraction reachesalpha; the group that straddles the boundary gets the remaining weight. The result is then mixed with the empirical group fractions according tomin_var_weight.- Parameters:
group_loss (
torch.Tensor) – Mean loss per group of the current batch, shape(4,).ref_loss (
torch.Tensor) – Scores the groups are ranked by, shape(4,).
- Returns:
The scalar robust loss and the weights, unsorted back into group order.
- Return type:
- compute_group_avg(losses, group_idx)[source]¶
Mean of a per-sample quantity within each group, plus the group counts.
- Parameters:
losses (
torch.Tensor) – Per-sample values of shape(N,)(losses or 0/1 correctness).group_idx (
torch.Tensor) – Group index per sample, shape(N,).
- Returns:
Per-group mean and per-group sample count, both shape
(4,). Empty groups get a mean of zero instead of a NaN.- Return type:
- update_exp_avg_loss(group_loss, group_count)[source]¶
Advance the moving average of the per-group losses by one batch.
Only groups present in the batch are updated, and a group’s first observation replaces the initial zero outright rather than being blended into it.
- Parameters:
group_loss (
torch.Tensor) – Output ofcompute_group_avg()for this batch.group_count (
torch.Tensor) – Output ofcompute_group_avg()for this batch.
- reset_stats()[source]¶
Zero the per-epoch running statistics.
adv_probsandexp_avg_losspersist across epochs and are kept.
- update_stats(actual_loss, group_loss, group_acc, group_count, weights=None)[source]¶
Fold one batch into the running averages reported by
get_stats().Per-group losses and accuracies are accumulated as sample-count weighted averages, the actual (robust) loss as a batch average, and the overall per-sample loss and accuracy as group-fraction weighted averages over the groups seen so far.
- Parameters:
actual_loss (
torch.Tensor) – The scalar robust loss of the batch.group_loss (
torch.Tensor) – Per-group mean loss, mean accuracy and sample count, shape(4,).group_acc (
torch.Tensor) – Per-group mean loss, mean accuracy and sample count, shape(4,).group_count (
torch.Tensor) – Per-group mean loss, mean accuracy and sample count, shape(4,).weights (
torch.Tensor, optional) – The group weights used; only their positivity is recorded, inupdate_data_countsandupdate_batch_counts.
- get_model_stats(model, weight_decay, stats_dict)[source]¶
Add the squared parameter norm and the implied L2 penalty to
stats_dict.- Parameters:
model (
torch.nn.Module) – Model whose parameters are summed over.weight_decay (
float) – Coefficient used for the reportedreg_loss.stats_dict (
dict) – Dictionary that is updated in place.
- Returns:
The same dictionary, with
model_norm_sqandreg_loss.- Return type:
- get_stats(model=None, weight_decay=None)[source]¶
Collect the running statistics as a flat dict for TensorBoard.
Contains per-group average loss, moving-average loss, accuracy and update-batch count keyed by
<name>_group:<y>-<confounder>, plus the aggregatesavg_actual_loss,avg_per_sample_loss,avg_acc,avg_group_accandworst_group_acc.- Parameters:
model (
torch.nn.Module, optional) – When given,get_model_stats()is appended; thenweight_decayis required.weight_decay (
float, optional) – Coefficient for the reported regularisation loss.
- Return type: