Hello! Let's continue our journey through the mathematical foundations of AI.
In the last lesson, we explored backpropagation as a systematic application of the chain rule. We saw that it allows us to calculate the gradient () of a complex loss function with respect to any parameter in a neural network. We also briefly touched on the idea that for vector-valued functions, the derivatives are no longer simple vectors but become matrices called Jacobians.
Today, we will formalize that concept and go one step further. The learning outcome for this lesson is to calculate Jacobians and Hessians for advanced optimization. We will define what these matrices are, see how to compute them, and, most importantly, understand why they are crucial for optimization methods that are more sophisticated than standard gradient descent.
1. The Jacobian: Generalizing Derivatives for Vector Functions
So far, we have focused on gradients, which are for scalar-valued functions (functions that output a single number, like a loss function ). But what if a function outputs a vector?
Consider a function that takes an -dimensional vector as input and produces an -dimensional vector as output.
How do we describe the rate of change here? We need to capture how each output component changes with respect to each input component. We organize these partial derivatives into an matrix called the Jacobian matrix, denoted as .
The element at row and column of the Jacobian is .
You can think of the gradient as a special case of the Jacobian where the output dimension .
The Jacobian : Data Science Basics
This video from ritvikmath provides an excellent, intuitive walk-through from single-variable derivatives to the full Jacobian matrix.
Watch the first part of the video, from the beginning to 03:39. It builds up the concept by starting with a simple derivative, moving to partial derivatives, and finally to the Jacobian for a multi-input, multi-output function.
Calculation Example
Let's compute the Jacobian for a function defined as:
We need to find the four partial derivatives:
Arranging them into a 2x2 matrix gives us the Jacobian:
The Jacobian and the Chain Rule in Neural Networks
The true power of the Jacobian becomes apparent when we revisit the chain rule from our last lesson. If we have a sequence of vector functions, say and , the chain rule for finding the derivative of with respect to is simply the product of their Jacobian matrices:
This is exactly what happens during backpropagation in a neural network. Each layer is a vector function, and propagating the gradient backward involves multiplying by the Jacobian of each layer.
The Jacobian : Data Science Basics
Let's continue with the same video, which now demonstrates this exact application within a simple neural network.
Watch from 03:39 to 09:54. The video shows how the overall gradient of a network's output with respect to its input can be found by multiplying the Jacobians of each layer. This elegantly frames backpropagation as a series of matrix multiplications.
2. The Hessian: Capturing Curvature with Second Derivatives
While the gradient and Jacobian tell us about the slope (first-order information), the Hessian matrix tells us about the curvature (second-order information) of a function. For a scalar-valued function , the Hessian is the matrix of all its second-order partial derivatives.
It can also be defined as the Jacobian of the gradient: .
The element at row and column of the Hessian is .
A key property, due to Clairaut's theorem, is that if the second partial derivatives are continuous, the order of differentiation doesn't matter (e.g., ). This means the Hessian matrix is symmetric.
M4ML - Multivariate Calculus - 2.7 The Hessian
This video from Imperial College London provides a quick and clear introduction to the Hessian matrix.
Watch from the beginning to 03:28. The video defines the Hessian, shows how to calculate it for a function with three variables, and points out its symmetric nature.
Calculation and Implementation in Python
Given your background in Python, let's see how this is done in code. Manually calculating these derivatives is tedious and error-prone. We can use a symbolic mathematics library like SymPy to do the heavy lifting.
Hessian Matrix: Concepts, Properties, and Applications
This article from DataCamp provides a great overview of the Hessian, but more importantly, it includes a concrete Python example using SymPy. This is a practical way to solidify your understanding.
Read the sections 'What Is the Hessian Matrix?', 'Hessian Matrix Example', and the following Python code block. This will cover the formal definition and then show you how to compute it for a sample function using SymPy.
3. Application: Advanced Optimization
Why do we care about curvature? Because it allows for more powerful optimization algorithms. Gradient descent only uses the direction of the slope; it's like walking downhill in a thick fog. Second-order methods use curvature information to take more intelligent steps, like using a topographic map to find the valley floor.
Newton's Method
The most famous second-order optimization algorithm is Newton's method. It works by creating a quadratic approximation of the function at the current point using the second-order Taylor expansion:
To find the minimum of this approximation, we set its gradient to zero and solve for . This gives the Newton's method update rule:
Notice the difference from gradient descent (). Instead of a scalar learning rate , Newton's method uses the inverse of the Hessian matrix, , to automatically determine the optimal step size and direction.
Levenberg–Marquardt Training
This document on Levenberg-Marquardt training provides a concise mathematical derivation of Newton's method in the context of neural network training. It shows exactly how the update rule is derived from the Taylor series expansion.
Read section 12.2.2, 'Newton's Method'. Focus on understanding how Equation 12.15 (the update rule) is derived from the preceding steps involving the Hessian matrix (H).
Classifying Critical Points
The Hessian also helps us classify critical points (where ) found by an optimization algorithm. By examining the eigenvalues of the Hessian at a critical point (which connects to our earlier lesson on eigenvalues):
- All eigenvalues are positive: The Hessian is positive definite. The point is a local minimum.
- All eigenvalues are negative: The Hessian is negative definite. The point is a local maximum.
- Eigenvalues have mixed signs: The Hessian is indefinite. The point is a saddle point.
Saddle points are a major challenge in high-dimensional optimization (like training neural networks), and second-order methods are better equipped to handle them.
M4ML - Multivariate Calculus - 2.7 The Hessian
Let's return to the Imperial College video, which nicely visualizes how the Hessian's properties relate to minima, maxima, and saddle points.
Watch from 03:28 to the end. It explains how to use the Hessian's determinant and its first element to classify critical points in a 2D space.
Test your understanding!
Consider the function . Calculate its gradient and Hessian. Is the point (0,0) a local minimum, maximum, or saddle point?
Show answer
-
Gradient:
At (0,0), the gradient is , so it is a critical point. -
Hessian:
The Hessian is constant for this function. -
Classify the point: We check the definiteness of the Hessian. We can find its eigenvalues by solving :
The roots are .
One eigenvalue is and the other is . Since the eigenvalues have mixed signs, the Hessian is indefinite, and (0,0) is a saddle point.
4. Practicality: Approximating the Hessian
While powerful, Newton's method has a major drawback for deep learning:
- Computation: Calculating the Hessian requires computing second derivatives, where is the number of parameters. For a model like GPT-3 with 175 billion parameters, this is impossible.
- Storage: Storing the Hessian matrix is also impossible.
- Inversion: Inverting the Hessian is an operation, which is prohibitively expensive.
For these reasons, pure Newton's method is not used for training large neural networks. Instead, we use quasi-Newton methods. These methods approximate the Hessian (or its inverse) using more easily available information, like the gradients from previous steps.
A very common and clever approximation in non-linear least squares problems is the Gauss-Newton algorithm, which approximates the Hessian using the Jacobian:
This is a beautiful connection between our two topics for today. We use the matrix of first derivatives (Jacobian) to approximate the matrix of second derivatives (Hessian), avoiding the expensive computation of the true second derivatives.
Levenberg–Marquardt Training
The same document on Levenberg-Marquardt training explains this approximation very clearly. This is a key practical insight for optimization in machine learning.
Read section 12.2.3, 'Gauss-Newton Algorithm'. Focus on understanding Equation 12.22, which presents the approximation H ≈ J^T*J, and how it modifies the update rule in Equation 12.23. This shows how the Jacobian can be used to bypass the full Hessian calculation.
Conclusion
Today, we have moved beyond the gradient to explore higher-order derivatives and their role in optimization.
Key Takeaways:
- The Jacobian matrix generalizes the derivative to vector-valued functions, representing all partial derivatives of each output with respect to each input. It is fundamental to the chain rule in backpropagation for vector operations.
- The Hessian matrix is the matrix of second-order partial derivatives of a scalar-valued function. It describes the function's local curvature.
- The Hessian is critical for advanced, second-order optimization methods like Newton's method, which uses curvature information to take more direct steps toward a minimum.
- The properties of the Hessian (its definiteness, checked via eigenvalues) allow us to classify critical points as minima, maxima, or saddle points.
- In practice, computing the full Hessian is often too expensive. Therefore, quasi-Newton methods approximate the Hessian, sometimes using the Jacobian (e.g., ).
Preview of the next lesson:
We have now covered the essential calculus needed for understanding how AI models learn and are optimized. We will now shift gears to the second major pillar of machine learning mathematics: probability and statistics. In the next lesson, we will start with one of the most foundational theorems in this field: Bayes' theorem, a principle that underpins our ability to reason about and update our beliefs in the face of new evidence.