๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #33

Hessian

If the gradient tells you the slope of the loss surface, the Hessian tells you its curvature โ€” is the surface bowl-shaped, saddle-shaped, or flat around this point? That distinction matters for understanding why some points where the gradient is zero are good minima and others aren't.

Definition

\[ \mathbf{H} = \begin{bmatrix} \dfrac{\partial^2 f}{\partial x_1^2} & \cdots & \dfrac{\partial^2 f}{\partial x_1 \partial x_n} \\ \vdots & \ddots & \vdots \\ \dfrac{\partial^2 f}{\partial x_n \partial x_1} & \cdots & \dfrac{\partial^2 f}{\partial x_n^2} \end{bmatrix} \]

The Hessian is the matrix of all second-order partial derivatives of a scalar-valued function โ€” it's the Jacobian of the gradient. For a function of \(n\) variables, it's an \((n,n)\) square matrix.

Numerical Example

\[ f(x,y) = x^2 + y^2 \] \[ \frac{\partial f}{\partial x}=2x,\ \frac{\partial f}{\partial y}=2y \quad\Rightarrow\quad \mathbf{H} = \begin{bmatrix}2 & 0\\0 & 2\end{bmatrix} \]

Both diagonal entries are positive and off-diagonal terms are zero โ€” this Hessian is positive definite everywhere, confirming \(f\) curves upward in every direction: it's a bowl shape with a true minimum at the origin.

What the Hessian Tells You About a Critical Point

Hessian PropertyMeaning at a Point Where the Gradient Is Zero
Positive definite (all eigenvalues > 0)Local minimum โ€” the function curves upward in every direction
Negative definite (all eigenvalues < 0)Local maximum โ€” the function curves downward in every direction
Indefinite (mixed positive/negative eigenvalues)Saddle point โ€” curves up in some directions, down in others

This connects directly back to Eigenvalues: the Hessian's eigenvalues determine which of these three cases you're in.

Why Deep Learning Mostly Avoids Computing the Hessian

For a network with \(n\) parameters, the Hessian is an \((n,n)\) matrix โ€” for a modest model with a million parameters, that's \(10^{12}\) entries, computationally infeasible to form or invert. This is exactly why deep learning optimizers (SGD, Adam, etc.) use only first-order information (the gradient), not the Hessian, unlike some classical optimization methods (e.g. Newton's method) that use curvature directly. Adam and similar optimizers approximate some curvature-like behavior cheaply, without ever forming the true Hessian โ€” covered in the Optimization category.

Code โ€” Computing a Small Hessian

import torch

def f(v):
    x, y = v[0], v[1]
    return x**2 + y**2

x = torch.tensor([1.0, 1.0], requires_grad=True)
H = torch.autograd.functional.hessian(f, x)
print(H)
# tensor([[2., 0.],
#         [0., 2.]])

Common Mistakes

  • Assuming a zero gradient always means a minimum โ€” it only means a critical point; the Hessian is what distinguishes a true minimum from a maximum or (very common in high-dimensional loss surfaces) a saddle point.
  • Expecting deep learning training to routinely use the Hessian โ€” in practice it almost never is, purely for computational-cost reasons, not because curvature information wouldn't be useful.

Interview Relevance

Q: "Why is it risky to assume a point where the gradient is zero is a good solution during training?" A zero gradient only signals a critical point โ€” it could be a true minimum, a maximum, or (especially in high-dimensional non-convex loss surfaces typical of deep networks) a saddle point, where the surface curves up in some directions and down in others. The Hessian's eigenvalues are what distinguish these cases, though it's rarely computed directly in practice due to cost.

Practice Question

For \(f(x,y) = x^2 - y^2\), compute the Hessian. Based on its diagonal entries, is the critical point at the origin a minimum, maximum, or saddle point?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Hessian โ€“ FAQs

Quick answers about learning Hessian in Deep Learning.

This free note from CodingNow 2.0 explains Hessian in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Hessian, is 100% free with no signup required.
With focused practice, most students grasp Hessian in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now