What a contrastive dual encoder is
An image classifier is trained to pick one of a fixed list of labels. CLIP learns from captions instead. Two separate encoders, one for images and one for text, map their inputs into the same 512-dimensional space, and training pulls each image towards its own caption and away from the other captions in the batch. Afterwards any text can serve as a label: write each class as a caption, embed it, and pick the caption that lies closest to the image. That is zero-shot classification, with no training on the classes themselves.
The page records CLIP ViT-B/32 (151,277,313 parameters) on 4 images (coloured shapes, a cat, a beagle, an astronaut) and 5 captions: “a drawing of colored shapes”, “a photo of a cat”, “a photo of a dog”, “a photo of an astronaut” and “a photo of a beagle”. Each encoder runs once per input, and the page shows all 12 blocks of both.
Two encoders
The Image Encoder ViT-B/32 row is a Vision Transformer (see the ViT tab). The 224 × 224 image is cut into a 7 × 7 grid of 32-pixel patches, each projected to width 768, and a learned [CLS] token is prepended: 50 tokens. Position embeddings are added and a pre-LayerNorm applied, then 12 bidirectional blocks follow, with 12 heads of 64 and an MLP, 768 → 3072 → 768. The MLP uses QuickGELU, OpenAI CLIP's sigmoid approximation of GELU, with which these weights were trained:
The Text Encoder causal transformer row reads the caption as byte-pair tokens from a 49,408-token vocabulary. Every caption here is 7 tokens: start-of-text, five words and end-of-text (EOT). Its 12 blocks of width 512 with 8 heads use a causal mask, like a GPT decoder, so only EOT has read the whole caption; the text encoder therefore pools at EOT, as the image encoder pools at [CLS].
Zero-shot classification
For one image, a softmax over the captions turns its row of the matrix into probabilities:
The Which caption? list shows them. Each image's top caption is a correct one: 99.9 % for the drawing, 99.3 % for the cat and 100.0 % for the astronaut. The beagle matches two captions and the softmax has to share between them: 88.8 % “a photo of a beagle” and 11.2 % “a photo of a dog”. These probabilities exist only relative to these five captions; another set of captions gives another softmax. The CLIP paper's prompts follow this pattern (“a photo of a {label}”), and it found that such templates classify better than bare class names.
Training with the symmetric contrastive loss
Training takes a batch of image–caption pairs and computes the full matrix of scaled similarities. Each image should pick out its own caption among the , and each caption its own image, so the loss averages a cross-entropy along the rows and one along the columns, with the matching pair as the target:
Each term is the InfoNCE loss of contrastive predictive coding, applied in both directions; ConVIRT had used this bidirectional form for medical images and their reports. The other captions in the batch serve as negatives, so larger batches give harder comparisons. CLIP trained on 400 million image–text pairs collected from the internet and matched the original ResNet-50's ImageNet accuracy zero-shot. The page records inference only.
Where it came from
ConVIRT (2020) introduced the bidirectional image–text contrastive objective on medical images and their reports. CLIP (Radford et al., 2021) scaled it to the open web, and ALIGN trained the same kind of dual encoder on over a billion noisy alt-text pairs. OpenCLIP retrained CLIP on public data and measured how it scales. SigLIP replaced the softmax over the batch with an independent sigmoid for every pair; a SigLIP encoder is the vision half of the VLM tab's SmolVLM. SAM 3 reads its concept prompts with a CLIP-style text encoder.
Papers
- Learning Transferable Visual Models From Natural Language Supervision CLIP, the model this page records: dual encoders trained contrastively on 400 million image–text pairs, and zero-shot classification with captions.
- Contrastive Learning of Medical Visual Representations from Paired Images and Text ConVIRT: the bidirectional image–text contrastive objective that CLIP adopts.
- Representation Learning with Contrastive Predictive Coding The InfoNCE loss: pick the true pair among negatives with a softmax.
- An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale The Vision Transformer, CLIP's image encoder here with 32-pixel patches.
- Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision ALIGN: the same dual-encoder recipe on over a billion uncurated alt-text pairs.
- Reproducible scaling laws for contrastive language-image learning OpenCLIP: CLIP retrained on public LAION data, with scaling laws for model, data and compute.
- Sigmoid Loss for Language Image Pre-Training SigLIP: a per-pair sigmoid loss in place of the batch softmax.
See also
- RetrievalEmbeddings Sentence embeddings · contrastive training with InfoNCE
- DiffusionSD 1.5 Stable Diffusion 1.5 · latent U-Net
- DiffusionSDXL Stable Diffusion XL · latent U-Net
- DiffusionShap-E Shap-E · generating an implicit 3D function
- DiffusionFew-step Few-step distillation · LCM, Turbo and consistency models on one prompt
- RetrievalTraining Training an embedding model · in-batch and hard negatives reshaping the space