Backpropagation

When the output is wrong, how much is each weight to blame?

A tiny network, one training example, and every number shown. The table at the bottom is the heart of how all neural networks learn: for each weight, backprop multiplies a few local slopes together to get "how much does the error change if I nudge this weight?"

The network after 3180 updates

w1 = 0.332 w2 = 0.564 w3 = 0.431 w4 = 0.661 w5 = −3.522 w6 = −3.496 w7 = 2.493 w8 = 2.554 i1 0.050 i2 0.100 h1 0.604 h2 0.608 o1 0.025 o2 0.975 target 0.01 target 0.99 bias b1 = 0.35 output bias b2 = 0.6

Train it

0.5
0.000231total error E
0.0253, 0.9749outputs (want 0.01, 0.99)
0.00000.0757950.1491,5900.2242,3850.2983,180updates

1. Forward pass: compute the answer

net_h1 = w1·i1 + w2·i2 + b1 = 0.3319·0.05 + 0.5638·0.10 + 0.35 = 0.422978
h1 = σ(net_h1) = 0.604196    h2 = σ(0.437645) = 0.607698
net_o1 = w5·h1 + w6·h2 + b2 = −3.652382    o1 = 0.025274
net_o2 = w7·h1 + w8·h2 + b2 = 3.658243    o2 = 0.974870
E = ½(t1 − o1)² + ½(t2 − o2)² = 0.000117 + 0.000114 = 0.000231105

What to notice

Output weights are easy. w5's blame is just three numbers multiplied: how wrong o1 is, how steep o1's sigmoid is, and how big h1 was (a big input means the weight mattered more).

Hidden weights reuse that work. w1 can't see the error directly. Its blame arrives through both outputs: output 1's blame times w5, plus output 2's blame times w7. Then the same local slopes. That reuse is what makes backprop fast enough for billions of weights.

The check column agrees. "Nudge and measure" gives the same numbers without any calculus, but it needs two extra forward passes per weight. For an LLM that would be billions of passes per step; backprop needs one.

2. Backward pass: blame for every weight

WeightLocal slopes multiplied together (chain rule)∂E/∂wCheck (nudge)New value = w − 0.50 × ∂E/∂w
w5 (o₁ − t₁) 0.01527 × o₁(1 − o₁) 0.02464 × h₁ 0.60420 0.0002273 0.0002273 −3.52227 → −3.52237898
w6 (o₁ − t₁) 0.01527 × o₁(1 − o₁) 0.02464 × h₂ 0.60770 0.0002287 0.0002287 −3.49556 → −3.49567564
w7 (o₂ − t₂) −0.01513 × o₂(1 − o₂) 0.02450 × h₁ 0.60420 −0.0002240 −0.0002240 2.49256 → 2.49267661
w8 (o₂ − t₂) −0.01513 × o₂(1 − o₂) 0.02450 × h₂ 0.60770 −0.0002252 −0.0002252 2.55431 → 2.55442028
w1 δo₁·w5 + δo₂·w7 −0.00225 × h₁(1 − h₁) 0.23914 × i₁ 0.05000 −0.0000269 −0.0000269 0.33191 → 0.33192420
w2 δo₁·w5 + δo₂·w7 −0.00225 × h₁(1 − h₁) 0.23914 × i₂ 0.10000 −0.0000538 −0.0000538 0.56382 → 0.56384840
w3 δo₁·w6 + δo₂·w8 −0.00226 × h₂(1 − h₂) 0.23840 × i₁ 0.05000 −0.0000270 −0.0000270 0.43058 → 0.43059246
w4 δo₁·w6 + δo₂·w8 −0.00226 × h₂(1 − h₂) 0.23840 × i₂ 0.10000 −0.0000539 −0.0000539 0.66116 → 0.66118492

At the start with learning rate 0.5 you should see w5 → 0.35891648 and w1 → 0.14978072, the same as the original worked example. δo means (o − t)·o(1 − o), the output neuron's blame.

The code that just ran

Everything on this page came from calling this code, which lives at the top of this same file.

Show or hide the source
final class TinyNet
{
    public array $w = ['w1' => 0.15, 'w2' => 0.20, 'w3' => 0.25, 'w4' => 0.30,
                       'w5' => 0.40, 'w6' => 0.45, 'w7' => 0.50, 'w8' => 0.55,
                       'b1' => 0.35, 'b2' => 0.60];   // biases stay fixed, as in the original example
    public array $in = [0.05, 0.10];
    public array $target = [0.01, 0.99];

    public function forward(): array
    {
        $w = $this->w; [$i1, $i2] = $this->in; [$t1, $t2] = $this->target;
        $nh1 = $w['w1'] * $i1 + $w['w2'] * $i2 + $w['b1'];  $h1 = sigmoid($nh1);
        $nh2 = $w['w3'] * $i1 + $w['w4'] * $i2 + $w['b1'];  $h2 = sigmoid($nh2);
        $no1 = $w['w5'] * $h1 + $w['w6'] * $h2 + $w['b2'];  $o1 = sigmoid($no1);
        $no2 = $w['w7'] * $h1 + $w['w8'] * $h2 + $w['b2'];  $o2 = sigmoid($no2);
        $E1 = 0.5 * ($t1 - $o1) ** 2;
        $E2 = 0.5 * ($t2 - $o2) ** 2;
        return compact('i1', 'i2', 't1', 't2', 'nh1', 'h1', 'nh2', 'h2', 'no1', 'o1', 'no2', 'o2', 'E1', 'E2') + ['E' => $E1 + $E2];
    }

    public function loss(): float { return $this->forward()['E']; }

    // BACKPROPAGATION. Each gradient is a product of simple local slopes:
    //   (o − t)      how the error changes with the output
    //   o·(1 − o)    how the sigmoid output changes with its input
    //   h or i       how that input changes with the weight
    // Hidden weights get the SUM of the blame flowing back from both outputs.
    public function gradients(): array
    {
        $f = $this->forward(); $w = $this->w;
        $do1 = ($f['o1'] - $f['t1']) * $f['o1'] * (1 - $f['o1']);   // δ of output 1
        $do2 = ($f['o2'] - $f['t2']) * $f['o2'] * (1 - $f['o2']);   // δ of output 2
        $back1 = $do1 * $w['w5'] + $do2 * $w['w7'];                 // blame reaching h1
        $back2 = $do1 * $w['w6'] + $do2 * $w['w8'];                 // blame reaching h2
        $s1 = $f['h1'] * (1 - $f['h1']);
        $s2 = $f['h2'] * (1 - $f['h2']);

        $g = fn($factors) => ['factors' => $factors, 'value' => array_product(array_column($factors, 1))];
        return [
            'w5' => $g([['(o₁ − t₁)', $f['o1'] - $f['t1']], ['o₁(1 − o₁)', $f['o1'] * (1 - $f['o1'])], ['h₁', $f['h1']]]),
            'w6' => $g([['(o₁ − t₁)', $f['o1'] - $f['t1']], ['o₁(1 − o₁)', $f['o1'] * (1 - $f['o1'])], ['h₂', $f['h2']]]),
            'w7' => $g([['(o₂ − t₂)', $f['o2'] - $f['t2']], ['o₂(1 − o₂)', $f['o2'] * (1 - $f['o2'])], ['h₁', $f['h1']]]),
            'w8' => $g([['(o₂ − t₂)', $f['o2'] - $f['t2']], ['o₂(1 − o₂)', $f['o2'] * (1 - $f['o2'])], ['h₂', $f['h2']]]),
            'w1' => $g([['δo₁·w5 + δo₂·w7', $back1], ['h₁(1 − h₁)', $s1], ['i₁', $f['i1']]]),
            'w2' => $g([['δo₁·w5 + δo₂·w7', $back1], ['h₁(1 − h₁)', $s1], ['i₂', $f['i2']]]),
            'w3' => $g([['δo₁·w6 + δo₂·w8', $back2], ['h₂(1 − h₂)', $s2], ['i₁', $f['i1']]]),
            'w4' => $g([['δo₁·w6 + δo₂·w8', $back2], ['h₂(1 − h₂)', $s2], ['i₂', $f['i2']]]),
        ];
    }

    // Sanity check without any calculus: nudge the weight up and down a hair
    // and see how much the loss moves. Should match backprop almost exactly.
    public function numericalGradient(string $name, float $eps = 1e-5): float
    {
        $keep = $this->w[$name];
        $this->w[$name] = $keep + $eps; $up = $this->loss();
        $this->w[$name] = $keep - $eps; $down = $this->loss();
        $this->w[$name] = $keep;
        return ($up - $down) / (2 * $eps);
    }

    public function step(float $lr): void
    {
        foreach ($this->gradients() as $name => $g) $this->w[$name] -= $lr * $g['value'];
    }
}