A neural network learns by repeating one loop: guess, measure how wrong the guess was, then nudge every weight in the direction that would have made the guess less wrong. That last step — figuring out which nudge to make — is entirely calculus.
This piece derives it from scratch: the chain rule that makes backpropagation possible, the derivative table for common activations, the surprisingly elegant softmax-plus-cross-entropy shortcut, and a full worked example you can trace by hand.
What a network actually computes
Strip away the hype and a neural network is a fairly plain machine: a chain of matrix multiplications and squashing functions that turns an input into a prediction.
Picture the network as a stack of layers, numbered . Each layer takes what the layer before it produced, mixes it with some weights, and passes the result through a non-linear function. Written out, layer does exactly two things:
The first line is a weighted vote: every unit in the layer looks at everything the previous layer produced, weighs each signal by an importance number, and adds a bias. The second line squashes that vote through an activation function , which is what lets the network represent curved, non-linear relationships rather than only straight lines.
A few pieces of notation worth keeping straight:
- — the weight matrix connecting layer to layer
- — the bias vector for layer , one free parameter per unit
- — the previous layer’s output, with , the raw input
- — an activation function applied element-wise
Once the signal reaches the last layer , the network has a prediction . A loss function then scores how far that prediction sits from the true answer . Everything from here on is about one question: how should each weight change to make smaller?
The chain rule, and why backpropagation works at all
Training means nudging every weight downhill, using gradient descent:
where is the learning rate — how big a step to take. The entire difficulty is computing that gradient, , for every single weight in the network, without recomputing the whole forward pass millions of times. That is exactly what the chain rule buys us.
The multivariable chain rule
The loss does not touch a weight directly — it only feels it through the chain of vectors that weight influences downstream. So we route the derivative through the intermediate variable :
Now the simplification that makes this tractable: because , the weight only ever appears in the formula for — nowhere else. So every term in that sum vanishes except the one where , leaving:
Passing the blame backward: the delta recurrence
To avoid recomputing this chain from scratch for every weight, define a single quantity per unit — its local error, usually written — that captures exactly how sensitive the loss is to that unit’s raw input:
The trick is that at layer can be written entirely in terms of the deltas one layer ahead, . Applying the chain rule once more, backward through the layer boundary:
Two of those factors have simple closed forms: , and . Substituting both in:
Written in matrix form, with denoting an element-wise (Hadamard) product, this becomes the single equation that is backpropagation:
Once you have for a layer, the gradients you actually need fall out immediately: and . One backward sweep, computing layer by layer from the output back to the input, hands you every gradient in the network. That is the entire algorithm.
Activation functions and their derivatives
The recurrence above needs at every layer, so it is worth having the common activations and their derivatives on hand.
| Function | Formula | Derivative |
|---|---|---|
| Sigmoid — squashes to | ||
| Tanh — squashes to | ||
| ReLU — the default for hidden layers | if , else |
Drag , , and below to move the point along the curve. The tangent line’s slope is — the exact quantity backpropagation needs at this unit.
Neuron explorer
Push far into either tail with sigmoid or tanh and watch the tangent go nearly flat. Switch to ReLU and the slope only ever takes two values: no in-between flattening at all.
Softmax and cross-entropy: an elegant special case
Classification problems with more than two categories almost always pair two specific ingredients — and the pairing is not a coincidence. It is chosen because the calculus collapses into something remarkably simple.
For a -class problem, the output layer uses softmax to turn raw scores into a probability distribution that sums to one:
and pairs it with categorical cross-entropy loss, which penalizes low confidence in the correct class:
where is a one-hot vector — all zeros except a single 1 marking the correct class.
Differentiating softmax
Softmax is trickier than most activations because every output depends on every input , not just its own. That means the derivative is a full Jacobian, and it splits into two cases.
When (a unit’s effect on its own output), the quotient rule gives:
When (how one unit’s score drags down another’s probability):
Both cases fold into one compact expression using the Kronecker delta :
The payoff: differentiating the loss
Chain the loss through every softmax output that influences:
The loss’s own derivative with respect to a probability is . Substituting and letting the terms cancel eventually yields:
Adjust the three raw scores, pick which class is actually correct, and watch the probabilities — and the error signal — update instantly.
Softmax playground
True class
Set the true class to whichever bar is already tallest and the loss drops close to zero. Set it to the shortest bar instead — the network is confidently wrong — and watch the loss spike while swings sharply negative.
A fully worked example, by hand
Formulas are easier to trust once you have pushed real numbers through them. Here is the smallest possible network — one input, one hidden unit, one output — worked from start to finish.
Setup
- Input , target
- Weights , ; biases ,
- Activation: sigmoid. Loss: squared error
1. Forward pass — make a prediction
- Hidden pre-activation:
- Hidden activation:
- Output pre-activation:
- Prediction:
- Loss — the network is fairly wrong, since the target was 0
2. Backward pass — assign blame
- Output error:
- Gradient for :
- Hidden error:
- Gradient for :
Both weights should shrink slightly — that is the direction that reduces the loss. With learning rate , the update would be and . Run this loop thousands of times, over thousands of examples, and those small nudges are the entirety of what “training a neural network” means.
Questions that usually come up next
Do I need to compute any of this by hand in practice?
No — frameworks like PyTorch and TensorFlow use automatic differentiation to compute every gradient for you. The value of working through the derivation is that it explains why those tools behave the way they do: why gradients vanish, why certain activation/loss pairings are standard, and what a training curve is actually measuring.
Why is it called “backpropagation” specifically?
Because the local error is computed starting at the output layer and propagated backward, layer by layer, reusing each layer’s result to compute the next. Reusing intermediate results this way is what makes the algorithm efficient.
What happens if the activation function is not differentiable everywhere?
ReLU technically has an undefined derivative at exactly , but in practice frameworks just assign it 0 or 1 there by convention — the probability of landing on that exact point during training is negligible.
Is gradient descent guaranteed to find the best possible weights?
No. The loss surface of a deep network is highly non-convex, so gradient descent generally finds a good minimum rather than provably the best one. In practice, over-parameterized networks tend to have many minima that perform comparably well.
Further reading
- Rumelhart, D. E., Hinton, G. E., & Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323(6088), 533–536.
- LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep learning. Nature, 521(7553), 436–444.
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.
- Nielsen, M. A. (2015). Neural Networks and Deep Learning. Determination Press.
- Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer.
Filed under
- Calculus
- Neural networks
- Backpropagation
- Chain rule
- Softmax