Convex sets and functions
When is minimizing a loss easy? Convex sets and functions, Jensen's inequality, the tangent and Hessian tests, the rules that make squared, logistic and hinge losses convex, why every local minimum is then global, and why neural networks are different.
A zero gradient is only a candidate
The topics Gradients, Jacobians and Hessians and Matrix calculus gave us the gradient of any loss we can write down, and Multiple integrals and change of variables turned expected losses into integrals. One question is still open: when we reach a point with zero gradient, have we found the best one?
A zero gradient and a positive second derivative are local facts. Take a one-dimensional "loss":
It has three critical points:
Two of them pass the second-derivative test. Gradient descent with step started at settles at . Started at , it settles at . Both runs end with a gradient of zero up to machine precision. Only one of them found the deeper well, and nothing measured at the end point tells them apart.
This topic describes functions for which local information is global: convex functions. The losses of least squares, logistic regression and support vector machines are convex. Neural network losses are not, and by the end of the topic we will see why.
Convex sets
Convexity starts with sets. A set is convex if, together with any two of its points, it contains the whole segment between them:
Many sets from ML are convex:
- a half-space — one side of a linear classifier's boundary;
- a norm ball for any norm, including the ball of the lasso; the triangle inequality proves it;
- the probability simplex , where softmax outputs live;
- positive semidefinite matrices (see Symmetric matrices and quadratic forms): if and , then .
A ring is not convex: the segment from to passes through the hole. Neither is the union of two disjoint disks.
The intersection of convex sets is convex: a segment that lies in each set lies in all of them. So a polyhedron , an intersection of half-spaces, is convex. Unions are generally not.
A convex combination of points is with and . All convex combinations form the convex hull, the smallest convex set that contains the points. Take the triangle with vertices , and . The point equals : every weight is non-negative, so the point is inside. For the equations , and leave only the weights . No other weights work, and one of these is negative, so the point is outside.
Convex functions and Jensen's inequality
A function is convex if its domain is convex and every chord lies on or above the graph:
The function is strictly convex if the inequality is strict whenever and . It is concave if is convex, like or . The link to sets is the epigraph, the region on and above the graph, : it is a convex set exactly when is a convex function.
For , , and , the graph at the midpoint gives . The chord gives . The chord is above the graph.
Every norm is convex, by the triangle inequality:
A norm is not strictly convex: along a ray from the origin it grows linearly, and the chord lies on the graph.
The definition extends from two points to any convex combination. This is Jensen's inequality:
With it says , so a variance is never negative. For a concave function the inequality flips. Applied to the logarithm, it gives the evidence lower bound of variational autoencoders.
First-order condition: the tangent lies below
The definition compares values at two points. For a differentiable there is an equivalent test with one point and its gradient: is convex exactly when every tangent plane lies below the graph,
Here is why convexity implies it. Apply the definition to the point :
Subtract , divide by and let . The left side becomes the directional derivative from Gradients, Jacobians and Hessians, and the bound follows.
Example: the tangent to at is , so for every . At : . At : .
The most useful consequence comes at a point with . The tangent plane there is flat, and the bound reads
For a convex function, a zero gradient certifies a global minimum.
Back to . Its tangent at is the horizontal line at height . At the graph lies below that line. The tangent test fails, so the function is not convex, and that is exactly why the run from the right got stuck.
Second-order condition: the Hessian
For a twice differentiable there is a test that uses curvature: is convex exactly when its Hessian is positive semidefinite everywhere,
The reason is to look along lines. Fix a point and a direction , and set . By the chain rule,
A function is convex exactly when its restriction to every line is convex: chords along a line are chords of . A function of one variable is convex exactly when . If , then never decreases, so bends away from each tangent line and stays above it, which is the first-order test. In the other direction, the tangent bounds at two points and , added together, give , so never decreases and .
A positive definite Hessian everywhere gives strict convexity. The converse fails: is strictly convex, since its derivative is strictly increasing, but its second derivative at is zero.
For a quadratic the Hessian is everywhere. The matrix has eigenvalues and , so its quadratic is convex. The matrix has eigenvalues and . With and we get , while the midpoint gives , above the chord.
A less obvious example is log-sum-exp, . Its gradient is , and its Hessian is . For any ,
because this is the variance of when is drawn with probabilities . So log-sum-exp is convex. It is not strictly convex: adding the same number to every logit adds that number to , so along it is linear.
Building convex losses
Checking a Hessian every time is tedious. Instead, we build convex functions from convex pieces with four rules:
- A non-negative weighted sum with is convex.
- An affine substitution is convex when is.
- A pointwise maximum is convex.
- A composition is convex when is convex and is convex and non-decreasing.
Products and arbitrary compositions are not on the list. Both and are convex, but their product is concave.
Now take a linear model with weights , an example and a label . The margin is linear in . The three classic losses are convex in :
- squared loss : the convex of an affine function of ;
- logistic loss : a convex function of the margin (its second derivative is positive), with an affine substitution;
- hinge loss : the maximum of two affine functions.
The training loss averages the per-example losses with weights and often adds a ridge penalty or a lasso penalty with . By rule 1, the result is still convex. The same rules show that is convex: it is , non-decreasing for , applied to the norm of an affine map.
The hinge loss has a kink at , so the gradient and Hessian tests do not apply there. The chord definition still does, and in optimization the gradient is replaced by a subgradient, a slope of any line that touches the graph from below.
Every local minimum is global
A point is a local minimum of if for all in some ball around . For a convex on a convex set , every local minimum is global.
The proof takes one line of convexity. Suppose some had . For small the point lies in , since is convex, and inside the ball around , since . The chord inequality gives
which contradicts local minimality.
Two consequences follow. The minimizers form a convex set. A strictly convex function has at most one minimizer.
Example: fit to one data point , with squared loss,
Every point of the line is a minimizer: a convex set, but not a single answer. A ridge penalty adds to the Hessian. The Hessian becomes positive definite, the loss strictly convex, and the minimizer unique. For it is .
Convexity does not promise that a minimizer exists. The function is convex, its values approach , but no point reaches it. Logistic loss on linearly separable data behaves the same way: scaling up a separating always lowers the loss. A ridge penalty fixes this too.
Why neural networks are not convex
A neural network composes linear maps with nonlinear activations, and its loss is generally not convex in the weights. The smallest example is a "network" with two weights in a row, , fitted to one point :
The points and both have loss . Their midpoint has loss , above the chord. The origin also has a zero gradient, and its Hessian has eigenvalues and : it is a saddle point, not a minimum.
The same thing happens in real networks. Swapping two hidden units, together with their incoming and outgoing weights, gives the same function and the same loss. So every good set of weights has copies, and the average of two copies mixes different units into what is usually a worse network. Matrix factorization, k-means and mixture models fail convexity for the same reason.
Convex pieces still matter. With the features of a network fixed, training its last layer is logistic regression, and the results of this topic apply.
Convexity tells us that a point with zero gradient is the global answer. It does not tell us how to get there, or how fast. That is the question of the next topic, Unconstrained optimization: gradient descent, the step size, and how the condition number of the Hessian decides the speed.
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.