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 over all of . "Unconstrained" means any vector is allowed. For a model, is the loss and is the parameter vector .
First-order condition. If is a local minimum, then
Otherwise a small step against the gradient, the direction of steepest descent (see the topic Gradients, Jacobians and Hessians), would lower . 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:
So the Hessian's quadratic form decides (see Symmetric matrices and quadratic forms):
- necessary: at a local minimum ;
- sufficient: and give a strict local minimum;
- eigenvalues of both signs give a saddle.
The gap between and is real. Both and have zero gradient and Hessian at the origin. The first has a minimum there, the second a saddle.
Convex case. If is convex, alone means a global minimum: the tangent plane lies below the graph, and here it is flat.
For least squares, is the normal equations (see Inner products, norms and orthogonality). That is a linear system we solve once. For logistic regression, is a nonlinear system with no closed form. So we iterate.
Gradient descent
The gradient points uphill, so we step the other way:
The number is the step size; in ML it is called the learning rate. Training a network is this loop: backpropagation computes , we subtract times it, and repeat.
How large should be? Take the simplest loss, a 1-D parabola with curvature and minimum at :
Write the error as . One step gives
Every step multiplies the error by the same factor . Take , so , and start at :
- : the factor is ; , and the error shrinks steadily from one side;
- : the factor is , and is the minimum;
- : the factor is ; the iterates , , jump across the minimum and fly apart.
A small step is safe but slow. A large one overshoots. Where the line runs depends on , the curvature : 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 -smooth if its gradient is -Lipschitz:
For a twice differentiable this means every eigenvalue of lies in at every . For a quadratic with , the constant is . Do not confuse the constant with the loss ; the argument tells them apart.
Smoothness puts a parabola above the graph:
For a twice differentiable , write . Taylor's formula along the segment gives this with the last term replaced by at some point between and , and that quadratic form is at most , because no eigenvalue exceeds (the Rayleigh quotient, see Symmetric matrices and quadratic forms). Set and get the descent lemma:
For the bracket is positive, so each step lowers unless the gradient is already zero. The guaranteed decrease is largest at :
This is the standard safe step. It needs no convexity. Add the decreases over the first steps; the values of in between cancel:
If is bounded below, the smallest of these gradients has . The gradient must become small, but nothing says at which kind of critical point.
For linear regression with the Hessian is , so , the top singular value of squared over (see The singular value decomposition). For a network is unknown. People try learning rates on a log grid, and a loss that blows up means 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 with and eigenvalues from to . Since , gradient descent reads
In the eigenbasis of (the spectral theorem) the coordinates decouple. The coordinate along eigenvector is multiplied by at every step: the 1-D story, times over. But one must serve all directions. The steepest one, , caps the step below . The flattest one, , then shrinks only by per step. The ratio
is the condition number. Our running example is , a ravine ten times steeper across than along, .
- : the steep coordinate dies in one step, the flat one shrinks by . From : , , .
- , near the limit : the flat factor improves to , but the steep factor is . From : , , a zigzag across the ravine.
The best fixed step balances the two extremes. With the error shrinks by per step, so accuracy takes about steps, because . Double and you double the training time.
In ML the culprit is often feature scale. For linear regression on centered, uncorrelated features, is diagonal with the feature variances on it. Area in square meters (standard deviation ) next to the number of rooms (standard deviation ) gives . Standardize both features and . 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:
Here , often , and is plain descent. Unrolled, the velocity is a weighted sum of past gradients:
Components that flip sign cancel. Components that persist add up, so steps along the ravine grow.
On a quadratic with tuned and the error shrinks per step by
instead of for descent with the best fixed step. The step count drops from about to about . For the factor is against ; shrinking the error a million times from takes steps instead of in simulation. For the gain is a hundredfold.
Nesterov's variant takes the gradient at the look-ahead point and keeps the rate beyond quadratics, for every function whose Hessian eigenvalues stay between and 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 and a step chosen by hand. Newton's method uses the second-order Taylor model
and jumps to its minimum. Setting the gradient in to zero (see Matrix calculus) gives the Newton step:
In one dimension this is .
On a quadratic the model is exact, so Newton lands on the minimum in one step, whatever is. For our ravine . Newton divides each eigen-direction by its own curvature , the per-direction step that descent cannot have.
Near a strict minimum, Newton converges quadratically: the error is roughly squared at each step. Take with minimum at . The step gives , that is . From the errors are
The price is the Hessian: numbers to store and about operations to solve the system. For parameters that is entries, so nobody trains a large network this way. Far from the minimum the full step can overshoot. Damped Newton takes with shrunk until decreases enough. That works only if points downhill, and a positive definite Hessian guarantees it: . Where the Hessian is not positive definite, the Newton step may point uphill: the method solves 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 examples:
The exact gradient needs all examples: for a million images, one step of descent is one pass over the data. Stochastic gradient descent (SGD) draws a random mini-batch of examples instead:
Because the batch is random, : the estimate is unbiased. Like any sample mean, it has a variance equal to the per-example variance divided by . A step costs gradients instead of , so early in training many cheap noisy steps beat a few exact ones.
The noise has a price. At the minimum , but the individual are not zero. With a constant , SGD keeps jittering in a region whose size grows with . That is why training uses learning-rate schedules that lower over time. Momentum combines with SGD as before.
The toolbox so far:
| Method | Uses | Cost per step | Steps on a quadratic |
|---|---|---|---|
| Gradient descent | gradient | about | |
| Momentum | gradient | about | |
| Newton | gradient and Hessian | about | one; quadratic near the minimum |
| SGD | mini-batch gradient | examples | noisy; needs a decaying |
All of this assumed that may be any vector. Often it may not: softmax outputs must be nonnegative and sum to , and a weight norm may be bounded. On a boundary the minimum need not have zero gradient. The function on is smallest at , where . 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.
Answers are saved on the server and tied to this browser until you sign in.