AI Knowledge Tree

Optimization problems, minima, and gradients

Formalize an optimization problem and characterize minima through the gradient and the Hessian.

⏱ 30 minPrerequisites:Matrix calculus & the chain rule

Training a model is minimizing a loss. Before any algorithm, the question is what a solution even is: where are the minima of a function, how do you recognize one, and what distinguishes a true minimum from a saddle? The gradient and the Hessian answer this locally, and they are the only information first- and second-order methods ever use.

The problem

An unconstrained optimization problem is

min⁡x∈Rnf(x),\min_{x \in \mathbb{R}^n} f(x),

where f:Rn→Rf: \mathbb{R}^n \to \mathbb{R} is the objective. A point x⋆x^\star is a global minimum if f(x⋆)≤f(x)f(x^\star) \le f(x) for all xx, and a local minimum if the inequality holds only in a neighborhood. Optimization algorithms find local minima; extra structure (convexity, a later lesson) is what promotes them to global.

First-order condition

At a smooth interior minimum the gradient vanishes. The gradient

∇f(x)=(∂f∂x1,…,∂f∂xn)⊤\nabla f(x) = \left(\frac{\partial f}{\partial x_1}, \dots, \frac{\partial f}{\partial x_n}\right)^\top

points in the direction of steepest ascent, and its negative is steepest descent.

Theorem (necessary condition). If x⋆x^\star is a local minimum and ff is differentiable there, then ∇f(x⋆)=0\nabla f(x^\star) = 0.

Proof. Along any direction dd, the function g(t)=f(x⋆+td)g(t) = f(x^\star + td) has a local minimum at t=0t = 0, so g′(0)=∇f(x⋆)⊤d=0g'(0) = \nabla f(x^\star)^\top d = 0. Since this holds for every dd, the gradient must be the zero vector. ■\qquad\blacksquare[1]

A point with ∇f=0\nabla f = 0 is a stationary point. It can be a minimum, a maximum, or a saddle; the first-order condition alone cannot tell them apart.

Second-order condition

The Hessian ∇2f(x)\nabla^2 f(x) is the symmetric matrix of second partials, [∇2f]ij=∂2f/∂xi∂xj[\nabla^2 f]_{ij} = \partial^2 f / \partial x_i \partial x_j. It is the local curvature. Expanding ff to second order around a stationary point x⋆x^\star:

f(x⋆+d)≈f(x⋆)+12 d⊤∇2f(x⋆) d,f(x^\star + d) \approx f(x^\star) + \tfrac{1}{2}\, d^\top \nabla^2 f(x^\star)\, d,

since the gradient term drops. The sign of the quadratic form d⊤∇2f dd^\top \nabla^2 f\, d decides the type, using the definiteness language from quadratic forms:

  • ∇2f(x⋆)≻0\nabla^2 f(x^\star) \succ 0 (positive definite): every direction curves up, a strict local minimum.
  • ∇2f(x⋆)≺0\nabla^2 f(x^\star) \prec 0 (negative definite): a local maximum.
  • Indefinite (mixed eigenvalue signs): a saddle point, a minimum along some directions and a maximum along others.

Saddles, not bad local minima, are the dominant stationary points of high-dimensional loss surfaces, which shapes how neural-network optimizers behave[2].

Directional derivatives and descent directions

A direction dd is a descent direction at xx if ∇f(x)⊤d<0\nabla f(x)^\top d < 0: moving slightly along dd decreases ff, because the directional derivative is negative. The steepest descent direction is d=−∇f(x)d = -\nabla f(x), the choice the next lessons build on.

The Hessian's eigenvalues classify a critical point: all positive gives a bowl (minimum, left); mixed signs give a saddle (right).
The Hessian's eigenvalues classify a critical point: all positive gives a bowl (minimum, left); mixed signs give a saddle (right).

Check it on arrays

python · numpy
import numpy as np

# f(x,y) = x^2 + 3y^2 (bowl): minimum at origin, Hessian = diag(2,6) > 0
def f(v): return v[0]**2 + 3*v[1]**2
def grad(v): return np.array([2*v[0], 6*v[1]])
def hess(v): return np.array([[2.0, 0.0], [0.0, 6.0]])

x0 = np.array([0.0, 0.0])
print("grad at origin:", grad(x0), " (stationary)")
eig = np.linalg.eigvalsh(hess(x0))
print("Hessian eigenvalues:", eig, "-> all > 0 -> local min")

# A saddle: g(x,y) = x^2 - y^2, Hessian = diag(2,-2) indefinite
def hess_saddle(v): return np.array([[2.0, 0.0], [0.0, -2.0]])
print("saddle Hessian eigenvalues:", np.linalg.eigvalsh(hess_saddle(x0)),
      "-> mixed signs -> saddle")

# Descent direction check: -grad should have negative directional derivative
x = np.array([1.0, 1.0])
d = -grad(x)
print("grad . d =", grad(x) @ d, "< 0 -> descent direction")

# Finite-difference gradient check
eps = 1e-6
fd = np.array([(f(x + eps*np.eye(2)[i]) - f(x - eps*np.eye(2)[i]))/(2*eps) for i in range(2)])
print("analytic grad:", grad(x), " finite-diff:", np.round(fd, 6))

The bowl's Hessian is positive definite (a minimum), the saddle's is indefinite, the negative gradient is a descent direction, and finite differences confirm the analytic gradient.

Exercises

Exercise 1. For f(x,y)=x2+3y2f(x, y) = x^2 + 3y^2, find every stationary point by hand, then classify it using the Hessian. Confirm it is a strict local minimum.

Solution

Step 1. Set the gradient to zero. With ∇f=(2x, 6y)⊤\nabla f = (2x,\, 6y)^\top, the only solution of 2x=02x = 0 and 6y=06y = 0 is the origin (0,0)(0, 0). There is exactly one stationary point.

Step 2. Form the Hessian, which is constant here:

∇2f=(2006).\nabla^2 f = \begin{pmatrix} 2 & 0 \\ 0 & 6 \end{pmatrix}.

Step 3. A diagonal matrix has its diagonal entries as eigenvalues, so the eigenvalues are 22 and 66, both positive. The Hessian is positive definite, so the origin is a strict local minimum. Since ff is a sum of squares, it is in fact the global minimum, with f(0,0)=0f(0,0) = 0.

Exercise 2. The first-order condition ∇f(x⋆)=0\nabla f(x^\star) = 0 is necessary but not sufficient. Show that f(x,y)=x2−y2f(x, y) = x^2 - y^2 has a stationary point at the origin that is neither a minimum nor a maximum, and identify the directions of increase and decrease.

Solution

Step 1. Compute ∇f=(2x, −2y)⊤\nabla f = (2x,\, -2y)^\top. It vanishes only at (0,0)(0, 0), so the origin is the unique stationary point.

Step 2. The Hessian is ∇2f=(200−2)\nabla^2 f = \left(\begin{smallmatrix} 2 & 0 \\ 0 & -2 \end{smallmatrix}\right), with eigenvalues +2+2 and −2-2. Mixed signs make it indefinite, so the origin is a saddle, not an extremum.

Step 3. Read the directions off the quadratic form. Along the xx-axis (y=0y = 0), f=x2>0f = x^2 > 0 for x≠0x \ne 0: the function increases. Along the yy-axis (x=0x = 0), f=−y2<0f = -y^2 < 0 for y≠0y \ne 0: it decreases. The origin is a minimum along xx and a maximum along yy, the defining shape of a saddle.

Exercise 3 (code). Pick a random symmetric 5×55 \times 5 matrix as a Hessian and the quadratic f(d)=12d⊤Hdf(d) = \tfrac12 d^\top H d. Verify numerically that d=−∇f(x)d = -\nabla f(x) is a descent direction at a nonzero xx whenever the directional derivative ∇f(x)⊤d\nabla f(x)^\top d is negative, and that this holds at every nonzero point.

Solution
python · numpy
import numpy as np

rng = np.random.default_rng(0)
M = rng.normal(size=(5, 5))
H = M + M.T                                  # symmetric Hessian (any signature)

# f(x) = 0.5 x^T H x, grad = H x (H symmetric)
grad = lambda x: H @ x

# At many random nonzero points, -grad is a descent direction unless grad == 0.
ratios = []
for _ in range(1000):
    x = rng.normal(size=5)
    g = grad(x)
    d = -g
    dd = g @ d                               # directional derivative along -grad
    ratios.append(dd / (g @ g))              # equals -1 whenever grad != 0
    assert dd <= 0                           # never an ascent direction

# -grad gives directional derivative -||grad||^2 <= 0, strictly < 0 when grad != 0.
ratios = np.array(ratios)
assert np.allclose(ratios, -1.0)
print("directional derivative ratio (all):", round(ratios.max(), 6))

The directional derivative along −∇f-\nabla f equals −∥∇f∥2-\lVert \nabla f\rVert^2, which is negative at every point where the gradient is nonzero. Steepest descent is always a valid descent direction away from stationary points.

The next lesson defines the geometric property, convexity, that makes a stationary point a global minimum.


References

  1. [1]Nocedal, J., & Wright, S. J. (2006). Numerical Optimization (2nd ed.), §2.1. Springer. First- and second-order optimality conditions for unconstrained problems (doi:10.1007/978-0-387-40065-5).↩
  2. [2]Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning, ch. 8. MIT Press. Saddle points and the structure of high-dimensional loss surfaces.↩