The learning loop

Step a line through predict, measure, find the direction, and update, and watch training and test loss fall.

BeginnerExplained in The learning loop

The learning loop

A model (the line) improves by repeating four steps. Step through one round at a time, or let it run.

  1. 1PredictUse the current line to guess y for every training x.
  2. 2Measure the lossAverage the squared misses into one number.
  3. 3Find the directionWork out which way to nudge w and b to lower the loss.
  4. 4UpdateMove w and b a small step that way. Repeat.
  • Training points (used to learn)
  • Test points (held back)
  • Current line

The line starts out wrong. Press Next step to begin the first round.

0.50

How far each update moves w and b.

Round0
Training loss0.1007
Test loss0.0858
Slope w-0.5
Intercept b0.85

Challenges

0 of 3 complete

  1. Choose a step size that gets the training loss below 0.02 within 15 rounds (the default needs 20). (not yet)
  2. Find a step size that makes the training loss end up higher than where it started. (not yet)
  3. Train until the training loss stops changing, then compare it with the test loss. (not yet)

Try this

  • Step through one full round with Next step and read each note.
  • Run with a small step size, then with the largest. Compare the loss curves.
The learning loop in NumPyPython with NumPy
import numpy as np

# Training points (used to learn) and test points (held back).
x_train = np.array([0.1, 0.25, 0.4, 0.55, 0.7, 0.9])
y_train = np.array([0.30, 0.31, 0.47, 0.50, 0.66, 0.71])
x_test = np.array([0.2, 0.5, 0.8])
y_test = np.array([0.29, 0.53, 0.66])

def mse(w, b, x, y):
    return np.mean((w * x + b - y) ** 2)

w, b = -0.5, 0.85  # a badly wrong starting line, as in the demo
lr = 0.5           # step size (learning rate)

for n in range(1, 201):          # n = round number
    guess = w * x_train + b                 # 1. predict
    miss = guess - y_train                  # 2. measure (the loss averages miss ** 2)
    grad_w = 2 * np.mean(miss * x_train)    # 3. find the direction:
    grad_b = 2 * np.mean(miss)              #    slopes of the loss for w and b
    w, b = w - lr * grad_w, b - lr * grad_b  # 4. update: a small step downhill
    if n in (1, 10, 50, 200):
        print(f"round {n:3}: train loss {mse(w, b, x_train, y_train):.4f}, "
              f"test loss {mse(w, b, x_test, y_test):.4f}")

print(f"learned line: y = {w:.3f} x + {b:.3f}")
round   1: train loss 0.0803, test loss 0.0727
round  10: train loss 0.0281, test loss 0.0268
round  50: train loss 0.0014, test loss 0.0014
round 200: train loss 0.0012, test loss 0.0009
learned line: y = 0.567 x + 0.218

Try "embedding", "softmax", "overfitting", or "backpropagation".