TensorTonic
Study Plans
Study Plan
Implement the SigLIP Pairwise Loss
Loss FunctionsLanguage ModelsComputer Vision
Medium

Compute the independent pairwise logistic loss for B aligned image-text pairs. Normalize every image or text embedding vector x by

x^=xmax⁡(∥x∥2,10−12).\widehat{x}=\frac{x}{\max(\lVert x\rVert_2,10^{-12})}.x=max(∥x∥2​,10−12)x​.

The norm is Euclidean, so a zero row remains zero. For image vector viv_ivi​, text vector tjt_jtj​, supplied multiplier a, and bias b, the pair logit is

ℓij=av^iTt^j+b.\ell_{ij}=a\widehat v_i^{\mathsf T}\widehat t_j+b.ℓij​=aviT​tj​+b.

The label zijz_{ij}zij​ is 1 for a matched diagonal pair and -1 otherwise. The loss is the mean over all image-text pairs:

L=−1B2∑i=1B∑j=1Blog⁡σ(zijℓij).L=-\frac{1}{B^2}\sum_{i=1}^{B}\sum_{j=1}^{B}\log\sigma(z_{ij}\ell_{ij}).L=−B21​i=1∑B​j=1∑B​logσ(zij​ℓij​).

Here, i and j are pair indices, σ\sigmaσ is the logistic sigmoid, and log is the natural logarithm. The supplied logit_scale is the multiplier a itself, not its logarithm.

Return a Python dictionary of PyTorch tensors: loss is a scalar; logits and pair_labels both have shape (B, B). All three outputs use the embedding floating dtype and device, including pair_labels.

Loading visualization...

Examples

Input: image_embeddings = [[1,0],[0,1]], text_embeddings = [[1,0],[0,1]], logit_scale = 2, bias = -1

Output: {"loss":0.313262,"logits":[[1,-1],[-1,1]],"pair_labels":[[1,-1],[-1,1]]}

Explanation: Every signed pair logit equals one, so all four logistic terms are equal.

Input: image_embeddings = [[3,4]], text_embeddings = [[6,8]], logit_scale = 10, bias = 0

Output: {"loss":0.000045,"logits":[[10]],"pair_labels":[[1]]}

Input: image_embeddings = [[1,0],[0,1]], text_embeddings = [[0,1],[1,0]], logit_scale = 2, bias = -0.5

Output: {"loss":1.337745,"logits":[[-0.5,1.5],[1.5,-0.5]],"pair_labels":[[1,-1],[-1,1]]}

Hint 1

torch.nn.functional.normalize bounds the normalization denominator.

Hint 2

Tensor.fill_diagonal_ updates the diagonal of a newly created matrix.

Hint 3

torch.nn.functional.logsigmoid evaluates log-sigmoid stably.

Requirements

  • Use PyTorch and preserve floating dtype and device.
  • Evaluate the signed logistic loss stably for large positive or negative margins.
  • Do not modify input tensors.

Constraints

  • Image and text embeddings are finite rank-two PyTorch tensors with identical positive shape (batch, features).
  • Embeddings share floating dtype and device; editor inputs use float32.
  • logit_scale and bias are finite Python numbers.
  • Zero rows and noncontiguous inputs are valid.
Try Similar Problems
Implement Symmetric CLIP Contrastive LossMediumAssign Multimodal RoPE CoordinatesMediumPatchify a Batch of ImagesEasyCompare Sequence and Token Length NormalizationMediumImplement the DPO LossMediumScore a KL-Regularized Policy BatchMediumCompute a Clipped GRPO SurrogateHardBuild an Assistant-Only SFT Loss MaskEasySigmoid Focal LossMediumGeneralized IoU LossMediumComplete IoU LossMediumDice LossEasy

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Implement the SigLIP Pairwise Loss

Loss FunctionsLanguage ModelsComputer Vision
Medium

Compute the independent pairwise logistic loss for B aligned image-text pairs. Normalize every image or text embedding vector x by

x^=xmax⁡(∥x∥2,10−12).\widehat{x}=\frac{x}{\max(\lVert x\rVert_2,10^{-12})}.x=max(∥x∥2​,10−12)x​.

The norm is Euclidean, so a zero row remains zero. For image vector viv_ivi​, text vector tjt_jtj​, supplied multiplier a, and bias b, the pair logit is

ℓij=av^iTt^j+b.\ell_{ij}=a\widehat v_i^{\mathsf T}\widehat t_j+b.ℓij​=aviT​tj​+b.

The label zijz_{ij}zij​ is 1 for a matched diagonal pair and -1 otherwise. The loss is the mean over all image-text pairs:

L=−1B2∑i=1B∑j=1Blog⁡σ(zijℓij).L=-\frac{1}{B^2}\sum_{i=1}^{B}\sum_{j=1}^{B}\log\sigma(z_{ij}\ell_{ij}).L=−B21​i=1∑B​j=1∑B​logσ(zij​ℓij​).

Here, i and j are pair indices, σ\sigmaσ is the logistic sigmoid, and log is the natural logarithm. The supplied logit_scale is the multiplier a itself, not its logarithm.

Return a Python dictionary of PyTorch tensors: loss is a scalar; logits and pair_labels both have shape (B, B). All three outputs use the embedding floating dtype and device, including pair_labels.

Loading visualization...

Examples

Input: image_embeddings = [[1,0],[0,1]], text_embeddings = [[1,0],[0,1]], logit_scale = 2, bias = -1

Output: {"loss":0.313262,"logits":[[1,-1],[-1,1]],"pair_labels":[[1,-1],[-1,1]]}

Explanation: Every signed pair logit equals one, so all four logistic terms are equal.

Input: image_embeddings = [[3,4]], text_embeddings = [[6,8]], logit_scale = 10, bias = 0

Output: {"loss":0.000045,"logits":[[10]],"pair_labels":[[1]]}

Input: image_embeddings = [[1,0],[0,1]], text_embeddings = [[0,1],[1,0]], logit_scale = 2, bias = -0.5

Output: {"loss":1.337745,"logits":[[-0.5,1.5],[1.5,-0.5]],"pair_labels":[[1,-1],[-1,1]]}

Hint 1

torch.nn.functional.normalize bounds the normalization denominator.

Hint 2

Tensor.fill_diagonal_ updates the diagonal of a newly created matrix.

Hint 3

torch.nn.functional.logsigmoid evaluates log-sigmoid stably.

Requirements

  • Use PyTorch and preserve floating dtype and device.
  • Evaluate the signed logistic loss stably for large positive or negative margins.
  • Do not modify input tensors.

Constraints

  • Image and text embeddings are finite rank-two PyTorch tensors with identical positive shape (batch, features).
  • Embeddings share floating dtype and device; editor inputs use float32.
  • logit_scale and bias are finite Python numbers.
  • Zero rows and noncontiguous inputs are valid.
Try Similar Problems
Implement Symmetric CLIP Contrastive LossMediumAssign Multimodal RoPE CoordinatesMediumPatchify a Batch of ImagesEasyCompare Sequence and Token Length NormalizationMediumImplement the DPO LossMediumScore a KL-Regularized Policy BatchMediumCompute a Clipped GRPO SurrogateHardBuild an Assistant-Only SFT Loss MaskEasySigmoid Focal LossMediumGeneralized IoU LossMediumComplete IoU LossMediumDice LossEasy

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.