SECTION 02 / 4

The Supervised Learning Setup, Loss and Guess Work

5 min read
On this page 8 headings
BUILD INTUITION WITH NUMBERS

Measure how wrong two predictions are

Targets are [0, 1], predictions are [0.2, 0.8]. Here we choose mean squared error, with no extra factor of ½.

  1. Calculate each squared error.

    (0.2−0)2=0.04,(0.8−1)2=0.04(0.2-0)^2=0.04,\qquad(0.8-1)^2=0.04
  2. Average over the two examples.

    L=0.04+0.042=0.04L=\frac{0.04+0.04}{2}=0.04

What changed? The loss compresses prediction errors into one number. Summing instead of averaging would give 0.08, so check the question’s convention.

Quick check: If both predictions exactly matched their targets, what would this loss be?

0: both squared errors would be zero.

Swipe long formulas, tables or code sideways to see the rest.

Start here Before learning any weights we need to say clearly what we are learning and how we measure mistakes. Every machine learning problem in this course has the same five parts. Then we try the simplest possible way to learn — guessing — and see why it does not scale.

1. Accept some error

A single perceptron cannot perfectly classify data that is not linearly separable. “Cannot deal with” means: whatever line it draws, some points end up on the wrong side.

From now on we accept that driving the error to 0 is hard in most cases. Instead we aim for the minimum possible error.

2. The five parts of supervised machine learning

The learning algorithm keeps changing the parameters so that the loss gets smaller.
The learning algorithm keeps changing the parameters so that the loss gets smaller.
Open image ↗
Data, model, parameters, learning algorithm and loss function as five connected boxes
Part Meaning Examples
Data n pairs {(xᵢ, yᵢ)} movie features + like/dislike
Model our guess of how y depends on x y^=11+e−wTx\hat{y} = \frac{1}{1 + e^{-w^{T}x}}, or y^=wTx\hat{y} = w^{T}x, or y^=xTWx\hat{y} = x^{T}Wx
Parameters numbers inside the model to learn w (and W)
Learning algorithm how we change the parameters perceptron learning algorithm, gradient descent
Loss / objective / error function how wrong the model is; guides the algorithm L(w)=∑i(y^i−yi)2L(w) = \sum _{i} (\hat{y}_{i} - y_{i})^{2}

The learning algorithm should aim to minimise the loss function.

Lecture example — movie setup Data: {xi=x_{i} = movie, yi=y_{i} = like/dislike}. Model: y^=11+e−wTx=\hat{y} = \frac{1}{1 + e^{-w^{T}x}} = probability of liking. Parameters: w. Algorithm: gradient descent. Loss: L(w)=∑i=1n(y^i−yi)2L(w) = \sum _{i=1}^{n} (\hat{y}_{i} - y_{i})^{2} (squared error). Find the w that makes this smallest.

3. A tiny model to learn with

To keep things simple:

  • only one input x,
  • w0w_{0} is renamed b (bias), as most books do,
  • task: predict criticsRating (y) from imdbRating (x).

Model: f(x)=11+e−(wx+b)f(x) = \frac{1}{1 + e^{-(wx + b)}}

Training data: N pairs (x, y). Objective:

minimise L(w,b)=(1/N)∑i=1N(yi−f(xi))2L(w, b) = (1/N) \sum _{i=1}^{N} (y_{i} - f(x_{i}))^{2}

4. What does “training” mean?

Lecture example — two points Train with (x,y)=(x, y) = (0.5, 0.2) and (2.5, 0.9). We want to find w*, b* such that f(0.5) → 0.2 and f(2.5) → 0.9. In other words: find a sigmoid curve that passes through both points.

Training = changing w and b until the curve passes through the data points.
Training = changing w and b until the curve passes through the data points.
Open image ↗
Three sigmoid curves; only the best one passes through both training points

5. Learning by guess work

Start with a random guess: w=3,b=−1w = 3, b = -1. How bad is it? (The lecture uses ½ in front, since N=2N = 2.)

L(w,b)=12×[(0.9−f(2.5))2+(0.2−f(0.5))2]L(w, b) = \frac{1}{2} \times [(0.9 - f(2.5))^{2} + (0.2 - f(0.5))^{2}]

Step by step:

  1. f(2.5)=σ(3×2.5−1)=σ(6.5)=11+e−6.5=11+0.0015=f(2.5) = \sigma (3 \times 2.5 - 1) = \sigma (6.5) = \frac{1}{1 + e^{-6.5}} = \frac{1}{1 + 0.0015} = 0.9985
  2. f(0.5)=σ(3×0.5−1)=σ(0.5)=11+0.6065=f(0.5) = \sigma (3 \times 0.5 - 1) = \sigma (0.5) = \frac{1}{1 + 0.6065} = 0.6225
  3. (0.9−0.9985)2=(−0.0985)2=0.0097(0.9 - 0.9985)^{2} = (-0.0985)^{2} = 0.0097
  4. (0.2−0.6225)2=(−0.4225)2=0.1785(0.2 - 0.6225)^{2} = (-0.4225)^{2} = 0.1785
  5. L=12×(0.0097+0.1785)=12×0.1882=L = \frac{1}{2} \times (0.0097 + 0.1785) = \frac{1}{2} \times 0.1882 = 0.0941

(The slide rounds f(2.5) to 0.99 and f(0.5) to 0.62.) We want L as close to 0 as possible. So we keep changing w and b by hand:

w b Loss (computed) Comment
0.50 0.00 0.0731 better than the first guess
−0.10 0.00 0.1481 worse — went the wrong way
0.94 −0.94 0.0217 better
1.42 −1.73 0.0029 much better
1.65 −2.08 0.0003 almost there
1.78 −2.27 0.0000 (nearly) perfect

(The slides show 0.14, 0.0214 and 0.0028 for three of these rows — small rounding differences from the values above.)

The exact best answer can be worked out because σ(wx+b)=y\sigma (wx + b) = y means wx+b=log⁡(y/(1−y))wx + b = \log (y/(1 - y)):

  • 0.5w+b=log⁡(0.2/0.8)=−1.3860.5w + b = \log (0.2/0.8) = -1.386
  • 2.5w+b=log⁡(0.9/0.1)=2.1972.5w + b = \log (0.9/0.1) = 2.197
  • Subtract: 2w=3.583→2w = 3.583 \to w=1.792w = 1.792, then b=−1.386−0.896=−2.282b = -1.386 - 0.896 = -2.282.

So the guesses were walking towards (1.79, −2.28).

6. The error surface and why guessing fails

With only 2 parameters we can draw the loss for every (w, b) — this is the error surface. Then just pick the lowest point.

Each point on the surface is one (w, b) and its loss. Guessing jumps around; we want to walk downhill.
Each point on the surface is one (w, b) and its loss. Guessing jumps around; we want to walk downhill.
Open image ↗
A loss landscape over w and b; random guesses hop around while a walker heads downhill to the lowest point

Problems with guessing / plotting everything:

  • With many data points and many parameters (real networks have millions), plotting every combination is impossible (intractable).
  • Even here, the plot covers only w, b in (−6, +6), not (−∞, +∞).

Geometrically, the guess-work algorithm is just hopping around on this surface. We need a principled way to move downhill to the minimum — that is gradient descent (next page).

In one line: Supervised learning == data + model + parameters + learning algorithm + loss; training means finding parameters with the smallest loss, and guessing does not scale.

Exam words: supervised learning, data, model, parameters, learning algorithm, objective/loss/error function, squared error loss, training, error surface, intractable.

Quick check

  1. Iny^=11+e−wTxIn \hat{y} = \frac{1}{1 + e^{-w^{T}x}}, which part is the parameter?
  1. (x is the data; the formula is the model.)
  1. Compute the loss 12[(0.9−f(2.5))2+(0.2−f(0.5))2]\frac{1}{2}[(0.9 - f(2.5))^{2} + (0.2 - f(0.5))^{2}] for w=0,b=0w = 0, b = 0.
f(x)=σ(0)=0.5f(x) = \sigma (0) = 0.5 for any x. 12[(0.4)2+(−0.3)2]=12[0.16+0.09]=0.125\frac{1}{2}[(0.4)^{2} + (-0.3)^{2}] = \frac{1}{2}[0.16 + 0.09] = 0.125.
  1. Why can’t we just plot the error surface for a real network?
It has a huge number of parameters, so the number of combinations to try is impossibly large; and each parameter can be any real number.
  1. At the best w, b for the two-point example, what is the loss?
0 — the sigmoid passes exactly through both points.
  1. Name two possible learning algorithms from the lecture.
Perceptron learning algorithm and gradient descent.
YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑