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

Contrastive 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.

forward(simi)[source]

Compute the contrastive loss from a similarity matrix.

Parameters:

simi (torch.Tensor) – Pairwise similarity matrix of shape [B * rep, B * rep], where B is batch_size.

Returns:

A scalar tensor containing the loss value.

Return type:

torch.Tensor

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

spamosaic.loss.set_diag

Set the diagonal of a square tensor to a specified value.

Classes

spamosaic.loss.CL_loss

Contrastive loss for multi-view (multi-modality) alignment.