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: Layer

Compute 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_pairs value counts directed anchor-positive pairs. Anchors without a valid positive are excluded from the loss mean and reflected in valid_anchor_fraction.

Parameters:
  • temperature (float, default: 0.1)

  • cross_subject_only (bool, default: True)

call(embeddings, labels, subject_ids=None, sample_weight=None)[source]

Return the scalar loss and positive-pair diagnostics.

Parameters:
  • embeddings (Tensor)

  • labels (Tensor)

  • subject_ids (Tensor | None, default: None)

  • sample_weight (Tensor | None, default: None)

Return type:

dict[str, Tensor]

get_config()[source]

Returns the config of the object.

An object config is a Python dictionary (serializable) containing the information needed to re-instantiate it.

Return type:

dict