Contrastive Self-Supervised Learning
Contrastive learning trains a network to represent two different views of the same underlying thing as similar, and different things as dissimilar, producing useful features without any human-provided labels at all.
Prerequisites: Learned Embeddings for Categorical Features, Transfer Learning and Fine-Tuning
Supervised learning needs labels, and labels are expensive — someone had to decide, by hand, what the "right answer" is for every training example. Most raw data in the world has no labels at all: years of unlabeled tick data, unlabeled news text, unlabeled satellite images. Contrastive learning is a way to train a genuinely useful network on that unlabeled data by inventing its own task, one that needs no human annotation whatsoever.
The trick: create two different, distorted "views" of the same underlying example — say, two overlapping crops of one chart image, or one bar of returns and a slightly noised copy of it — and train the network to recognize that they came from the same source, while also recognizing that a view from a completely different example is not the same source. It's like teaching someone to recognize a person from photos taken at different angles, lighting, and distance: they never need a label saying "this is person X," they just need to learn "these two photos, despite looking different, are the same person" versus "these two photos are different people." Do that across millions of photo pairs and person-recognition ability emerges as a byproduct, with the labels of who each person is never entering the picture.
The mechanics, one symbol at a time
For an example , create two augmented views and (say, two noised or cropped versions), and pass both through the same encoder to get representations , . Call a positive pair (same underlying source) and every other example in the batch a source of negative pairs. A common contrastive loss, InfoNCE, for one positive pair is:
In words: is a similarity score between two representations (typically cosine similarity — how aligned two vectors are, close to 1 if they point the same way). The numerator rewards the positive pair for being similar; the denominator sums similarity to every other example in the batch (the negatives), including the positive itself. Minimizing this loss means pushing the positive pair's similarity up relative to all the negatives — the representation of should be closer to its true partner than to any unrelated example . (the temperature) controls how sharply the model is punished for near-misses: a small makes the loss much harsher on negatives that are almost as similar as the positive, forcing tighter separation.
Worked example 1: computing the loss for a small batch
A batch has one positive pair with cosine similarity , and two negatives with similarities and . Temperature .
Scaled similarities: positive , negatives and .
Exponentiate: (positive, appears in both numerator and denominator), , .
Denominator: .
Now suppose training has pulled the positive pair much closer, , everything else unchanged: scaled , . Denominator . Loss: , lower — the loss falls as the positive pair becomes relatively more similar than the negatives, exactly the direction training pushes it.
Worked example 2: the role of temperature
Same scenario as above ( values ) but with a much smaller temperature, : scaled similarities become . Exponentials: , , . Denominator .
A far smaller loss than gave for the identical similarities — a low temperature makes an already-decent separation look almost perfect to the loss function, so gradients shrink and the model coasts. Push too low the other direction (very tiny) and gradients for genuinely close negatives explode instead. This is why is a sensitive hyperparameter in practice: it rescales how harshly "almost as similar as the positive" is punished, and getting it wrong either starves training of signal or destabilizes it.
The exponentials inside InfoNCE mean small changes in similarity, after dividing by a small , translate into large changes in the loss — the explorer above's exponential curve is exactly this amplification, and it's why temperature has an outsized effect on training dynamics.
What this means in practice
In quant work, contrastive pretraining is used to build reusable representations of price series, limit-order-book snapshots, or news text from vast unlabeled history, before fine-tuning a small labeled head on a specific supervised task — the same transfer-learning pattern used in vision and language, applied to market data where labels (like "this was profitable" or "this preceded a crash") are far scarcer than raw data itself. The quality of the learned representation depends heavily on the choice of augmentations: for a chart image, cropping and slight noise are natural "same underlying thing" views, but for financial time series it is genuinely unclear what a label-preserving augmentation even is — shuffle too aggressively and you destroy the temporal structure that made the series meaningful in the first place, which is an open and actively debated design problem in applying contrastive learning to finance.
Contrastive learning trains an encoder using only the fact that two views came from the same source or different sources, no human labels required — pulling same-source representations together and pushing different-source ones apart produces features that transfer well to downstream supervised tasks.
The classic confusion: assuming any pair of "similar-looking" augmented examples is automatically a valid positive pair. If the augmentation destroys the property you actually care about — for instance, randomly shuffling a return series destroys momentum, so two shuffled views of a momentum-driven period no longer share the property a downstream model needs — the model learns to be invariant to exactly the signal you wanted it to detect, silently ruining the representation for its intended use.
Practice in interviews
Further reading
- Chen et al., A Simple Framework for Contrastive Learning of Visual Representations (SimCLR)
- Oord, Li & Vinyals, Representation Learning with Contrastive Predictive Coding