Lesson 6.1 · 8 min read · about 55 min to master at a very slow pace

Training as Minimising a Loss

Training has two steps: choose a loss that measures how wrong the model is, then minimise it. Worked through for a straight line with mean squared error: the gradients, one update, and the loss going down.

Not startedJump to quiz

Two decisions

  1. Choose a loss that measures how badly the model with parameters does on the data.
  2. Minimise it, usually with gradient descent: .

Mean squared error for a line

For and examples:

Differentiate with the chain rule:

Worked example: one gradient step

Data . Start at , ; residuals are .

  • .
  • .
  • .
  • With : , .
  • New predictions ; new , down from 3.

Neural networks do exactly this, with millions of parameters and backpropagation (Section 7) to get the gradients.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passWhy are the gradients negative when the line is too low?

The residuals are positive (the line is below the points), so and come out negative. Gradient descent subtracts them, so and increase and the line moves up towards the data, which is what we want.

First passWhat is the difference between a loss function and an objective function?

Strictly:

  • Loss : the error on one example.
  • Cost, or empirical risk: the average loss over the training set, .
  • Objective: whatever training actually minimises. That is the cost, plus a penalty if you regularise: .

Books and frameworks often use the words loosely (PyTorch calls everything a "loss"), and this lesson calls the loss. Here is the average squared error with no penalty, so it is both the cost and the objective. In an exam, define the terms you use.

Going deeperDoes the objective function represent the error?

Mostly. With no penalty, the objective is the average error on the training data. Two caveats:

  • With a regularisation penalty, it is error plus a cost for large weights, so a lower objective doesn't always mean a lower training error.
  • It is training error, not the error on new data that you actually care about. That is why a validation set is kept aside (Section 8).

Cheatsheet

MSE gradients for a line

; ; .

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateData ; line (so , ). What is the MSE ?
Q2calculateFor the same data and line, . What is its value?
Q3interpretAt , both gradients are negative. What will a gradient descent step do?
Q4conceptWhat are the two decisions that define how a network is trained?
Q5calculateAfter one step with , the line becomes and the MSE drops to about 0.66. What was the MSE before?