LossFeedbackSampler#

class ts3.models.supervised.LossFeedbackSampler(labels, factor=1.0, *, grow_divisor=0.96, shrink_divisor=1.04, overfit_margin=2.0, bounds=(0.8, 100.0), num_classes=None, num_samples=None, generator=None)[source]#

Bases: torch.utils.data.sampler.Sampler

Samples elements randomly from a given list of indices, without replacement.

Each class contributes factor[c] times its own index count to an epoch: the integer part repeats every index, the fractional part draws that share at random. step() moves the factors between epochs; a caller that never steps keeps the mix it started with.

Parameters:
  • labels – per-sample class label, used to build the per-class index map.

  • factor (float | Sequence[float]) – initial oversampling factor, one value for every class or one per class.

  • grow_divisor (float) – a sampled-more class is divided by this, so below 1. Both divisors at 1.0 hold the mix wherever factor put it.

  • shrink_divisor (float) – a sampled-less class is divided by this, so above 1.

  • overfit_margin (float) – val-train gap, in train-loss sigmas, past which a hard class stops growing. 0.0 stops any class whose val loss exceeds its train loss.

  • bounds (tuple[float, float]) – (low, high) clip applied to any factors set_factors() is given.

  • num_classes (Optional[int]) – class count, inferred from labels when None, which assumes every class from 0 to the largest label is present.

  • num_samples (Optional[int]) – epoch length; defaults to the initial oversampled index count.

  • generator (Generator) – Generator used in sampling.

set_factors(factors)[source]#

Clip to bounds and rebuild the index list.

Return type:

None

step(train_loss, train_labels, val_loss, val_labels)[source]#

Move every class’s factor from one epoch’s losses, then rebuild the index list.

Above-average train loss is sampled more unless the val-train gap exceeds overfit_margin sigmas, below-average less, and a class missing from either split keeps its factor.

Return type:

None