spamosaic.loss
Loss functions for contrastive alignment in SpaMosaic.
Provides a multi-view contrastive loss and a small utility to set a matrix diagonal.
- class spamosaic.loss.CL_loss(*args: Any, **kwargs: Any)[source]
Bases:
ModuleContrastive loss for multi-view (multi-modality) alignment.
Encourages embeddings of the same sample across modalities to be similar while pushing apart embeddings from different samples.
- Parameters:
batch_size (int) – Number of samples per mini-batch (per modality).
rep (int, optional) – Number of modalities or views. Default is
3.bias (float, optional) – Small constant added inside the log term for numerical stability. Default is
0.
- spamosaic.loss.set_diag(matrix, v)[source]
Set the diagonal of a square tensor to a specified value.
- Parameters:
matrix (torch.Tensor) – 2D square tensor of shape
[N, N]whose diagonal will be modified.v (float) – Value to set on the diagonal.
- Returns:
The same tensor with its diagonal set to
v.- Return type:
torch.Tensor
Functions
Set the diagonal of a square tensor to a specified value. |
Classes
Contrastive loss for multi-view (multi-modality) alignment. |