Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to the next lesson in our journey through the mathematical foundations of AI.
Introduction
In our previous lesson, we defined and computed the Jacobian matrix, which generalizes the first derivative to vector-valued functions. We established that for a function , the Jacobian is an matrix of all first-order partial derivatives.
Today, we will build directly on that foundation to explore second derivatives in a multivariable context. This lesson focuses on defining and computing the Hessian matrix, which is the multivariable analogue of the second derivative for a scalar-valued function. The Hessian is a critical tool in machine learning, forming the bedrock of second-order optimization algorithms (like the Newton-Raphson method) and for analyzing the loss landscape of a model to distinguish between minima, maxima, and saddle points.
Our key insight today will be to understand the Hessian of a scalar function as the Jacobian of its gradient vector .
From Gradient to Hessian
Let's formalize this connection. Consider a scalar-valued function . We know its first derivative is the gradient, , which you'll recall is a vector containing all the first-order partial derivatives:
Notice that the gradient itself is a vector-valued function, mapping from to . From our last lesson, we know that the derivative of a vector-valued function is its Jacobian matrix.
Therefore, the Hessian matrix of , denoted or , is simply the Jacobian of the gradient function .
Second Derivatives, Bilinear Maps, and Hessian Matrices
These lecture notes from MIT's 'Matrix Calculus for Machine Learning' course provide a rigorous definition of the Hessian. Pay close attention to how it's constructed as the derivative of the gradient.
Please read Section 12.1, titled 'Hessian matrices of scalar-valued functions' on page 87. Focus on the formal definition of the Hessian, H, as the derivative of the gradient, (∇f)′, and the explanation for why it is always a symmetric matrix.
As the notes explain, since the Hessian is the Jacobian of the gradient , it will be an square matrix. Its components are all the second-order partial derivatives of :
The full matrix is:
A fundamental property, as mentioned in the reading, is that the Hessian matrix is symmetric, meaning . This is due to the equality of mixed partials (also known as Clairaut's or Schwarz's theorem), which states that for functions with continuous second partial derivatives, the order of differentiation does not matter:
This symmetry property has profound implications in optimization theory and is not just a computational convenience.
Computing the Hessian Matrix
The process for computing the Hessian follows directly from its definition:
- Calculate the gradient vector, .
- Calculate the partial derivative of each component of the gradient with respect to each input variable.
- Arrange these second-order derivatives into an matrix.
Let's watch a clear, worked-out example.
M4ML - Multivariate Calculus - 2.7 The Hessian
This video from Imperial College London demonstrates the step-by-step computation of a Hessian matrix for a function of three variables.
Watch from 01:07 to 03:28. The presenter first computes the gradient (referred to as the Jacobian in this scalar case) and then differentiates its terms again to build the Hessian. Note how the symmetry across the leading diagonal is confirmed in the final result.
Test your understanding!
In machine learning, loss functions are often quadratic in form. Consider the following simple quadratic function , where :
Compute the Hessian matrix for this function.
Show answer
First, we compute the gradient vector .
So, the gradient is .
Now, we compute the Jacobian of the gradient to find the Hessian.
The first row of the Hessian is the gradient of the first component of :
The second row of the Hessian is the gradient of the second component of :
Assembling these into a matrix gives the Hessian:
Notice that the Hessian for this quadratic function is a constant matrix, and it is symmetric, as expected. This means the curvature of this function is the same everywhere.
Geometric Interpretation: Curvature
We can compute the Hessian, but what does it represent geometrically? In single-variable calculus, the second derivative tells us about the function's curvature (concave up or down). The Hessian matrix does the same for multivariable functions.
It describes the local curvature of the function's surface at a given point. However, since we're in dimensions, curvature isn't a single number but a property that can be different in every direction. The Hessian captures all of this directional information.
The following video provides an excellent derivation of the Hessian from the perspective of the "directional second derivative." This is a more profound way to understand its role.
Gradients, Hessians, and All Those Derivative Tests
This video from OptWhiz provides a strong geometric intuition by showing how the Hessian naturally arises when you try to define a second derivative along an arbitrary direction.
Please watch from 09:05 to 12:54. The core idea is that the Hessian matrix H allows you to find the curvature (second derivative) along any direction vector \mathbf{v} (with unit length) via the quadratic form \mathbf{v}^T H \mathbf{v}. This is a fundamental concept for optimization.
The key formula from the video, , is central to the theory of optimization. By analyzing the properties of the matrix (for example, whether this expression is positive for all directions ), we can determine if a point is a local minimum, maximum, or saddle point. We will explore this in detail in a future module.
Conclusion
In this lesson, we generalized the concept of a second derivative to multivariable scalar functions, arriving at the Hessian matrix.
Key Takeaways:
- The Hessian matrix is the second derivative of a scalar-valued function .
- Formally, it is the Jacobian of the gradient, , and is always an square matrix.
- Due to the equality of mixed partials (Clairaut's theorem), the Hessian is always a symmetric matrix ().
- Geometrically, the Hessian describes the local curvature of the function's surface. The quadratic form gives the curvature in the direction of vector .
Preview of the Next Lesson:
We have now defined the gradient (first derivative) and the Hessian (second derivative). Just as the first derivative is used for linear approximation in single-variable calculus (), the gradient and Hessian are the key components of multivariable approximations. In our next lesson, we will combine them to construct the second-order Taylor approximation of a function, a powerful tool for analyzing and optimizing functions in machine learning.