BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Paper implementations22 of 28 in topic

849. SigLIP pairwise sigmoid loss

MediumPaper implementations Pro

Why it matters SigLIP is the vision encoder in PaliGemma and many open VLMs. The loss is a favourite interview contrast with CLIP's softmax InfoNCE.

Paper: "Sigmoid Loss for Language Image Pre-Training", Zhai, Mustafa, Kolesnikov and Beyer, 2023, arXiv 2303.15343.

CLIP uses a softmax over the whole batch. SigLIP treats every image-text pair as its own binary problem. From the paper's pseudocode:
- t = exp(t_prime)
- normalise every image and text embedding to unit L2 norm
- logits = img @ txt.T * t + b, shape (n, n)
- labels = 2 * eye(n) - 1 (so +1 on the diagonal, -1 elsewhere)
- loss = -sum(log_sigmoid(labels * logits)) / n

Write siglip_loss(img, txt, t_prime, b).

Inputs: img and txt are lists or ndarrays of shape (n, dim), with row i of img matching row i of txt. t_prime and b are floats (the paper starts b at a large negative value, -10, so the many negative pairs do not dominate early training).

Compute log_sigmoid(x) stably as -log(1 + exp(-x)) using np.logaddexp(0, -x). Return a Python float. No rounding.

Functiondef siglip_loss(img, txt, t_prime, b)

Example 1

Input: siglip_loss([[1.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], 0.0, 0.0)

Output: 1.0064

Example 2

Input: img = [[1.0, 0.0], [0.0, 1.0]], txt = [[0.0, 1.0], [1.0, 0.0]], t_prime = 0.0, b = 0.0

Output: 2.006409

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Normalise rows, then logits = zi @ zt.T * exp(t_prime) + b.

Hint 2

labels = 2 * np.eye(n) - 1.

Hint 3

-log_sigmoid(x) = np.logaddexp(0, -x); sum everything and divide by n, not n * n.

Common mistakes
  • Dividing by n * n instead of n.
  • Using t_prime directly instead of exp(t_prime).
  • Forgetting to normalise the embeddings.
Understand the idea

Softmax contrastive loss needs the whole similarity matrix normalised per row and column, which couples every pair in the batch. The sigmoid loss scores each pair alone: positives on the diagonal should have a high logit and negatives a low one. With orthogonal unit embeddings, t' = 0 and b = 0, the diagonal logits are 1 and the off-diagonal ones are 0, so the loss is (2 log(1 + e^-1) + 2 log 2) / 2, about 1.0064.

The bias b = -10 at init stops the many negatives from dominating early training.

Shapes and data flow
img, txt: (n, dim). logits and labels: (n, n). Output: scalar.
Topics
Paper implementations
Similar questions
  • Knowledge distillation loss with temperature (Hinton et al., 2015)Medium
  • ALiBi linear attention biases (Press et al.) instead of position embeddingsMedium
  • Mix a batch with mixupMedium
  • Compute a multi-token prediction loss over several headsMedium
All Paper implementations problems
Report
849 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def siglip_loss(img, txt, t_prime, b):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS