Search

Search papers and lessons

Sigmoid Loss

Level 3 · 13 min read

The sigmoid loss scores every image-text pair in a batch as a yes-or-no question, so it needs no global normalisation and scales to very large batches.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Losses & regularization
  • Probability
  • Neural network basics
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

Training an image model and a text model together means teaching them which image goes with which caption. The sigmoid loss treats every image-caption pair in a batch as a separate yes-or-no question: is this pair a match? Each pair is scored on its own, so the loss never needs to compare one pair against all the others. That makes it cheap in memory and easy to spread across many devices.

Builds on: Cross-Entropy Loss, Sigmoid Function, Dot Product · Leads to: Sigmoid Loss for Language Image Pre-Training

Where it's used

SigLIP image-text models (Google, 2023). The sigmoid loss was introduced for language-image pre-training in the SigLIP paper. The model learns from web-scale pairs of pictures and their alt-text captions, and the resulting image encoder is then used for Zero-Shot Learning: you give it a picture and a list of possible captions, and it picks the caption that scores highest.

Open vision encoders in later multimodal systems. Because the SigLIP image encoder is released with weights, other vision-language systems reuse it instead of training their own. PaliGemma, a 2024 vision-language model from Google, uses a SigLIP encoder as its image front end.

Training on small hardware. The paper reports a setup with only four TPU-v4 chips. Each pair's term can be computed on its own, so the batch's similarity table can be computed in chunks spread across devices instead of being gathered whole, which lets the batch grow larger on the same chips. This is the practical reason people choose it when accelerators are scarce.

How it works

Take a tiny batch of two image-caption pairs: image 1 with caption 1 (a match), and image 2 with caption 2 (also a match). Every image can be paired with every caption, so there are four pairs to consider.

  1. Turn each image and each caption into a vector, and make each vector length 1. Call the image vectors x1,x2x_1, x_2 and the caption vectors y1,y2y_1, y_2.
  2. Compute the dot product xi⋅yjx_i \cdot y_j for every combination. A dot product near 1 means the two vectors point the same way. Check: two identical unit vectors have dot product 1.
  3. Turn each dot product into a score sij=t (xi⋅yj)+bs_{ij} = t \, (x_i \cdot y_j) + b, where tt is a positive scale and bb is a bias. Both are learned. Check: with t=1t = 1 and b=0b = 0, the score is just the dot product.
  4. Label each pair zijz_{ij}: +1+1 if the image and caption match, −1-1 if they do not. Here z11=z22=+1z_{11} = z_{22} = +1 and z12=z21=−1z_{12} = z_{21} = -1.
  5. Pass each score through a sigmoid and take the negative log of the probability that the label is right. A matched pair with a high score costs little. An unmatched pair with a high score costs a lot. Check: a matched pair with score 0 gives −ln⁡0.5=0.693-\ln 0.5 = 0.693.
  6. Add up all the pair losses and divide by the number of images. The result is the loss for the batch.

The picture below shows the whole batch as a grid. Each cell is one independent yes-or-no problem.

flowchart LR
  I["images x1, x2"] --> D["dot products x_i · y_j"]
  T["captions y1, y2"] --> D
  D --> S["scores t·dot + b"]
  S --> P["per-pair loss -ln σ(z·s)"]
  P --> L["sum, divide by batch size"]

The plot shows the loss for one pair as a function of its score ss (with t=1t = 1, b=0b = 0), once for a matched pair and once for an unmatched pair.

−4−3−2−10123401234score sloss for this pair
  • matched pair (z = +1)
  • unmatched pair (z = -1)
A matched pair is cheap when its score is high. An unmatched pair is cheap when its score is low. Both are 0.693 at score 0.

The math

Definition

Definition: Pairwise sigmoid loss

For a mini-batch of nn image-caption pairs (Ii,Ti)(I_i, T_i), with image embeddings xix_i and text embeddings yjy_j (unit length), a learnable scale t=exp⁡(t′)t = \exp(t'), and a learnable bias bb, the loss is

L=−1n∑i=1n∑j=1nln⁡σ(zij (t xi⋅yj+b)),zij={+1i=j−1i≠jL = -\frac{1}{n} \sum_{i=1}^{n} \sum_{j=1}^{n} \ln \sigma\big(z_{ij}\,(t\, x_i \cdot y_j + b)\big), \qquad z_{ij} = \begin{cases} +1 & i = j \\ -1 & i \neq j \end{cases}

where σ(u)=1/(1+e−u)\sigma(u) = 1/(1 + e^{-u}) is the Sigmoid Function.

Here σ\sigma is the sigmoid, which maps any real number into the interval from 0 to 1. The quantity σ(zijsij)\sigma(z_{ij} s_{ij}) is the probability that the pair's label is right, where sij=t xi⋅yj+bs_{ij} = t\,x_i \cdot y_j + b is the score. The loss is the negative log of that probability, which is exactly the Cross-Entropy Loss for a yes-or-no decision. The sum runs over all n2n^2 pairs, and the division by nn (not n2n^2) is what the paper uses.

Step 2 in symbols: the dot product

For unit vectors, xi⋅yj=∑kxikyjkx_i \cdot y_j = \sum_k x_{ik} y_{jk}, a sum of products of matching entries. Because both vectors have length 1, the dot product is the Cosine Similarity of the two embeddings, so it lies between −1-1 and 11.

Step 3 in symbols: the score

sij=t (xi⋅yj)+bs_{ij} = t\, (x_i \cdot y_j) + b. The scale tt stretches the cosine similarity so that small differences matter, and the bias bb shifts the whole scale. In the paper, t′t' is initialised to ln⁡10\ln 10, which makes tt start at 10. The bias is initialised to −10-10.

Step 5 in symbols: one pair

For one pair with label zz and score ss:

ℓ(z,s)=−ln⁡σ(zs)=ln⁡(1+e−zs).\ell(z, s) = -\ln \sigma(z s) = \ln\left(1 + e^{-z s}\right).

Check with z=+1z = +1, s=0s = 0: ln⁡(1+e0)=ln⁡2=0.693\ln(1 + e^0) = \ln 2 = 0.693. Check with z=−1z = -1, s=0s = 0: the same value, because the two labels are symmetric at zero.

Step 6 in symbols: the batch

Substitute the pair losses into the definition and divide by nn. Every off-diagonal term is a negative example, and every diagonal term is a positive one. There are nn positives and n2−nn^2 - n negatives in each batch.

Worked examples

Example
1. One pair, score zero (tiny)
  1. Batch size n=1n = 1: one image, one caption, and they match. So z=+1z = +1.
  2. Set t=1t = 1 and b=0b = 0. The dot product is 00, so the score is s=1⋅0+0=0s = 1 \cdot 0 + 0 = 0.
  3. Compute σ(0)=1/(1+e0)=1/2=0.5\sigma(0) = 1/(1 + e^0) = 1/2 = 0.5. This is the probability the model gives to "match".
  4. The pair loss is −ln⁡0.5=ln⁡2=0.6931-\ln 0.5 = \ln 2 = 0.6931.
  5. Divide by n=1n = 1. The batch loss is 0.69310.6931.

Interpretation: a model that is completely unsure about a matching pair pays about 0.69 per pair.

Example
2. The two-by-two batch (standard)

Use t=1t = 1 and b=0b = 0 to keep the numbers short, so each score equals its dot product. Dot products of unit vectors lie between −1-1 and 11. Here x1⋅y1=0.9x_1 \cdot y_1 = 0.9, x1⋅y2=−0.5x_1 \cdot y_2 = -0.5, x2⋅y1=0x_2 \cdot y_1 = 0, x2⋅y2=0.5x_2 \cdot y_2 = 0.5.

  1. Matched pair (1,1): z=+1z = +1, s=0.9s = 0.9. Loss =ln⁡(1+e−0.9)=ln⁡(1.4066)=0.3412= \ln(1 + e^{-0.9}) = \ln(1.4066) = 0.3412.
  2. Unmatched pair (1,2): z=−1z = -1, s=−0.5s = -0.5. Then zs=0.5z s = 0.5, so loss =ln⁡(1+e−0.5)=ln⁡(1.6065)=0.4741= \ln(1 + e^{-0.5}) = \ln(1.6065) = 0.4741.
  3. Unmatched pair (2,1): z=−1z = -1, s=0s = 0. Then zs=0z s = 0, so loss =ln⁡2=0.6931= \ln 2 = 0.6931.
  4. Matched pair (2,2): z=+1z = +1, s=0.5s = 0.5. Loss =ln⁡(1+e−0.5)=0.4741= \ln(1 + e^{-0.5}) = 0.4741.
  5. Add the four losses: 0.3412+0.4741+0.6931+0.4741=1.98250.3412 + 0.4741 + 0.6931 + 0.4741 = 1.9825.
  6. Divide by n=2n = 2: 1.9825/2=0.99121.9825 / 2 = 0.9912.

Interpretation: the unmatched pair (2,1) is the most expensive term, because the model gives it a score of zero, which is no evidence either way. Raising its score would not help; lowering it would.

Example
3. A batch of four at initialisation (realistic)

Four image-caption pairs. Use the paper's starting values t=10t = 10 and b=−10b = -10. Suppose the cosine similarities (dot products of unit vectors) are:

image \ caption1234
10.300.10-0.050.00
20.050.250.10-0.10
3-0.100.000.400.05
40.100.200.000.35
  1. Scores are s=10⋅d−10s = 10 \cdot d - 10. The matched (diagonal) scores are: s11=−7s_{11} = -7, s22=−7.5s_{22} = -7.5, s33=−6s_{33} = -6, s44=−6.5s_{44} = -6.5.
  2. Matched losses are ln⁡(1+e−s)\ln(1 + e^{-s}), which for these scores are 7.00097.0009, 7.50067.5006, 6.00256.0025 and 6.50156.5015. Their sum is 27.005427.0054.
  3. Unmatched scores are s=10d−10s = 10 d - 10 for the twelve off-diagonal entries, and their losses are ln⁡(1+es)\ln(1 + e^{s}). The largest unmatched score is s42=−8s_{42} = -8 (similarity 0.20), with loss ln⁡(1+e−8)=0.00034\ln(1 + e^{-8}) = 0.00034.
  4. The twelve unmatched losses are tiny. Their sum is about 0.001050.00105.
  5. Total before dividing: 27.0054+0.0011=27.006527.0054 + 0.0011 = 27.0065.
  6. Divide by n=4n = 4: 27.0065/4=6.751627.0065 / 4 = 6.7516.

Interpretation: at this start, the loss is almost entirely the positives. Every matched score is far below zero because the bias starts at −10-10, so the model is pushed to raise the matched similarities. The negatives are already very cheap, which is the intended starting point. The loss does not come from wrongly confident negatives here.

Try it: the same batch, but with the bias set to b=0b = 0 instead of −10-10. What is the loss for the matched pair in row 1, column 1? Answer: the score becomes s=10⋅0.30+0=3s = 10 \cdot 0.30 + 0 = 3, so the loss is ln⁡(1+e−3)=ln⁡(1.0498)=0.0486\ln(1 + e^{-3}) = \ln(1.0498) = 0.0486. That is far smaller than the 7.00097.0009 it was with b=−10b = -10. Starting with a large negative bias makes the matched loss large at first, so the bias is not a free choice. It is one of the knobs the paper tuned.

Common mistakes and tradeoffs

It is tempting to think the sigmoid loss is simply logistic regression applied to every pair. It is close, but the scores sijs_{ij} are dot products of learned embeddings, and both encoders are trained at the same time. The loss trains the two encoders to place matching pairs together. Logistic regression trains a single weight vector on fixed features.

It is tempting to think a softmax loss with more classes is the same thing. In the softmax version (see Softmax and Contrastive Learning), each image has to pick out its caption from the whole batch, so the denominator sums over every caption. In fact the sigmoid loss has no such denominator: each pair is scored on its own, so the terms can be computed independently. This is the reason it can be split across devices without gathering all the embeddings.

It is tempting to think negatives are free because each one is cheap. In fact there are n2−nn^2 - n negatives and only nn positives in a batch. In a batch of 16,000 pairs that is about 256 million negatives against 16,000 positives. The loss is still dominated by the negatives in aggregate, so the paper studied how the balance of positives and negatives in the batch changes the result.

It is tempting to treat the bias as decoration. The bias sets where the scores start. Because most pairs in a batch are negatives, a bias near the prior (the fraction of pairs that actually match, which is tiny) keeps early training from being dominated by the negatives. The paper starts the bias negative for this reason.

When to use it. Prefer the sigmoid loss when the batch is large or spread across many devices, when memory is tight, or when you want each pair judged on its own. The paper reports that it does better than softmax at small batch sizes and roughly matches it at large ones.

When not to use it. If the task is to pick the single best caption from a list and you need a probability distribution over that list, a Softmax over candidates gives you exactly that. The sigmoid loss gives each pair an independent probability, so the numbers do not add up to one across candidates. At very large batch sizes the two losses perform about the same.

Alternatives. The softmax contrastive loss (InfoNCE) is the standard alternative, used by Contrastive Learning methods such as CLIP. A Hinge Loss also scores pairs independently, but it has no probabilistic reading. See Softmax Temperature for how the scale tt acts in the softmax case.

History

Logistic curves were studied in the 1830s and 1840s, when Pierre-François Verhulst used one to describe population growth. The sigmoid as a neural-network activation became common in the 1980s, when backpropagation was popularised and sigmoid units were the default. Cross-entropy was already the usual loss for yes-or-no predictions.

For contrastive learning, the softmax form was popularised by the contrastive predictive coding work of van den Oord and colleagues in 2018, and then by CLIP in 2021, which trained image and text encoders on web pairs with that softmax loss. The SigLIP paper, by Zhai and colleagues, published at ICCV in 2023, replaced the softmax with the pairwise sigmoid loss and reported that it trains better at small batch sizes and uses less memory.

Recap

  • A batch of nn image-caption pairs gives n2n^2 pairs. Each is labelled +1+1 (match) or −1-1 (no match).
  • Each pair gets a score t (xi⋅yj)+bt\,(x_i \cdot y_j) + b, where tt and bb are learned.
  • Each pair's loss is −ln⁡σ(z s)-\ln \sigma(z\,s), the cross-entropy of a yes-or-no decision.
  • The loss is the sum over pairs divided by nn. No pair depends on any other pair's normalisation.
  • The bias should start negative so that early training starts near the rarity of positives.

Read next: Sigmoid Loss for Language Image Pre-Training for the full results, and Zero-Shot Learning for how the trained encoders are used.

Check yourself

QuestionRecall: in the sigmoid loss, what is the label zijz_{ij} for an image ii and a caption jj that do not belong together?Show answer

zij=−1z_{ij} = -1. Matched pairs (where i=ji = j) get +1+1, and every other pair gets −1-1.

QuestionComputation: with t=1t = 1, b=0b = 0, what is the loss for a matched pair whose dot product is x⋅y=−2x \cdot y = -2? Give the number to three decimal places.Show answer

The score is s=−2s = -2 and the label is z=+1z = +1, so the loss is ln⁡(1+e−zs)=ln⁡(1+e2)=ln⁡(8.389)=2.127\ln(1 + e^{-z s}) = \ln(1 + e^{2}) = \ln(8.389) = 2.127.

QuestionWhy: the softmax contrastive loss needs every image to be compared against every caption in the batch at once. Why does the sigmoid loss not need this, and why does that help on many devices?Show answer

The softmax loss puts all the scores of an image into one denominator, so the batch's similarity table must be assembled before any term can be finished. The sigmoid loss gives each pair its own sigmoid term, so each term can be computed from its two embeddings alone. The terms can then be computed on different devices and summed, without gathering all the embeddings or holding the full table in memory.

QuestionApplied: you train a sigmoid-loss model with a batch of 8,192 pairs and find that its early loss is very large and unstable. The bias is currently initialised to 0. What change would you make first, and why?Show answer

Initialise the bias to a negative value such as −10-10 instead of 0. With a bias of 0, the huge number of negatives all push their scores down at once, so early updates are dominated by them. A negative bias starts all scores near the prior (the rarity of matched pairs), so the early updates are gentler. Check the change on a small run before scaling up.

Sources