Backpropagation
The networks that recognise images and write texts have billions of parameters, but they are all trained with the same algorithm: backpropagation. Here we see it on the smallest possible network, and then in a deep network, where it shows its most famous limit.
Forwards and backwards
The tiny network has an input x, a hidden neuron with the ReLU and an output neuron with the sigmoid. Forwards: z₁ = w₁x + b₁, h = max(z₁, 0), z₂ = w₂h + b₂, p = σ(z₂). The loss, for an example with correct answer 1, is −ln p.
Backwards, the chain rule is applied, from the output towards the input. For the sigmoid with cross-entropy the derivative with respect to z₂ is simply p − t. From there each layer multiplies by its local derivative: w₂ to go back to h, the derivative of the ReLU to go back to z₁, x to reach w₁.
In the lab, on the left, move the weights and watch the values going forwards and the gradients going backwards. On the right, a deep network: how much gradient reaches each layer.
Three experiments
The worked example. With x = 1, w₁ = 0.5, b₁ = 0, w₂ = 2 and b₂ = −1 the network gives p = 0.5 and loses 0.693. Backwards p − t = −0.5, then −0.25 for w₂ and −1 for w₁. All the gradients are negative: every weight must be increased. Press “Un passo di discesa” (one descent step) and watch the loss go down.
The dead neuron. Take w₁ to −0.5: z₁ is negative, the ReLU gives zero, and the gradients of w₁, b₁ and w₂ become zero. Only that of b₂ remains. A neuron that is dead for all the examples no longer learns anything.
The vanishing gradient. With ten layers of sigmoids the gradient that reaches the first layer is millions of times smaller than that of the last: the derivative of the sigmoid does not exceed 0.25, and 0.25 to the tenth power is less than a millionth. With the ReLU it stays of the same order of magnitude: it is one of the reasons why today it is used everywhere.
The same experiment in Python
The chapter’s worked example, in NumPy.
import numpy as np
x, t = 1.0, 1.0
w1, b1, w2, b2 = 0.5, 0.0, 2.0, -1.0
z1 = w1 * x + b1
h = max(z1, 0.0) # ReLU
p = 1 / (1 + np.exp(-(w2 * h + b2))) # sigmoid
print("loss", round(-np.log(p), 4))
d_z2 = p - t # sigmoid + cross-entropy
d_w2, d_b2 = d_z2 * h, d_z2
d_z1 = d_z2 * w2 * (1.0 if z1 > 0 else 0.0) # the ReLU lets the gradient through only if z1 > 0
d_w1, d_b1 = d_z1 * x, d_z1
print("gradients", d_w1, d_b1, d_w2, d_b2)
print("0.25 to the power of 10:", 0.25 ** 10) # the vanishing gradient with ten sigmoids
In the book
This lab accompanies chapter 16, “Neural networks”, the last in the book: the perceptron and XOR, the multilayer network, activation functions, backpropagation by hand and in NumPy on Ordito’s customers, scikit-learn’s networks, early stopping and the penalty on the weights, and the bridge to deep learning.
The “Inside Ordito” series
- Chapter 1 The line of the five stores
- Chapter 2 A simulated store
- Chapter 3 Where the leak hides
- Chapter 4 The valley of the five stores
- Chapter 5 The threshold
- Chapter 6 Why was it returned?
- Chapter 7 Build a tree
- Chapter 8 Boosting step by step
- Chapter 9 The margin
- Workshop A Price is place
- Chapter 10 Shapley by hand
- Chapter 11 Rotate the cloud
- Chapter 12 Groups and dense areas
- Chapter 13 Isolating a point
- Chapter 14 Naive forecasts
- Workshop B A year in production
- Chapter 15 The ink stain
- Chapter 16 Backpropagation