The recipe
Every machine-learning system, from a spam filter to a chatbot, is made of the same four ingredients. Papers describe them in the same order, so learning to spot them is the first step to reading papers.
- Data: examples to learn from, such as emails marked spam or not, or trillions of tokens of text.
- Model: a function with adjustable numbers (the parameters) that turns an input into an output. Papers call its design the architecture.
- Loss: one number that says how wrong the model’s outputs are on the data. Papers call it the objective.
- Optimizer: the method that adjusts the parameters to lower the loss, usually a form of gradient descent.
Training is running the optimizer over the data until the loss stops improving. Then comes the real test: does the model work on examples it has never seen? That ability is called generalisation, and it is the whole point. A model that only works on its training examples is useless.
Your first model: a straight line
Here is a small, real-looking dataset: eight students, how many hours each studied, and their test score. The simplest model you could try is a straight line.
score = w × hours + bTwo parameters. w (the weight) is how many points each hour of study adds. b (the bias) is the score with no study at all.
loss = average of (prediction − real score)²The mean squared error from the last lesson.
The dots are the students; the thin lines show each prediction’s error. Try to fit the line yourself with the two sliders and watch the error number. Then press the button and let gradient descent do it.
This interactive part needs JavaScript.
Gradient descent finds the best line in a few steps: w ≈ 4.5 and b ≈ 47, with the average squared error falling from 155 to under 1. Now the model can predict: a student who studies 10 hours should score about 4.5 × 10 + 47 ≈ 92, even though no one in the data studied that long.
Quick check
Too simple, too clever, just right
A model can fail to generalise in two opposite ways.
Underfitting
- The model is too simple for the pattern, like a straight line through data that curves.
- It is wrong on the training data and on new data.
- Fix: a more flexible model, or better inputs.
Overfitting
- The model is so flexible that it memorises the training examples, noise and all.
- Almost perfect on training data, poor on new data.
- Fix: more data, a simpler model, or stop training earlier.
To catch overfitting, data is split in two (often three) before training starts. The training set is what the model learns from. The test set is locked away and used only at the end, to measure how well the model works on things it has never seen. A third validation set is used along the way to choose settings, so the test set stays untouched.
Three ways to learn
Machine learning comes in three main flavours, depending on where the “right answers” come from. A modern language model uses all three, one after another.
| The model learns from | Example | In a language model | |
|---|---|---|---|
| Supervised learning | Examples labelled by people with the right answer | Emails marked spam or not spam | Fine-tuning on written examples of good assistant replies |
| Self-supervised learning | Data that contains its own answers: hide part of it and predict it | Predict the next word of a sentence | Pretraining: predict every next token of trillions of tokens of text |
| Reinforcement learning | Trying things and getting a reward or a penalty | A program that learns Go by playing millions of games | Rewarding correct maths answers or passing code tests (how DeepSeek-R1 learned to reason) |
The middle row is the breakthrough behind large language models. Labelling data by hand is slow and expensive, but text labels itself: every sentence on the internet is an example of which word comes next. That is why models can learn from trillions of tokens without anyone marking a single answer.
Which kind of learning is each one?
-
Learning to recognise cats from 10,000 photos each tagged “cat” or “not cat”
Answer: Supervised
-
Predicting the next token of web pages
Answer: Self-supervised
-
A robot that gets a point each time it picks up a cup without dropping it
Answer: Reinforcement
-
Hiding random words in sentences and guessing them
Answer: Self-supervised
-
Rewarding a model when its code passes the tests
Answer: Reinforcement
-
Training on question-and-answer pairs written by experts
Answer: Supervised
Numbers or categories
Models also differ in what they output. Regression predicts a number, like a test score or tomorrow’s temperature. Classification picks a category, like spam or not spam, or which of 1,000 kinds of object is in a photo. A classifier usually outputs a probability for every category through a softmax, exactly as you saw in the probability lesson.
Seen this way, a language model is a classifier. The categories are the tokens of its vocabulary, 128,256 of them for Llama 3.1 8B, and each step picks one. It just runs again and again, feeding each choice back in, to write a whole reply.
Remember
- Every ML system has four ingredients: data, a model (architecture), a loss (objective) and an optimizer.
- Fitting a line (score = w × hours + b) by gradient descent is the whole recipe in miniature; language models run it with billions of parameters.
- The goal is generalisation. Underfitting is too simple; overfitting is memorising. A locked test set tells them apart.
- Supervised (labelled answers), self-supervised (data predicts itself) and reinforcement learning (rewards). Pretraining is self-supervised.
- A language model is a classifier over its vocabulary, run once per token.
Words to know
- Parameter
- An adjustable number in a model. Weights and biases are parameters.
- Architecture
- The design of a model: what kind of function it is, before its numbers are learned.
- Generalisation
- Working well on new examples, not just the ones learned from.
- Overfitting
- Memorising the training examples instead of learning the pattern.
- Training, validation and test sets
- Data to learn from, data to choose settings with, and locked data to measure the final result.
- Contamination
- Test questions leaking into training data, which makes scores look better than the model is.
- Supervised learning
- Learning from examples labelled with the right answer.
- Self-supervised learning
- Learning from data that supplies its own answers, like predicting the next token.
- Reinforcement learning
- Learning by trying actions and receiving rewards or penalties.
- Regression and classification
- Predicting a number, or picking a category.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
The Bitter Lesson (R. Sutton)
One page that explains why general learning plus more computing keeps winning in AI.
-
Deep learning (LeCun, Bengio, Hinton)
A readable overview by three of the field’s founders, written just as deep learning took over.
-
Dive into Deep Learning (free book)
A free, hands-on textbook with runnable code, used at hundreds of universities. Also available in Chinese.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonNeural networks: small functions, stacked deep