eegproc.deep_learning.supervised.supervised_contrastive_loss module
Supervised contrastive regularization for EEG representations.
The loss operates on a rank-2 embedding produced by an encoder or by the pre-logit portion of a classifier. It is intentionally independent of any particular prediction head so it can be used with dense, hybrid, or variational classifiers.
- class eegproc.deep_learning.supervised.supervised_contrastive_loss.SupervisedContrastiveLoss(temperature=0.1, cross_subject_only=True, **kwargs)[source]
Bases:
LayerCompute supervised contrastive loss and batch diagnostics.
By default, a positive pair must share the same class label and come from different subjects. Same-label samples from the same subject are then excluded from both the positive set and the denominator. This focuses the objective on subject-invariant class structure instead of letting repeated windows from one subject dominate the contrastive signal.
The returned
positive_pairsvalue counts directed anchor-positive pairs. Anchors without a valid positive are excluded from the loss mean and reflected invalid_anchor_fraction.- Parameters:
temperature (
float, default:0.1)cross_subject_only (
bool, default:True)