In P0.2 you ran the learning loop: predict, measure the loss, update. One part of that loop quietly needed something: the right answer. We handed the machine for every . But where does the right answer come from? A person? The data? Nowhere?
The answer splits machine learning into kinds. They differ in what plays the role of the right answer, and so in what data you must collect and what the model can do. There are four main kinds. Almost every method in this course belongs to one of them, and the best systems today use several at once.
The four kinds at a glance
Tap through the four tabs. Each shows a small picture of the data, then what the model learns from, what it outputs, examples, and where this course teaches it. The last two rows tie each kind back to the loop from P0.2.
We now go through each kind slowly.
Supervised learning: learn from labelled examples
Supervised learning is what P0.2 did. Every training example is a pair: an input and its label , the right answer. The model predicts , the loss measures the gap to , and the update shrinks it. The label acts like a teacher who marks each answer, which is where the name comes from.
There are two main shapes of supervised problem, depending on what the label is:
- Classification: the label is a category. Is this email spam or not? Which of the digits 0 to 9 is in this picture? Which of 1,000 object types is in this photo?
- Regression: the label is a number. What will this house sell for? How many minutes will this delivery take? (P0.2’s line-fitting was regression.)
A quick test: ask whether “close” counts. If the answers are categories and a wrong one is simply wrong (“spam” vs “not spam”, “cat” vs “dog”), it is classification. If the answer is a quantity where being off by a little is much better than being off by a lot (a price, a temperature, a year), it is regression.
The catch is the labels. Someone must supply them: a doctor marking scans, a volunteer tagging photos, a customer’s recorded purchase. Labelling is often slow and costly, and the labels can contain mistakes that the model then learns. This cost is the main reason the other kinds exist. Collecting labels well is a topic of its own (P12.2).
The full setup is in P3.A1, regression in P3.A2 and classification in P3.A7.
Unsupervised learning: no answers, find structure
In unsupervised learning the data has no labels, only inputs. There is no right answer to compare with, so the model’s job is different: find structure that is already in the data. The three most common jobs:
- Clustering: split the examples into groups of similar ones. Example: group customers by what they buy (P3.C1).
- Dimensionality reduction: describe each example with fewer numbers, keeping what matters. Example: squeeze 100 measurements per example into 2 numbers you can plot (P3.C3, P3.C4).
- Anomaly detection: learn what usual looks like, and flag what does not fit. Example: a sensor reading that does not resemble any normal reading (P3.C5).
Many unsupervised methods still have a loss. It is just computed from the data alone. In k-means clustering, for instance, the loss is how far each point is from the centre of its group, and the loop moves the centres to shrink it.
How do you know it worked? This is the hard part. With a supervised model you can compare predictions with held-back labels and count mistakes. With clustering, no label says “these 4 groups are the right ones”. Different algorithms, or the same one with different settings, give different groups, and none is “the truth”. People judge by scores computed from the data (how tight and well separated the groups are), by looking at the groups, and above all by asking whether the result is useful for the task. Treat unsupervised results as suggestions to examine.
Self-supervised learning: the data labels itself
Labels are expensive, but raw data is everywhere: the web holds an enormous amount of text and images. Self-supervised learning is a clever way to use it. We make the labels out of the data itself, by hiding a piece and asking the model to predict it:
- Predict the next word of a text. (The answer is the word that really came next.)
- Fill in a masked word. Hide a word in the middle, predict it from both sides. This is how the language model BERT was pretrained (Devlin et al., 2018): it hid about 15% of the tokens (word pieces).
- Fill in hidden image patches. The masked autoencoder (He et al., 2021) hides a large share of an image’s patches (75%, in the paper’s main setting) and rebuilds them.
A worked example: one sentence becomes training pairs
Take the sentence “the cat sat on the mat”: 6 words. Using “predict the next word from the words so far”, it gives 5 training pairs. Nobody labelled anything. We just slid along the text:
| Pair | Input (the context so far) | Right answer (the next word) |
|---|---|---|
| 1 | the | cat |
| 2 | the cat | sat |
| 3 | the cat sat | on |
| 4 | the cat sat on | the |
| 5 | the cat sat on the | mat |
In general, a sentence of words gives pairs (here ). A large text collection therefore gives an enormous number of examples for free. The masked-word version makes pairs another way: from “the cat sat on the mat”, hide “sat” to get the input “the cat ___ on the mat” with the answer “sat”. Real systems split text into pieces called tokens, which are often parts of words, and use much longer contexts; this is a simplified picture (P10.A2).
Now look at the P0.2 loop. The prediction is a guess at the hidden word. The right answer is the hidden word. The loss measures how surprised the model is by it. The update nudges the parameters to be less surprised next time. It is exactly the supervised loop. That is why people say, precisely:
Why it matters. Because there is no labelling cost, self-supervised training can use vastly more data than labelling by hand ever could. This is the first stage of large language models such as GPT (P10.B2) and of many modern vision models (P5.16). To predict the next word well, a model must pick up grammar, facts and patterns in the text, and the numbers it learns turn out to be useful for many other tasks. Some people loosely call this “unsupervised” too, because no human labels exist. The two words are used inconsistently in the wild, so in this course “unsupervised” means “no target at all” and “self-supervised” means “targets made from the data”.
Reinforcement learning: learn from rewards
Now a different situation. A program plays a video game. There is no dataset of “correct moves”. It simply tries things and sees what happens. Reinforcement learning (RL) has a new cast of characters:
- An agent that makes choices (the game-playing program, a robot).
- An environment it acts in (the game, the room).
- Actions the agent takes, and a reward, a number the environment gives back: points scored, +1 for reaching the goal, −1 for falling.
The agent’s goal is to collect as much reward as possible over time. What it learns is a policy: a rule that chooses an action in each situation. Some methods learn the policy directly; others learn how good each action is expected to be and then pick the best one (the Atari player in this lesson worked that way, P9.5). It learns by trial and error.
Two features make RL different from the kinds above:
- No right answer is ever given. The reward says how good the result was, not which action would have been best. Compare “that answer is wrong, it should be cat” (supervised) with “you scored 3” (reinforcement).
- Rewards can be delayed. In a chess game, the reward comes at the end (win or lose). The agent must work out which of its many earlier moves deserve credit or blame. This credit assignment problem is a core difficulty of RL.
The agent’s own actions also decide what data it sees next, so it must balance trying new things against repeating what already works (exploration versus exploitation, P9.2).
Famous examples: programs that learned to play Atari video games from the screen pixels and score (Mnih et al., DeepMind, 2013 and 2015), and AlphaGo, which beat the Go champion Lee Sedol 4 to 1 in 2016. The pipeline of AlphaGo is itself a mix: it first learned from expert human games (supervised), then improved by playing against itself (reinforcement). Its successor AlphaGo Zero (2017) used only self-play reinforcement learning, starting from random play, without human game data. RL is also used for robot control (often first in simulation) and, importantly for this course, to tune chatbots (see below).
RL gets a full Part of its own, P9.1 to P9.9. Its loss is built from rewards: actions that led to more reward are made more likely (P9.6).
The same data, seen four ways
A photo collection shows how the kinds differ. Same pictures, four different jobs:
| Kind | What you do with the photos | The learning signal |
|---|---|---|
| Supervised | Each photo has a tag, “cat” or “dog”. Learn to tag new photos. | The human tag |
| Unsupervised | No tags. Group photos that look alike, and see what groups appear. | None given; a score from the data |
| Self-supervised | No tags. Hide patches of each photo and rebuild them. | The hidden patch |
| Reinforcement | Not a dataset: a robot with a camera tries to pick up a cat toy. | Reward: +1 if it grasps the toy |
The loop of P0.2, kind by kind
| Kind | Where the “right answer” comes from | What the loss measures |
|---|---|---|
| Supervised | A label supplied by people or records | Gap between prediction and label |
| Unsupervised | There is none | How well the structure fits the data (e.g. distance to group centres) |
| Self-supervised | Hidden part of the data | Gap between the prediction and the hidden part |
| Reinforcement | None; only a reward from the environment | Built from rewards: more reward is better (P9.6) |
The loop is the same everywhere: guess, score, adjust. Only the source of the score changes. Note too that the kinds describe the training signal, not the model: the same neural network can be trained in any of these ways.
Real systems mix the kinds
Real systems rarely stick to one kind. Here are the main combinations.
Semi-supervised learning uses a few labelled examples and many unlabelled ones together. Suppose you have 1,000 labelled photos and 99,000 unlabelled ones. One common idea is to train on the labelled ones, let the model label the unlabelled ones it is confident about, and train on those too (“pseudo-labels”). It is useful when labels are scarce but raw data is plentiful. Pretraining on unlabelled data and then fine-tuning on a few labels (P5.10) has a similar spirit.
A chat assistant is built in stages. The best documented example is InstructGPT (Ouyang et al., 2022). OpenAI said that ChatGPT was trained with similar methods (RLHF), with small differences in how the data was collected. The InstructGPT recipe:
- Pretraining (self-supervised). A language model learns to predict the next word on a huge amount of text. This is the base model, GPT-3 in that paper (P10.B2).
- Supervised fine-tuning. People write example answers to prompts, and the model is trained to match them. These are labelled examples (P10.D1).
- Reinforcement learning from human feedback (RLHF). People rank several of the model’s answers. A reward model learns to score answers the way people do, and RL then adjusts the language model to earn higher scores (P10.D2).
So one assistant uses three kinds. Systems made after that paper are built in a similar spirit, but the details of closed models are not public, so we do not guess at them.
Two other ways to slice learning
Generative versus discriminative. This is a separate axis, not a fifth kind. A discriminative model tells things apart: given , predict the label, which is (a spam filter). A generative model learns what the data itself looks like and can create new examples (a language model writing text, an image generator). You can have, for example, a supervised discriminative model (spam filter) and a self-supervised generative one (a language model). P8.1 treats this carefully.
Batch versus online. In batch learning you train on a fixed dataset, then freeze the model and use it (P0.2’s training versus inference). In online learning the model keeps updating as new data arrives, for example a system that adapts to changing user habits. Most deployed chatbots are used with their parameters frozen while you talk to them. Handling data that changes over time is covered in P12.5.
Which kind is it? A game
Eight real situations. Tap the kind of learning that fits. You get an explanation either way. Some real systems fit more than one kind in different stages, but each scenario here describes one stage.
Choosing a kind in practice
A rough guide to which kind fits a problem:
- You have inputs and reliable labels for what you want to predict: supervised.
- You have inputs only and want to explore or summarise: unsupervised.
- You have a lot of raw data and want a general-purpose model: self-supervised pretraining, then adapt it.
- The problem is a sequence of decisions with feedback over time: reinforcement.
Intuition
Think of how a child might learn. Supervised: a parent points at an animal and says “dog”. Unsupervised: the child sorts toys into piles on their own. Self-supervised: a child hears “the cat sat on the …” and guesses the next word, then hears it. Nobody corrected them: the sentence itself was the answer. Reinforcement: a child learns to ride a bike through wobbles and falls, with no one saying which muscle move was wrong.
Where it breaks: a human child mixes all four at once and understands what they are doing. A machine uses one defined signal and a loss. It can still pick up skills nobody named (a next-word predictor learns grammar and facts), but only ones that help it do better on that signal.
Math
The four kinds differ in the objective being minimised. With model parameters and a loss for one example:
- Supervised: , where is a given label.
- Unsupervised: , with no . For example, k-means uses the squared distance from to its nearest centre.
- Self-supervised: the same form as supervised, but is a part of . For next-word prediction, with words : , the average surprise at the real next word (P10.B2).
- Reinforcement: the agent chooses actions with a policy, and the goal is to maximise the expected total reward, . There is no label in this formula (P9.3 adds discounting).
Code
The loop is the same, only the “answer” changes. Self-supervised pairs from raw text need no labels: this code builds them for our sentence.
Runs real Python in your browser. First run downloads about 6 MB (once; your browser keeps it).
Change the sentence and watch the number of pairs. A sentence of words gives next-word pairs and masked-word pairs.
Go deeper Is self-supervised learning a kind of unsupervised learning?
Opinions differ, and the words are used inconsistently. The training data has no human labels, so some call it unsupervised. But the training procedure uses a definite target and a supervised-style loss, so many researchers keep it as its own kind. What matters is that you know what the signal is. You will see both usages in the wild, so check what an author means.
Go deeper Other names you will meet
Contrastive learning (P5.16, P8.13) is a self-supervised idea where the target is “these two views of the same thing should be close together, different things far apart”. Active learning is about choosing which examples a person should label next (P12.2). Transfer learning reuses a model trained on one task as the starting point for another (P5.10). Few-shot and meta-learning are in P11.6. Weak supervision uses noisy, cheap labels in place of careful ones.
Check yourself
Check your understanding
A model is trained on one million photos that volunteers tagged "cat" or "dog", and then tags new photos. Which kind of learning is this?
Each training photo comes with a human-supplied label, and the label is a category. That is supervised learning, and because the label is a category, it is classification.
Which statement about self-supervised learning is the most accurate?
The hidden part of the data (next word, masked word, missing patch) is the answer, and a loss measures the gap, exactly like supervised learning. The training signal is strong. Predicting hidden data is not reinforcement learning, which learns from rewards for actions. It works on text, images and more.
In reinforcement learning, what does the agent get in place of the right action for each situation?
The environment gives rewards, often delayed, and the agent must work out which of its earlier actions deserve the credit. It does not receive per-example labels or correct actions.
In the InstructGPT paper (2022), which description of how the assistant models were built is correct?
The paper starts from GPT-3 (pretrained by predicting next words), then uses supervised fine-tuning on labeler demonstrations, then RLHF using human rankings of outputs. One system, three kinds of learning.
A company groups its customers into 4 clusters with no labels. Which statement is true?
Unsupervised results have no ground truth to compare with, so judging them is hard and partly depends on usefulness. Finishing does not prove correctness. A person choosing settings is normal and does not make it supervised: what matters is that the training data has no labels.
Exercises
- Classification or regression? (a) The number of bikes rented tomorrow. (b) Whether a tumour is benign or malignant. (c) The year a photo was taken, as a number. (d) Which of 10 digits is written.
Show answer
(a) regression, (b) classification, (c) usually regression, because being off by 1 year is much better than being off by 50; you could also frame it as classification into decades, which is a design choice. It is a number on a scale, (d) classification. - How many next-word training pairs does the sentence “I like to read books” give? How many masked-word pairs (hide one word at a time)?
Show answer
5 words, so 4 next-word pairs and 5 masked-word pairs. - A hospital has 200 scans labelled by doctors and 20,000 unlabelled scans. Which kind of learning uses both, and why would you use it?
Show answer
semi-supervised. Labels are costly and scarce, but unlabelled scans are plentiful and add information about what scans look like. - For a robot vacuum that learns a route, what could the reward be, and why is it hard that rewards are delayed?
Show answer
for example +1 for each clean floor tile and a penalty for bumping. A bump may be caused by a turn several moves earlier, so the agent has to work out which earlier action to blame (credit assignment). - In one sentence each, say what plays the role of the “right answer” and of the loss when a language model learns next-word prediction.
Show answer
the right answer is the word that really came next in the text. The loss measures how surprised the model is by that word. - A friend says “my spam filter is generative, so it is unsupervised”. What is wrong with this?
Show answer
generative versus discriminative is a different axis from the kinds of learning. Whether a model is generative or discriminative does not decide whether it is supervised. A classic spam filter, Naive Bayes, is generative, and it is still supervised, because it is trained on emails labelled spam or not spam. - Why is it hard to say whether a clustering of customers is “right”? Give one way to judge it anyway.
Show answer
no ground-truth labels exist to compare with. You can compute a score such as how tight the groups are, and check whether the groups are useful, for example whether they lead to better offers.