How can neural networks be trained and what is the role of backpropagation in this process?

A network predicts 1 when the target is 0. Should you lower the first weight by 1? The prediction error alone cannot tell you. Backpropagation computes how sensitive the loss is to each weight and…

A network predicts 1 when the target is 0. Should you lower the first weight by 1? The prediction error alone cannot tell you. Backpropagation computes how sensitive the loss is to each weight and bias at their current values. The optimizer then decides how far to move them. For the whole training loop, start with how neural networks make and improve predictions. Here we will trace one backward pass and check its numbers without trusting the derivative formulas.

Start at the loss, not at the first weight

Consider a network with one input, one sigmoid hidden unit, and one linear output. For this example, the input is x = 2 and the target is 0. Its parameters are the input weight w = 0.25, bias b = −0.5, and output weight v = 2. Keep them fixed throughout the backward pass.

Forward pass. First calculate z = wx + b = 0. The sigmoid gives h = 1 / (1 + exp(−z)) = 0.5. The prediction is v × h = 1; the half-squared error is L = (1 − 0)² / 2 = 0.5. Save the hidden value: the backward pass needs it both for the output weight and for the slope of the sigmoid.

Follow the chain rule backward

Start with the slope of the loss with respect to the prediction: for half-squared error it is prediction minus target, or 1. Work backward through the product v × h. A change in v is scaled by h, so ∂L/∂v = 1 × 0.5 = 0.5. A change in h is scaled by v, so ∂L/∂h = 1 × 2 = 2. The same prediction error produces different slopes.

Next, the sigmoid’s local slope is h(1 − h) = 0.25. Multiply the incoming slope by that local slope: ∂L/∂z = 2 × 0.25 = 0.5. Finally, z = wx + b gives ∂L/∂w = 0.5 × x = 1 and ∂L/∂b = 0.5 × 1 = 0.5. That multiplication at each operation is the chain rule. If a parameter feeds multiple paths to the loss, add the contributions from those paths.

The optimizer’s turn. The gradient in parameter order (w, b, v) is (1, 0.5, 0.5). No parameter has changed yet. Plain gradient descent with learning rate 0.1 subtracts 0.1 times each component from its original parameter, giving (0.15, −0.55, 1.95). Changing the learning rate changes this update, not the gradient we just computed. Using the new output weight to calculate the old hidden gradient would mix two different forward passes.

Check the backward pass with finite differences

Before taking that optimizer step, perturb one original parameter at a time. Evaluate the same loss twice and divide the change by the distance between the probes: [L(p + ε) − L(p − ε)] / (2ε). Run the following in Python 3 with no additional packages. It uses a step of 1e-5 and requires each absolute difference from the analytic gradient to be below 1e-8.

from math import exp

x, target = 2.0, 0.0
params = (0.25, -0.5, 2.0)  # w, b, v

def forward(w, b, v):
    h = 1.0 / (1.0 + exp(-(w * x + b)))
    error = v * h - target
    return h, error, 0.5 * error**2

h, error, loss = forward(*params)
v = params[2]
dz = error * v * h * (1.0 - h)
grads = (dz * x, dz, error * h)
print(f"loss={loss:.6f}")
eps, tol = 1e-5, 1e-8
for i, name in enumerate(("w", "b", "v")):
    plus, minus = list(params), list(params)
    plus[i] += eps
    minus[i] -= eps
    numeric = (forward(*plus)[2] - forward(*minus)[2]) / (2 * eps)
    delta = abs(grads[i] - numeric)
    print(f"{name}: analytic={grads[i]:.6f} numeric={numeric:.6f} error={delta:.2e}")
    assert delta < tol

All three checks passed. The largest absolute discrepancy in our run was about 4.34e-11, for the input weight. If you omit the factor x from its derivative, the analytic value becomes 0.5 while the numerical estimate stays near 1. That isolates the mistake to the input-weight path rather than the output weight.

When can a gradient check mislead you?

Keep the input batch and any random masks fixed between probes. Too large a step averages slopes over a curved region; too small a step can lose precision when nearly equal losses are subtracted. This tolerance is for this small, smooth example, not every dtype or loss scale. A coordinate check takes two forward evaluations per parameter: useful on a small test case, expensive as a training method.

At the kink of ReLU(z) = max(0, z), the left slope is 0 and the right slope is 1. A centered difference at zero returns 0.5, but there is no unique derivative there. A library can choose 0 at the kink without having a broken backward pass. Move the test point so neither probe crosses zero. See how activation functions differ for the shapes. Even correct gradients can be too small to train an early layer: repeatedly multiplying small local slopes is one cause of vanishing gradients.

Sources and further reading

Goodfellow, Bengio, and Courville work through reverse accumulation in Deep Learning, section 6.5. For the checks used by an automatic differentiation library, see the PyTorch gradient-checking notes.

As an Amazon Associate I earn from qualifying purchases. For the print edition, see Deep Learning by Ian Goodfellow, Yoshua Bengio, and Aaron Courville (Amazon US).