1.  [](/)
2.  [Diagrams](/diagrams)
3.  Contrastive Learning: How Two Encoders Learn One Space

[Back to Diagrams](/diagrams)IR Foundations

# Contrastive Learning: How Two Encoders Learn One Space

Why a text query can retrieve an image: the pairing is the only supervision.

![Contrastive learning trains a model to answer one question: are these two things related? You give it a batch of N anchor-positive pairs (an image and its own caption for CLIP, an audio clip and its description for CLAP) and nobody ever labels how similar a pair is, because the pairing itself is the entire supervision signal. Inside one batch each anchor has one positive and N-1 negatives, since every other item is by definition a non-match. CLIP scores the batch as an N by N grid of cosine similarities where the diagonal holds the true pairings, and InfoNCE pulls the diagonal together while pushing everything off it apart. Batch size is therefore a first-class hyperparameter rather than a throughput knob: CLIP trained at 32,768 so each anchor faced 32,767 simultaneous negatives and could not win on coarse cues. CLIP uses a softmax over the batch, SigLIP replaces it with a pairwise sigmoid that needs no global normalization and so trains stably at large scale.](/diagrams/contrastive-learning.webp)Full screen

Why a text query can retrieve an image: the pairing is the only supervision.

Contrastive learning trains a model to answer exactly one question: are these two things related or not. You hand it a batch of N (anchor, positive) pairs, an image and its own caption for CLIP, an audio clip and its own description for CLAP, a query and a relevant document for a text model. Nobody ever labels how similar a pair is. The pairing is the entire supervision signal, which is why this scales to hundreds of millions of examples scraped off the internet.

Inside one batch, each anchor has exactly one positive and N-1 negatives, because every other item in the batch is by definition a non-match. CLIP scores the whole batch as an N by N grid of cosine similarities where the diagonal is the set of true pairings. Training pulls the diagonal together and pushes everything off it apart. That is InfoNCE: a softmax cross-entropy where the correct class is the matching positive and the wrong classes are the rest of the batch.

The consequence people miss is that batch size becomes a first-class hyperparameter rather than a throughput knob. CLIP trained at 32,768, so every anchor was contrasted against 32,767 negatives at once and could not win by learning coarse features like this-is-a-photo versus this-is-text. At batch 32 the task is easy enough to pass on superficial cues, which is why small-batch fine-tuning often produces embeddings less discriminative than the model you started from. The common floor is 256, and 1024 or more is better.

SigLIP changes one thing and it matters: it replaces the softmax with a pairwise sigmoid loss. A softmax has to normalize across the whole batch, which couples every example to every other and makes very large batches expensive to coordinate. A sigmoid scores each pair independently, so it trains stably at scale without that global normalization. Same idea, cheaper arithmetic.

What you get at inference is the part that pays for all of it: two encoders that map into one space, so a text query can retrieve an image it has never been paired with. The model was trained to rank pairs, and that is precisely what retrieval asks it to do.

## Where this diagram appears

*   [Guide: How CLIP, SigLIP and CLAP Work →](/guides/contrastive-learning-how-clip-siglip-and-clap-work)

Run this on your own data

Mixpeek turns video, images, audio, and documents in your object storage into searchable, timestamped results through one API.

[Search your own data](https://studio.mixpeek.com/namespaces/create?mode=managed&utm_source=homepage&utm_medium=diagram_detail&utm_campaign=mvs_onramp)

## Related diagrams

[

![Diagram placing BM25, SPLADE and dense embeddings on a single axis measuring how much vocabulary survives compression, with what each one stores and why production systems run more than one.](/diagrams/sparse-vs-dense.webp)

Sparse and Dense Are One Spectrum, Not Two Camps



](/diagrams/sparse-vs-dense)[

![A 3072-dimensional embedding encodes everything about a video and distinguishes nothing: lighting, camera angle, dominant color, who is on screen, the room and the vibe of the room are all smeared across the same floats at equal weight, with no name you can filter on. That is why a search for a person holding a coffee cup returns videos with people, videos with cups, and a surprising number with neither, and why reranking and hybrid BM25 each help only a little. Decomposition is where the dimension reduction actually lives: a shot detector turns a thirty-second video into eight scenes with timestamps, a face identity model turns a frame into bounding boxes and 512-dimension face embeddings, a multimodal extractor turns a scene into a Gemini embedding plus OCR output plus dominant colors, and a custom plugin does the same for a proprietary file type. Each extractor's output becomes its own collection with its own schema and its own embedding space, so the features are named and filterable where a smear is not. Reduction alone leaves a smarter vector database; the leverage is placement. A new ad is placed against a brand taxonomy at ingest, and because the taxonomy that locates it is the same retriever a user invokes to ask for ads in that brand, enrichment and search become one traversal in two directions. Six primitives compose in one direction: bucket, extractor, collection, retriever, taxonomy, cluster.](/diagrams/the-3072-dimension-problem.webp)

The 3072-Dimension Problem: Reduce, Then Place



](/diagrams/the-3072-dimension-problem)[

![An ordinary embedding model spreads information across all dimensions with no ordering guarantee, so dimension 900 may carry as much signal as dimension 5 and truncating the vector discards random pieces of meaning. Matryoshka Representation Learning changes the objective so the model packs the most general information into the front of the vector and progressively finer detail toward the back, which makes every prefix a complete usable coordinate. The nesting is drawn as five bars that all start at dimension 0 and grow: the first 64 dimensions carry coarse semantics, enough to tell a cat photo from a car photo; 128 is the prefix the retrieval cascade indexes the whole corpus at; 256 gives mid resolution, enough to separate breeds of cat or a refund request from a refund confirmation; and the full 1024 gives fine resolution for ranking near-duplicates. The mechanism is a modified loss rather than a special architecture: one forward pass produces one d-dimensional output, the training loss is computed independently at each prefix size in the set 64, 128, 256, 512, 1024, and those losses are summed, so the front of the vector has to work on its own. What it buys is a retrieve-coarse, rerank-fine cascade: index 10,000,000 items at a 128-dimension truncation costing 5.1 GB instead of the full 41 GB, retrieve a top-200 shortlist with the truncated query, fetch the full 1024-dimension vectors for those 200 candidates only, and return a final top 10.](/diagrams/matryoshka-representation-learning.webp)

Matryoshka Representation Learning: One Vector, Five Resolutions



](/diagrams/matryoshka-representation-learning)