Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the user guide for the big picture.

nidl.losses.CoMMLoss

class nidl.losses.CoMMLoss(temperature=0.1, weights=None)[source]

Bases: Module

CoMM loss [1], multimodal InfoNCE with missing modalities.

For two augmented views v \in \{1, 2\} of each sample, let z_v^{(m)} be the embedding of modality m \leq M, and z_v^{(M + 1)} the multimodal embedding, which fuses all the modalities. The loss aligns each embedding of one view with the multimodal embedding of the other view:

\mathcal{L} = \frac{1}{M + 1} \sum_{k=1}^{M + 1} \frac{1}{2}
\Big(\ell\big(Z_1^{(k)}, Z_2^{(M + 1)}\big)
+ \ell\big(Z_2^{(k)}, Z_1^{(M + 1)}\big)\Big),

where Z_v^{(k)} gathers the embeddings of the n samples of the batch, and \ell is the InfoNCE loss [2] with temperature \tau on \ell_2-normalized embeddings. In \ell(A, B), (a_i, b_i) is a positive pair, and the other samples of A and B are the negatives:

\ell(A, B) = \frac{1}{2n} \sum_{i=1}^{n}
\big(\ell_i(A, B) + \ell_i(B, A)\big), \qquad
\ell_i(A, B) = -\log
\frac{\exp(a_i \cdot b_i / \tau)}
{\sum_{j=1}^{n} \exp(a_i \cdot b_j / \tau)
+ \sum_{j \neq i} \exp(a_i \cdot a_j / \tau)}.

With missing modalities, the term of modality m only uses the samples having it (both in Z_v^{(m)} and Z_v^{(M + 1)}), and is skipped if they are fewer than two.

Adapted from https://github.com/Duplums/CoMM.

Parameters:
temperaturefloat, default=0.1

Scale logits by the inverse of the temperature.

weightssequence of float or None, default=None

Optional weight of each term (one per modality, then the multimodal term). The loss is then the mean of the weighted terms, \frac{1}{K} \sum_k w_k \ell_k, over the K computed terms (the weights are not normalized).

References

[1]

Benoit Dufumier, Javiera Castillo-Navarro, et al., “What to align in multimodal contrastive learning?”, ICLR 2025.

[2]

Aaron van den Oord, Yazhe Li, Oriol Vinyals, “Representation Learning with Contrastive Predictive Coding”, arXiv 2018.

__init__(temperature=0.1, weights=None)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(z1, z2, present=None)[source]

Forward implementation.

Parameters:
z1list of torch.Tensor

The M + 1 embeddings of the first view, each of shape (batch_size, n_features): one per modality, then the multimodal one.

z2list of torch.Tensor

The M + 1 embeddings of the second view.

presenttorch.Tensor or None, default=None

Bool mask of shape (batch_size, M), True where a sample has a modality. If None, all the modalities are present.

Returns:
outputsdict

The CoMM loss (“loss”), the mean SSL accuracy (“ssl_acc”), and the loss and accuracy of each computed term k (“ssl_loss_k”, “ssl_acc_k”), from 0 to M, M being the multimodal term.

infonce(z1, z2)[source]

InfoNCE loss \ell(z_1, z_2) and accuracy between two batches of normalized embeddings of shape (n, n_features), where z1[i] and z2[i] are positive pairs. The accuracy is the percentage of samples of z1 whose nearest sample of z2 is their positive.