Optimization and Calculus for Machine Learning

Hessians and Curvature

Second derivatives, Hessians, and curvature as tools for understanding optimizer behavior.

Lesson
31

Goal

Understand curvature as the next layer of information after the gradient.

The gradient tells us:

Which direction looks downhill right here?

The Hessian tells us:

How quickly is that downhill direction changing as we move?

That matters because optimization does not only depend on direction. It also depends on the shape of the loss surface.

Motivation

Gradient descent uses first-derivative information:

θt+1=θtηL(θt)\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t)

The gradient points in the direction of steepest local increase, so the negative gradient points locally downhill.

But the gradient alone does not tell us whether the local surface is:

Those shapes affect whether a learning rate is safe.

A step size that works in a flat direction may explode in a steep direction. A step size that is safe in a steep direction may crawl in a flat direction.

That is the optimization reason we care about curvature.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
f(x)f(x)scalar functionA one-dimensional function.
L(θ)L(\theta)loss functionLoss as a function of model parameters.
θ\thetaparameter vectorThe values the optimizer changes.
L(θ)\nabla L(\theta)gradientVector of first partial derivatives.
f(x)f'(x)first derivativeLocal slope in one dimension.
f(x)f''(x)second derivativeHow the slope is changing.
HHHessianMatrix of second partial derivatives.
λ\lambdaeigenvalueCurvature scale in a Hessian eigenvector direction.
vvdirection vectorA direction in parameter space.

One Dimension: Slope Versus Curvature

Start with a one-dimensional function:

f(x)=x2f(x) = x^2

The first derivative is:

f(x)=2xf'(x) = 2x

This tells us the slope at each point.

The second derivative is:

f(x)=2f''(x) = 2

This tells us that the slope changes at a constant rate.

Now compare:

g(x)=10x2g(x) = 10x^2

Then:

g(x)=20xg'(x) = 20x

and:

g(x)=20g''(x) = 20

Both are bowls. But g(x)g(x) is a sharper bowl than f(x)f(x).

That sharpness is curvature.

Curvature As “How Fast The Slope Changes”

A useful phrase:

The gradient tells you the current tilt. The second derivative tells you how quickly the tilt is changing.

For one-dimensional functions:

This is local information. It describes the function near a point, not necessarily everywhere.

Why Curvature Affects Gradient Descent

Consider:

f(x)=ax2f(x) = ax^2

where a>0a > 0.

Then:

f(x)=2axf'(x) = 2ax

A gradient descent update is:

xt+1=xtη2axtx_{t+1} = x_t - \eta 2a x_t

Factor out xtx_t:

xt+1=(12aη)xtx_{t+1} = (1 - 2a\eta)x_t

The multiplier (12aη)(1 - 2a\eta) controls the behavior.

If η\eta is too large relative to aa, the update overshoots. If it is much too large, it diverges.

Since:

f(x)=2af''(x) = 2a

larger curvature means a smaller safe learning rate.

This is the link back to learning-rate schedules: the schedule changes the step size, but curvature helps explain why some step sizes are safe and others are not.

Two Dimensions: The Hessian

For a multivariable loss:

L(θ1,θ2)L(\theta_1, \theta_2)

there is not just one second derivative. There are second partial derivatives.

The Hessian collects them into a matrix:

H=[2Lθ122Lθ1θ22Lθ2θ12Lθ22]H = \begin{bmatrix} \frac{\partial^2 L}{\partial \theta_1^2} & \frac{\partial^2 L}{\partial \theta_1 \partial \theta_2} \\ \frac{\partial^2 L}{\partial \theta_2 \partial \theta_1} & \frac{\partial^2 L}{\partial \theta_2^2} \end{bmatrix}

Read the diagonal entries as same-coordinate curvature:

Read the off-diagonal entries as interaction curvature:

For smooth losses, the two mixed partials are usually equal:

2Lθ1θ2=2Lθ2θ1\frac{\partial^2 L}{\partial \theta_1 \partial \theta_2} = \frac{\partial^2 L}{\partial \theta_2 \partial \theta_1}

So the Hessian is often symmetric.

Worked Example: Diagonal Hessian

Let:

L(x,y)=x2+10y2L(x, y) = x^2 + 10y^2

The gradient is:

L(x,y)=[2x20y]\nabla L(x, y) = \begin{bmatrix} 2x \\ 20y \end{bmatrix}

The Hessian is:

H=[20020]H = \begin{bmatrix} 2 & 0 \\ 0 & 20 \end{bmatrix}

Interpretation:

So the surface is much steeper in the yy direction than in the xx direction.

A learning rate that is comfortable along xx may bounce along yy.

Hessian Eigenvectors: Principal Curvature Directions

Because the Hessian is a matrix, it transforms direction vectors.

When HH is symmetric, its eigenvectors give special directions of curvature.

If:

Hv=λvHv = \lambda v

then moving in direction vv has curvature strength λ\lambda.

For a Hessian:

This connects back to earlier linear algebra:

The Hessian is locally telling us the shape of the loss surface by decomposing it into special curvature directions.

That is the same pattern as PCA and covariance geometry: use eigenvectors to find the important directions of a quadratic-like object.

Bowls, Hills, and Saddles

At a point where the gradient is zero, curvature helps classify the point.

GradientHessian behaviorLocal shape
L=0\nabla L = 0all eigenvalues positivelocal minimum candidate
L=0\nabla L = 0all eigenvalues negativelocal maximum candidate
L=0\nabla L = 0mixed signssaddle point
L0\nabla L \ne 0any Hessiannot stationary; still has slope

A saddle point is especially important in high-dimensional optimization.

In one direction, it may curve upward. In another direction, it may curve downward.

So a point can have zero gradient without being a minimum.

The Local Quadratic Approximation

Near a point θ\theta, a smooth loss can be approximated by:

L(θ+Δ)L(θ)+L(θ)TΔ+12ΔTHΔL(\theta + \Delta) \approx L(\theta) + \nabla L(\theta)^T \Delta + \frac{1}{2}\Delta^T H \Delta

Symbol table for this expression:

SymbolMeaning
Δ\DeltaSmall proposed movement in parameter space.
L(θ)TΔ\nabla L(\theta)^T \DeltaFirst-order change predicted by the gradient.
12ΔTHΔ\frac{1}{2}\Delta^T H \DeltaSecond-order correction from curvature.

This formula says:

  1. start with the current loss;
  2. add what the gradient predicts;
  3. correct that prediction using curvature.

This is why second derivatives show up in optimization theory. They describe the local quadratic bowl that the optimizer is moving through.

Why We Do Not Usually Build The Full Hessian

For a model with nn parameters, the Hessian is an n×nn \times n matrix.

If a model has one million parameters, the Hessian would have one trillion entries.

That is too large to store or invert directly in ordinary neural network training.

So modern optimizers usually do not build the full Hessian.

Instead, they use cheaper approximations or indirect signals:

For now, the goal is not to implement a full second-order optimizer. The goal is to understand what curvature means and why it affects training.

Connection To Programming

A Hessian for a two-variable scalar function can be represented as a 2x2 matrix.

For:

L(x,y)=x2+10y2L(x, y) = x^2 + 10y^2

we can write:

function gradient(x: number, y: number): [number, number] {
  return [2 * x, 20 * y];
}

function hessian(): [[number, number], [number, number]] {
  return [
    [2, 0],
    [0, 20],
  ];
}

To inspect curvature along a direction vv, compute:

vTHvv^T H v

For a unit vector vv, this gives the curvature in that direction.

function curvatureInDirection(
  hessian: [[number, number], [number, number]],
  direction: [number, number],
): number {
  const [x, y] = direction;
  const hx = hessian[0][0] * x + hessian[0][1] * y;
  const hy = hessian[1][0] * x + hessian[1][1] * y;

  return x * hx + y * hy;
}

For direction [1, 0], the curvature is 2.

For direction [0, 1], the curvature is 20.

What Curvature Does Not Solve

Curvature information does not choose the right model.

It does not fix bad data.

It does not make nonconvex optimization easy.

It does not remove the need for validation checks.

It does explain why some regions of the loss surface are easy to optimize and others are awkward.

Why Hessians Show Up In ML

Hessians and curvature show up because training behavior depends on the local geometry of the loss surface.

They help explain:

You do not need the full Hessian to train a neural network today. But you do need the concept to understand many optimizer arguments.

Summary

The gradient is first-order information:

L(θ)\nabla L(\theta)

It tells us local slope.

The Hessian is second-order information:

Hij=2LθiθjH_{ij} = \frac{\partial^2 L}{\partial \theta_i \partial \theta_j}

It tells us local curvature.

The eigenvectors of the Hessian identify special curvature directions:

Hv=λvHv = \lambda v

The eigenvalues tell us how curved the loss is in those directions.

Curvature matters because the same learning rate can be safe in flat directions and unstable in steep directions.

Exercises

  1. In your own words, explain the difference between a first derivative and a second derivative.
  2. For f(x)=3x2f(x) = 3x^2, compute f(x)f'(x) and f(x)f''(x).
  3. For L(x,y)=4x2+y2L(x, y) = 4x^2 + y^2, compute the gradient and Hessian.
  4. In Problem 3, which direction is steeper: the xx direction or the yy direction? Why?
  5. Suppose a Hessian has eigenvalues 1010 and 0.10.1. What does that suggest about the local loss surface?
  6. Suppose a Hessian has eigenvalues 55 and 2-2 at a point where the gradient is zero. What kind of point is this likely to be?
  7. Explain why a learning rate that works in a flat direction might fail in a steep direction.
  8. Write TypeScript-style pseudocode for computing vTHvv^T H v for a 2x2 Hessian and a 2D direction vector.

Looking Ahead

Curvature lets us distinguish broad bowls, steep valleys, and saddle points.

The next optimization question is:

When can we guarantee that local downhill movement behaves nicely?

That leads to convexity.