From rules to examples
For most of computing history, if you wanted a computer to do something, you wrote down the rules. To filter spam you might write: if an email contains "FREE MONEY", send it to junk. That works until spammers start writing "FR3E M0NEY", and then you are writing rules forever.
Machine learning flips the arrangement. Instead of writing rules, we collect examples, such as thousands of emails labelled spam or not spam, and let an algorithm find rules that fit them.
Traditional programming
Machine learning
A definition often attributed to Arthur Samuel captures the spirit: machine learning gives computers the ability to learn without being explicitly programmed. Samuel wrote the phrase "machine learning" into the title of a 1959 paper about his checkers program, which improved by playing against itself until it outplayed its creator.
The three ingredients
Nearly every machine-learning system, from a spam filter to a large language model, is cooked from the same three ingredients.
- 1
Data: examples to learn from
A dataset of examples, each an input paired with the answer we want. This is called supervised learning because every example comes with its answer.
- 2
A model: a function with knobs
A function that turns an input into a prediction, written (read "y-hat"). The parameters (theta) are its knobs. Different knob settings give different predictions.
- 3
A loss: a score for wrongness
A number that says how bad the predictions are. Averaged over the dataset, it becomes the quantity we want to make small:
Put them together and "learning" gets a precise meaning. It is a search for the knob settings with the lowest loss:
The fourth ingredient, an optimiser that performs that search, is the subject of the next chamber.
A tiny example: coffee and code
Let's make it concrete. We survey eight researchers and record two numbers for each: cups of coffee per day () and lines of code written per day, in tens ().
| Researcher | Coffee | Code |
|---|---|---|
| Ada | 0.5 | 4.4 |
| Grace | 1.5 | 5.2 |
| Alan | 2.0 | 9.6 |
| Katherine | 3.0 | 10.5 |
| Claude | 3.5 | 13.7 |
| Emmy | 4.5 | 13.8 |
| John | 5.5 | 19.2 |
| Hedy | 6.5 | 21.1 |
A natural first model is a straight line:
It has exactly two parameters. The weight sets the slope and the bias sets the intercept. Remember those names: every neuron in every neural network has weights and a bias too.
Measuring wrongness
For a single researcher, the squared error measures how far the prediction is from the truth:
Average it over everyone and you get the mean squared error (MSE), the loss for our line:
Why square the errors? Three good reasons:
- No cancellation. Every term is positive, so a prediction that's too high can't hide one that's too low.
- Big mistakes hurt more. An error of 4 costs 16 times as much as an error of 1.
- It's smooth. Squares are easy to differentiate, which will matter enormously in the next chamber.
Why not simply average the signed errors, , instead of squaring them?
Your turn: fit the line
Time to be the optimiser. Drag the knobs and watch the dashed red residuals, the gaps between prediction and truth. Toggle Squares to see each squared error as an area: the MSE is the average of those areas.
Interactive lab
Fit the line by hand
Mean squared error
14.16
Goal: MSE ≤ 1.47
Your model
ŷ = 1·x + 9
Notice what you just did. You searched a two-dimensional space of settings , and every point in that space had a loss. That surface of losses is called the loss landscape. You explored it with your eyes and hands.
Modern networks have millions or billions of knobs, and nobody can tune those by hand. We need a systematic way to know which way to turn each knob. That is exactly what the gradient gives us, in Chamber 2.
The same calculation takes a few lines of Python with NumPy:
import numpy as np
x = np.array([0.5, 1.5, 2.0, 3.0, 3.5, 4.5, 5.5, 6.5]) # cups of coffee
y = np.array([4.4, 5.2, 9.6, 10.5, 13.7, 13.8, 19.2, 21.1]) # lines of code (tens)
def mse(w, b):
y_hat = w * x + b # the model's predictions
return np.mean((y_hat - y) ** 2) # the loss
print(mse(1.0, 9.0)) # 14.16 (a poor guess)
print(mse(3.0, 2.0)) # 1.29 (much better)
Watch: where this is heading
Neural networks use exactly this recipe with a far more flexible model: data, a function with (many) knobs, a loss, and an optimiser. This beautiful visual introduction shows what those knobs look like inside a network.
The real goal: new data
Here's a subtle point that separates practitioners from scientists. We don't actually care about the loss on our eight researchers. We already know their answers! We care about the ninth researcher, the one we haven't met.
A model that memorises its training data is like a student who memorises the answer key: perfect on the practice exam, lost on the real one. The ability to perform well on unseen data is called generalisation, and measuring it honestly is one of the central themes of this course. You'll learn the tools in Chamber 5 and the discipline in Chamber 7.
Key takeaways
- Machine learning learns rules from examples instead of hand-writing them.
- The recipe: data , a model with parameters , a loss , and an optimiser that minimises it.
- Mean squared error averages squared residuals, so errors can't cancel and big mistakes cost more.
- Learning is optimisation, , but the real goal is performance on new data.
Checkpoint
Prove it to the labyrinth
Answer every question to clear this chamber. First-try answers earn the most XP.
In supervised learning, what does the training data consist of?
A model predicts where the true target is . What is the squared error ?
Which statement best describes what learning means for a model ?
End of the chamber
Clear this chamber
- Questions in this chamber (0/4 solved)Next unsolved
- Bonus: Fit the line (+30 XP)