spamosaic.loss

class spamosaic.loss.CL_loss(*args: Any, **kwargs: Any)[source]

Bases: Module

Contrastive Loss for multi-view representation alignment.

This loss function is designed to encourage representations from the same sample across multiple modalities to be similar, while pushing apart representations from different samples.

Parameters:
  • batch_size (int) – Number of samples in a mini-batch (per modality).

  • rep (int, optional) – Number of modalities or views (default: 3).

  • bias (float, optional) – Small constant added to negative sample logits to avoid instability in log computations (default: 0).

forward(simi)[source]

Compute contrastive loss from a similarity matrix.

Parameters:

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

Returns:

Scalar loss value.

Return type:

torch.Tensor

spamosaic.loss.set_diag(matrix, v)[source]

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

Parameters:
  • matrix (torch.Tensor) – A 2D square tensor whose diagonal will be modified.

  • v (float) – Value to set on the diagonal.

Returns:

The modified tensor with updated diagonal values.

Return type:

torch.Tensor

Functions

spamosaic.loss.set_diag

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

Classes

spamosaic.loss.CL_loss

Contrastive Loss for multi-view representation alignment.