Hello! Welcome to our next lesson in matrix calculus.
In our last session, we defined the Jacobian matrix as the derivative of a vector-valued function . We saw it as the best linear approximation of the function at a point, with its rows being the transposed gradients of the component functions.
Today, we will build directly on that knowledge to tackle one of the most powerful tools in calculus: the chain rule, but adapted for vector and matrix operations. Our goal is to understand how to find the derivative of a composite function like . This is not just a mathematical curiosity; it is the core mechanism that powers backpropagation, the algorithm used to train virtually all modern deep learning models. Your background in computer science will be particularly helpful here, as we'll see that the chain rule can be beautifully expressed as an algorithm on a graph.
1. From Scalar to Vector Chain Rule
Let's begin by recalling the single-variable chain rule you know from introductory calculus. If you have a composition of functions, say where , the derivative of with respect to is given by:
You find the derivative of the "outer" function with respect to its input, multiply it by the derivative of the "inner" function, and substitute to express the result in terms of .
We can generalize this to vector-valued functions. Suppose we have a composition and , where , , and are vectors. The derivatives of these functions are their Jacobian matrices. The chain rule in this context becomes a product of these matrices.
The vector chain rule is:
This looks identical to the scalar version, but the terms are now Jacobians. Let's check the dimensions to see why this works.
- If , then its Jacobian is a matrix.
- If , then its Jacobian is an matrix.
The product is an matrix multiplication, which results in an matrix. This is exactly the correct size for the Jacobian of the composite function , which maps .
The following article gives a clear explanation of this rule.
The Matrix Calculus You Need For Deep Learning
Read this section from 'The Matrix Calculus You Need For Deep Learning' to see the formal statement of the vector chain rule and how it generalizes the simpler scalar rules.
Please read the subsection titled 'Vector chain rule'. Pay attention to the matrix multiplication structure and how the Jacobian matrices are defined. Don't worry if the discussion of diagonal matrices for element-wise operations isn't immediately obvious; we'll see examples of this later.
2. A Computational View: Backpropagation and Graphs
While the matrix multiplication formula is correct, it can be cumbersome. For the complex, deeply nested functions found in neural networks, a more algorithmic approach is needed. This is where the concept of a computational graph comes in.
A computational graph represents a function as a directed graph where nodes are operations (like addition, matrix multiplication, or an activation function) and edges represent the flow of data.

The process of using the chain rule on such a graph is called backpropagation. It consists of two passes:
- Forward Pass: We feed the input into the graph and compute the value at each node until we get the final output. This is just evaluating the function.
- Backward Pass: We start at the final output and work backward. At each node, we calculate the derivative of the final output with respect to that node's output. The chain rule provides a simple, local recipe for this: multiply the incoming "upstream" gradient by the "local" gradient of the node itself.
The following video brilliantly explains this process.
This video from a University of Michigan course on deep learning provides one of the best intuitions for backpropagation. It starts with a simple scalar example on a graph before generalizing to vectors.
First, watch the segment from 07:57 to 14:17. This walks through a scalar example and introduces the key terms: upstream gradient, local gradient, and downstream gradient. Understanding this simple case is crucial.
As the video shows, the chain rule at each node is:downstream_gradient = upstream_gradient * local_gradient
Now, let's see how this works with vectors, which is the scenario in deep learning.
- The local gradient of a node is its Jacobian matrix.
- The upstream gradient (the derivative of the final scalar loss with respect to the node's vector output) is a vector.
- The multiplication becomes a matrix-vector product.
Let's continue with the same video to see this generalization.
Now, let's see how the same logic extends to vector operations. This part of the video explains how the local gradient becomes a Jacobian matrix and the chain rule becomes a matrix-vector product.
Watch the segment from 38:26 to 47:00. Focus on how the scalar multiplication from the previous example is replaced by a matrix-vector product involving the Jacobian. The example of the ReLU function shows a practical case where the Jacobian is sparse (diagonal), making the computation very efficient.
The video makes a critical point for computational efficiency: in practice, we almost never explicitly construct the full, dense Jacobian matrix. For most operations in neural networks, the Jacobian is very sparse (e.g., diagonal for element-wise operations) or has a regular structure. The backpropagation algorithm implements an efficient implicit Jacobian-vector product without ever forming the matrix itself. This insight is key to making deep learning scalable.
3. A Worked Example
Let's apply these ideas to a concrete problem. Consider a simple linear layer followed by a squared norm, which is a common setup for a loss function.
- Let be an input vector.
- Let be a weight matrix.
- The intermediate variable is , so .
- The final output is a scalar loss .
Our goal is to find the gradient of the loss with respect to the input, .
We can decompose this into a two-step computational graph: .
The chain rule states:
Let's compute each part:
-
"Upstream" Gradient ():
This is the derivative of a scalar with respect to the vector . We found this gradient in a previous lesson:This is a row vector.
-
"Local" Gradient ():
This is the derivative of the vector function with respect to the vector . This is a linear transformation, so its derivative is simply the matrix of the transformation itself:This is an matrix.
Now, we apply the chain rule by multiplying the two results:
The result is a product, giving a row vector, which is the correct shape for the gradient .
Finally, we substitute back in to get the final expression in terms of the input and parameters :
If we want this as a column vector (the gradient, ), we simply take the transpose:
This component-wise approach is a powerful and reliable way to handle complex derivatives.
Test your understanding!
Using the same setup ( and ), find the derivative of the loss with respect to the matrix W, i.e., .
Hint: Start with the chain rule . You'll need the "upstream" gradient from our example. The main challenge is finding the "local" gradient , which is a bit tricky. Think about it component-wise: how does a single element change with respect to a single element ?
Show answer
We want to compute . We use the chain rule again.
The upstream gradient is the same as before: .
The tricky part is the local gradient. Let's look at a single component of the output, . The derivative of with respect to an element of the matrix is:
Using a more advanced form of the chain rule for matrix derivatives (which can be derived from the component-wise version), the gradient is given by:
Let's check the shapes. is and is . Their outer product is an matrix, which is the same shape as , so the dimensions match.
Substituting , we get:
This is a fundamental result used in deriving the update rules for linear regression.
Conclusion
This lesson connected the Jacobian from our last session to the chain rule, providing the machinery to differentiate complex, nested vector functions.
Key Takeaways:
- The chain rule for vector functions, , is a product of Jacobian matrices.
- In deep learning, this process is algorithmically implemented as backpropagation on a computational graph.
- The core idea of backpropagation is a recursive application of the chain rule: each node in the graph computes its "downstream" gradient by multiplying its "local" gradient (Jacobian) with the "upstream" gradient it receives from the next node.
- Efficient implementations use implicit, structured Jacobian-vector products rather than forming dense matrices.
Preview of the Next Lesson:
So far, we have focused exclusively on first-order derivatives (gradients and Jacobians). These are essential for optimization algorithms like gradient descent, as they tell us the direction of steepest ascent. However, to understand the curvature of a function—whether a critical point is a minimum, maximum, or saddle point—we need to look at second-order derivatives. In the next lesson, we will introduce the Hessian matrix, the generalization of the second derivative to multivariate functions, and see how it helps us classify critical points and analyze the convexity of a function.