Contents
09Self-supervised learning: labels made from the data
9.1The idea: predict a hidden part from the rest
Self-supervised learning removes the human from the labeling loop entirely. We invent a pretext task whose answers we can compute automatically: hide part of an input and predict it from the rest; predict the future of a sequence from its past; recognize that two distorted copies of a photo show the same scene. The trick, in the words of Murphy (2022, §1.3.3), is to “create proxy supervised tasks from unlabeled data”: both the inputs and the labels already come from the data itself. We usually do not care about the pretext task itself. What we want is the representation the model learns while solving it, which can then be reused for the downstream task we actually care about, often with very few labels. The video’s analogy is the student who makes their own fill-in-the-blank exercises: covering words in the textbook costs nothing, yet guessing them well requires understanding the text.
9.2Predicting words: word2vec
An early, influential example is word2vec (Mikolov et al., 2013): for each word in a large corpus, predict the words within a few positions of it. Every position provides training pairs for free, and to predict neighbours well the model must give similar vectors to words used in similar contexts; famously, “king” - “man” + “woman” lands near “queen”.
9.3Masked language modeling: BERT
BERT (Devlin et al., 2019) applies the fill-in-the-blank idea to whole sentences. In each training sentence, 15% of the tokens are chosen at random; the model must predict the original tokens at those positions M from all the other tokens x_{\setminus M}:
This is an ordinary multiclass cross-entropy, with the vocabulary as the set of classes and the hidden word as the label. Because the [MASK] symbol never appears in later tasks, a chosen token is replaced by [MASK] only 80% of the time, by a random token 10% of the time and left unchanged 10% of the time. Masking is what lets the model use context on both sides of a word: a model allowed to see the word it must predict could simply copy it. BERT was pretrained on BooksCorpus (800 million words) and English Wikipedia (2,500 million words), then fine-tuned with small labeled sets for each task (Devlin et al., 2019).
9.4Next-token prediction: GPT
GPT-style models (Radford et al., 2018) predict each token from the tokens before it. The chain rule of probability writes the probability of a whole sequence as a product of such predictions,
so minimizing the next-token loss
is exactly maximum likelihood for the text. Every position is a training example: the ten-word sentence of the video, “the cat sat on the mat because it was tired”, gives nine prediction targets (ten if a start-of-text symbol is prepended), and a corpus of billions of words gives billions of targets for free. GPT-1 (Radford et al., 2018) trained a 12-layer Transformer this way on over 7,000 unpublished books and then fine-tuned it on small labeled tasks, raising the best score on the RACE reading-comprehension test by 5.7 points; GPT-3 (Brown et al., 2020) scaled the same objective to 175 billion parameters and 300 billion tokens.
9.5Contrastive learning: SimCLR and InfoNCE
For images, predicting hidden pixels exactly is hard and often beside the point. Contrastive methods instead learn to tell which views belong together. SimCLR (Chen et al., 2020) takes a batch of N images and makes two random augmentations of each (random crop, colour distortion, blur), giving 2N views. An encoder network f (a ResNet) maps each view to a representation h, and a small projection head g maps h to a vector z. The two views of the same image form a positive pair; the other 2(N-1) views in the batch serve as negatives. With cosine similarity \mathrm{sim}(z_i,z_j)=z_i^\top z_j/(\|z_i\|\|z_j\|) and a temperature \tau>0, the loss for a positive pair (i,j) is
Reading the loss. The fraction is a softmax over the 2N-1 candidates k\ne i, so (17) is just the cross-entropy of a (2N-1)-way classification problem: “which of these views is my twin?” The loss pulls the positive pair together and pushes the negatives away. It was introduced as InfoNCE by van den Oord et al. (2018), who showed that its optimal score is proportional to the density ratio p(x\mid c)/p(x) and that minimizing it maximizes a lower bound on the mutual information between the two views, I\ge\log N-\mathcal{L}_N, where N is the number of candidates. Its gradient weights each negative by its softmax probability, so the negatives that look most like the anchor, the hard negatives, get the largest push (Chen et al., 2020). The temperature controls how sharply: a small \tau concentrates the loss on the hardest negatives.
Worked example 6. An anchor has cosine similarity 0.8 with its positive and 0.3, 0.1 and -0.2 with three negatives (N=4 candidates). With \tau=0.5 the scores are 1.6,\,0.6,\,0.2,\,-0.4, the softmax probabilities are 0.572,\,0.210,\,0.141,\,0.077, and the loss is -\log0.572=0.560. With \tau=1 the same similarities give probability 0.405 for the positive and loss 0.905; with \tau=0.1, probability 0.992 and loss 0.008. The mutual-information bound at \tau=0.5 is \log4-0.560=0.827 nats.
The results were striking. A linear classifier trained on frozen SimCLR features of a ResNet-50 reached 69.3\% top-1 accuracy on ImageNet, and a network four times wider reached 76.5\%, matching a ResNet-50 trained with all the labels. Fine-tuned with only 1\% of the labels, about 13 images per class, SimCLR reached 75.5\% top-5 accuracy, against 48.4\% for the same network trained on those labels alone (Chen et al., 2020).
9.6Pretrain, then fine-tune
The usual way to use self-supervised learning has two stages. First, pretrain a large model with a self-supervised loss on a huge unlabeled dataset. Then adapt it to the task at hand with a small labeled set, either by training only a linear classifier on top of the frozen representation (the linear evaluation or linear probe, which measures how good the representation is) or by fine-tuning all the weights with a supervised loss. The pretrained model already knows what is generally useful (edges, shapes and object parts; grammar and word meaning), so the supervised stage needs far fewer labels. Conversational assistants follow the same pattern: next-token pretraining on large amounts of text, then fine-tuning with examples and preferences provided by people.
9.7Is self-supervised learning supervised or unsupervised?
Both, depending on which aspect you look at, and an interviewer who asks usually wants you to say so. By its data it is unsupervised: there are no human labels. By its training it is supervised: there is an input, a target and a supervised loss such as cross-entropy, and the methods and code are those of supervised learning. A useful one-line definition: self-supervised learning is supervised learning in which the labels are computed from the inputs.