Optimization problems, minima, and gradients
Formalize an optimization problem and characterize minima through the gradient and the Hessian.
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
where is the objective. A point is a global minimum if for all , 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
points in the direction of steepest ascent, and its negative is steepest descent.
Theorem (necessary condition). If is a local minimum and is differentiable there, then .
Proof. Along any direction , the function has a local minimum at , so . Since this holds for every , the gradient must be the zero vector. [1]
A point with 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 is the symmetric matrix of second partials, . It is the local curvature. Expanding to second order around a stationary point :
since the gradient term drops. The sign of the quadratic form decides the type, using the definiteness language from quadratic forms:
- (positive definite): every direction curves up, a strict local minimum.
- (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 is a descent direction at if : moving slightly along decreases , because the directional derivative is negative. The steepest descent direction is , the choice the next lessons build on.
Check it on arrays
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 , 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 , the only solution of and is the origin . There is exactly one stationary point.
Step 2. Form the Hessian, which is constant here:
Step 3. A diagonal matrix has its diagonal entries as eigenvalues, so the eigenvalues are and , both positive. The Hessian is positive definite, so the origin is a strict local minimum. Since is a sum of squares, it is in fact the global minimum, with .
Exercise 2. The first-order condition is necessary but not sufficient. Show that 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 . It vanishes only at , so the origin is the unique stationary point.
Step 2. The Hessian is , with eigenvalues and . 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 -axis (), for : the function increases. Along the -axis (), for : it decreases. The origin is a minimum along and a maximum along , the defining shape of a saddle.
Exercise 3 (code). Pick a random symmetric matrix as a Hessian and the quadratic . Verify numerically that is a descent direction at a nonzero whenever the directional derivative is negative, and that this holds at every nonzero point.
Solution
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 equals , 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]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]Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning, ch. 8. MIT Press. Saddle points and the structure of high-dimensional loss surfaces.↩