Intro to AI : Part 4

Loss and Backpropagation

Mehmet Kerem Turkcan
Associate Research Scientist
Center for Smart Streetscapes, Columbia University
New York, USA
keremturkcan.com;  mkt2126@columbia.edu

Move with the arrow keys, a presentation clicker, or the buttons at the bottom left; the gear at the bottom right opens the slide settings.

Where Parts 1 to 3 left us

  1. The game: the model assigns a probability to every token that could come next.
  2. Weights: predictions come from arithmetic steered by millions of adjustable numbers.
  3. The promise: training "measures the mistake and nudges every weight to shrink it"; Parts 2 and 3 used the recipe without opening it.
  4. Distance: Part 3 placed number tokens on a line, so a mistake has a magnitude.

Today we open the recipe: what a mistake costs, and how every weight receives its nudge.

How wrong is 66?

You: What is 34 plus 33?
The model: 66

Part 3 gave mistakes a magnitude: the model missed the right answer by 1.

Question for the class: should a mistake of 2 cost twice as much as a mistake of 1, or four times as much; and should a mistake of 2 in the other direction cost the same?

Both halves of that question have real answers, and both are choices we make; the function that scores a mistake comes first.

For the classroom: take a vote on "twice or four times" before advancing; the vote gets settled two slides from here.

The loss function

The recorded correct answer is called the ground truth. A loss function is a function that reads the ground truth and the prediction and returns one number, the loss: zero for a perfect answer, larger for a worse one. Training has exactly one goal: make the average loss over the training data small.

ground truthpredictionmistakeloss = magnitude of the mistake
6766−11
676700
676922
67801313

This first loss scores a mistake by its magnitude alone; the next slide meets its main rival.

Two shapes for one idea

Two natural choices score a mistake of magnitude m: the absolute error, m itself, and the squared error, m times m. The plot shows the price of each vote: under the square, a mistake of 2 costs 4, and a mistake of 5 costs 25, so large mistakes dominate the training.

−5 −2 0 2 5 0 10 25 mistake loss squared absolute 4 2 25 5

The square has a second gift, visible at the bottom of the plot: its steepness shrinks as the mistake shrinks, so a learner slows down as it closes in; the V stays equally steep to the last instant and invites overshooting. The walk downhill uses this shrinking slope.

The case for symmetry

Both shapes are symmetric: a mistake of 2 over and a mistake of 2 under cost the same. Symmetry is a statement about the world, chosen on purpose: for 34 plus 33, answering 65 and answering 69 are equally wrong, so the loss should not care about direction.

A symmetric loss tells the learner: I have no preference between overshooting and undershooting; land close, from either side.

Question for the class: name a prediction where being over is worse than being under.

The case against symmetry

When the two directions cost differently in the world, the loss should tilt the same way. A cafeteria orders lunches before knowing the demand: a missing meal costs 1 (a student unfed), a wasted meal costs 0.2 (food thrown away).

30 missing 0 30 wasted loss missing meals: slope 1 wasted meals: slope 0.2 exact
An illustrative scenario: suppose demand will be 100 or 120, each equally likely. Stocking 100 risks 20 missing meals, an expected cost of 0.5 \cdot 20 \cdot 1 = 10; stocking 120 risks 20 wasted meals, an expected cost of 0.5 \cdot 20 \cdot 0.2 = 2. The tilt moved the best answer up.

The spam filter from Part 3 faces the same tilt: deleting a real message costs far more than passing one spam, so its training weights the two mistakes differently. Choosing a loss is deciding which mistakes you can live with.

For the classroom: have students set the two costs for a weather service predicting road ice (salting for nothing, or leaving ice untreated) and vote on the tilt direction.

1801: the lost asteroid

The squared error is two centuries old, and an asteroid made it famous. Ceres, discovered by Giuseppe Piazzi on the first night of 1801, vanished into the Sun's glare after only weeks of observations. Carl Friedrich Gauss fitted an orbit to the scraps by making the squared errors small, and in December of that year Ceres reappeared close to his prediction.

Adrien Marie Legendre published the method first, in 1805, naming it least squares; Gauss then claimed he had used it since 1795, and the two argued about who initially discovered the idea for years.

The choice of the square was already a policy, in the sense that an orbit should treat overshooting and undershooting a star's position as equally bad.

Walking downhill

Give the loss a landscape. A toy model predicts with one weight: \text{prediction} = w \cdot 10, ground truth 67, squared loss. Plotting loss against w draws a valley with its bottom at w = 6.7. Training is a walk: stand at w, measure the slope under your feet, and step downhill, the step proportional to the slope. In physics words: your velocity is set by the steepness; fast high on the wall, slower as the floor flattens, settled where the slope is zero. This walk is called gradient descent; gradient is the field's word for the slope.

start: w = 3 w = 6.7 the weight w loss
The hops shrink because the slope shrinks: the squared loss keeping its promise.
stepwpredlossslopenew w
13.0030.01369−7403.74
23.7437.4876−5924.33
34.3343.3561−4744.81
44.8148.1359−3795.18
… approaching w = 6.7, where the slope is 0
\text{new } w = w - r \cdot \text{slope}

Step size r = 0.001. The slope needs no calculus: nudge w a little, watch the loss, and divide the change by the nudge; calculus gives the exact value, the nudge gives a measurement anyone can take.

The step size

The step size, called the learning rate, decides the walk's character. For this valley the arithmetic is exact: each step multiplies the distance to the bottom by (1 - 200r). Three settings tell the whole story.

r = 0.001: glide 3.00 → 3.74 → 4.33 → 4.81 r = 0.009: zigzag, closing 3.00 → 9.66 → 4.33 → 8.59 r = 0.011: zigzag, escaping 3.00 → 11.14 → 1.37 → 13.09
Question for the class: the model answers worse after more training. Which of the three walkers is it, and which knob would you turn?

The rolling ball

Plain descent resets its speed at every step: velocity from slope, with no "memory". Give the walker mass instead, and the physics changes by one line: the slope now sets the acceleration, and a little friction drains the speed.

The ball builds velocity down the long wall, coasts across dips that strand a walker, and settles because friction wins once the floor levels; practitioners call this momentum, in use since the 1980s.

physicstraining
positionthe weight's value
heightthe loss
steepnessthe slope of the loss
velocitythe running step, remembered
accelerationthe slope's push on that step
frictiondamping, so the ball settles
the walker stops in the dip the ball rolls through
Each advance runs one racer on the same terrain: first the walker, then the ball. Momentum can also overshoot; the friction term is what tames it.

Millions of directions

With two weights the valley becomes a bowl, and the slope becomes an arrow pointing uphill in the weight plane; descent steps against the arrow. With millions of weights nothing changes except the bookkeeping: every step needs the slope of the loss with respect to every single weight.

weight 1 weight 2 the slope points uphill lowest loss
A bowl over two weights: the floor directions are the weights, and height is the loss. The blue rings mark equal loss; the amber walk steps against the uphill arrow, and the hops shrink as the floor flattens.
Question for the class: the nudge test gives one slope per forward pass. A model with a billion weights needs how many forward passes for one training step?

Ratios in a chain

A two stage toy: input 2, times w1 = 3 gives h = 6, times w2 = 4 gives the prediction 24; ground truth 20, so the mistake is 4 and the squared loss is 16. On a bicycle, if the pedals turn the wheel at ratio 2 and the wheel moves the road at ratio 4, the pedals move the road at ratio 8: ratios along a chain multiply. The same chain runs through the toy: w1 moves h at ratio 2 (the input), h moves the prediction at ratio 4 (that is w2), and the prediction moves the loss at ratio 8 (twice the mistake); so w1 moves the loss at 2 \cdot 4 \cdot 8 = 64.

x = 2 h = 6 prediction 24 mistake 4 loss 16 × w1 = 3 × w2 = 4 truth 20 ratio 8 8 × 4 = 32 32 × 2 = 64 the backward pass: the product grows as it walks left
questionnudge test (nudge 0.01)multiplied ratios
prediction per w10.08 / 0.01 = 82 × 4 = 8
loss per w2about 48.46 × 8 = 48
loss per w1about 64.62 × 4 × 8 = 64

The nudge test lands near the product, and closer as the nudge shrinks; calculus gives the product exactly.

One backward sweep

In a deep network, every weight's ratio product shares the same tail: the ratios from the loss back to that weight's layer. So compute once, from the loss backward, layer by layer, reusing the shared products as you go; when the sweep reaches the input, every weight holds its slope. The procedure is called backpropagation, short for backward propagation of errors.

input layer 1 layer 2 layer 3 loss × ratio × ratio, reused × ratio, reused again one backward sweep prices every weight on the way past

The count is the whole point: the nudge test needs one forward pass per weight, a billion passes for a billion weights; backpropagation delivers every slope for about the cost of two passes, one forward and one backward.

The 1986 paper by Rumelhart, Hinton, and Williams made the method standard; the idea had appeared earlier, in Paul Werbos's 1974 thesis.

The debt from Part 2

Part 2 promised: the correction signal weakens as it travels backward through many steps. Now the arithmetic is yours: the backward sweep multiplies one local ratio per step, so long products of small ratios shrink toward nothing while long products of large ratios explode.

An appealing, but wrong, assumption

"Deeper is always better." Because every extra layer multiplies one more ratio into the backward product, careless depth starves the early layers of signal or blows the nudges up; the table shows both fates.

steps traveledeach ratio 0.5: producteach ratio 2: product
10.52
20.254
30.1258
50.0332
100.0011024

The first column is the vanishing gradient from Part 2: the earliest weights receive almost no nudge; the second is its twin, the exploding gradient. The gated designs from Part 2, among them the LSTM, answer exactly this product; so do careful learning rates.

Reading a loss curve

Training prints one plot above all others: loss against training steps. You can now read it like terrain. The early cliff is the walk down the high wall; the long tail is the flat valley floor; a sudden spike is a step that jumped too far and landed on a wall.

a step too far the cliff the floor training steps loss
For the classroom: show an unlabeled loss curve that turns upward at the end and ask which of the three walkers from the step size slide produced it.

The LLM's own loss

Which loss trains the language model itself? The game from Part 1 scores like this: after the true next token is revealed, the loss depends on the probability the model had given it: large probability, small loss; small probability, enormous loss.

probability given to the true tokenloss
1.000
0.900.11
0.500.69
0.102.3
0.014.6

Computed with the logarithm formula the field uses; its details arrive in Part 5.

Notice the tilt: this loss has no symmetric partner, because there is no such thing as overshooting a probability of 1. Being confidently wrong at 0.01 costs about forty times more than being nearly sure at 0.9. The principle holds at the top of the field: the chosen shape encodes chosen values, and the LLM's loss says that confident wrongness is the cardinal sin.

Check for understanding

1. Under the squared loss with ground truth 67, what do predictions 65 and 69 cost, and what does the equality tell you about the loss?

4 and 4; the loss is symmetric, so direction carries no cost.

2. The cafeteria again: shortage costs 1 per meal, waste costs 0.2, and demand is 100 or 120 with equal chances. Compute both expected costs and choose the stock.

Stocking 100 carries an expected cost of 10; stocking 120 carries 2; order 120, because the tilt pushes the best answer up.

3. A network has one million weights. How many forward passes does one training step cost with the nudge test, and about how many passes with backpropagation?

About a million and one against about two.

For the classroom: reveal each answer only after the class commits.

Part 4 Overview

  1. Loss: a chosen function scoring the error between ground truth and prediction; the average loss is what training shrinks.
  2. Shape as policy: symmetric when direction is indifferent; tilted when the world's costs tilt, as with the cafeteria and the spam filter.
  3. Gradient descent: loss as landscape; velocity from steepness, the learning rate as the step size, momentum as acceleration with friction.
  4. Backpropagation: ratios along the chain multiply, one backward sweep prices every weight, and long products of small ratios explain the fading signal of Part 2.

Part 5 slows training down to its arithmetic: vectors, matrices, softmax, and the backward sweep at full width. Attention keeps its appointment in Part 9.

Sources and further reading

  • G. Piazzi, 1801: the discovery of Ceres.
  • A. M. Legendre, 1805: the least squares appendix; the method's first publication.
  • C. F. Gauss, 1809: Theoria Motus; the Ceres orbit story and the priority claim to 1795.
  • A. L. Cauchy, 1847: the first gradient descent method.
  • B. T. Polyak, 1964: the heavy ball method; momentum's formal debut.
  • P. Werbos, 1974: the Harvard thesis where backpropagation appeared early.
  • D. Rumelhart, G. Hinton, and R. Williams, 1986: backpropagation made standard; the paper also uses a momentum term.

Slides: Mehmet Kerem Turkcan, Center for Smart Streetscapes, Columbia University; keremturkcan.com; mkt2126@columbia.edu

Intro to AI, Part 4: Loss and BackpropagationM. K. Turkcan, Columbia University