Speak Maths · Master’s road
Machine learning: a model that learns by walking downhill
A model is a rule with knobs. Learning means turning the knobs until the mistakes are small, and the gradient tells you which way to turn. Here is the whole alphabet, and a model guessing prices.
Draft: written by an AI helper and not yet checked by a teacher.
The alphabet
Read each sign aloud, the way it is written under it.
- Say: bold x is in R nMeans: one example, written as a list of n real numbers (its features)
- Say: y hat equals f theta of xMeans: the model’s guess for x, using its parameters theta; the hat means a guess, not the truth
- Say: L of theta equals the expected value of y hat minus y, all squaredMeans: the loss: how wrong the guesses are on average, for the parameters theta
- Say: theta star is the arg min over theta of L of thetaMeans: theta star is the value of theta that makes the loss smallest, not the smallest loss itself
- Say: grad theta of L, or del L with respect to thetaMeans: the arrow of partial derivatives of the loss with respect to the parameters: the steepest way uphill
- Say: sigma of z equals one over one plus e to the minus zMeans: squashes any score z into a number between 0 and 1, so it can be read as a probability
- Say: softmax of z, i-th entry, equals e to the z i over the sum over j of e to the z jMeans: turns a list of scores into a list of probabilities that add up to 1
The story: A model learns to guess prices
Tap a line to see its words.
- A model guesses the price of a flat from its size x: the guess is the parameter theta times x.
- It has seen two flats. Size 1 sold for 7, and size 3 sold for 11.
- The loss is half the average squared mistake.
- With theta equal to 0 the model always guesses 0, and the loss is 42.5.
- Working out the slope of the loss with respect to theta gives 5 theta minus 20.
- At theta equal to 0 the gradient is minus 20: the loss falls steeply as theta grows.
- One step of size 0.1 against the gradient moves theta to 2, and the loss drops to 12.5.
- The gradient vanishes where 5 theta equals 20, so the best parameter is 4.
- A new flat of size 2 now gets the guess 8.
In words this story is 649 letters long; in symbols, 130.
Read it
What does it say?
Write it
Which notation says this?
Move theta a small step eta against the gradient of the loss.
The sigmoid of z is one over one plus e to the minus z.
The best parameters are the ones that make the loss smallest.
One example is a list of n real numbers.
The model’s guess for x, using the parameters θ.
The gradient of the loss with respect to θ.
Softmax of the two scores 0 and 0 gives a half and a half.
The sigmoid of a very negative score is close to 0.
Gradient descent with step 0.01: the new θ is the old θ minus 0.01 times the gradient.
At the best parameters the gradient of the loss is zero.
The best θ is the one with the smallest loss, and it is 4.
The sigmoid of 2 is one over one plus e to the minus 2.
Build it
Tap the tiles in order. One tile does not belong.
The loss is the expected value of the squared difference between the guess and the truth.
Softmax of z, entry i, is e to the z i divided by the sum of all the e to the z j.
y hat equals f theta of x.
θ star is the arg min over θ of L of θ.
Sigma of z equals one over one plus e to the minus z.
The next θ is the current θ minus η times the gradient of the loss.
Challenges
Creative puzzles that use every sign of this chapter. Take your time; there is no clock.
1 Three examples, three models
The model f_θ(x) = θx is tried with θ = 1, 2 and 3 on three examples (x, y). Judge each sentence: true or false?
| ✓ | |||
| ✓ | ✓ | ||
A tick means the model in that row guesses that example exactly: ŷ = y. L is the average of the squared errors over the three examples.
Hitting the most examples is not the same as the smallest average error: θ = 2 is exact twice but misses the third badly, and θ = 1 does better on average.
2 Same letters, different meanings
Pair each line with what it says.
Tap a line, then its meaning.
arg min gives the place where the loss is smallest, min gives the smallest value; and the gradient points uphill, so its negative points downhill.
3 One step downhill
Gradient descent: the new parameters are the old ones minus a small step η times the gradient. One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
The gradient points uphill, so to go downhill you subtract it.
4 A squashed score
The score is ln 3. What number does the sigmoid give?
e^{−ln 3} = ⅓, so σ = 1 ∕ (1 + ⅓) = ¾. The bigger the score, the nearer to 1.
5 Scores to chances
Three scores go in, three probabilities come out. Which list is it?
e^{ln 1}, e^{ln 2}, e^{ln 3} are 1, 2, 3. Their sum is 6, so the shares are 1/6, 2/6, 3/6, and they add up to 1.
6 The first step
The loss is L(θ) = (θ − 3)². Start at θ₀ = 5 and take one step: subtract one quarter of the gradient. Where do you land?
∇L = 2(θ − 3) = 4 at θ = 5, so θ₁ = 5 − 4/4 = 4: one step closer to the minimum at 3.
7 Say how wrong
The loss is the average of the squared gaps between the guess ŷ and the truth y. Write it as a function of θ. Then compare.
One way:
Squaring makes every gap count as positive, and the average (the expected value) turns many gaps into one number.
8 Chances that add up
Softmax turns scores z into chances. Write that every chance is positive and that all the chances add up to 1. Then compare.
One way:
Each is positive because e^{z} > 0, and they add up to 1 because each is divided by the sum of all of them.
9 Four pairs of scores
Four score lists for two classes. A tick shows a fact about the probabilities they give. Judge each sentence.
| ✓ | ✓ | |||
| ✓ | ||||
| ✓ | ✓ | |||
| ✓ | ✓ |
A tick means the scores in that row have the property in that column; z₁ is the first score.
Softmax only sees the differences between scores: (0, 0) and (1, 1) both give a half each, and (ln 2, 0) and (3, 3 − ln 2) both give two thirds and one third. The sigmoid of one score is a half only at 0. A softmax probability is never exactly 1.
10 Squashed numbers
Each line is a probability in disguise. Pair it with its value.
Tap a line, then its meaning.
σ(ln 3) = 1/(1 + ⅓) = ¾ and σ(−ln 3) = 1/(1 + 3) = ¼, so σ(−z) = 1 − σ(z). The exponentials of 0, ln 2, ln 5 are 1, 2, 5, and the third share is 5/8.
11 Who is who
Six letters of the alphabet of learning. Pair each with its role.
Tap a line, then its meaning.
The hat separates the guess from the truth; θ are the knobs the loss depends on; η is the small step; x is the input.
12 A hat for a guess
The model’s output is a guess, not the truth, so it wears a hat. One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
y is the truth we are given; ŷ = f_θ(x) is what the model says. Mixing them up hides the whole point of the loss.
13 Mistakes that cancel
The loss is the average of the squared gaps between guess and truth. One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
Without the square, a guess 3 too high and a guess 3 too low would average to no mistake at all. Squaring makes every gap count.
14 The best knobs
θ star is the setting of the knobs where the loss is smallest. One tile is wrong. Find it, then fix it.
Tap the tile that is wrong.
arg max would pick the worst setting; min alone would give the smallest loss, a number, not the θ that reaches it.
15 Two steps down
The loss is θ². Start at 8 and take two gradient steps, each a quarter of the gradient. Where are you?
∇L = 2θ. From 8: 8 − ¼·16 = 4. From 4: 4 − ¼·8 = 2. Each step halves θ, so it never overshoots 0.
16 The biggest share
Three scores, the third the largest. What probability does the third class get?
The exponentials are 2, 2 and 4, which add up to 8; the third share is 4/8 = ½. Dividing by the sum of the scores themselves is the trap.
17 One flat, one mistake
The model guesses a price as θ times the size. A flat of size 3 sold for 12 and θ is 5. What is the squared mistake?
The guess is 5 · 3 = 15, the gap is 15 − 12 = 3, and squared it is 9.
18 How many numbers?
An example is described by twelve measurements. Where does it live?
A list of 12 real numbers is a point of R¹²; the exponent counts the numbers, not their size.
19 The rule of learning
Learning is one rule repeated: move the knobs a small step against the gradient of the loss. Write the rule. Then compare.
One way:
The gradient points uphill, so the minus sign takes you downhill; η keeps the step small so you do not leap over the valley.
Chapter finished. The symbols are yours.
Tell your own
Write a two-line story of something being learned by trial and error — a guess, a loss, a step — in notation only: a model, a mistake, a gradient, a better theta.
Kept on this device only.