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

Subtitles:

Tap a line to see its words.

  1. A model guesses the price of a flat from its size x: the guess is the parameter theta times x.
  2. It has seen two flats. Size 1 sold for 7, and size 3 sold for 11.
  3. The loss is half the average squared mistake.
  4. With theta equal to 0 the model always guesses 0, and the loss is 42.5.
  5. Working out the slope of the loss with respect to theta gives 5 theta minus 20.
  6. At theta equal to 0 the gradient is minus 20: the loss falls steeply as theta grows.
  7. One step of size 0.1 against the gradient moves theta to 2, and the loss drops to 12.5.
  8. The gradient vanishes where 5 theta equals 20, so the best parameter is 4.
  9. 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.

2 Same letters, different meanings

Pair each line with what it says.

Tap a line, then its meaning.

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.

4 A squashed score

The score is ln 3. What number does the sigmoid give?

5 Scores to chances

Three scores go in, three probabilities come out. Which list is it?

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?

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.

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.

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.

10 Squashed numbers

Each line is a probability in disguise. Pair it with its value.

Tap a line, then its meaning.

11 Who is who

Six letters of the alphabet of learning. Pair each with its role.

Tap a line, then its meaning.

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.

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.

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.

15 Two steps down

The loss is θ². Start at 8 and take two gradient steps, each a quarter of the gradient. Where are you?

16 The biggest share

Three scores, the third the largest. What probability does the third class get?

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?

18 How many numbers?

An example is described by twelve measurements. Where does it live?

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.

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.