cat /en/backpropagation/
MDN-119:35
SERIES Inside Ordito · Chapter 16

Backpropagation

· Artificial Intelligence · 5 min read

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.

DEMO Backpropagation · about this demo · full screen ↗

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.

COVER COMING SOON
mltp · 2026

Machine Learning. Teoria, matematica e pratica degli algoritmi

Michele di Nuzzo · in Italian

Every algorithm in two steps: first derive it, with the mathematics it needs and an example worked by hand, then write it from scratch in NumPy. Only at the end use it with scikit-learn.

[the book’s page] [shop links coming soon]

The “Inside Ordito” series