braindecode.augmentation.LabelSmoothing#

class braindecode.augmentation.LabelSmoothing(epsilon, num_classes=None, random_state=None)[source]#

Label smoothing from Szegedy et al. (2016) [1].

Replaces each target with its convex combination with the uniform distribution over the classes, penalizing over-confident predictions. The signals are returned unchanged; only the targets change.

The smoothed targets are floating-point distributions of shape batch_size, num_classes (each row sums to one), so the training loss must accept target distributions, e.g. soft cross-entropy -(y * torch.log_softmax(logits, dim=-1)).sum(-1).mean() or torch.nn.KLDivLoss. The transform composes before target-mixing transforms such as braindecode.augmentation.Mixup, whose fractional-target path then mixes the smoothed distributions. The fixed-expansion mode (n_augmentation > 0) of braindecode.augmentation.AugmentedDataLoader is not supported: it cannot concatenate the unsmoothed original targets with the reshaped smoothed copies and rejects the combination with a ValueError pointing to n_augmentation = 0.

Parameters:
  • epsilon (float) – Smoothing parameter between 0 and 1: the weight of the uniform distribution mixed into the targets. 0 disables smoothing.

  • num_classes (int | None, optional) – Number of classes. Required when the targets are class indices; when the targets are already distributions of shape batch_size, num_classes, it must match their last dimension. Defaults to None.

  • random_state (int | numpy.random.Generator, optional) – Seed to be used to instantiate numpy random number generator instance. The transform is deterministic, so the seed only feeds the application-probability machinery, which is inactive here. Defaults to None.

References

[1]

Christian Szegedy, Vincent Vanhoucke, Jonathon Shlens and Zbigniew Wojna (2016). Rethinking the Inception Architecture for Computer Vision. In 2016 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), pp. 2818-2826. Online: https://arxiv.org/abs/1512.00567

Methods

get_augmentation_params(*batch)[source]#

Return transform parameters.

Parameters:
  • X (tensor.Tensor) – The data.

  • y (tensor.Tensor) – The labels.

Returns:

params – Contains

  • epsilonfloat

    Smoothing parameter between 0 and 1.

  • num_classesint | None

    The number of classes.

Return type:

dict

static operation(X, y, epsilon, num_classes=None)[source]#

Smooth targets toward the uniform distribution over the classes.

See [1] for details.

Parameters:
  • X (Tensor) – EEG data in form batch_size, n_channels, n_times. Returned unchanged.

  • y (Tensor) – Target of length batch_size holding class indices (float values are accepted when they are integral), or a floating-point tensor of shape batch_size, num_classes holding target distributions.

  • epsilon (float) – Smoothing parameter between 0 and 1: the weight of the uniform distribution mixed into the targets. 0 returns the unsmoothed targets.

  • num_classes (int | None) – Number of classes. Required when y holds class indices; when y already holds distributions, it must match the size of the last dimension of y. Defaults to None.

Returns:

X, y. Where X is unchanged and y is a floating-point tensor of shape batch_size, num_classes whose rows sum to one.

Return type:

tuple[Tensor, Tensor]

References

[1]

Christian Szegedy, Vincent Vanhoucke, Jonathon Shlens and Zbigniew Wojna (2016). Rethinking the Inception Architecture for Computer Vision. In 2016 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), pp. 2818-2826. Online: https://arxiv.org/abs/1512.00567