Search

Search papers and lessons

Sigmoid Loss for Language Image Pre-Training

Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov et al. · 2023

SigLIP trains an image encoder and a text encoder on web image-caption pairs, replacing the softmax contrastive loss with a per-pair sigmoid loss that needs no batch-wide normalisation.

On this page

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

TL;DR

SigLIP trains an image encoder and a text encoder together on pairs of pictures and captions taken from the web. It replaces the softmax contrastive loss used by CLIP with a sigmoid loss that scores each image-caption pair as its own yes-or-no question. Because no pair is compared against a normalisation over the whole batch, the loss uses less memory, splits easily across many chips, and does better than softmax when batches are small. The authors also find that a batch of about 32,000 pairs is enough, and that much larger batches gain little.

Builds on: Contrastive Learning, Sigmoid Loss, Vision Transformer (ViT) · Leads to: OpenVLA: An Open-Source Vision-Language-Action Model

Why it mattered

Image-text pre-training has become a usual way to get a general vision model: train on huge numbers of web pairs, then use the image encoder for Zero-Shot Learning, retrieval and fine-tuning. CLIP and ALIGN made this work at scale with a softmax loss, and the cost of that loss grows with the square of the batch size. SigLIP showed that the same job can be done with a simpler loss that runs well on fewer chips. The released models and the big_vision codebase give groups without a large cluster a practical starting point.

The problem

A contrastive model learns two encoders. An image encoder ff and a text encoder gg map each image IiI_i and caption TiT_i to vectors. Matching pairs should point the same way; mismatched pairs should not. In a batch of ∣B∣|B| pairs, the standard recipe (Softmax contrastive loss, as in CLIP) asks, for each image, which of the ∣B∣|B| captions is its match, and for each caption, which image is its match. To answer, it must normalise each score by a sum over the whole batch. That sum forces the devices holding the batch to exchange embeddings, and it needs a full ∣B∣×∣B∣|B| \times |B| table of pairwise scores in memory. Both costs grow fast with batch size, while small batches give weaker training. The question the paper asks is whether the batch-wide normalisation is needed at all.

Before this paper, the softmax loss was the default for image-text contrastive learning. A sigmoid loss had been used for dimensionality reduction, and for supervised classification it had been reported to be slightly more robust than softmax. The paper brings that idea to image-text pre-training. It was posted to arXiv in March 2023.

How it works

Take a tiny batch of two pairs: image 1 with caption 1, and image 2 with caption 2. Each image can be compared with each caption, so there are four pairs to score. Here is the sigmoid version, one step at a time.

  1. Turn each image and each caption into a vector of length 1. Call them x1,x2x_1, x_2 and y1,y2y_1, y_2. In the paper these come from the image encoder ff and the text encoder gg, each followed by division by its own length.
  2. Take the dot product xi⋅yjx_i \cdot y_j for all four combinations. The dot product is large when two vectors point the same way. Check: a vector dotted with itself, when its length is 1, gives 1.
  3. Turn each dot product into a score sij=t (xi⋅yj)+bs_{ij} = t\,(x_i \cdot y_j) + b. The scale tt and the bias bb are learned. Check: with t=1t = 1 and b=0b = 0, the score equals the dot product.
  4. Label each pair zijz_{ij}: +1+1 if image ii and caption jj belong together, −1-1 if they do not.
  5. For each pair, pass zijsijz_{ij} s_{ij} through a sigmoid and take the negative log. This is the cost of that one yes-or-no question. 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 costs −ln⁡0.5=0.693-\ln 0.5 = 0.693.
  6. Add the costs of all pairs and divide by the number of images. That is the loss for the batch.

The grid below shows the batch. Each cell is one independent question, and no cell looks at the others to decide its cost.

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 cost −ln σ(z·s)"]
  P --> L["sum, divide by number of images"]

The plot shows the cost of one pair as its score changes, with t=1t = 1 and b=0b = 0. The matched curve falls as the score rises. The unmatched curve rises.

−6−4−202460123456score scost of the pair
  • matched pair (z = +1)
  • unmatched pair (z = -1)
Each curve is the cost of one yes-or-no question. At score 0 both costs are ln 2, about 0.693.

Worked examples

Example
1. A tiny batch with t = 1 and b = 0

Two pairs. The vectors are x1=y1=(1,0)x_1 = y_1 = (1, 0) and x2=y2=(0,1)x_2 = y_2 = (0, 1), so the dot products form the grid (1001)\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}. Take t=1t = 1 and b=0b = 0.

  1. Scores: sij=1⋅(xi⋅yj)+0s_{ij} = 1 \cdot (x_i \cdot y_j) + 0, so s11=1s_{11} = 1, s12=0s_{12} = 0, s21=0s_{21} = 0, s22=1s_{22} = 1.
  2. Labels: z11=z22=+1z_{11} = z_{22} = +1 (matched), z12=z21=−1z_{12} = z_{21} = -1 (unmatched).
  3. Matched cost, pair (1,1): −log⁡σ(1)=log⁡(1+e−1)=0.3133-\log \sigma(1) = \log(1 + e^{-1}) = 0.3133.
  4. Unmatched cost, pair (1,2): −log⁡σ(0)=−log⁡0.5=0.6931-\log \sigma(0) = -\log 0.5 = 0.6931.
  5. The other matched pair costs 0.3133 and the other unmatched pair costs 0.6931.
  6. Sum of the four costs: 0.3133+0.6931+0.6931+0.3133=2.01280.3133 + 0.6931 + 0.6931 + 0.3133 = 2.0128.
  7. Divide by the number of images, 2: the loss is 1.0061.006.

Interpretation: the matched pairs are already scored above zero, so they cost less than ln⁡2\ln 2. The unmatched pairs sit at score 0, where the cost is exactly ln⁡2\ln 2. The loss would fall if the unmatched scores were pushed below zero.

Example
2. The paper's starting values on a batch that is nearly right

Same two pairs, but now use the paper's starting values t=10t = 10 and b=−10b = -10. Suppose the encoders have already produced matched dot products of 0.9 and unmatched dot products of 0.1.

  1. Matched score: s11=10⋅0.9+(−10)=−1s_{11} = 10 \cdot 0.9 + (-10) = -1.
  2. Unmatched score: s12=10⋅0.1+(−10)=−9s_{12} = 10 \cdot 0.1 + (-10) = -9.
  3. Matched cost: −log⁡σ(−1)=log⁡(1+e1)=1.3133-\log \sigma(-1) = \log(1 + e^{1}) = 1.3133.
  4. Unmatched cost: z=−1z = -1, so the argument is zs=9z s = 9, and the cost is log⁡(1+e−9)=0.000123\log(1 + e^{-9}) = 0.000123.
  5. Two matched pairs and two unmatched pairs give 2(1.3133)+2(0.000123)=2.62692(1.3133) + 2(0.000123) = 2.6269.
  6. Divide by 2: the loss is 1.31341.3134.

Interpretation: the matched pairs are still on the wrong side of zero, because a matched pair needs a score above zero, and −1-1 falls short. The unmatched pairs cost almost nothing. The bias starts at −10-10 so that the many negatives do not dominate early training. Raising the matched dot products toward 1 would push the matched scores to 0 and above.

Example
3. Realistic: memory on four chips, and the chunked form

The paper reports that, with four TPU-v4 chips, SigLIP fits a batch of 4096, while a CLIP model fits only 2048. The reason is the size of the table that must sit in memory. Take a batch of ∣B∣=4096|B| = 4096 split over D=4D = 4 chips.

  1. Vanilla softmax loss: the full score table is 4096×4096=16,777,2164096 \times 4096 = 16{,}777{,}216 entries.
  2. Per-device size: b=4096/4=1024b = 4096 / 4 = 1024 images and captions per chip.
  3. Chunked sigmoid loss: each chip holds one 1024×10241024 \times 1024 table, which is 1,048,5761{,}048{,}576 entries.
  4. Ratio: 16,777,216/1,048,576=16=D216{,}777{,}216 / 1{,}048{,}576 = 16 = D^2.

Interpretation: the largest table in memory shrinks by D2D^2, so the same chips can take a larger batch. The paper's point is that a bigger batch then becomes affordable, not that bigger is always better.

Try it: the same method with a batch of ∣B∣=32,768|B| = 32{,}768 on D=8D = 8 chips. How many entries are in the full score table, and how many are in one chip's chunk?

Answer: the full table is 327682=1,073,741,82432768^2 = 1{,}073{,}741{,}824 entries. Each chip has b=32768/8=4096b = 32768 / 8 = 4096, so its chunk is 40962=16,777,2164096^2 = 16{,}777{,}216 entries. The ratio is 64=D264 = D^2.

The math

A <Definition term="Sigmoid loss (pairwise)"> for a batch of ∣B∣|B| image-text pairs: each pair (Ii,Tj)(I_i, T_j) gets the label zij=+1z_{ij} = +1 when i=ji = j and zij=−1z_{ij} = -1 otherwise, and the loss is the average over all pairs of −log⁡σ(zijsij)-\log \sigma(z_{ij} s_{ij}), where σ\sigma is the Sigmoid Function and sijs_{ij} is the score.

Step 1 in symbols: unit vectors

Each embedding is divided by its own length: xi=f(Ii)/∥f(Ii)∥2x_i = f(I_i) / \lVert f(I_i) \rVert_2 and yj=g(Tj)/∥g(Tj)∥2y_j = g(T_j) / \lVert g(T_j) \rVert_2. After this, xi⋅yjx_i \cdot y_j is the Cosine Similarity of the two embeddings.

Step 2 in symbols: the score

sij=t (xi⋅yj)+bs_{ij} = t \, (x_i \cdot y_j) + b

The scale is t=exp⁡(t′)t = \exp(t'), where t′t' is a free parameter, so tt stays positive. The bias bb is a free parameter too. The paper starts t′t' at log⁡10\log 10, so t=10t = 10, and starts bb at −10-10. The pseudocode in the paper's Algorithm 1 computes the same score, logits = dot(zimg, ztxt.T) * t + b.

Step 3 in symbols: the cost of one pair

Lij=−log⁡σ(zijsij)=log⁡(1+e−zijsij)L_{ij} = -\log \sigma(z_{ij} s_{ij}) = \log\left(1 + e^{-z_{ij} s_{ij}}\right)

The second form follows from the first because σ(u)=1/(1+e−u)\sigma(u) = 1 / (1 + e^{-u}), so −log⁡σ(u)=log⁡(1+e−u)-\log \sigma(u) = \log(1 + e^{-u}). Write u=zijsiju = z_{ij} s_{ij}. A large positive uu makes e−ue^{-u} tiny, so the cost is near 0. A large negative uu makes the cost near −u-u.

Step 4 in symbols: the batch loss

L=1∣B∣∑i=1∣B∣∑j=1∣B∣Lij\mathcal{L} = \frac{1}{|B|} \sum_{i=1}^{|B|} \sum_{j=1}^{|B|} L_{ij}

The inner sum runs over every caption for image ii. Each term is independent, so the sum needs no normalisation factor. Within the batch there are ∣B∣|B| positive pairs and ∣B∣2−∣B∣|B|^2 - |B| negative pairs. For a batch of 16,384, that is 163842−16384=268,419,07216384^2 - 16384 = 268{,}419{,}072 negatives for 16,384 positives.

For contrast: the softmax loss

The softmax loss in CLIP normalises across each row and each column of the score grid. Its image-to-text term for image ii is −log⁡et xi⋅yi∑jet xi⋅yj-\log \dfrac{e^{t\, x_i \cdot y_i}}{\sum_{j} e^{t\, x_i \cdot y_j}}, and its text-to-image term is the same with the sum over images. The denominator is the global normalisation that the sigmoid loss removes. Each denominator needs every score in its row or column, which is why the softmax loss needs the whole batch in one place.

Step 5 in symbols: the chunked form for many devices

Suppose the batch is split across DD devices, each holding b=∣B∣/Db = |B| / D images and captions. The paper rewrites the loss as a sum over devices: each device computes the positive pairs it holds and the negatives against one neighbouring chunk, then passes that chunk on to the next device. Each device only ever materialises a b×bb \times b table, so memory drops from ∣B∣2|B|^2 to b2b^2. Because each pair is an independent term, the pieces add up to the full loss without any global step.

Results

  • Small batches favour sigmoid. With batches smaller than 16k, the sigmoid loss outperforms softmax by a large margin in the SigLiT setting (Figure 2). As batches grow, softmax catches up.
  • Batch size saturates. SigLiT performance saturates at about 32k, and the best SigLiT model peaks at 256k with 84.7% zero-shot ImageNet accuracy using a Base text tower. The paper sweeps batch sizes from 512 to 1 million. For SigLIP, 32k is best, whereas softmax needed 98k and still did not beat sigmoid. A batch of 307k hurts both losses.
  • Cheap strong results. SigLiT with a frozen ViT-g/14 image tower, precomputed embeddings and a Large text tower reaches 84.5% zero-shot ImageNet accuracy with batch 20k on four chips in two days (Table 1). With a frozen B/8 checkpoint and four chips for one day, it reaches 79.7%. SigLIP trained from scratch reaches 73.4% in five days on 32 chips.
  • Versus CLIP. The paper says reference [30] and CLIP require about 5 and 10 days respectively on 256 TPUv3 cores. For the Base model, SigLIP at 196 patches reaches 76.2% on ImageNet validation against 68.3% for the CLIP Base baseline in Table 3.
  • Multilingual. With 100 languages, mSigLIP at Base size reaches 34.9% on XM3600 text-to-image retrieval, more than six points above the 28.5% reported for a LiT model with a 4B-parameter image tower.
  • Bias term (Table 4). With bias initialised to −10-10 and t′=log⁡10t' = \log 10, ImageNet zero-shot accuracy is 63.0%, against 62.0% with no bias. With bias 0 and t′=log⁡1t' = \log 1, it is 53.7%.
  • Stability. Lowering β2\beta_2 in Adam and Adafactor from 0.999 to 0.95 stabilises large-batch training.

Limits: most results use the paper's own WebLI and LiT datasets and ImageNet zero-shot accuracy as the main yardstick. The paper reports robustness to label and data noise only for a medium model and one batch size. The mining experiment (Figure 6) masks negatives in a controlled way, and the authors say that finding an efficient way to include more negatives is promising but not trivial.

Common mistakes and tradeoffs

It is tempting to think the sigmoid loss ignores negatives because it looks at one pair at a time. In fact it sums over all ∣B∣2−∣B∣|B|^2 - |B| negatives. The bias sets the starting point so that the negatives do not dominate early on. The paper also shows that randomly removing negatives hurts performance, so the negatives do real work.

It is tempting to think a larger batch is always better. In fact performance saturates near 32k for SigLIP, and 307k hurts both losses.

It is tempting to think the sigmoid loss is softmax with another name. In fact softmax makes the pairs in each row compete for one probability mass, while the sigmoid treats each pair as independent. The largest gap is at small batches.

It is tempting to think the bias is a detail. Table 4 shows it matters for accuracy.

When to use it: small or moderate batches, limited chips, or many devices where the gather step of softmax is costly. When not to use it: if you already have a stable softmax recipe at very large batch and memory is not a constraint, the advantage is smaller.

Reading guide

Check yourself

QuestionFor one pair with score s=0s = 0, what is its cost −log⁡σ(zs)-\log \sigma(z s), whether matched or not?Show answer

Since zs=0z s = 0 either way, the cost is −log⁡σ(0)=−log⁡0.5=ln⁡2≈0.693-\log \sigma(0) = -\log 0.5 = \ln 2 \approx 0.693. A pair at score 0 is undecided, and the two labels cost the same.

QuestionA batch has 1024 pairs. How many negative pairs does it contain?Show answer

There are ∣B∣2−∣B∣=10242−1024=1,048,576−1024=1,047,552|B|^2 - |B| = 1024^2 - 1024 = 1{,}048{,}576 - 1024 = 1{,}047{,}552 negative pairs, against 1024 positive pairs.

QuestionWhy does the paper start the bias at −10-10 rather than 0?Show answer

Most pairs in a large batch are negatives. With a bias of 0, a random starting model gives many negatives scores near 0, which is a cost of ln⁡2\ln 2 each, and the summed gradient is dominated by pushing them down. Starting at −10-10 makes the negatives already cheap, so the first updates do not over-correct. Table 4 shows that the bias with −10-10 improves accuracy.

QuestionA team has eight chips and wants a batch of 65,536 pairs. Using the chunked sigmoid loss, what is the size of the largest table each chip holds, and why is this the relevant quantity?Show answer

Each chip holds b=65536/8=8192b = 65536 / 8 = 8192 images and captions, so its table is 8192×8192=67,108,8648192 \times 8192 = 67{,}108{,}864 entries. This is the largest table in memory at once, so it sets whether the batch fits. The vanilla loss would need the full 65536265536^2 table on one device, which is 4,294,967,2964{,}294{,}967{,}296 entries.

Sources

Read the full paper

Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov et al., 2023. arXiv:2303.15343