TL;DR
Self-supervised learning is training on unlabelled data by manufacturing the labels from the data itself. Hide part of an input, such as a word in a sentence or a patch of an image, and train the model to predict the hidden part from what remains. The answer is always available, so no human has to label anything.
The features the model learns to make those predictions are useful for other tasks. A model that can fill in a missing word has to understand grammar and meaning, and those learned features can then be reused with a small labelled set.
Where you'll see it: in BERT-style language models pretrained on raw text, in image encoders pretrained on unlabelled photos before fine-tuning, and in the sentence embeddings used for search.
Intuition
The answer is hidden inside the input
Labelling is expensive. Someone has to look at each photo and write "dog", or read each paragraph and mark its sentiment. The web, by contrast, holds vast amounts of text and images that nobody has labelled. Self-supervised learning asks a simple question: can the data supply its own answers?
Take a sentence such as "the cat sat on the mat". Cover the word "sat" and ask the model to guess it from "the cat ___ on the mat". The correct answer is the word that was covered, so the training signal is free. Nobody had to write "sat" as a label; it was already in the text.
The name is a little misleading. The learning is supervised in form, because there is a target and a loss, but the targets are created automatically from the raw data. The task used to create them is called a pretext task. The pretext task is only a means. What we keep is the representation the model builds while solving it.
flowchart LR A["Unlabelled data"] --> B["Hide part of the input"] B --> C["Model predicts the hidden part"] A --> D["Hidden part is the target"] C --> E["Loss compares prediction with target"] D --> E E --> F["Learned representation"] F --> G["Fine-tune on a small labelled task"]
Why the features transfer
To predict a masked word well, a model has to know which words fit a context. To tell two crops of the same photo apart from crops of other photos, it has to recognise what the photo is of. These skills are general. The representation learned on the pretext task becomes the starting point for a downstream task such as classification, which is why this idea sits so close to Transfer Learning.
The plot below shows why the training loss pushes the model in the right direction. If the model assigns probability to the correct hidden word, the loss is . A confident correct guess costs almost nothing, and a confident wrong one costs a lot.
- loss = -ln p
The math
The general recipe
Let be an unlabelled example. A transformation produces a corrupted or partial view , and the model must recover a target derived from :
The loss is ordinary supervised loss. What changes is that the pairs come from the data, not from a human. Everything that makes supervised training work, including Gradient Descent and a Loss Function, applies unchanged.
Masked prediction
For a sequence , choose a set of positions to hide. The model sees the rest and predicts each hidden token. The loss is
where denotes the visible tokens. Each term is the Cross-Entropy Loss of the correct token under the model's Softmax output. Masked language modelling, used by BERT, follows this formula. Masked autoencoders for images apply the same idea to patches.
Contrastive learning
A different family asks the model to make two views of the same example similar and views of different examples dissimilar. Given an anchor embedding , one positive (another view of the same example), and negatives , the InfoNCE loss is
where is a similarity score, often the cosine similarity, and is a temperature that controls how sharply the scores are compared. The fraction is a Softmax over the candidates, so the loss is small when the positive scores much higher than every negative. This is the form used in the contrastive methods of Contrastive Learning.
Why more negatives help
The denominator contains one term per negative. With more negatives, the model has to separate the positive from a larger crowd, so the learned features must carry finer distinctions. This is one reason contrastive methods use large batches.
Worked examples
Sentence: "the cat sat on the mat". Hide "sat" and let the model output probabilities over candidate words for that position. Suppose it gives "sat" probability , "slept" and "ran" .
- The correct token is "sat", so the loss is .
- Compute , so the loss is .
- Suppose the model had given "sat" only . The loss would be .
- Averaged over the masked positions in a longer text, these numbers are the quantity training pushes down.
- A model that gives "slept" more weight than "sat" is penalised, because "sat" is what the text actually says.
Interpretation: the loss is zero only when the model is certain of the true word. Nothing in the calculation needed a human label, only the original sentence.
An anchor embedding is compared with one positive and two negatives. Use and the similarity scores , and .
- Exponentiate each score: , and .
- Sum the three terms: .
- The probability that the positive is picked out is .
- The loss is .
- Now suppose the first negative had similarity as well. The positive's share drops to , and the loss rises to about .
Interpretation: the loss is small when the positive stands out from the negatives. A negative that looks almost identical to the anchor is the hardest case, and it is the one that most shapes the learned features.
Tradeoffs and failure modes
Self-supervised learning pays off when unlabelled data is plentiful and labelled data is scarce. It is a poor fit when the labelled set is already large and the target is narrow, since a supervised model trained directly may do as well with less compute.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Pretext task is too easy | The model solves it with shortcuts, such as low-level texture, and learns little | Harder masking, stronger augmentation |
| Pretext task is unrelated to the target | Features do not transfer | Choose a pretext task that needs the target's skills |
| Contrastive batch is small | Too few negatives, weak features | Larger batches or a memory bank |
| False negatives | Two examples of the same class are pushed apart | Sample negatives more carefully or use a supervised signal |
| Pretraining data differs from deployment | Features fit the wrong domain | Pretrain on closer data or [[transfer-learning |
Two failure modes are worth knowing. The first is collapse: a model can make all representations identical, which gives zero loss on some objectives. Contrastive methods use negatives, and some newer methods use asymmetric designs and stop-gradient tricks, to prevent this. The second is evaluating only on the pretext task. A model that predicts masked words well may still be a poor classifier, so the honest test is always the downstream task. Augmentations, a form of Data Augmentation, are central to creating the two views that contrastive methods need, and a bad choice of augmentation changes what the model learns.
History
The idea of predicting parts of a signal from other parts is old. Early work on word embeddings used this principle implicitly. Word2vec, published by Mikolov and colleagues in 2013, trained a model to predict a word from its neighbours, and the vectors it learned captured meaning well enough to become standard.
Pretraining on unlabelled text became mainstream with BERT in 2018, from Devlin and colleagues. BERT used masked language modelling, the task described above, on large text corpora and then fine-tuned the model on many labelled tasks. It set new results across a wide range of language benchmarks and made the pretrain then fine-tune pattern the default for text. Denoising autoencoders, which reconstruct an input from a corrupted copy, predate it and share the same basic move.
In vision, the same pattern took longer. Pretext tasks such as predicting image rotations, solving jigsaw puzzles from shuffled patches, and colouring grayscale photos were tried in the late 2010s, but they lagged behind supervised pretraining on ImageNet. Contrastive methods, such as SimCLR in 2020, and masked autoencoders in 2021 closed much of that gap. Self-supervision is now common in vision, speech and text, because it lets models learn from data that no one has labelled.
Check yourself
QuestionRecall: what is a pretext task, and what is the final goal of training with one?Show answer
A pretext task is an automatically generated prediction problem, such as guessing a masked word, whose labels come from the data itself. The goal is not to solve the pretext task for its own sake but to learn a representation that is useful for downstream tasks, which are then solved with a small amount of labelled data.
QuestionComputation: a masked model gives the correct token probability . What is the loss for that position, to three decimal places?Show answer
The loss is .
QuestionWhy: why do contrastive methods need negatives, and what would go wrong if they used only positive pairs?Show answer
With only positive pairs, the easiest way to make the positives similar is to make every representation the same, which gives a perfect score and teaches nothing. Negatives push different examples apart and prevent this collapse, so the features must keep information that distinguishes one example from another.
QuestionApplied: a team has one million unlabelled product photos and 2,000 labelled ones for a defect classifier. Should they start from self-supervised pretraining, and what should they check?Show answer
Probably yes: the unlabelled photos are plentiful, and 2,000 labels are few for training a deep model from scratch. They should pretrain on photos that resemble the factory images, fine-tune on the 2,000 labelled examples, and compare against training only on the labelled set. They should hold out a test set of labelled defects, since accuracy on the pretext task says little about defect detection.