Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to your next lesson on the mathematical foundations of AI.
Introduction
In our last lesson, we defined the gradient, , and established its geometric meaning as the direction of a function's steepest ascent. This is a crucial tool for optimization. However, the functions we optimize in machine learning, such as the cost function of a neural network, are rarely simple. They are almost always composite functions, where the output of one function becomes the input to another.
For example, a single neuron might compute a linear combination of its inputs, , and then pass this result through a non-linear activation function, . To optimize the weights using gradient descent, we need to find the gradient of the final loss with respect to , which requires us to 'look through' the composition of functions.
Today, we will learn how to do this by deriving and applying the chain rule for composite functions in multiple variables. This powerful rule is the fundamental mechanism behind the backpropagation algorithm, which is used to train virtually all modern neural networks. You will see how the component-wise intuition from single-variable calculus extends to a far more elegant and powerful formulation using the linear algebra you've already mastered.
The Chain Rule: From Components to Matrices
Let's start with an intuitive picture. Imagine a function , where the intermediate variables and are themselves functions of other variables, say and . So, and . The final output is therefore a composite function of and .
How does a small change in affect the final output ? The change propagates along all possible paths in the dependency graph.

This dependency suggests that the total change in with respect to is the sum of the changes propagated through and . This gives us the component-wise form of the chain rule:
And similarly for :
This form is useful, but it can become incredibly cumbersome with more variables and deeper compositions. Your background in data structures and algorithms might lead you to appreciate that we need a more scalable and systematic representation. This is where linear algebra comes in.
The Derivative as a Linear Map
To generalize the chain rule elegantly, we must first upgrade our view of the derivative. For multivariable functions, the derivative at a point is not just a number (a slope) but the best linear approximation of the function's change near that point. This linear approximation is a linear map, which can be represented by a matrix.
This matrix of all first-order partial derivatives has a special name: the Jacobian matrix.
A note on the multivariable chain rule
To formalize the chain rule, we first need to reinterpret the derivative. This note from the Stanford AI Lab explains the concept of the derivative as a linear map and defines the Jacobian matrix, which is the matrix representation of this map.
Please read the sections 'The derivative as a linear map' and 'Jacobian and gradient'. Pay close attention to how the total derivative is defined as a linear map and how the Jacobian matrix emerges as its numerical implementation. Note the connection between the gradient (which we know) and the Jacobian of a scalar-valued function.
As the reading explains:
- For a vector-valued function , its derivative is the Jacobian matrix , an matrix of partial derivatives.
- For a scalar-valued function , its Jacobian is a row vector. This is simply the transpose of the gradient vector we studied in the last lesson: .
The Chain Rule as Matrix Multiplication: The Formal Proof
With this new perspective, the chain rule becomes a beautifully simple statement: the derivative of a composition of functions is the composition of their derivatives. Since the derivatives are linear maps (matrices), their composition is simply matrix multiplication.

Now, let's go through the formal derivation. Your preference for mathematical rigor will be satisfied here. The following document proves this theorem concisely by composing the linear approximations of the functions.
Now, let's walk through a concise and formal derivation of the chain rule. This document from UC Berkeley uses the idea of linear approximation and o() notation to show how composing functions leads directly to multiplying their derivative matrices (Jacobians).
Read the entire short document. Section 1 formalizes the linear approximation for multivariable functions. Pay close attention to how f and g are approximated by matrices Df and Dg (which are Jacobians). The key result is on page 2, where composing the approximations leads to the matrix product Df◦g = DfDg. The conceptual summary at the end ties it all together.
Let's connect this back to our initial example: where .
- The function is scalar-valued. Its derivative matrix is its Jacobian, a matrix:
- The function is vector-valued. Its derivative matrix is its Jacobian, a matrix:
The chain rule states that the derivative of the composite function, , is the product of these matrices:
Performing the matrix multiplication gives:
The resulting matrix contains the partial derivatives of the composite function with respect to and . You can see that this single matrix equation elegantly reproduces the two component-wise formulas we started with.
Test your understanding!
Let's consider a slightly more complex composition relevant to neural networks. Let be a scalar loss, which is a function of a vector . The vector is a function of a vector , so . The vector is a linear function of an input vector , given by , where is an weight matrix. The full composition is .
Our goal is to find the derivative of the loss with respect to the input , which is the Jacobian . Use the chain rule in matrix form to write down an expression for in terms of the Jacobians of , , and the linear transformation.
Show answer
Let . Then the composition is .
Applying the chain rule repeatedly:
Let's look at the dimensions:
- is the Jacobian of , so it is a matrix (a row vector).
- is the Jacobian of , so it is an matrix.
- The function is a linear transformation. Its derivative with respect to is simply the matrix itself, which is .
So, the final product is:
The result is a matrix, which is exactly the correct shape for the Jacobian of the scalar-valued function with respect to the -dimensional input . This systematic, dimension-aware process is the essence of backpropagation.
Conclusion
Today, we have formally derived the multivariable chain rule, a pivotal concept for your goal of understanding the theory behind modern AI.
Key Takeaways:
- The derivative of a multivariable function can be seen as its best linear approximation, represented by the Jacobian matrix.
- The chain rule for composite functions elegantly simplifies to the multiplication of their Jacobian matrices: .
- This matrix formulation is not only more general and powerful than the component-wise approach but is also the fundamental principle that enables the computation of gradients in complex, deeply nested functions like neural networks (i.e., backpropagation).
- The gradient is a special case of the Jacobian for scalar-valued functions, where .
Preview of the Next Lesson:
We've now formally introduced the Jacobian matrix and seen its role in the chain rule. In the next lesson, we will focus exclusively on the Jacobian. We will solidify our understanding by learning how to compute the Jacobian matrix for a vector-valued function and interpret its meaning, preparing us for more advanced topics in optimization and model analysis.