Skip to content
BIASED

Unconstrained optimization

How to find the minimum of a loss: optimality conditions, gradient descent and the step size, the condition number, momentum, Newton's method and stochastic gradients.

How to recognize a minimum

The topic Convex sets and functions showed that for a convex loss every local minimum is global. It did not say how to find one. This topic does: first we learn to recognize a minimum, then to walk to it.

We minimize a differentiable f:Rn→Rf: \mathbb R^n \to \mathbb R over all of Rn\mathbb R^n. "Unconstrained" means any vector is allowed. For a model, ff is the loss L(θ)L(\theta) and xx is the parameter vector θ\theta.

First-order condition. If x∗x^* is a local minimum, then

∇f(x∗)=0.\nabla f(x^*) = 0.

Otherwise a small step against the gradient, the direction of steepest descent (see the topic Gradients, Jacobians and Hessians), would lower ff. Points with zero gradient are called critical (or stationary). Minima, maxima and saddle points are all critical.

Second-order conditions. At a critical point the gradient term of the Taylor expansion vanishes:

f(x∗+h)≈f(x∗)+12 h⊤∇2f(x∗) h.f(x^* + h) \approx f(x^*) + \tfrac12\, h^\top \nabla^2 f(x^*)\, h.

So the Hessian's quadratic form decides (see Symmetric matrices and quadratic forms):

  • necessary: at a local minimum ∇2f(x∗)⪰0\nabla^2 f(x^*) \succeq 0;
  • sufficient: ∇f(x∗)=0\nabla f(x^*) = 0 and ∇2f(x∗)≻0\nabla^2 f(x^*) \succ 0 give a strict local minimum;
  • eigenvalues of both signs give a saddle.

The gap between ⪰\succeq and ≻\succ is real. Both x12+x24x_1^2 + x_2^4 and x12−x24x_1^2 - x_2^4 have zero gradient and Hessian diag⁡(2,0)\operatorname{diag}(2, 0) at the origin. The first has a minimum there, the second a saddle.

Convex case. If ff is convex, ∇f(x∗)=0\nabla f(x^*) = 0 alone means a global minimum: the tangent plane lies below the graph, and here it is flat.

For least squares, ∇L(θ)=0\nabla L(\theta) = 0 is the normal equations X⊤Xθ=X⊤yX^\top X \theta = X^\top y (see Inner products, norms and orthogonality). That is a linear system we solve once. For logistic regression, ∇L(θ)=0\nabla L(\theta) = 0 is a nonlinear system with no closed form. So we iterate.

Gradient descent

The gradient points uphill, so we step the other way:

xk+1=xk−η ∇f(xk).x_{k+1} = x_k - \eta\, \nabla f(x_k).

The number η>0\eta > 0 is the step size; in ML it is called the learning rate. Training a network is this loop: backpropagation computes ∇L(θ)\nabla L(\theta), we subtract η\eta times it, and repeat.

How large should η\eta be? Take the simplest loss, a 1-D parabola with curvature a>0a > 0 and minimum at cc:

f(x)=a2(x−c)2,f′(x)=a(x−c).f(x) = \tfrac a2 (x - c)^2, \qquad f'(x) = a(x - c).

Write the error as ek=xk−ce_k = x_k - c. One step gives

ek+1=ek−ηa ek=(1−ηa) ek.e_{k+1} = e_k - \eta a\, e_k = (1 - \eta a)\, e_k.

Every step multiplies the error by the same factor 1−ηa1 - \eta a. Take f(x)=2(x−3)2f(x) = 2(x - 3)^2, so a=4a = 4, and start at x0=0x_0 = 0:

  • η=0.1\eta = 0.1: the factor is 0.60.6; x1=1.2x_1 = 1.2, and the error shrinks steadily from one side;
  • η=0.25=1/a\eta = 0.25 = 1/a: the factor is 00, and x1=3x_1 = 3 is the minimum;
  • η=0.6\eta = 0.6: the factor is −1.4-1.4; the iterates 7.27.2, −2.88-2.88, 11.2311.23 jump across the minimum and fly apart.

A small step is safe but slow. A large one overshoots. Where the line runs depends on aa, the curvature f′′f'': the steeper the bowl, the smaller the step it tolerates.

Smoothness and a safe step

In many dimensions curvature differs by direction and by point. We need one number that bounds it. A function is LL-smooth if its gradient is LL-Lipschitz:

∥∇f(x)−∇f(y)∥≤L ∥x−y∥for all x,y.\lVert \nabla f(x) - \nabla f(y) \rVert \le L\, \lVert x - y \rVert \quad \text{for all } x, y.

For a twice differentiable ff this means every eigenvalue of ∇2f(x)\nabla^2 f(x) lies in [−L,L][-L, L] at every xx. For a quadratic 12x⊤Ax−b⊤x\tfrac12 x^\top A x - b^\top x with A⪰0A \succeq 0, the constant is L=λmax⁡(A)L = \lambda_{\max}(A). Do not confuse the constant LL with the loss L(θ)L(\theta); the argument tells them apart.

Smoothness puts a parabola above the graph:

f(y)≤f(x)+∇f(x)⊤(y−x)+L2∥y−x∥2.f(y) \le f(x) + \nabla f(x)^\top (y - x) + \tfrac L2 \lVert y - x \rVert^2.

For a twice differentiable ff, write h=y−xh = y - x. Taylor's formula along the segment gives this with the last term replaced by 12h⊤∇2f(z) h\tfrac12 h^\top \nabla^2 f(z)\, h at some point zz between xx and yy, and that quadratic form is at most L2∥h∥2\tfrac L2 \lVert h \rVert^2, because no eigenvalue exceeds LL (the Rayleigh quotient, see Symmetric matrices and quadratic forms). Set y=x−η∇f(x)y = x - \eta \nabla f(x) and get the descent lemma:

f(x−η∇f(x))≤f(x)−η(1−Lη2)∥∇f(x)∥2.f\big(x - \eta \nabla f(x)\big) \le f(x) - \eta \Big(1 - \frac{L\eta}{2}\Big) \lVert \nabla f(x) \rVert^2.

For 0<η<2/L0 < \eta < 2/L the bracket is positive, so each step lowers ff unless the gradient is already zero. The guaranteed decrease is largest at η=1/L\eta = 1/L:

f(xk+1)≤f(xk)−12L∥∇f(xk)∥2.f(x_{k+1}) \le f(x_k) - \frac{1}{2L} \lVert \nabla f(x_k) \rVert^2.

This is the standard safe step. It needs no convexity. Add the decreases over the first KK steps; the values of ff in between cancel:

12L∑k=0K−1∥∇f(xk)∥2≤f(x0)−f(xK)≤f(x0)−inf⁡f.\frac{1}{2L} \sum_{k=0}^{K-1} \lVert \nabla f(x_k) \rVert^2 \le f(x_0) - f(x_K) \le f(x_0) - \inf f.

If ff is bounded below, the smallest of these KK gradients has ∥∇f∥2≤2L (f(x0)−inf⁡f)/K\lVert \nabla f \rVert^2 \le 2L\,(f(x_0) - \inf f)/K. The gradient must become small, but nothing says at which kind of critical point.

For linear regression with L(θ)=12N∥Xθ−y∥2L(\theta) = \frac{1}{2N} \lVert X\theta - y \rVert^2 the Hessian is X⊤X/NX^\top X / N, so L=σ12/NL = \sigma_1^2 / N, the top singular value of XX squared over NN (see The singular value decomposition). For a network LL is unknown. People try learning rates on a log grid, and a loss that blows up means η\eta went past the limit.

The ravine and the condition number

Near a strict minimum every smooth loss looks like a quadratic, so a quadratic tells the local story. Take f(x)=12x⊤Axf(x) = \tfrac12 x^\top A x with A≻0A \succ 0 and eigenvalues from μ=λmin⁡\mu = \lambda_{\min} to L=λmax⁡L = \lambda_{\max}. Since ∇f(x)=Ax\nabla f(x) = Ax, gradient descent reads

xk+1=(I−ηA) xk.x_{k+1} = (I - \eta A)\, x_k.

In the eigenbasis of AA (the spectral theorem) the coordinates decouple. The coordinate along eigenvector viv_i is multiplied by 1−ηλi1 - \eta\lambda_i at every step: the 1-D story, nn times over. But one η\eta must serve all directions. The steepest one, LL, caps the step below 2/L2/L. The flattest one, μ\mu, then shrinks only by 1−ημ1 - \eta\mu per step. The ratio

κ=Lμ=λmax⁡λmin⁡\kappa = \frac{L}{\mu} = \frac{\lambda_{\max}}{\lambda_{\min}}

is the condition number. Our running example is A=diag⁡(1,10)A = \operatorname{diag}(1, 10), a ravine ten times steeper across than along, κ=10\kappa = 10.

  • η=1/L=0.1\eta = 1/L = 0.1: the steep coordinate dies in one step, the flat one shrinks by 0.90.9. From (10,1)(10, 1): (9,0)(9, 0), (8.1,0)(8.1, 0), (7.29,0)(7.29, 0).
  • η=0.19\eta = 0.19, near the limit 0.20.2: the flat factor improves to 0.810.81, but the steep factor is −0.9-0.9. From (10,1)(10, 1): (8.1,−0.9)(8.1, -0.9), (6.56,0.81)(6.56, 0.81), a zigzag across the ravine.

The best fixed step balances the two extremes. With η=1/L\eta = 1/L the error shrinks by 1−1/κ1 - 1/\kappa per step, so accuracy ε\varepsilon takes about κln⁡(1/ε)\kappa \ln(1/\varepsilon) steps, because (1−1/κ)k≤e−k/κ(1 - 1/\kappa)^k \le e^{-k/\kappa}. Double κ\kappa and you double the training time.

In ML the culprit is often feature scale. For linear regression on centered, uncorrelated features, X⊤X/NX^\top X / N is diagonal with the feature variances on it. Area in square meters (standard deviation 3030) next to the number of rooms (standard deviation 11) gives κ=900\kappa = 900. Standardize both features and κ=1\kappa = 1. This is one reason inputs are normalized before training.

Momentum

Gradient descent has no memory: each step sees only the current gradient. In a ravine the component across it flips sign every step, while the component along it keeps its sign. Momentum, or Polyak's heavy ball, keeps a velocity:

vk+1=βvk−η∇f(xk),xk+1=xk+vk+1.v_{k+1} = \beta v_k - \eta \nabla f(x_k), \qquad x_{k+1} = x_k + v_{k+1}.

Here β∈[0,1)\beta \in [0, 1), often 0.90.9, and β=0\beta = 0 is plain descent. Unrolled, the velocity is a weighted sum of past gradients:

vk+1=−η∑j=0kβj ∇f(xk−j)(v0=0).v_{k+1} = -\eta \sum_{j=0}^{k} \beta^j\, \nabla f(x_{k-j}) \quad (v_0 = 0).

Components that flip sign cancel. Components that persist add up, so steps along the ravine grow.

On a quadratic with tuned η\eta and β\beta the error shrinks per step by

κ−1κ+1\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}

instead of κ−1κ+1\frac{\kappa - 1}{\kappa + 1} for descent with the best fixed step. The step count drops from about κln⁡(1/ε)\kappa \ln(1/\varepsilon) to about κln⁡(1/ε)\sqrt\kappa \ln(1/\varepsilon). For κ=10\kappa = 10 the factor is 0.520.52 against 0.820.82; shrinking the error a million times from (1,1)(1, 1) takes 2727 steps instead of 6969 in simulation. For κ=104\kappa = 10^4 the gain is a hundredfold.

Nesterov's variant takes the gradient at the look-ahead point xk+βvkx_k + \beta v_k and keeps the κ\sqrt\kappa rate beyond quadratics, for every function whose Hessian eigenvalues stay between μ>0\mu > 0 and LL everywhere. In PyTorch both are one argument: SGD(params, lr, momentum=0.9, nesterov=True).

Newton's method

Gradient descent uses a linear model of ff and a step chosen by hand. Newton's method uses the second-order Taylor model

f(xk+d)≈f(xk)+∇f(xk)⊤d+12 d⊤∇2f(xk) df(x_k + d) \approx f(x_k) + \nabla f(x_k)^\top d + \tfrac12\, d^\top \nabla^2 f(x_k)\, d

and jumps to its minimum. Setting the gradient in dd to zero (see Matrix calculus) gives the Newton step:

∇2f(xk) dk=−∇f(xk),xk+1=xk+dk.\nabla^2 f(x_k)\, d_k = -\nabla f(x_k), \qquad x_{k+1} = x_k + d_k.

In one dimension this is xk+1=xk−f′(xk)/f′′(xk)x_{k+1} = x_k - f'(x_k) / f''(x_k).

On a quadratic the model is exact, so Newton lands on the minimum in one step, whatever κ\kappa is. For our ravine d=−A−1Ax=−xd = -A^{-1}Ax = -x. Newton divides each eigen-direction by its own curvature λi\lambda_i, the per-direction step that descent cannot have.

Near a strict minimum, Newton converges quadratically: the error is roughly squared at each step. Take f(x)=x−ln⁡xf(x) = x - \ln x with minimum at 11. The step gives xk+1=2xk−xk2x_{k+1} = 2x_k - x_k^2, that is xk+1−1=−(xk−1)2x_{k+1} - 1 = -(x_k - 1)^2. From x0=0.5x_0 = 0.5 the errors are

−0.5, −0.25, −0.0625, −3.9⋅10−3, −1.5⋅10−5, −2.3⋅10−10.-0.5,\ -0.25,\ -0.0625,\ -3.9 \cdot 10^{-3},\ -1.5 \cdot 10^{-5},\ -2.3 \cdot 10^{-10}.

The price is the Hessian: n2n^2 numbers to store and about n3n^3 operations to solve the system. For n=107n = 10^7 parameters that is 101410^{14} entries, so nobody trains a large network this way. Far from the minimum the full step can overshoot. Damped Newton takes xk+1=xk+t dkx_{k+1} = x_k + t\, d_k with t≤1t \le 1 shrunk until ff decreases enough. That works only if dkd_k points downhill, and a positive definite Hessian guarantees it: ∇f(xk)⊤dk=−∇f(xk)⊤[∇2f(xk)]−1∇f(xk)<0\nabla f(x_k)^\top d_k = -\nabla f(x_k)^\top [\nabla^2 f(x_k)]^{-1} \nabla f(x_k) < 0. Where the Hessian is not positive definite, the Newton step may point uphill: the method solves ∇f=0\nabla f = 0 and converges just as happily to a maximum or a saddle. Practical codes then add a multiple of the identity to the Hessian until it is positive definite. Quasi-Newton methods such as BFGS and L-BFGS build a positive definite Hessian approximation from gradient differences, so their step always points downhill; L-BFGS is the default solver for logistic regression in scikit-learn.

Stochastic gradients and what comes next

A training loss is an average over NN examples:

L(θ)=1N∑i=1Nℓi(θ).L(\theta) = \frac1N \sum_{i=1}^N \ell_i(\theta).

The exact gradient needs all NN examples: for a million images, one step of descent is one pass over the data. Stochastic gradient descent (SGD) draws a random mini-batch Bk\mathcal B_k of BB examples instead:

gk=1B∑i∈Bk∇ℓi(θk),θk+1=θk−η gk.g_k = \frac1B \sum_{i \in \mathcal B_k} \nabla \ell_i(\theta_k), \qquad \theta_{k+1} = \theta_k - \eta\, g_k.

Because the batch is random, E[gk]=∇L(θk)\mathbb E[g_k] = \nabla L(\theta_k): the estimate is unbiased. Like any sample mean, it has a variance equal to the per-example variance divided by BB. A step costs BB gradients instead of NN, so early in training many cheap noisy steps beat a few exact ones.

The noise has a price. At the minimum ∇L=0\nabla L = 0, but the individual ∇ℓi\nabla \ell_i are not zero. With a constant η\eta, SGD keeps jittering in a region whose size grows with η\eta. That is why training uses learning-rate schedules that lower η\eta over time. Momentum combines with SGD as before.

The toolbox so far:

Method Uses Cost per step Steps on a quadratic
Gradient descent gradient nn about κln⁡(1/ε)\kappa \ln(1/\varepsilon)
Momentum gradient nn about κln⁡(1/ε)\sqrt\kappa \ln(1/\varepsilon)
Newton gradient and Hessian about n3n^3 one; quadratic near the minimum
SGD mini-batch gradient BB examples noisy; needs a decaying η\eta

All of this assumed that θ\theta may be any vector. Often it may not: softmax outputs must be nonnegative and sum to 11, and a weight norm may be bounded. On a boundary the minimum need not have zero gradient. The function f(x)=xf(x) = x on x≥1x \ge 1 is smallest at x=1x = 1, where f′(1)=1f'(1) = 1. How optimality conditions change under constraints is the subject of the next topic, Lagrange multipliers and KKT conditions.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.