Welcome back. In our last lesson, we developed the tools to find the gradient of a scalar function, like a loss function, with respect to its vector or matrix parameters. We used a coordinate-free differential approach, which allowed us to handle complex expressions elegantly. This gave us the derivative at the very end of a computational graph.
Today, we will generalize this concept to handle the intermediate stages. Most layers in a neural network are vector-to-vector transformations. The derivative of such a function is no longer a vector (the gradient) but a matrix, which we call the Jacobian. We will then introduce the vector chain rule, showing how the Jacobians of individual layers can be multiplied together to find the gradient of the entire network. This is the mathematical foundation of backpropagation, the engine that powers deep learning.
1. From Gradients to Jacobians
In the previous lesson, we considered functions that map a vector to a scalar, . The derivative, the gradient , was a vector containing the partial derivatives .
Now, consider a function that maps a vector to another vector, , where . We have output components, each a scalar function of the input components:
To capture how the entire output vector changes with respect to the input vector , we need to compute all possible partial derivatives, . The Jacobian matrix is the object that neatly organizes these partials.
[PDF] Matrix Calculus You Need For Deep Learning - arXiv
This paper provides a rigorous yet practical introduction. We'll use it to formalize the definition of the Jacobian.
Please read the section titled "Matrix calculus" from the beginning. You can start at the introduction and continue through the general definition up to the end of the large matrix equation defining the Jacobian, ending with this sentence. Focus on how the Jacobian is constructed by stacking the gradients of each scalar output function f_i(x). We will adhere to the numerator layout convention presented here, where the Jacobian has shape m \times n.
As defined in the paper, for a function with , the Jacobian matrix is an matrix where the entry at row and column is .
Notice that the -th row of the Jacobian is simply the gradient of the scalar function with respect to , . So, the Jacobian is a stack of gradient vectors.
Example: The Jacobian of a Linear Layer
Consider a fundamental operation in neural networks: an affine transformation , where and . This function maps to . The -th component of is:
Let's find the partial derivative for some arbitrary index .
The term is 1 if and 0 otherwise. Thus, the only term that survives the sum is when .
This means the entry in the -th row and -th column of the Jacobian is just the matrix element . Therefore, the Jacobian of the function is simply the matrix .
This is a beautifully simple and important result. The Jacobian of a linear transformation is the transformation matrix itself.
Example: The Jacobian of an Element-wise Function
What about activation functions, like ReLU or sigmoid, which are applied element-wise? Let where for some scalar function .
Since only depends on , this partial derivative is zero if . If , it's simply the derivative of evaluated at , which we denote .
This means the Jacobian is a diagonal matrix:
This diagonal structure is a key reason for the computational efficiency of backpropagation in modern deep learning frameworks. We rarely need to materialize the full Jacobian; we only need to know its effect, which for element-wise operations is just an element-wise multiplication.
2. The Vector Chain Rule
Neural networks are deeply nested composite functions. A 3-layer MLP is of the form . To find the derivative of the entire composition, we need a chain rule that works for vector-valued functions and their Jacobians.
This video provides an excellent, intuitive derivation of the vector chain rule, building up from a multi-input, single-output case to the general matrix form.
First, watch from the start of the derivation to understand the "trace every pathway" intuition. For an output W and input R, a change in R affects W through all intermediate variables (X, Y, Z), and the total change is the sum of the changes along each path. Then, watch the critical segment from here, where the mess of summed partials is elegantly refactored into a product of Jacobian matrices. This is the main takeaway. Finally, see how this generalizes to compositions of multiple functions from this point on.
The key insight from the video is that the chain rule for vector functions is a direct generalization of the scalar case. If we have a composition of functions and , the chain rule allows us to find the derivative of with respect to by multiplying their Jacobians:
Let's check the dimensions. If and , then:
- is an Jacobian.
- is a Jacobian.
- The product is a matrix multiplication, resulting in a matrix, which is the correct dimension for .
The order of multiplication is crucial and works "backwards" from the final output. For a deep neural network , the Jacobian is the product of the Jacobians of each layer, starting from the last one.

3. Backpropagation: The Chain Rule on a Computational Graph
While the chain rule tells us what to compute (a product of Jacobians), backpropagation is an efficient algorithm for how to compute it on a computational graph.
At its core, backpropagation is about calculating the gradient of a final scalar loss, , with respect to every parameter and intermediate variable in the network. For any variable in the graph, we want to compute .
Let's consider a single "gate" or operation in the graph that computes . During the forward pass, we compute and store the value of . During the backward pass, we assume we have already received the gradient from the subsequent parts of the network. This is the upstream gradient, . Our goal is to compute the downstream gradients, and .
The chain rule gives us the answer directly:
Here, is the upstream gradient (which is a row vector since L is a scalar), and is the local Jacobian of the gate. The downstream gradient is found by multiplying the upstream gradient by the local Jacobian.

This process starts at the very end of the network, where the gradient of the loss with respect to itself, , is simply the scalar 1. We then recursively apply this rule, moving backward through the graph, gate by gate, until we have the gradients for all parameters.
CS231n Winter 2016: Lecture 4: Backpropagation, Neural Networks 1
Andrej Karpathy's classic CS231n lecture provides a definitive explanation of this process.
First, watch from this segment to understand the local/global gradient passing mechanism. Each 'gate' in the computational graph receives an upstream (global) gradient and multiplies it by its own local gradient to pass downstream. Then, watch the crucial discussion on vector gradients from this point. Karpathy explains that while the derivatives are technically Jacobian matrices, we almost never form them explicitly because they are often very large and sparse (e.g., diagonal for element-wise operations like ReLU). This practical insight is key to understanding how autograd systems are implemented efficiently.
As Karpathy emphasizes, although the theory involves Jacobian-matrix products, in practice, the backward pass for most common operations (element-wise functions, matrix multiplication, summation) can be implemented much more efficiently without ever constructing the full, dense Jacobian matrix. This is how libraries like PyTorch and TensorFlow achieve their performance.
Conclusion
In this lesson, we have built the complete mathematical machinery for automatic differentiation.
Key Takeaways:
- The Jacobian matrix is the generalization of the gradient for vector-valued functions , with dimensions .
- For an affine layer , the Jacobian is simply . For an element-wise activation function, the Jacobian is a diagonal matrix.
- The vector chain rule expresses the Jacobian of a composite function as the product of the Jacobians of its constituent functions: .
- Backpropagation is a practical algorithm that applies the chain rule recursively on a computational graph. At each node, it computes downstream gradients by multiplying the upstream gradient by the local Jacobian.
We are now equipped to derive the gradients for any part of a neural network. In our next lesson, we will put this into practice by deriving the gradients for the most common loss functions in deep learning: Mean Squared Error (for regression) and Cross-Entropy (for classification). We'll see how combining the Jacobian of the final activation function with the gradient of the loss often leads to a surprisingly simple and intuitive result.