CLIP — Learning Pictures From the Words Next to Them

  1. 1Attention Before Transformers — Score, Squash, Multiply
  2. 2Query, Key, Value — Dot-Product Attention from Scratch
  3. 3Attention Is All You Need — The Transformer, Block by Block
  4. 4Attention Meets Pixels — Windows, Axes and the First Fully Attentional Networks
  5. 5DETR — Detection as Set Prediction, No Anchors and No NMS
  6. 6An Image Is Worth 16×16 Words — ViT, Patch by Patch
  7. 7DeiT — Training a Vision Transformer on ImageNet Alone
  8. 8Swin Transformer — Windows, Shifts and a Pyramid for Dense Prediction
  9. 9Convolutions Strike Back — Was It the Attention or the Recipe?
  10. 10Masked Autoencoders — Hide 75 %, Encode the Rest
  11. 11CLIP — Learning Pictures From the Words Next to Them
  12. 12Vision Transformers Today — May 2022 to October 2026, Year by Year

Summary

How CLIP turns 400 million image–caption pairs into one N × N matching game, why the loss is a cross-entropy in both directions with a learned temperature, and how a sentence template turns the result into a classifier that has never seen the dataset. After this read you can compute the loss of a batch by hand.

Table of Contents

  1. 1. Why Not Just Predict the Caption?2
  2. 2. The Pipeline, Module by Module2
  3. 3. Zero-Shot Transfer by Prompting4
  4. 4. Results5
  5. 5. What Came Next6
  6. 6. References6
  7. 7. Connections6
TrivediInformational[Page 1]
S. TrivediOct 2026

A supervised vision model learns a fixed list of classes from a crowd-labelled dataset; add a task and you collect labels again. Meanwhile the web is full of pictures with text next to them, already describing what is in the picture, for free. CLIP by Radford et al. [RADF21] is the recipe that makes that text the supervision: encode the image, encode the caption, and train both encoders so that the right pairs match and the wrong pairs do not. Classification then becomes a question you ask in English. The whole system is in one picture:

the N captions of the batchText encoder: 12-layer transformerT1T2T3T4T5W_t, L2-normalisedN imagesImageencoderViT-L/14W_i, L2-normalisedI1I2I3I4I5I1·T1I1·T2I1·T3I1·T4I1·T5I2·T1I2·T2I2·T3I2·T4I2·T5I3·T1I3·T2I3·T3I3·T4I3·T5I4·T1I4·T2I4·T3I4·T4I4·T5I5·T1I5·T2I5·T3I5·T4I5·T5logits L = (I·T) / τ : an N × N matrixdiagonal: the N real pairs; the rest: N² − N wrong onesrow i: softmax over the N captions,cross-entropy with target i→ L(I→T)column j: softmax over the N images,cross-entropy with target j→ L(T→I)L = ½ (L(I→T) + L(T→I))τ learned, init 0.07, 1/τ clipped at 100N = 32,768 in the paper: 2³⁰ cells a step
Fig. 1 CLIP pre-training. Two encoders, a linear projection each, L2-normalised vectors, and an N × N matrix of scaled cosines. Each row must pick its own column, each column its own row; the diagonal is the answer key.

NOTE: CLIP generates nothing. Two encoders and a dot product; it ranks captions it is given and writes none. DALL·E used CLIP to re-rank its samples, which is a different model.

Brush up first:

1. Why Not Just Predict the Caption?

The first thing the authors tried was the obvious one: a transformer that writes the caption of each image, VirTex-style. It was too slow. "A 63 million parameter transformer language model ... learns to recognize ImageNet classes three times slower than a much simpler baseline that predicts a bag-of-words encoding of the same text" (§2.2, Figure 2). Their diagnosis: web text is "a wide variety of descriptions, comments, and related text that co-occur with images", so predicting the exact words is the wrong task. Predicting which text as a whole is paired with which image is enough, and swapping the prediction for that contrastive objective gave "a further 4× efficiency improvement in the rate of zero-shot transfer to ImageNet".

  • Caption generation → bag-of-words: 3× faster.
  • Bag-of-words → contrastive matching: 4× faster again.
  • About 12× in total for the same zero-shot ImageNet accuracy. The task got easier and the model got better; that is the trade CLIP makes.

The contrastive form itself is a simplification of ConVIRT [ZHAN20], which did the same for medical images and reports; ALIGN [JIA21] published the same idea in the same month with noisier data.

2. The Pipeline, Module by Module

Symbols: a batch of N pairs (Ii,Ti); f is the image encoder and g the text encoder; Wi and Wt are learned linear projections into a shared space of de dimensions (768 for ViT-L/14, 512 for the B models, Table 20 of the paper); t is a learned scalar and τ=e−t the temperature.

2.1 The Data

WIT, 400 million (image, text) pairs collected from the internet, built from 500,000 search queries with up to 20,000 pairs per query so that no single query dominates (§2.2). No labels. The only augmentation used anywhere is a random square crop.

2.2 Two Encoders, One Linear Projection Each

  • Text. A 63 M-parameter transformer: 12 layers, width 512, 8 heads, a 49,152-token BPE vocabulary, at most 76 tokens, masked (causal) self-attention. The activation at the [EOS] token is the text feature (§2.4). For ViT-L/14 the text tower grows to 12 layers of width 768 with 12 heads.
  • Image. Either a modified ResNet (RN50 up to RN50x64) or a ViT: B/32, B/16, L/14. The ResNet details (attention pooling, anti-aliasing) are one line in the paper and one line here; this series is about transformers.
  • Projection. Each tower's output goes through a linear projection to the shared space and is L2-normalised. "We instead use only a linear projection"; the non-linear heads of the contrastive literature made no difference in training efficiency.

Let's write the two unit vectors:

𝐮i=Wi⊤f(Ii)‖Wi⊤f(Ii)‖2∈ℝdeimage i𝐯j=Wt⊤g(Tj)‖Wt⊤g(Tj)‖2∈ℝdetext j(1)
  • Because both are unit vectors, the dot product 𝐮i·𝐯j is the cosine similarity, a number in [−1,1].
  • The text side is the cheap half: the base text tower is 63 M parameters, against the 307 M of a ViT-L (Table 1 of An Image Is Worth 16×16 Words — ViT, Patch by Patch).

2.3 The N × N Logits and the Temperature

Take every image against every caption in the batch and scale by the learned temperature. This is the line logits = np.dot(I_e, T_e.T) * np.exp(t) of the paper's Figure 3 pseudocode:

Lij=et𝐮i·𝐯j=cos(𝐮i,𝐯j)τ,L∈ℝN×N(2)
  • Lii is the real pair; the other N2−N entries are wrong pairs, and they are the negatives. Nobody labels them: they are whatever else landed in the batch.
  • τ is learned, not a fixed hyper-parameter. It is "initialized to the equivalent of 0.07 ... and clipped to prevent scaling the logits by more than 100 which we found necessary to prevent training instability" (§2.5). So et0=1/0.07=14.3 at the start, and et≤100, i.e. τ≥0.01, always.

Why the clip: a cosine lives in [−1,1], so the logits live in [−14.3,14.3] at initialisation and in [−100,100] at the clip. Suppose the right caption is at logit 100 and a wrong one at 0. The softmax probability of the wrong one is about e−100≈4×10−44, and by the saturation argument of Query, Key, Value — Dot-Product Attention from Scratch §4 its gradient is the same size: nothing. Let the scale run past 100 and every wrong pair goes invisible to the gradient. The paper says only that the clip was "necessary to prevent training instability"; this arithmetic is my reading of why, not theirs.

TrivediInformational[Page 1]
S. TrivediOct 2026

2.4 The Symmetric Cross-Entropy

Now the loss, and it is two losses. Row i of L is image i's opinion of all N captions; it must pick column i. Let's devise it step by step:

ℒI→T=−1N∑i=1Nlogexp(Lii)∑j=1Nexp(Lij)(3)
  • Inside the log is a softmax over row i, evaluated at the diagonal. It is an N-way multiple-choice: "which of these N captions is yours?"
  • The paper's pseudocode writes this as cross_entropy_loss(logits, labels, axis=0) with labels = np.arange(n). The label of image i is simply i.

Column j is caption j's opinion of all N images, and it must pick row j. Same game, other direction, and the two are averaged:

ℒT→I=−1N∑j=1Nlogexp(Ljj)∑i=1Nexp(Lij)ℒ=12(ℒI→T+ℒT→I)(4)
  • Eq. 3 normalises across a row, Eq. 4 across a column; the pseudocode's axis=0 and axis=1 are these two directions.
  • Chance level for either direction is lnN. With the paper's batch of 32,768 that is ln32,768=10.40 nats; the loss starts near 10.4 and has to fall from there, against 32,767 wrong captions per image, per step.

Play with the matrix below. Lower τ and every row sharpens; add pair noise and the diagonal stops being the obvious maximum. Watch what a sharp temperature does to a noisy batch: the loss explodes, which is the clip of Section 2.3 seen from the other side.

-1.1550.3
One batch, N × N logits, the diagonal is the answer. Rows are images, columns are captions; each cell is the softmax probability the image gives that caption, darker is higher, the outlined cells are the true pairs. Lower τ sharpens every row; noisier pairs make the right answer harder to find.

NOTE: the matrix is the expensive part. At N = 32,768 it has 32,768² = 1,073,741,824 entries, exactly 2³⁰: 4.29 GB in fp32, 2.15 GB in fp16, for one matrix, before the softmax and before any gradient. The paper shards it: each of the 256 GPUs computes "only the subset of the pairwise similarities necessary for their local batch", so one GPU holds 128 rows × 32,768 ≈ 4.2 M entries (about 17 MB in fp32) and gathers only the embeddings, 32,768 × 768 × 2 B ≈ 50 MB. The full matrix never exists anywhere.

2.5 Batch and Schedule

  • Batch 32,768; 32 epochs; Adam with cosine decay; mixed precision. 400 M × 32 = 12.8 billion image–text views.
  • The largest ResNet, RN50x64, took 18 days on 592 V100s (≈ 255,700 GPU-hours). ViT-L/14 took 12 days on 256 V100s (≈ 73,700 GPU-hours), then one extra epoch at 336 px. That last model, ViT-L/14@336px, is the one called "CLIP" in every result below.
TrivediInformational[Page 2]
S. TrivediOct 2026

3. Zero-Shot Transfer by Prompting

Here is the part that made people sit up. After pre-training there is no classifier head and no class vocabulary, so how do you classify? You write the classes down. For K classes, put each class name into a sentence, embed the sentences once with the text tower, and let the image pick the nearest one:

class namesone template, filled inpug"a photo of a pug."crane"a photo of a crane."kite"a photo of a kite."tractor"a photo of a tractor."boxer"a photo of a boxer."TextencoderT1T2T3T4T55 text embeddings,computed once;the paper averages80 templates eachImage encoderII·T_k / τ for k = 1…5softmax → argmaxboxerno training exampleof this dataset seen
Fig. 2 Zero-shot classification. Class names become sentences, the frozen text tower embeds them once, and the image embedding picks the class with the highest cosine. No example of the target dataset is used.
𝐯¯k=normalize(1|𝒫|∑p∈𝒫𝐯(p(namek))),y^=argmaxk𝐮·𝐯¯k(5)
  • 𝒫 is a set of prompt templates such as "A photo of a {label}."; p(namek) is the template filled with class k. With a single template the sum has one term.
  • The averaging happens in embedding space (§3.1.4), so the K averaged vectors are computed once and the test-time cost is one dot product per class, whatever |𝒫| is.

Why a sentence and not the bare word? Two reasons the paper gives:

  • Polysemy. A lone "boxer" is a dog or an athlete; a lone "crane" is a bird or a machine. Context in the sentence picks one.
  • Distribution gap. The text tower saw full sentences during pre-training, rarely a single word. "A photo of a {label}." puts the label back into the kind of text the model has seen, and alone it is worth +1.3 % on ImageNet. Ensembling 80 hand-written templates in embedding space adds +3.5 % more; together "almost 5 %" (Figure 4).

The figure below is a toy of that mechanism with made-up vectors; the only real numbers in it are the paper's gains quoted in the third tile. Step through the prompt set and watch the probability of the true class.

10.3
Dressing a label in a sentence (toy). Softmax over five classes for one toy image of a boxer. Template 1 is the bare word; 2 adds "a photo of a {label}."; 3 to 5 average more sentences in. Each single sentence lands a little off its concept (the wobble); averaging cancels that. Toy numbers; the paper's are +1.3 and +3.5 points on ImageNet.

NOTE: prompt engineering is part of the method, and it is worth five points. A "CLIP zero-shot" number quoted without the 80-template ensemble is a different system; compare like with like.

TrivediInformational[Page 3]
S. TrivediOct 2026

4. Results

4.1 ImageNet Without ImageNet

Zero-shot ImageNet top-1 for ViT-L/14@336px is 76.2 % (Table 11), with 95 % top-5. That matches the original ResNet-50, which was trained on the 1.28 million labelled ImageNet images; CLIP saw none of them. The previous best zero-shot result, Visual N-Grams, was 11.5 %.

Few-shot puts the same fact another way (§3.1.5, Figure 6): "zero-shot CLIP matches the average performance of a 4-shot linear classifier" trained on CLIP's own features across the suite, and on ImageNet it "matches the performance of a 16-shot linear classifier trained on the same feature space". Writing the class name down is worth sixteen labelled examples per class.

4.2 Twenty-Seven Datasets

The headline comparison is zero-shot CLIP against a supervised linear probe on ResNet-50 features, across 27 datasets (Figure 5). CLIP wins 16 of 27. The deltas, in percentage points:

datasetΔdatasetΔdatasetΔ
StanfordCars+28.9HatefulMemes+6.7Birdsnap−3.2
Country211+23.2CIFAR10+3.9MNIST−10.0
Food101+22.5CIFAR100+3.0FGVCAircraft−11.3
Kinetics700+14.5STL10+3.0RESISC45−11.9
SST2+12.4FER2013+2.8Flowers102−12.5
SUN397+7.8Caltech101+2.0DTD−16.6
UCF101+7.7ImageNet+1.9CLEVRCounts−18.2
OxfordPets+1.1GTSRB−18.4
PascalVOC2007+0.5PatchCamelyon−19.5
KITTI Distance−34.0
EuroSAT−37.1

Table. 1 Zero-shot CLIP minus a linear probe on ResNet-50 features, percentage points (Figure 5 of [RADF21]). Positive is a CLIP win.

  • The wins are the datasets the web talks about: cars, food, countries, scenes, actions.
  • The losses are the datasets the web does not: satellite crops (EuroSAT), distance to the car in front (KITTI), counting (CLEVR), traffic signs, lymph-node slides. Nobody captions a photo "satellite image of annual crop land". Zero-shot is not uniformly good; it is good where the text exists.

NOTE: "zero-shot" means no training examples of the target dataset, not no data. The 27-dataset suite was used repeatedly during development; the authors call it "undeniably co-adapted" (§6). And the paper's own estimate of the distance left: "around a 1000× increase in compute is required for zero-shot CLIP to reach overall state-of-the-art performance".

4.3 Zero-Shot Against a Linear Probe on CLIP Itself

A linear probe on CLIP's own frozen features (Table 10) is the ceiling that zero-shot is reaching for, and the gap tells you how much of a dataset is "in the text":

datasetzero-shotlinear probegap
ImageNet76.285.49.2
CIFAR1095.797.92.2
Food10193.895.92.1
OxfordPets93.595.11.6
StanfordCars78.891.512.7
Flowers10278.399.220.9
FGVCAircraft37.271.634.4
GTSRB52.392.440.1
EuroSAT59.698.138.5
KITTI Distance21.969.247.3
CLEVR Counts24.860.335.5

Table. 2 ViT-L/14@336px, zero-shot (Table 11 of [RADF21]) against a linear probe on the same features (Table 10 of [RADF21]), top-1 %.

  • On pets and food the gap is two points: the text already says it all. On EuroSAT the same features reach 98.1 % with a probe but 59.6 % zero-shot: the features know, the prompt cannot ask.
  • As a feature extractor CLIP also wins outright: the best CLIP beats Noisy-Student EfficientNet-L2 by +2.6 % on the 12-dataset suite and by about 5 % on all 27, winning 21 of 27 (§3.2).
  • Scaling behaves (Figure 9): average zero-shot error falls log-linearly across a 44× range of compute over five CLIP models, the data-scale lesson of An Image Is Worth 16×16 Words — ViT, Patch by Patch again.
TrivediInformational[Page 4]
S. TrivediOct 2026

4.4 Robustness to Distribution Shift

The result I find most convincing is Figure 13. Take zero-shot CLIP and a ResNet-101 with the same 76.2 % ImageNet accuracy and move both to shifted test sets:

test setResNet-101zero-shot CLIPΔ
ImageNetV264.370.1+5.8
ImageNet-R37.788.9+51.2
ObjectNet32.672.3+39.7
ImageNet-Sketch25.260.2+35.0
ImageNet-A2.777.1+74.4

Table. 3 Natural distribution shift at equal ImageNet accuracy, top-1 % (Figure 13 of [RADF21]). "Zero-shot CLIP models shrink the gap by up to 75 %."

  • ImageNet-A: 2.7 % → 77.1 %. A model that never fitted ImageNet's particular photographs has nothing to overfit to.

5. What Came Next

The symmetric softmax needs the global N×N normalisation, so SigLIP [ZHAI23] replaced it with a per-pair sigmoid that works at smaller batches and needs no all-gather of the matrix; LiT [ZHAI21] locked the image tower and trained only the text side; OpenCLIP [CHER22] and MetaCLIP [XU23] rebuilt the data in the open. Where the towers went from there, into Segment Anything, SigLIP 2 and the Perception Encoder, is the last post of the series, Vision Transformers Today — May 2022 to October 2026, Year by Year, which picks up in May 2022 with the pieces of this post and Masked Autoencoders — Hide 75 %, Encode the Rest on the table.

6. References

  1. [RADF21] Radford et al. Learning Transferable Visual Models From Natural Language Supervision. 2021.
  2. [ZHAN20] Zhang, Jiang, Miura, Manning, Langlotz. Contrastive Learning of Medical Visual Representations from Paired Images and Text. 2020.
  3. [JIA21] Jia et al. Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision. 2021.
  4. [ZHAI23] Zhai, Mustafa, Kolesnikov, Beyer. Sigmoid Loss for Language Image Pre-Training. 2023.
  5. [ZHAI21] Zhai et al. LiT: Zero-Shot Transfer with Locked-image text Tuning. 2021.
  6. [CHER22] Cherti et al. Reproducible scaling laws for contrastive language-image learning. 2022.
  7. [XU23] Xu et al. Demystifying CLIP Data. 2023.
  8. [DOSO20] Dosovitskiy et al. An Image is Worth 16×16 Words. 2020.
  9. [VASW17] Vaswani et al. Attention Is All You Need. 2017.

7. Connections

What this page builds on, cites or links to, and what points back at it. A plain box is a “builds on”; “cited” and “linked in the text” are the lighter links. Hover a box for a preview.

POINTS AT THIS PAGEREADVision Transformers Today — May 2022 toOctober 2026, Year by YearPAPERRT-DETR-World — Naming Any Object inReal TimePAPER · LINKED IN THE TEXTMasked Autoencoders — Hide 75 %, Encodethe RestThis pageTHIS PAGE POINTS ATPAPERAttention Is All You Need — TheTransformer, Block by BlockPAPERAn Image Is Worth 16×16 Words — ViT,Patch by PatchPAPERMasked Autoencoders — Hide 75 %, Encodethe RestREFERENCE · CITEDContrastive Learning of Medical VisualRepresentations from Paired Images andTextREFERENCE · CITEDScaling Up Visual and Vision-LanguageRepresentation Learning With Noisy TextSupervisionREFERENCE · CITEDLiT: Zero-Shot Transfer withLocked-image text TuningREFERENCE · CITEDReproducible scaling laws forcontrastive language-image learningREFERENCE · CITEDDemystifying CLIP DataREFERENCE · CITEDSigmoid Loss for Language ImagePre-TrainingREAD · LINKED IN THE TEXTQuery, Key, Value — Dot-ProductAttention from ScratchREAD · LINKED IN THE TEXTVision Transformers Today — May 2022 toOctober 2026, Year by Year

Related

Go deeper

  1. Start with
    Read
  2. Paper
  3. Paper
  4. Paper
  5. Paper
  6. Read
  7. Read
  8. Paper
  9. Read
  10. Paper
  11. Paper
  12. Paper
  13. Paper
  14. Paper
  15. Read
  16. Paper
  17. You are here
    CLIP — Learning Pictures From the Words Next to Them
    Paper
  18. Then
    Read
  19. Paper

Go broader

<- Older [Page 6] Newer ->