Contrastive losses pull related pairs together and push unrelated pairs apart, installing the geometric property that makes embeddings useful for similarity search.
Imagine sorting a giant pile of photos onto a wall. Someone hands you photos in pairs and tells you these two are of the same dog, so hang them next to each other. Occasionally they also say this one and that one are unrelated, so move them apart. After thousands of these instructions, the wall organises itself. Dogs end up near dogs, cats near cats, sunsets near sunsets. Nobody told you what the categories were. The pulling close and pushing apart did the sorting for you. That is the recipe behind teaching a system how to turn text or pictures into a useful layout. The instruction repeats across millions of pairs until the wall arrangement matches meaning.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Contrastive learning is the engine behind every modern embedding model. The intuition is simple: make similar things close and different things far. But the implementation details determine whether the model trains well, scales to billions of pairs, and produces useful production geometry.
The pull and push objective in detail
Contrastive learning operates on pairs of inputs. For a query q (a sentence, image, code snippet, whatever), the training signal includes one positive k+ (a semantically related counterpart) and one or more negatives k-_i (unrelated counterparts).
The loss has two components, often combined into a single formula:
- Increase
sim(q, k+): pull positives together. - Decrease
sim(q, k-_i): push negatives apart.
Where positives come from varies by task. For sentence embeddings, common sources include question-answer pairs from datasets like NQ and MS MARCO, paraphrase pairs from natural language inference (NLI) data, multilingual translation pairs, and synthetic pairs generated by LLMs. SimCSE showed that even dropout-augmented self-pairs (the same sentence encoded twice with dropout enabled, producing two slightly different vectors) work as positives.
Negatives come from three sources: in-batch (every other example in the batch), random sampling from the corpus, and hard-negative mining (semantically similar but incorrect documents, found via BM25 or a prior model's top-K). The mix of negative sources is one of the most consequential design decisions in embedding training.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- OpenAI's cpt-text models, earlier OpenAI embedding models, were trained with contrastive pre-training on large sets of paired text.
- BAAI BGE-M3 uses multi-stage contrastive training with hard-negative mining and multilingual batches.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does InfoNCE outperform triplet loss at scale?
InfoNCE benefits from more negatives: the lower bound on mutual information tightens with batch size. Triplet loss only sees one negative per triplet, so its gradient signal is bounded. InfoNCE also avoids margin tuning, which is fiddly.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Saying contrastive loss "minimises distance" without mentioning that it ALSO maximises distance for negatives. The contrast is the whole point.
60 second bullets to scan on the way to the call.
Define what a positive pair and a negative pair are in contrastive training.
State the dual goal: pull positives together, push negatives apart.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.