Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Computing the Jacobian Matrix

Hello! Welcome back to your course on the mathematical foundations of AI.

Introduction

In our last lesson, we formally derived the multivariable chain rule. We saw that the key to a clean, scalable formulation was to reinterpret the derivative of a multivariable function not as a single value, but as a linear map represented by a matrix. We named this the Jacobian matrix. You learned that the chain rule then elegantly becomes a product of these matrices, a concept that underpins the backpropagation algorithm.

Today's lesson drills down into the Jacobian itself. While you understand its role, the goal now is to master the mechanics of its construction. We will formally define the structure of the Jacobian and, most importantly, practice the skill this lesson is centered on: computing the Jacobian matrix for any given vector-valued function. We will also explore the different notational conventions you may encounter in academic literature.

This lesson solidifies a core computational skill required for analyzing and optimizing complex models.

From Gradient to Jacobian: A Natural Extension

Let's begin by solidifying the connection between the concepts you've already mastered—the derivative and the gradient—and the Jacobian. The video below provides an excellent conceptual bridge, progressing from simple to more complex functions.

The Jacobian Matrix

This video, by Christopher Lum, uses a 'crawl, walk, run' approach to build up the idea of the Jacobian. It's a great way to see how the Jacobian naturally extends the concepts of the derivative and the gradient.

Please watch from the beginning to timestamp 20:01. The video will first recap derivatives and gradients, which will be a quick review for you. Then, pay close attention as it introduces the Jacobian for a vector-valued function as a 'stack of gradients' and explicitly defines its matrix structure.

As the video explains, we can think of this progression in terms of function mappings:

  1. Derivative: For a function , the derivative is a scalar that tells us the local rate of change.
  2. Gradient: For a function , the gradient is a vector. Each component tells us the rate of change of the scalar output with respect to one of the input variables.
  3. Jacobian: For a function , the Jacobian is a matrix. It captures how each of the output components changes with respect to each of the input components.

Formal Definition and Computation

Let's formalize the structure highlighted in the video. A vector-valued function is essentially a stack of scalar-valued functions, where each function takes an -dimensional vector as input:

The Jacobian matrix of , denoted as or , is the matrix of all first-order partial derivatives.

The entry in the -th row and -th column is given by:

Expanding this out gives the full matrix:

Notice that the -th row of the Jacobian is simply the transpose of the gradient of the -th component function: . This connects back to the "stack of gradients" idea.

For a rigorous, text-based definition and a concise example, please review the following resource.

Vector and Matrix Differentiation

These notes from an MIT course provide a formal, mathematical definition of the Jacobian matrix. This will reinforce the structure we've just discussed.

Read the section titled 'Jacobian' (pages 10-12). It formally defines the Jacobian by stacking gradient vectors and provides a clear computational example. Note the m x n dimensions.

A Concrete Computational Example

Now, let's see the computation in action.

The Jacobian Matrix

Let's return to the video by Christopher Lum to see a worked example of computing a Jacobian matrix from scratch.

Please watch the segment from 23:49 to 27:06. The instructor calculates the Jacobian for a function \mathbf{f}: \mathbb{R}^2 o \mathbb{R}^3, resulting in a 3 imes 2 matrix. Follow along with each partial derivative calculation.

The following image provides three more quick examples, which are excellent for checking your understanding of how the dimensions of the input and output vectors determine the shape of the resulting Jacobian matrix.

Examples of Jacobian Matrix Calculation
This image shows the computation of the Jacobian for three different vector-valued functions, illustrating how the matrix dimensions change based on the number of input variables and output functions.
Test your understanding!

Given your experience with regression models, consider a function that maps a 2D parameter vector to a 3D prediction vector . Let the input data points be fixed at .

The function is defined as:

Compute the Jacobian matrix . What are its dimensions?

Show answer

The function maps from (input ) to (output ). Therefore, the Jacobian will be a matrix. The components of are:

The variables are and . We compute the partial derivatives for each row:

Row 1: Gradient of

Row 2: Gradient of

Row 3: Gradient of

Assembling these into the matrix gives the Jacobian:

This matrix is often called the design matrix in the context of linear regression. Each element tells you how sensitive a specific prediction is to a small change in a parameter .

An Important Note on Notational Conventions

As you delve deeper into AI literature, you will find that not all authors define the Jacobian with the same dimensions. This can be a source of significant confusion. The two main conventions are the Numerator Layout and the Denominator Layout.

Vector and Matrix Differentiation

To prepare you for advanced readings, it is critical to be aware of the different notational conventions for matrix derivatives. This resource explains the two main 'layouts' you will encounter.

Please read the sections 'Motivation for Layouts', 'Numerator Layout', and 'Denominator Layout' (pages 18-27). Focus on understanding how the two layouts arise from different ways of writing the linear approximation equation. Note that our course, and most of the ML community, uses the Numerator Layout.

To summarize this important point:

  • Numerator Layout (what we use): For , the Jacobian is an matrix. The number of rows equals the dimension of the output (numerator), and the number of columns equals the dimension of the input (denominator). The -th row is .
  • Denominator Layout: The Jacobian is an matrix (the transpose of the numerator layout). The -th column is .

Being aware of this distinction is crucial. If you encounter a Jacobian in a paper with unexpected dimensions, it is likely due to the author's choice of layout convention. Always check the author's definitions.

Conclusion

In this lesson, we focused on the practical and definitional aspects of the Jacobian matrix, a cornerstone of multivariate calculus for machine learning.

Key Takeaways:

  • The Jacobian matrix of a function is the matrix of all first-order partial derivatives, where the element .
  • Conceptually, it can be constructed by stacking the transposed gradients of the output component functions, , as its rows.
  • Each element represents the sensitivity of the -th output to a change in the -th input.
  • There are two main notational conventions (numerator and denominator layouts) that result in transposed versions of the Jacobian. Being aware of this is vital for reading academic literature.

Preview of the Next Lesson:
We have now generalized the first derivative from a scalar (derivative) to a vector (gradient) and finally to a matrix (Jacobian). In the next lesson, we will apply the same thinking to the second derivative. You will learn to define and compute the Hessian matrix for a scalar-valued function. The Hessian is fundamental to second-order optimization methods and for classifying the nature of critical points as minima, maxima, or saddle points.

Can't find a good explanation? Sign up and we'll make it for you

Sign up