Skip to content
BIASED

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":

f(x)=x4−2x2+12x,f′(x)=4x3−4x+12.f(x) = x^4 - 2x^2 + \tfrac12 x, \qquad f'(x) = 4x^3 - 4x + \tfrac12.

It has three critical points:

xx −1.057-1.057 0.1270.127 0.9300.930
f(x)f(x) −1.515-1.515 0.0320.032 −0.517-0.517
f′′(x)f''(x) 9.429.42 −3.81-3.81 6.396.39

Two of them pass the second-derivative test. Gradient descent with step 0.050.05 started at x=1.5x = 1.5 settles at 0.9300.930. Started at x=−1.5x = -1.5, it settles at −1.057-1.057. 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 CC is convex if, together with any two of its points, it contains the whole segment between them:

x,y∈C,  θ∈[0,1] ⟹ θx+(1−θ)y∈C.x, y \in C,\ \ \theta \in [0, 1] \ \Longrightarrow\ \theta x + (1 - \theta) y \in C.

Many sets from ML are convex:

  • a half-space {x:a⊤x≤b}\{x : a^\top x \le b\} — one side of a linear classifier's boundary;
  • a norm ball {x:∥x−c∥≤r}\{x : \lVert x - c \rVert \le r\} for any norm, including the ℓ1\ell_1 ball of the lasso; the triangle inequality proves it;
  • the probability simplex {p:pi≥0, ∑ipi=1}\{p : p_i \ge 0,\ \sum_i p_i = 1\}, where softmax outputs live;
  • positive semidefinite matrices (see Symmetric matrices and quadratic forms): if v⊤Av≥0v^\top A v \ge 0 and v⊤Bv≥0v^\top B v \ge 0, then v⊤(θA+(1−θ)B)v≥0v^\top (\theta A + (1 - \theta) B) v \ge 0.

A ring {x:1≤∥x∥≤2}\{x : 1 \le \lVert x \rVert \le 2\} is not convex: the segment from (−1.5,0)(-1.5, 0) to (1.5,0)(1.5, 0) 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 {x:Ax≤b}\{x : Ax \le b\}, an intersection of half-spaces, is convex. Unions are generally not.

A convex combination of points x1,…,xkx_1, \dots, x_k is ∑iθixi\sum_i \theta_i x_i with θi≥0\theta_i \ge 0 and ∑iθi=1\sum_i \theta_i = 1. All convex combinations form the convex hull, the smallest convex set that contains the points. Take the triangle with vertices (0,0)(0, 0), (3,0)(3, 0) and (0,3)(0, 3). The point (1,1)(1, 1) equals 13(0,0)+13(3,0)+13(0,3)\tfrac13 (0, 0) + \tfrac13 (3, 0) + \tfrac13 (0, 3): every weight is non-negative, so the point is inside. For (2,2)(2, 2) the equations 3θ2=23\theta_2 = 2, 3θ3=23\theta_3 = 2 and θ1+θ2+θ3=1\theta_1 + \theta_2 + \theta_3 = 1 leave only the weights (−13,23,23)\big(-\tfrac13, \tfrac23, \tfrac23\big). No other weights work, and one of these is negative, so the point is outside.

Convex functions and Jensen's inequality

A function ff is convex if its domain is convex and every chord lies on or above the graph:

f(θx+(1−θ)y)≤θf(x)+(1−θ)f(y)for all x,y and θ∈[0,1].f(\theta x + (1 - \theta) y) \le \theta f(x) + (1 - \theta) f(y) \quad \text{for all } x, y \text{ and } \theta \in [0, 1].

The function is strictly convex if the inequality is strict whenever x≠yx \ne y and 0<θ<10 < \theta < 1. It is concave if −f-f is convex, like log⁡x\log x or x\sqrt x. The link to sets is the epigraph, the region on and above the graph, {(x,t):t≥f(x)}\{(x, t) : t \ge f(x)\}: it is a convex set exactly when ff is a convex function.

For f(x)=x2f(x) = x^2, x=0x = 0, y=2y = 2 and θ=12\theta = \tfrac12, the graph at the midpoint gives f(1)=1f(1) = 1. The chord gives 12⋅0+12⋅4=2\tfrac12 \cdot 0 + \tfrac12 \cdot 4 = 2. The chord is 11 above the graph.

Every norm is convex, by the triangle inequality:

∥θx+(1−θ)y∥≤θ∥x∥+(1−θ)∥y∥.\lVert \theta x + (1 - \theta) y \rVert \le \theta \lVert x \rVert + (1 - \theta) \lVert y \rVert.

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:

f(∑iθixi)≤∑iθif(xi),f(E[X])≤E[f(X)].f\Big(\sum_i \theta_i x_i\Big) \le \sum_i \theta_i f(x_i), \qquad f(\mathbb E[X]) \le \mathbb E[f(X)].

With f(x)=x2f(x) = x^2 it says (E[X])2≤E[X2](\mathbb E[X])^2 \le \mathbb E[X^2], 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 ff there is an equivalent test with one point and its gradient: ff is convex exactly when every tangent plane lies below the graph,

f(y)≥f(x)+∇f(x)⊤(y−x)for all x,y.f(y) \ge f(x) + \nabla f(x)^\top (y - x) \quad \text{for all } x, y.

Here is why convexity implies it. Apply the definition to the point x+θ(y−x)x + \theta (y - x):

f(x+θ(y−x))≤f(x)+θ(f(y)−f(x)).f(x + \theta (y - x)) \le f(x) + \theta \big(f(y) - f(x)\big).

Subtract f(x)f(x), divide by θ\theta and let θ→0\theta \to 0. The left side becomes the directional derivative ∇f(x)⊤(y−x)\nabla f(x)^\top (y - x) from Gradients, Jacobians and Hessians, and the bound follows.

Example: the tangent to eye^y at 00 is 1+y1 + y, so ey≥1+ye^y \ge 1 + y for every yy. At y=1y = 1: 2.718≥22.718 \ge 2. At y=−2y = -2: 0.135≥−10.135 \ge -1.

The most useful consequence comes at a point x∗x^* with ∇f(x∗)=0\nabla f(x^*) = 0. The tangent plane there is flat, and the bound reads

f(y)≥f(x∗)for all y.f(y) \ge f(x^*) \quad \text{for all } y.

For a convex function, a zero gradient certifies a global minimum.

Back to x4−2x2+12xx^4 - 2x^2 + \tfrac12 x. Its tangent at x=0.930x = 0.930 is the horizontal line at height −0.517-0.517. At x=−1.057x = -1.057 the graph lies 0.9980.998 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 ff there is a test that uses curvature: ff is convex exactly when its Hessian is positive semidefinite everywhere,

∇2f(x)⪰0for all x.\nabla^2 f(x) \succeq 0 \quad \text{for all } x.

The reason is to look along lines. Fix a point xx and a direction vv, and set g(t)=f(x+tv)g(t) = f(x + t v). By the chain rule,

g′′(t)=v⊤∇2f(x+tv) v.g''(t) = v^\top \nabla^2 f(x + t v)\, v.

A function is convex exactly when its restriction to every line is convex: chords along a line are chords of ff. A function of one variable is convex exactly when g′′≥0g'' \ge 0. If g′′≥0g'' \ge 0, then g′g' never decreases, so gg 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 ss and tt, added together, give (g′(s)−g′(t))(s−t)≥0\big(g'(s) - g'(t)\big)(s - t) \ge 0, so g′g' never decreases and g′′≥0g'' \ge 0.

A positive definite Hessian everywhere gives strict convexity. The converse fails: x4x^4 is strictly convex, since its derivative 4x34x^3 is strictly increasing, but its second derivative at 00 is zero.

For a quadratic f(x)=12x⊤Ax+b⊤xf(x) = \tfrac12 x^\top A x + b^\top x the Hessian is AA everywhere. The matrix (2112)\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} has eigenvalues 11 and 33, so its quadratic is convex. The matrix (1221)\begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} has eigenvalues 33 and −1-1. With b=0b = 0 and v=(1,−1)v = (1, -1) we get f(v)=f(−v)=−1f(v) = f(-v) = -1, while the midpoint gives f(0)=0f(0) = 0, above the chord.

A less obvious example is log-sum-exp, lse⁡(z)=log⁡∑iezi\operatorname{lse}(z) = \log \sum_i e^{z_i}. Its gradient is p=softmax⁡(z)p = \operatorname{softmax}(z), and its Hessian is diag⁡(p)−pp⊤\operatorname{diag}(p) - p p^\top. For any vv,

v⊤(diag⁡(p)−pp⊤)v=∑ipivi2−(∑ipivi)2≥0,v^\top \big(\operatorname{diag}(p) - p p^\top\big) v = \sum_i p_i v_i^2 - \Big(\sum_i p_i v_i\Big)^2 \ge 0,

because this is the variance of viv_i when ii is drawn with probabilities pp. So log-sum-exp is convex. It is not strictly convex: adding the same number to every logit adds that number to lse⁡\operatorname{lse}, so along (1,…,1)(1, \dots, 1) it is linear.

Building convex losses

Checking a Hessian every time is tedious. Instead, we build convex functions from convex pieces with four rules:

  1. A non-negative weighted sum ∑iwifi\sum_i w_i f_i with wi≥0w_i \ge 0 is convex.
  2. An affine substitution f(Ax+b)f(Ax + b) is convex when ff is.
  3. A pointwise maximum max⁡ifi(x)\max_i f_i(x) is convex.
  4. A composition h(g(x))h(g(x)) is convex when gg is convex and hh is convex and non-decreasing.

Products and arbitrary compositions are not on the list. Both xx and −x-x are convex, but their product −x2-x^2 is concave.

Now take a linear model with weights ww, an example xix_i and a label yi∈{−1,+1}y_i \in \{-1, +1\}. The margin mi=yi w⊤xim_i = y_i\, w^\top x_i is linear in ww. The three classic losses are convex in ww:

  • squared loss (w⊤xi−yi)2(w^\top x_i - y_i)^2: the convex u2u^2 of an affine function of ww;
  • logistic loss log⁡(1+e−mi)\log\big(1 + e^{-m_i}\big): a convex function of the margin (its second derivative is positive), with an affine substitution;
  • hinge loss max⁡(0, 1−mi)\max(0,\, 1 - m_i): the maximum of two affine functions.

The training loss averages the per-example losses with weights 1n\tfrac1n and often adds a ridge penalty λ∥w∥2\lambda \lVert w \rVert^2 or a lasso penalty λ∥w∥1\lambda \lVert w \rVert_1 with λ≥0\lambda \ge 0. By rule 1, the result is still convex. The same rules show that ∥Xw−y∥2\lVert Xw - y \rVert^2 is convex: it is t2t^2, non-decreasing for t≥0t \ge 0, applied to the norm of an affine map.

The hinge loss has a kink at m=1m = 1, 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 x∗x^* is a local minimum of ff if f(x∗)≤f(x)f(x^*) \le f(x) for all xx in some ball around x∗x^*. For a convex ff on a convex set CC, every local minimum is global.

The proof takes one line of convexity. Suppose some y∈Cy \in C had f(y)<f(x∗)f(y) < f(x^*). For small θ>0\theta > 0 the point z=(1−θ)x∗+θyz = (1 - \theta) x^* + \theta y lies in CC, since CC is convex, and inside the ball around x∗x^*, since ∥z−x∗∥=θ∥y−x∗∥\lVert z - x^* \rVert = \theta \lVert y - x^* \rVert. The chord inequality gives

f(z)≤(1−θ)f(x∗)+θf(y)<f(x∗),f(z) \le (1 - \theta) f(x^*) + \theta f(y) < f(x^*),

which contradicts local minimality.

Two consequences follow. The minimizers form a convex set. A strictly convex function has at most one minimizer.

Example: fit w∈R2w \in \mathbb R^2 to one data point x=(1,1)x = (1, 1), y=1y = 1 with squared loss,

f(w)=(w1+w2−1)2.f(w) = (w_1 + w_2 - 1)^2.

Every point of the line w1+w2=1w_1 + w_2 = 1 is a minimizer: a convex set, but not a single answer. A ridge penalty λ(w12+w22)\lambda (w_1^2 + w_2^2) adds 2λI2\lambda I to the Hessian. The Hessian becomes positive definite, the loss strictly convex, and the minimizer unique. For λ=0.1\lambda = 0.1 it is w1=w2≈0.476w_1 = w_2 \approx 0.476.

Convexity does not promise that a minimizer exists. The function exe^x is convex, its values approach 00, but no point reaches it. Logistic loss on linearly separable data behaves the same way: scaling up a separating ww 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, y^=a b x\hat y = a\, b\, x, fitted to one point x=y=1x = y = 1:

f(a,b)=(ab−1)2,∇2f(a,b)=(2b24ab−24ab−22a2).f(a, b) = (ab - 1)^2, \qquad \nabla^2 f(a, b) = \begin{pmatrix} 2b^2 & 4ab - 2 \\ 4ab - 2 & 2a^2 \end{pmatrix}.

The points (1,1)(1, 1) and (−1,−1)(-1, -1) both have loss 00. Their midpoint (0,0)(0, 0) has loss 11, above the chord. The origin also has a zero gradient, and its Hessian has eigenvalues 22 and −2-2: 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.

Full notes

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