# Contrastive learning

> Canonical: https://skillalmanac.com/glossary/contrastive-learning | Last verified: 2026-11-25 | Index status: index-eligible

Contrastive learning is a way of training a model to produce embeddings, lists of numbers describing an input, in which similar examples sit close together and dissimilar ones sit far apart. It can work without human labels.

Also called: contrastive loss, contrastive learning explained, contrastive representation learning.

## In plain language

Contrastive learning teaches a model by comparison. Imagine sorting photos into piles without being told what is in them: you are only told that two photos are the same scene seen differently, such as a crop and a recolour of one picture, and that photos of other scenes are different. The model learns to give the matching pair similar number lists and the others different ones. Its main use is learning useful summaries of data when labels are scarce. The limits: the choice of how to alter the examples shapes what the model learns, and the method can need large batches and long training, which costs computing time and money.

## Technical definition

A contrastive objective takes pairs of inputs and pulls the embeddings of positive pairs together while pushing negative pairs apart. As Lilian Weng's overview describes it, without labels a positive pair is usually a sample and an augmented version of itself, and other samples in the batch often serve as negatives. The SimCLR paper (Chen et al., ICML 2020) reports that the composition of data augmentations is central to defining effective prediction tasks, that a learnable nonlinear transformation between the representation and the contrastive loss improves the learned representations, and that contrastive learning benefits from larger batches and longer training than supervised learning.

## How it shows up when you use agents

Contrastive learning does not usually appear as a command in a coding agent. It shows up inside the models you call. Many [embedding](/glossary/embedding) models, which turn text or images into number lists for search, are described as trained so that related items land close together. When you ask Claude Code, Codex or Cursor to add similarity search to a project, you are relying on that property without seeing the training.

What breaks when it is misunderstood: people assume any embedding measures any kind of similarity. A contrastive model learns the similarity its training pairs defined. The SimCLR paper treats the choice of augmentations as central, which means the notion of "same" was a design decision. Two items you consider related may sit far apart, and the reverse.

A practical next step: when an agent picks an embedding model, ask what the model was trained to treat as similar, then test it on a small set of your own pairs before building on it. Keep that test set separate from anything used to tune the model. Contrastive methods connect to [unsupervised learning](/glossary/unsupervised-learning) because positives can be made from the data itself. The inspected skills are adjacent data checks, not demonstrations of contrastive training or embedding quality.

## Common confusions

- **Contrastive versus supervised learning.** A supervised model learns from labelled answers. A contrastive model can learn from which examples go together, and Weng notes it works in both supervised and unsupervised settings.
- **Contrastive learning versus SimCLR.** SimCLR is one framework for visual representations. The idea is wider than that paper.
- **Positive versus negative pairs.** Positives are examples meant to be similar, such as a sample and its augmented version. Negatives are examples treated as different, often other items in the batch.
- **Myth: no labels means no design choices.** The augmentations and the loss define what the model learns, as the SimCLR abstract states for augmentations.

## Worked examples in this catalogue

### [split-leakage-check](https://skillalmanac.com/skills/skill-almanac/split-leakage-check)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill checks train, validation and test files for duplicates, shared groups, time and label leakage and prints JSON.

Why it exercises the concept: In a SimCLR-style setup, keep augmented views of each original in one split. This CSV/JSONL checker can flag shared supplied group IDs and text-copy signals, but explicitly misses image similarity and transformations. Track original-image provenance separately. Illustrative, not executed.

### [data-analysis-eda](https://skillalmanac.com/skills/skill-almanac/data-analysis-eda)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill is a fixed exploratory-analysis procedure and CSV profiler that keeps findings descriptive, evidenced and caveated.

Why it exercises the concept: It does not fit models, so it is a contrast case rather than a demonstration. Illustrative, not executed.

### [llm-eval-test-sets](https://skillalmanac.com/skills/skill-almanac/llm-eval-test-sets)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill builds and validates an eval dataset for any LLM feature, with a checker for duplicates, missing graders, imbalance and leakage.

Why it exercises the concept: An adjacent LLM evaluation-data example: the checker validates case structure and selected wording-based leakage signals. It does not verify answer truth, run models or evaluate contrastive embeddings. Illustrative, not executed.

## Related terms

- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Unsupervised learning](https://skillalmanac.com/glossary/unsupervised-learning.md)
- [Neural network](https://skillalmanac.com/glossary/neural-network.md)
- [Deep learning](https://skillalmanac.com/glossary/deep-learning.md)
- [Computer vision](https://skillalmanac.com/glossary/computer-vision.md)
- [Machine learning (ML)](https://skillalmanac.com/glossary/machine-learning.md)

## Sources

- [A Simple Framework for Contrastive Learning of Visual Representations (SimCLR) — Chen, Kornblith, Norouzi, Hinton](https://arxiv.org/abs/2002.05709), arXiv (accessed 2026-11-24)
- [Contrastive Representation Learning](https://lilianweng.github.io/posts/2021-05-31-contrastive/), Lil'Log (Lilian Weng) (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
