Backpropagation in Neural Networks
Backpropagation and Computation Graphs
Why gradients are needed
Training a model with stochastic gradient descent (SGD) requires the gradient of its loss with respect to each trainable parameter. For a neural network, these parameters include the weight matrices in its layers and the final output weights.
A network computes its prediction through a sequence of operations: it transforms an input feature vector with weight matrices, applies activation functions, and eventually produces a score or prediction. The loss compares that prediction with the target. Although the gradient of the loss with respect to each parameter can be found by repeatedly applying calculus rules, doing so by hand becomes tedious as networks grow more complex.
Backpropagation automates this gradient calculation. It works by representing a mathematical expression as a computation graph and applying the chain rule systematically.
Computation graphs
A computation graph is a directed acyclic graph (DAG) that represents how a mathematical expression is built from smaller operations.
Input nodes represent values such as features, targets, and parameters.
Operation nodes represent functions such as addition, multiplication, or an activation.
Intermediate nodes hold the results of subexpressions.
The output node represents the final expression, such as a loss.
For example, the expression c=a+b can be represented by a graph in which a and b flow into an addition operation, producing c. A more complex model can be represented by connecting many such operations together.
Computation graphs are useful for two related reasons:
They allow software to calculate gradients automatically. Deep-learning systems use this general approach to differentiate models.
They make the structure of a calculation visible, helping explain how each part contributes to the final gradient.
A graph also reveals shared subexpressions. If multiple computations depend on the same intermediate value, their gradient contributions can be combined rather than recomputed independently.
Local derivatives as edge values
Each operation in a computation graph has a local derivative: the derivative of its output with respect to one of its inputs. This derivative describes how a small change in that input changes the output.
For example, if c=a+b, increasing a by a small amount ϵ increases c by the same amount. Thus, ∂a∂c=1. The same reasoning gives ∂b∂c=1.
The local derivatives for common operations are:
Addition: c=a+b
∂a∂c=1
∂b∂c=1
Subtraction: c=a−b
∂a∂c=1
∂b∂c=−1
Increasing the subtracted input b decreases the output, which accounts for the negative derivative.
Multiplication: c=ab
∂a∂c=b
∂b∂c=a
Each input’s effect is scaled by the other input.
Squaring: c=a2
dadc=2a
This is the power rule for differentiation.
Maximum: c=max(a,b)
Away from the point where a=b:
∂a∂c=1 if a>b, and 0 if a<b.
∂b∂c=1 if b>a, and 0 if b<a.
Only the input that determines the maximum affects the output locally. At a=b, the maximum function is not differentiable in the ordinary sense; an optimization algorithm can use a valid subgradient there.
Logistic sigmoid
The logistic sigmoid is
Its derivative has the convenient form
As a becomes very large or very negative, the sigmoid approaches 1 or 0, respectively. Its derivative then approaches 0, illustrating how an activation can produce a very small gradient in saturated regions.
The chain rule on a graph
When one operation feeds into another, their derivatives combine through the chain rule. In a computation graph, the derivative along a path is the product of the local derivatives on that path.
For example, let
The local derivatives are
Therefore,
Since b=a2, this becomes
Because c=(a2)2=a4, the result agrees with the power rule. This illustrates the central graph-based principle: multiply local derivatives along a path to obtain the derivative through that path.
When a value affects the output through multiple paths, the contributions from those paths must be added.
Example: hinge loss for linear classification
Let ϕ(x) be the feature vector for an input x, let w be a weight vector, and let y be the label. A linear classifier computes the score
The margin is yf(x), and the hinge loss is
The computation proceeds through these intermediate values:
Compute the score w⊤ϕ(x).
Multiply the score by y to obtain the margin.
Subtract the margin from 1.
Take the maximum of that result and 0.
If the margin is greater than 1, the loss is 0 and its gradient is 0. If the margin is less than 1, the loss is active, and the gradient with respect to w is
Together, away from the boundary, this is
where 1{⋅} is 1 when its condition is true and 0 otherwise. At a margin exactly equal to 1, the hinge loss is nondifferentiable, so a subgradient is used.
The graph makes this result systematic: the local derivatives are combined along the path from the loss back to the weights.
Example: a two-layer neural network
Consider a neural network that computes a hidden representation, then a score, then a squared loss. Let x be an input, ϕ(x) its feature vector, V the hidden-layer weight matrix, and w the output weights. Define
Here, σ is applied elementwise, h is the hidden vector, s is the score, and r is the residual.
Gradient with respect to the output weights
The derivative of the squared loss with respect to the residual is 2r. The residual changes one-for-one with the score, and the score’s derivative with respect to w is h. Thus,
This has the same form as the gradient for squared-loss linear regression, with the hidden representation h playing the role of the feature vector.
Gradient with respect to the hidden-layer weights
The gradient flows backward from the loss through the score, the activation, and the matrix multiplication. For the sigmoid, the elementwise derivative is h⊙(1−h), where ⊙ denotes elementwise multiplication. Therefore,
The expression in parentheses is the error signal propagated to the hidden units; its outer product with ϕ(x)⊤ gives the gradient for the matrix V.
The same principles apply to deeper networks. Additional layers add more operations and local derivatives to the paths, but do not change the underlying chain-rule procedure.
Forward and backward passes
Backpropagation calculates gradients in two passes through the computation graph.
Forward pass: evaluate and store values
The forward pass moves from the input nodes toward the output. It computes the value of every intermediate expression and stores those values. They are needed later to evaluate local derivatives.
For a concrete squared-loss example, let
The score, residual, and loss are
The forward pass stores the intermediate values s=5, r=3, and L=9.
Backward pass: propagate derivatives
The backward pass moves from the loss toward the inputs and parameters. For each node, it computes a backward value, also called an adjoint: the derivative of the final loss with respect to the value at that node.
At the loss node,
For L=r2,
Since r=s−y, the derivative of r with respect to s is 1, so
Finally, since s=w⊤x, the gradient with respect to w is
Thus, the backward pass uses both the local derivatives and the stored forward values to calculate the gradient at the parameters.
In a general graph, each node passes gradient contributions to its inputs. If a node’s value contributes to the loss through multiple downstream paths, those contributions are summed. For vector- and matrix-valued operations, the same chain rule applies, with the relevant derivatives represented by Jacobians or equivalent vectorized calculations.
Using gradients to train models
Once backpropagation has computed gradients, SGD uses them to update parameters. For a parameter vector θ, a basic update is
where η is the step size, or learning rate. The update moves parameters in the direction that locally reduces the loss.
For a convex training objective, suitable optimization conditions can provide guarantees that gradient-based methods approach a global minimum. Many linear-model objectives with convex losses have this property. Neural-network training objectives, by contrast, are generally non-convex. They can contain multiple local minima and other difficult regions, so there is generally no guarantee that gradient descent will reach a global minimum.
Despite this theoretical difficulty, neural networks often train successfully in practice. Their optimization can require careful choices, and training behavior may depend strongly on the model and procedure.
Initialization
The starting parameter values can influence where optimization goes in a non-convex loss landscape. A poor initialization may lead to slow progress or an unfavorable solution.
Initializing all weights to zero is generally problematic for a multilayer network: units within a layer can become identical and receive identical updates, preventing them from learning distinct features. Random initialization breaks this symmetry. Pre-training can also be used to produce starting weights, though it is a separate technique.
Overparameterization
Overparameterization means providing a model with more hidden units or parameters than may seem strictly necessary. Some units may contribute little, while others may learn useful patterns. The extra capacity can give optimization more opportunities to find a useful representation, although it does not by itself guarantee successful training.
Adaptive step sizes
A single learning rate applies the same nominal step-size scale to every parameter. Adaptive methods such as AdaGrad and Adam instead adjust step sizes by parameter, using information from the history of gradients. This can improve optimization when different parameters require different update scales.
Vanishing and exploding gradients
A gradient that becomes extremely small is a vanishing gradient. It can produce tiny parameter updates and prevent early layers from learning effectively. A gradient that becomes extremely large is an exploding gradient. It can cause unstable updates, oscillation, or divergence.
Gradient behavior is influenced by weight initialization, the choice of step sizes, activation functions, and network architecture. These choices are made in part to keep gradients at scales that permit learning without causing instability.
Core ideas
A computation graph represents a function as connected elementary operations.
Local derivatives describe how each operation responds to changes in its inputs.
The chain rule combines local derivatives: contributions multiply along paths and add across separate paths.
The forward pass evaluates the function and stores intermediate values; the backward pass uses them to compute gradients.
Backpropagation is not limited to neural networks. It applies broadly to differentiable computations and allows models and loss functions to be changed without deriving every gradient manually.
Automatic differentiation through computation graphs supports a programming approach in which models and objectives can be expressed as compositions of operations, then differentiated automatically.

Ask anything
We've read this whole study set, so questions about it get the best answers.