Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Jacobians and the Multivariate Chain Rule

Recommended resources

In our previous lesson, we established how to compute derivatives of scalar-valued functions using the gradient and the Hessian. We saw that the gradient points in the direction of steepest ascent, and the Hessian describes the function's local curvature. These are fundamental tools for optimisation. Now, we will extend these concepts to functions that output vectors, not just single scalars.

This lesson will introduce you to the Jacobian matrix, the generalization of the derivative for vector-valued functions. We will then learn how to apply the multivariate chain rule, a powerful technique for differentiating compositions of such functions. Mastering this is crucial because many advanced machine learning models, most notably neural networks, are built by chaining together multiple vector-to-vector transformations. Understanding how to differentiate through this chain is the key to training them.

1. The Jacobian Matrix: Derivative of a Vector Function

In the last lesson, we took the derivative of a function and got a vector (the gradient). What happens if our function maps a vector to another vector, for example, ? How can we describe its "derivative"?

The derivative in this case should represent the best linear approximation of the function at a point. For a function mapping , this linear approximation is described by an matrix. This matrix is called the Jacobian.

The following video from Khan Academy provides an excellent geometric intuition for what the Jacobian represents. It shows how the transformation acts on a small neighborhood around a point and how the Jacobian matrix captures this local, linear behavior.

The Jacobian matrix

This video builds the Jacobian matrix from its geometric interpretation as a local linear approximation of a vector-valued function.

Please watch the video from the beginning to end (The Jacobian matrix). Pay close attention to how the columns of the Jacobian matrix are formed by observing how the output vector changes in response to small steps along each input axis.

As the video explains, the Jacobian matrix (often denoted as or ) contains all the first-order partial derivatives of the function . If has component functions and the input vector is , the Jacobian is constructed by stacking the gradients of each component function.

The following reading provides the formal definition.

The Matrix Calculus You Need For Deep Learning

This article section formally introduces the Jacobian as a stack of gradient vectors.

Please read the section titled "Matrix calculus". Note how the Jacobian is built by taking the gradient of each output function (f_1, f_2, etc.) with respect to the input vector and stacking these gradients as rows. This article uses the "numerator layout," which we will adhere to in this course.

So, the Jacobian matrix of at a point is defined as:

Each row of the Jacobian is the gradient (transposed into a row vector) of one of the output components of . The entry at row , column is , telling us how the -th output changes with respect to the -th input.

Example Calculation

Let's compute the Jacobian for the function defined by:

  1. Find the gradient of the first component function, :

  2. Find the gradient of the second component function, :

  3. Assemble the Jacobian by stacking the transposed gradients:

This is a matrix, as expected for a function from to .


2. The Multivariate Chain Rule

The true power of the Jacobian becomes apparent when we differentiate composite functions. Many complex systems, including neural networks, are built by chaining simpler functions together. For example, if we have and , the final output is a function of the initial input through the composition .

The multivariate chain rule states that the Jacobian of the composite function is the matrix product of the individual Jacobians.

The Chain Rule of Calculus - Eli Bendersky's website

This section from Eli Bendersky's website clearly states the general formula for the multivariate chain rule.

Read the section titled "Multivariate chain rule - general formulation". Pay close attention to the core formula: D(f \circ g)(a)=Df(g(a)) \cdot Dg(a). Notice that it's a matrix multiplication, and the Jacobian of the outer function f is evaluated at the output of the inner function, g(a).

The rule is:

This is a beautiful analogy to the single-variable case , but with scalar derivatives replaced by Jacobian matrices and multiplication replaced by matrix multiplication. The order matters!

Let's see this in action with a concrete example.

The Chain Rule of Calculus - Eli Bendersky's website

This section provides a detailed, step-by-step example of applying the chain rule.

Please read the entire section "Multivariate chain rule - examples". I encourage you to pause and perform the calculations yourself as you read. The example computes the rate of change of temperature along a parametric curve, which is a classic application.

Let's recap the example from the reading. We have a temperature field and a path through this field given by . We want to find how temperature changes with respect to time, . This is the derivative of the composition .

  • Function takes time to a position in space. Its Jacobian is a matrix (a column vector).
  • Function takes a position to a scalar temperature. Its Jacobian is a matrix (a row vector, which is just the transposed gradient ).

Applying the chain rule, , we multiply a matrix by a matrix, resulting in a matrix—a scalar, which is exactly what we expect for .


3. Application: The Essence of Backpropagation

The multivariate chain rule is not just a mathematical curiosity; it is the engine that powers the training of virtually all modern neural networks. A deep neural network is nothing more than a long chain of composed functions.

A visual representation of the multivariate chain rule applied to a sequence of functions. The derivative of the overall function F'(x) is the product of the Jacobian matrices of each function in the chain.

Imagine the diagram above represents a simple neural network.

  • is the input data.
  • , , and are the outputs of successive layers (activations).
  • is the final output, from which we compute a scalar loss, .

Training this network means finding the gradient of the loss with respect to the parameters within each transformation (e.g., in the function that maps to ). To find , we apply the chain rule:

Each term is a Jacobian matrix. The famous backpropagation algorithm is simply a computationally efficient procedure for calculating this long product of Jacobians, moving backward from the final loss to the earliest parameters in the network. We will revisit this in detail when we study neural networks, but now you understand its mathematical heart.


Conclusion

In this lesson, we have moved from scalar- to vector-valued functions and equipped ourselves with the necessary calculus tools.

Here are the key takeaways:

  • The Jacobian matrix is the generalization of the derivative for vector-valued functions. Its entries are all the first-order partial derivatives, capturing how each output component changes with respect to each input component.
  • Geometrically, the Jacobian defines the best local linear approximation of a vector function at a specific point.
  • The multivariate chain rule provides a way to differentiate compositions of functions by multiplying their Jacobian matrices in the correct order.
  • This rule is the mathematical foundation for the backpropagation algorithm, which is essential for training deep neural networks.

In the next lesson, we will focus on computing derivatives for specific types of matrix expressions that appear constantly in linear models, such as and . This will give you a practical "cookbook" for many common derivations in statistical machine learning.