Backpropagation › Jacobian Matrices and Gradient Flow
Language AI HandbookAll chapters Backpropagation › Jacobian Matrices and Gradient FlowOne section · Sections in this chapter5 / 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
Before diving into the full backward pass, it's worth understanding Jacobian matrices carefully, because they formalize how gradients flow through vector-valued operations.
For a function , the Jacobian is the matrix of all partial derivatives:
Entry tells us how much the -th output changes when we perturb the -th input.
In neural network backpropagation, the loss is a scalar, and each operation in the network transforms a vector to another vector. At each node, we don't technically need the full Jacobian. We only need the vector-Jacobian product (VJP), which maps the upstream gradient vector to a downstream gradient vector.
To see why the full Jacobian is unnecessary, consider a layer that maps . The full Jacobian would be a matrix of 262,144 entries. But we only need to propagate a gradient vector of length 512 backward. The VJP computes this directly in time, the same cost as forming the Jacobian, but without ever explicitly materializing it. For larger layers, the savings are more dramatic, and specialized operations like batch normalization have structured Jacobians that admit even faster VJP computations.
Vector-Jacobian Products (VJPs)
In practice, backpropagation never explicitly forms full Jacobian matrices. Instead, it computes vector-Jacobian products. If the upstream gradient is a row vector (with the same shape as the output), the downstream gradient is:
This produces a row vector with the same shape as the input. The key insight is that computing this product is much cheaper than computing explicitly, then multiplying. PyTorch's autograd system is built entirely around this VJP abstraction.
This VJP perspective also clarifies what backpropagation is computing at each step. The upstream gradient represents "how the loss changes when the output of this node changes." The Jacobian represents "how the output of this node changes when the input changes." Their product gives "how the loss changes when the input changes," which is exactly the downstream gradient we need to propagate.
Local Gradients for Common Operations
The power of computational graphs is that each operation only needs to define its own local Jacobian (or VJP). The full gradient is then assembled by chaining these local gradients via the multivariate chain rule.
Here are the local gradients for the key operations in a neural network:
Addition: . The Jacobian with respect to each input is the identity matrix . Gradients pass through unchanged: and . This is why residual connections are so effective: the skip path provides a direct gradient highway back to earlier layers. The gradient flows through without any attenuation from the addition node itself.
Multiplication by scalar : . The gradient with respect to is (scale up or down), and the gradient with respect to is .
Matrix multiplication: . The Jacobian with respect to is , giving . The gradient with respect to is the outer product . Notice the transpose: gradients flow backward through the weight matrix transposed, which is why the backward pass through a linear layer involves while the forward pass involves .
Elementwise nonlinearity: . The Jacobian is a diagonal matrix with on the diagonal, so , where denotes elementwise multiplication. Because the Jacobian is diagonal, we never form a full matrix; we just do an elementwise multiply.