Backpropagation › Forward Pass and Backward Pass
Language AI HandbookAll chapters Backpropagation › Forward Pass and Backward PassOne section · Sections in this chapter6 / 20 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
With computational graphs and the chain rule in hand, we can now describe the complete backpropagation algorithm. The algorithm has two phases: a forward pass that computes outputs and stores intermediate values, and a backward pass that computes gradients using those stored values.
The Forward Pass
During the forward pass, we evaluate the network layer by layer, storing all intermediate values. These stored values are critical because the backward pass needs them to compute local gradients.
For a 2-layer network with input , weights , , biases , , and activation , the forward pass computes:
where:
- : the pre-activation values at layer (also called logits)
- : the post-activation values at layer
- : the predicted probability distribution
- : the one-hot encoded target
- : the cross-entropy loss (scalar)
After the forward pass, we have stored . These are the "cache" that the backward pass will consume. The reason we store specifically (and not just ) is that the activation derivative needs the pre-activation value, not the post-activation value. For sigmoid, though, we can recover from the activation value itself using , so storing either is fine.
The Backward Pass
The backward pass applies the chain rule in reverse, starting from the loss and working backward through each operation.
Gradient of loss with respect to output. We start with . For softmax + cross-entropy, this has the elegant form:
This says: the gradient at the output layer is simply the difference between prediction and target. Larger errors produce larger gradients. This elegant form is not a coincidence: the cross-entropy loss was designed to pair with softmax precisely because the combination yields this clean gradient formula. This pairing is sometimes called the "softmax-cross-entropy trick," and it avoids numerical instability that would arise from computing the softmax and cross-entropy gradients separately.
Gradient through the second layer. Using the matrix multiply gradient rules:
Gradient through the activation function. Passing through :
where is the elementwise derivative of the activation function evaluated at the stored pre-activation values.
Gradient through the first layer:
The pattern is clear: at each layer, we multiply the upstream gradient by the transpose of each weight matrix to flow backward through layers, then multiply elementwise by the activation derivative at the nonlinearity, then pass the result further back. The upstream gradient arrives from the right (closer to the loss), we compute local contributions for this layer's parameters, and we send a modified gradient leftward to the previous layer.