ENع
←Neural networks
Lesson 13 · Deep learning

Neural networks

14 min10 steps5 questionsIntermediate
About this lesson

A neural network wires simple neurons together in layers, and every link carries a weight. Data flows forward through the layers to an answer; the loss says how wrong it is; backpropagation sends the error backwards to give every weight its gradient; and gradient descent nudges each weight a little. Follow a tiny 2-2-1 network through one full round of that loop, with every number worked out, then watch it learn XOR, a task no single neuron can learn.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 13 · Interactive
01 / 10

Neurons in layers

A neural network wires simple neurons together in layers. Ours has an input layer of 2, a hidden layer of 2 and an output layer of 1. Each link carries a weight: the signal travelling along it is multiplied by that number.

input layer    x1, x2         the numbers we feed in
hidden layer   h1, h2         neither input nor answer
output layer   y              the network's answer

each neuron:   sum of (input x weight) + bias,
               then its activation function
links:         6 weights, plus 3 biases (h1, h2, y)
Loading figure…
or ← →
Read the lesson step by step
  1. Neurons in layers

    A neural network wires simple neurons together in layers. Ours has an input layer of 2, a hidden layer of 2 and an output layer of 1. Each link carries a weight: the signal travelling along it is multiplied by that number.

    input layer    x1, x2         the numbers we feed in
    hidden layer   h1, h2         neither input nor answer
    output layer   y              the network's answer
    
    each neuron:   sum of (input x weight) + bias,
                   then its activation function
    links:         6 weights, plus 3 biases (h1, h2, y)
  2. The task: XOR

    The task is XOR: answer 1 when exactly one input is 1, otherwise 0. No single neuron can learn it, because no one straight line splits the 1s from the 0s. A hidden layer can: its neurons learn useful in-between features.

    x1 x2   XOR
     0  0    0
     0  1    1
     1  0    1
     1  1    0
    
    on a grid, the two 1s sit on opposite corners:
    one straight line can never put them on one side
  3. Forward: the hidden layer

    Forward pass, with input (1, 1). Each hidden neuron multiplies every input by its weight, adds them up, then adds its bias. h1: 1×1 + 1×1 + 0 = 2. h2: 1×1 + 1×1 − 1 = 1.

    y, z, h = forward(p, x)          x = (1, 1)
    
    z1 = x1*1 + x2*1 + 0   = 1 + 1 + 0  = 2
    z2 = x1*1 + x2*1 + (-1) = 1 + 1 - 1 = 1
    
    weights on the links, bias inside each neuron
  4. The activation: ReLU

    Next each neuron applies its activation function. Ours is ReLU: keep a positive sum, turn a negative one into 0. Both sums are positive, so h1 = 2 and h2 = 1. For input (0, 0), h2's sum, −1, would become 0.

    relu(z) = max(0, z)
    h1 = relu(2) = 2
    h2 = relu(1) = 1
    input (0, 0):  z2 = -1  ->  h2 = relu(-1) = 0
    
    without this bend, any stack of layers would
    add up to one straight-line model again
  5. Forward: the output

    The output neuron does the same with the hidden values: 2×1 + 1×(−1) + 0 = 1. So the network answers 1 for (1, 1), but XOR says 0: it is wrong. Its weights are not trained yet.

    y = h1*1 + h2*(-1) + 0 = 2 - 1 + 0 = 1
    target t = 0   (1 XOR 1 = 0)
    
    untrained outputs for all four inputs:
      00->0  01->1  10->1  11->1
    only (1, 1) is wrong
  6. Loss: how wrong?

    The loss measures how wrong the answer is. We use half the squared error, (y − t)² / 2, where t = 0 is the target. (1 − 0)² / 2 = 0.5. Learning means changing the weights so the loss gets smaller.

    loss = (y - t) ** 2 / 2
         = (1 - 0) ** 2 / 2 = 0.5
    
    the 1/2 makes the slope simple:
    d loss / d y = y - t = 1   (the error)
  7. Backward: share the blame

    Backpropagation sends the error, y − t = 1, backwards. Each weight's gradient is the error reaching its neuron times the signal it carried: 1 × 2 = 2 for h1's output weight. The chain rule passes the error back through every weight.

    g = backward(p, x, t)            error e = y - t = 1
    output weights:  e*h1 = 2   e*h2 = 1   bias: 1
    error at h1 = e * 1  * relu'(2) = 1 * 1  * 1 = 1
    error at h2 = e * -1 * relu'(1) = 1 * -1 * 1 = −1
    hidden weights:  into h1 1, 1   into h2 −1, −1
    (each = error at the neuron x the input, here 1)
  8. Update: a small step

    Gradient descent then moves every weight against its gradient, scaled by the learning rate 0.05: 1 − 0.05 × 2 = 0.9. The hidden weights become 0.95 and 1.05. Run (1, 1) again: the output is now 0.41, closer to 0.

    p = update(p, g, lr)            lr = 0.05
    h1 -> y:   1 - 0.05 * 2    = 0.9
    h2 -> y:  -1 - 0.05 * 1    = −1.05
    into h1:   1 - 0.05 * 1    = 0.95
    into h2:   1 - 0.05 * (-1) = 1.05
    biases:   h1 −0.05, h2 −0.95, y −0.05
    again:    y = 0.4075, loss 0.083 (was 0.5)
  9. Repeat: the network learns

    Training repeats this for every example, again and again. After 200 passes (epochs) over the four examples, the loss for a pass falls from 0.5 to 0.000047, and the outputs are 0, 1, 1, 0: the network has learned XOR.

    epoch   loss over the 4 examples
        1   0.5
       10   0.11
       50   0.014
      200   0.000047
    
    outputs now:  00->0.01  01->1  10->1  11->0
    weights now:  1 1 1.24 1.24 0.99 −1.6
  10. Deep learning: scale it up

    Deep learning runs this same loop, forward, loss, backward, update, on networks with many hidden layers and millions or billions of weights: GPT-3 has 175 billion. Libraries such as PyTorch work out every gradient automatically.

    the same four lines, at any size:
      1  y, z, h = forward(p, x)
      2  loss = (y - t) ** 2 / 2
      3  g = backward(p, x, t)
      4  p = update(p, g, lr)
    
    our network:   9 numbers to learn (6 weights, 3 biases)
    GPT-3 (2020):  175 billion

Quiz Unlocks after the last step.