SGD and Backpropagation for Neural Networks
Backpropagation calculates the gradient of a loss with respect to network weights for an example.
Try it: Backpropagation, Step by Step
A forward pass through a 2-2-1 network, then the backward pass that applies the chain rule node by node to get for every weight and bias, and one gradient-descent update that changes the loss.
How it works
- Forward: compute each hidden pre-activation and activation, then the sigmoid output y and the loss.
- Backward from the loss: for squared error, or for binary cross-entropy.
- Output weights: ; the error sent back to is .
- Hidden units: multiply by the activation's local derivative, then .
- Update every parameter and run the forward pass again to see the new loss.
Default run (12 steps): Inputs x = (1, 0.5), target t = 1. Hidden activation sigmoid, output sigmoid, loss: squared error L = (y - t)2. First the forward pass. … New forward pass with the updated weights: y = 0.6086, loss 0.203 -> 0.1532 (lower: the step moved downhill).
Simplified: One training example, two inputs, two hidden units, one output unit. Binary cross-entropy is evaluated from the output logit (same value, no log(0)). Toy dimensions, not a full real model.
Loading the simulation…
The Two-Direction Calculation
Backpropagation calculates the gradient of a loss with respect to a neural network's weights for one example (x, y). It does this by evaluating the network in two directions. The forward pass moves from the input toward the output and records the quantities produced by each layer. The backward pass starts at the output and moves toward earlier layers, combining the information needed to compute derivatives.
The forward pass is necessary because the backward calculation uses forward quantities, including each layer's pre-activation and output. The backward pass is necessary because it carries loss sensitivity from the final layer toward earlier layers.
Forward Pass State
Think of the network as a sequence of composed functions. The input is the initial output, written as o_0 = x. At layer t, the network first forms a pre-activation vector by multiplying the layer's weight matrix by the previous output: a_t = W_(t-1)o_(t-1). It then applies the activation function to produce o_t = σ(a_t). The forward pass repeats this process from the input toward the final output.
Tracing One Layer's Forward Quantities
Identify the quantities that must be available when the layer connecting o_(t-1) to o_t is evaluated.
- 1Receive the previous output
The layer receives o_(t-1), which was produced by the preceding layer.
- 2Form the pre-activation
The layer computes a_t = W_(t-1)o_(t-1).
- 3Apply the activation
The layer computes o_t = σ(a_t).
- 4Retain the forward state
The pre-activation a_t and output o_(t-1) remain relevant to the later backward calculation.
The forward pass produces the intermediate values that the backward pass needs, not only the final prediction.
Backward Error Signals
The backward pass begins at the final layer. For the squared loss used in the source material, the final error vector is δ_T = o_T - y. Earlier error vectors are then computed by combining the next layer's weights, the next layer's error vector, and the derivative of the next layer's activation. This recursion moves the loss information toward the first layer.
The chain rule explains why an early weight cannot be differentiated by looking only at its immediate layer. A change in that weight changes the next layer's output, which changes later outputs, which changes the loss. The derivative of the full network is therefore assembled from the derivatives of these successive transformations, in the appropriate order.
Jacobian View
A Jacobian organizes partial derivatives by output component and input variable. Its shape records how many outputs depend on how many inputs. This makes it useful for describing the derivative of a layer whose input and output are vectors rather than single numbers.
Reading Two Jacobian Cases
Compare the Jacobian description for a linear function with the Jacobian description for an element-wise sigmoid transformation.
- 1Linear function
For f(w) = Aw, the Jacobian is A itself.
- 2Element-wise sigmoid
For an element-wise sigmoid transformation, the Jacobian is diagonal.
- 3Interpret the diagonal
The diagonal entries contain the individual sigmoid derivatives.
- 4Interpret the off-diagonal entries
The off-diagonal entries are zero for this element-wise transformation.
The Jacobian is not merely a generic matrix label: its shape and entries describe which output components respond to which input variables.
An Edge Gradient
Consider the weights connecting layer t - 1 to layer t. The layer computes a_t = W_(t-1)o_(t-1), followed by o_t = σ(a_t). When differentiating with respect to the weights in this particular matrix, the earlier output o_(t-1) is treated as fixed. The derivative for an edge is then assembled from three local pieces: the error at the destination, the activation derivative at the destination, and the output at the source.
This factorization is a debugging aid. If the source output, destination activation derivative, and relevant error vector are correct, inspect the final assembly of the edge derivative rather than immediately blaming the forward pass or the recursive backward calculation.
Gradient Discrepancy Diagnosis
A gradient mismatch should be investigated as a sequence of checkpoints. First inspect the forward pre-activations a_t and outputs o_t. Then inspect the final error δ_T and the recursively computed earlier error vectors δ_t. Finally inspect the derivative assembled for each edge. This order follows the dependency structure of backpropagation and helps distinguish a forward error from a backward error or an edge-calculation error.
Common mistake Checking only the final gradient value
What it looks likeA computed edge gradient differs from the expected value, so the entire backpropagation procedure is treated as incorrect.
Why it failsThe gradient depends on forward pre-activations, forward outputs, error vectors, and the final edge assembly.
Do this insteadInspect the checkpoints in dependency order: a_t, o_t, δ_T, earlier δ_t values, and then the edge derivative.
Common mistake Ignoring stored forward quantities
What it looks likeThe backward calculation is treated as if it needs only the final prediction.
Why it failsThe edge derivative needs the source output and the destination pre-activation.
Do this insteadRetain the forward values needed by the later backward calculation.
Common mistake Treating an early weight as affecting only its immediate layer
What it looks likeThe derivative of an early weight is computed without accounting for later layers.
Why it failsA change in an early weight propagates through successive layer transformations before affecting the loss.
Do this insteadUse the chain rule to combine the local derivatives across the composed functions.
SGD Connection
Backpropagation supplies the gradient of the loss with respect to the network's weights for one example. That gradient is the information an SGD procedure uses when working with the weights. The key boundary to remember is that backpropagation is the gradient-calculation procedure: it traces how the loss depends on the weights and produces the corresponding derivatives.
Practice Check
A gradient for an early-layer edge is incorrect. The forward pre-activations and outputs are correct, and the final error vector is correct. The earlier error vectors are also correct. Which part should you inspect next, and which three quantities should you verify before inspecting the final assembly?
Hints
- Use the dependency order of the backpropagation checkpoints.
- Focus on the local calculation for the edge's destination and source.
What do you think happens?
Which checkpoint is the most likely next place to inspect?
Reveal answer
The source output, destination activation derivative, and relevant error vector
Once the forward quantities and recursively computed error vectors are correct, the remaining issue may be in the local edge-gradient inputs or in how the edge derivative was assembled.
Key Takeaways
- 1The forward pass computes each layer's pre-activation and output, while the backward pass computes error vectors and partial derivatives.
- 2The chain rule connects local derivatives across the successive functions that compose a neural network.
- 3A Jacobian organizes partial derivatives by output component and input variable; its shape records the relationship between the numbers of outputs and inputs.
- 4An edge gradient depends on the destination error, the destination activation derivative, and the source output.
- 5When a gradient is unexpected, inspect forward quantities, error vectors, and final edge-gradient assembly as separate checkpoints.
At a glance
- Backpropagation calculates the gradient of a loss with respect to network weights for one example.
- The forward pass creates the intermediate values that the backward pass needs.
- The backward pass applies the chain rule from the final layer toward earlier layers.
- Jacobians organize the partial derivatives of vector-valued layer transformations.
- Gradient debugging should separate forward-state errors, backward-error errors, and final edge-gradient assembly errors.