Backpropagation and Neural Networks
Backpropagation and Automatic Differentiation
Purpose of Backpropagation
Training a neural network requires computing how the loss changes when its parameters change. Backpropagation is an efficient algorithm for computing the gradient of a loss with respect to model parameters. It applies the chain rule repeatedly through the operations that make up the model.
Backpropagation is also an instance of automatic differentiation: once a function is built from differentiable operations, a system can compute its derivatives by applying the local derivative rules for those operations. The same ideas apply to many differentiable computations, not only neural networks.
Loss Functions and Stochastic Gradient Descent
Let θ denote the model parameters. For an example with input xi and label yi, the model produces an output hθ(xi), which is compared with the label using an individual-example loss, such as cross-entropy.
The overall loss is often the average of the losses across a dataset:
where M is the number of examples and ℓ is the loss for one example.
In stochastic gradient descent (SGD), a training step uses a randomly sampled example or a small batch to estimate the gradient. For a sampled example indexed by k, a basic update is
where η is the learning rate. The central computational problem is to evaluate the gradient with respect to all parameters efficiently, even when the model is a large composition of layers and operations.
Differentiable Computations as Circuits
A computation can be represented as a computational graph or circuit. Its nodes are inputs, intermediate values, and operations; its edges show which values each operation uses.
Operations can include:
Arithmetic, such as addition, subtraction, multiplication, and division.
Elementary differentiable functions, such as exponentials, cosine, and sigmoid.
Neural-network operations, such as matrix multiplication and activation functions.
A circuit can reuse an intermediate result in several later computations. A formula, in contrast, would have to expand and repeat that computation each time it is used. This sharing makes the circuit representation useful for describing efficient computations.
The gradient-computation principle
Suppose a differentiable circuit with N operations computes a scalar-valued function f of L inputs:
The gradient has one component for each input,
but all of its components can be computed in time proportional to the circuit size, or O(N), assuming each basic operation takes constant time. Evaluating the function itself also takes O(N) time.
This is the key efficiency result behind backpropagation: computing the gradient of a scalar output can take time on the same order as evaluating the computation that produced it. The gradient can itself be represented through a circuit.
Application to neural networks
A neural network and its loss can be viewed as a differentiable circuit. In this setting:
The circuit inputs of interest are the parameters θ.
The scalar output is usually the loss.
The circuit operations are the layers and other computations used to evaluate the model.
For many commonly used network operations, the number of arithmetic operations required for a forward evaluation is proportional to the number of parameters or other relevant model dimensions. The gradient can therefore be computed with comparable asymptotic cost.
For example, multiplying a D×D matrix by a vector involves D2 parameters and takes O(D2) arithmetic operations. Its backward computations also take time proportional to D2.
Forward and Backward Passes
The forward pass evaluates the model and computes its outputs or loss. The backward pass computes derivatives by moving backward through the computational graph.
Backpropagation works because of the chain rule. Rather than repeatedly differentiating the entire model from scratch, each operation passes a derivative to the earlier values that influenced it. Each step needs only local information about that operation and the derivative arriving from later in the graph.
The Chain Rule as a Backward Transformation
Consider a function that maps an input vector z∈Rm to an intermediate vector u∈Rn, followed by a function that maps u to a scalar J:
Suppose the gradient of the final output with respect to the intermediate vector is known, ∇uJ. The chain rule gives the gradient with respect to the earlier input:
where Jg(z) is the Jacobian matrix of g. Its entry in row j and column i is
The Jacobian has shape n×m, so its transpose has shape m×n. Multiplying it by the n-dimensional gradient ∇uJ produces the m-dimensional gradient ∇zJ.
In component form,
Each input-coordinate derivative is a weighted sum of derivatives with respect to the intermediate coordinates.
Why the chain rule supports efficient computation
To compute ∇zJ, the backward step needs:
The local function g and its derivative at the point z.
The current value of z.
The incoming gradient ∇uJ.
It does not need to know the full internal structure of f. All the effects of computations after u have already been summarized in ∇uJ. This allows a complex computation to be treated as a sequence of local backward steps.
Composing Backward Steps Across Layers
Suppose a computation applies a sequence of modules:
The backward computation proceeds in reverse order:
Compute the local derivative of J with respect to u2.
Use the backward rule for m2 to obtain the derivative with respect to u1.
Use the backward rule for m1 to obtain the derivative with respect to x.
The same procedure works for any number of modules. The modules need not be identical: one may be a matrix multiplication, another an activation function, and another a normalization operation. Each operation uses its own local derivative rule, and the rules are composed by passing gradients backward.
Gradients with Respect to Parameters
Neural-network layers depend on both intermediate activations and parameters, such as weights and biases. A layer can be viewed as a function of both its input and its parameters.
For example, a layer may map an activation z to u using parameters θℓ:
The backward computation can produce gradients with respect to the input, ∇zJ, and with respect to the layer’s parameters, ∇θℓJ. Parameters are differentiated through the same chain-rule principle as activations: the layer’s output depends on both, so the incoming gradient is propagated to each relevant input.
A useful way to understand the computation is:
Propagate gradients through activations. Starting from the loss, compute derivatives with respect to intermediate values as the calculation is traversed backward.
Compute parameter gradients. At each layer, use the incoming gradient and the layer’s local dependence on its parameters to calculate derivatives with respect to those parameters.
These computations can be interleaved. Some parameter gradients can be calculated as soon as the needed activation and incoming gradient are available; they do not all have to wait until the gradient has reached the network’s original input.
Affine Layers: Matrix Multiplication and Bias
Consider an affine transformation
where z∈Rm, W∈Rn×m, and b∈Rn. Let the incoming gradient be
Gradient with respect to the input
The Jacobian of u with respect to z is W. Therefore,
The backward rule for the input is multiplication by the transpose of the forward weight matrix.
Gradient with respect to the weights
For an individual weight Wij,
The full weight-gradient matrix is therefore the outer product
The derivative with respect to each weight combines two quantities: the gradient arriving at the weight’s output-side unit and the activation entering on its input side.
Gradient with respect to the bias
Since ui depends on bi with derivative 1,
Rank-one property for a single example
For a single example, the weight gradient δzT is an outer product, so it has rank at most one. This property can be useful in methods that exploit the structure of individual-example gradients.
When gradients are averaged across multiple examples, the result is generally a sum of outer products and need not be rank one.
Elementwise Activation Functions
Suppose an activation function is applied independently to each coordinate:
where σ is a differentiable scalar function applied elementwise. Because output coordinate ui depends only on input coordinate zi, the Jacobian is diagonal. Its diagonal entries are σ′(zi).
Given δ=∇uJ, the backward rule is
where ⊙ denotes elementwise multiplication. Thus, each incoming gradient is multiplied by the derivative of the activation at the corresponding input value.
The Jacobian does not need to be explicitly constructed: the elementwise products compute its effect directly. Common activation functions have different forms, but their backward computations follow this same principle.
Computational Cost of the Backward Pass
For an affine layer with W∈Rn×m:
The forward matrix-vector product costs O(nm).
Computing the input gradient WTδ costs O(nm).
Computing the weight gradient δzT requires producing nm entries and costs O(nm).
For an elementwise activation of dimension m:
The forward computation takes O(m) time.
The backward computation also takes O(m) time.
Consequently, the backward pass for these operations has the same asymptotic cost as the forward pass. Its exact runtime and memory use depend on the operations and implementation, but it remains on the same order for standard layers.
Computational Graphs, Memory, and Scheduling
The graph structure determines which intermediate values and gradients depend on one another. This affects how computations can be scheduled and how memory can be used.
Some calculations can proceed independently once their required values are available, allowing parallel execution.
Intermediate activations may need to be retained because later backward computations use them—for example, the weight gradient for an affine layer needs its input z.
Once a stored value is no longer needed, it can be discarded to free memory.
In multi-processor or multi-GPU systems, the dependency graph helps determine when values must be communicated.
Thus, backpropagation is not only a rule for calculating derivatives; the dependencies in its computational graph also shape practical decisions about execution, parallelism, and memory.
Applications Beyond Ordinary First-Order Training
Automatic differentiation can be applied to computations that themselves contain gradient calculations or optimization steps, as long as the resulting computation is differentiable.
Differentiating through an optimization step
Suppose one gradient-descent step defines updated parameters
An outer objective can evaluate the loss after this update, such as L(θ′). This outer objective is a function of the original θ because θ′ depends on θ. Automatic differentiation can differentiate through the update rule and compute the outer gradient.
The same idea extends to running an algorithm for multiple steps and then measuring its final result. The sequence of updates becomes part of the computational graph. This is useful in settings such as meta-learning, where an algorithm or training procedure is itself treated as something to optimize.
Hessian-vector products
Let f(x) be a scalar function and let v be a fixed vector. Define
Differentiating g gives
where ∇2f(x) is the Hessian matrix. This computes a Hessian-vector product without constructing the entire Hessian.
The product can be computed efficiently by differentiating through a computation for g. The full Hessian may be too large to form explicitly, but its product with a vector can be practical. Such computations support some second-order methods and other advanced optimization procedures, although many commonly used optimizers do not require them.
Higher-order differentiation
The same principle can be applied repeatedly: if a derivative computation is itself expressed as a differentiable program, automatic differentiation can differentiate through it. This makes some higher-order derivatives accessible without explicitly building every large derivative matrix.
Key Takeaways
Backpropagation computes gradients by repeatedly applying local chain-rule transformations in reverse through a computational graph.
A scalar-valued differentiable circuit with N operations can have its gradient computed in time proportional to N.
The gradient passed backward summarizes the effect of all later computations, so each local step needs only its own operation and the incoming gradient.
For an affine layer u=Wz+b, the backward rules are ∇zJ=WT∇uJ, ∇WJ=(∇uJ)zT, and ∇bJ=∇uJ.
For an elementwise activation u=σ(z), the backward rule is ∇zJ=σ′(z)⊙∇uJ.
Standard layers have backward computations with the same asymptotic cost as their forward computations.
Because gradient calculations can themselves be differentiated, automatic differentiation also supports computations such as gradients through optimization steps and Hessian-vector products.

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