Course contents 11 / 16

11 / 16 · Optimization

Convex sets and functions

When local information certifies a global minimum: convex sets and functions, the tangent and Hessian tests, operations that keep convexity, strong convexity and smoothness, and which machine learning losses are convex.

On this page 10

Run gradient descent with step 0.050.05 on the function

f(x)=x4−2x2+12x,f′(x)=4x3−4x+12.\begin{aligned} f(x) &= x^4 - 2x^2 + \tfrac12 x , \\ f'(x) &= 4x^3 - 4x + \tfrac12 . \end{aligned}

Started at x=1.5x = 1.5, it stops at 0.9300.930 with f=−0.517f = -0.517. Started at −1.5-1.5, it stops at −1.057-1.057 with f=−1.515f = -1.515. Both runs report convergence, and at both end points the derivative is zero and the second derivative is positive:

Stationary point xx−1.057-1.0570.1270.1270.9300.930
f(x)f(x)−1.515-1.5150.0320.032−0.517-0.517
f′′(x)=12x2−4f''(x) = 12x^2 - 49.429.42−3.81-3.816.396.39

A test that looks only at the point, as the gradient and the Hessian of the calculus section do, cannot tell the global minimum from the other one. For a function of one variable we can list the stationary points and compare them; for the loss of a model with millions of weights we cannot.

The way out is to restrict the class of functions until the local test becomes a global one. That class is the convex functions, and the restriction costs less than it seems: least squares, logistic regression, the support vector machine and the lasso all have convex losses.

Convex sets#

A set C⊆RnC \subseteq \mathbb R^n is convex if it contains the segment between any two of its points. For xx and yy in CC, the segment is the set of points θx+(1−θ)y\theta x + (1 - \theta)y with θ∈[0,1]\theta \in [0, 1]:

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 .
Figure

A convex set, a non-convex set and a convex hull

Convex and non-convex sets with chords, and the convex hull of pointsThree panels. An ellipse with a green chord inside it. A crescent crossed by a chord whose ends are green and whose middle, outside the set, is red and dashed. The triangle that is the convex hull of six points, with the point (1, 1) inside and the point (2, 2) outside.convexnot convexconvex hullchord stays insidechord leaves the set(1, 1) inside(2, 2) outside(0, 0)(3, 0)(0, 3)Convex and non-convex sets with chords, and the convex hull of pointsThree panels. An ellipse with a green chord inside it. A crescent crossed by a chord whose ends are green and whose middle, outside the set, is red and dashed. The triangle that is the convex hull of six points, with the point (1, 1) inside and the point (2, 2) outside.convexnot convexconvex hullchord stays insidechord leaves the set(1, 1) inside(2, 2) outside(0, 0)(3, 0)(0, 3)
Left, an ellipse: every chord stays inside. Centre, a crescent, a disc with another disc removed: the middle of the chord (red dashes) crosses the removed part. Right, the convex hull of six points is the triangle (0, 0), (3, 0), (0, 3). The point (1, 1) has weights ⅓, ⅓, ⅓; the point (2, 2) needs the weight −⅓ and lies outside.

A half-space {x:a⊤x≤b}\{x : a^\top x \le b\} is convex, because the constraint is linear. If a⊤x≤ba^\top x \le b and a⊤y≤ba^\top y \le b, then

a⊤(θx+(1−θ)y)=θ a⊤x+(1−θ) a⊤y≤θb+(1−θ)b=b.\begin{aligned} a^\top\big(\theta x + (1 - \theta)y\big) &= \theta\, a^\top x + (1 - \theta)\, a^\top y \\ &\le \theta b + (1 - \theta) b = b . \end{aligned}

A linear classifier splits the space into two half-spaces. Other convex sets met throughout machine learning:

  • norm balls {x:∥x−c∥≤r}\{x : \lVert x - c\rVert \le r\}, for every norm, so the L1 ball of the lasso too: by the triangle inequality and homogeneity, ∥θ(x−c)+(1−θ)(y−c)∥≤θ∥x−c∥+(1−θ)∥y−c∥≤r\lVert\theta(x - c) + (1 - \theta)(y - c)\rVert \le \theta\lVert x - c\rVert + (1 - \theta)\lVert y - c\rVert \le r;
  • the probability simplex {p:pi≥0, ∑ipi=1}\{p : p_i \ge 0,\ \sum_i p_i = 1\}, the set of distributions on nn outcomes and the range of the softmax: a weighted average of non-negative vectors is non-negative, and its entries sum to θ+(1−θ)=1\theta + (1 - \theta) = 1;
  • the positive semidefinite matrices, since v⊤Av≥0v^\top A v \ge 0 and v⊤Bv≥0v^\top B v \ge 0 give θ v⊤Av+(1−θ) v⊤Bv≥0\theta\, v^\top A v + (1 - \theta)\, v^\top B v \ge 0; covariance and kernel matrices live here.

A ring {1≤∥x∥≤2}\{1 \le \lVert x\rVert \le 2\} is not convex: it contains (1.5,0)(1.5, 0) and (−1.5,0)(-1.5, 0) but not their midpoint, the origin. Neither is the union of two disjoint discs, for the same reason with one point in each disc.

New convex sets come from old ones. If xx and yy lie in every CiC_i of a family of convex sets, the segment between them does too, so any intersection of convex sets is convex. A finite intersection of half-spaces, {x:Ax≤b}\{x : Ax \le b\}, is a polyhedron; the feasible sets of linear programs and of the SVM are polyhedra. The image of a convex set under an affine map x↦Ax+bx \mapsto Ax + b is convex, and so is its preimage (Boyd and Vandenberghe, EE364a, lecture 2). Both follow from one identity: the map sends θx+(1−θ)y\theta x + (1 - \theta)y to θ(Ax+b)+(1−θ)(Ay+b)\theta(Ax + b) + (1 - \theta)(Ay + b), the same combination of the images. An ellipsoid, for example, is the image of a ball. Unions do not preserve convexity, as the two discs show.

A convex combination of points x1,…,xkx_1, \dots, x_k is a weighted average ∑iθixi\sum_i \theta_i x_i with θi≥0\theta_i \ge 0 and ∑iθi=1\sum_i \theta_i = 1. The convex hull conv⁡{x1,…,xk}\operatorname{conv}\{x_1, \dots, x_k\} is the set of all convex combinations. It is the smallest convex set containing the points: a convex set that contains them contains every convex combination, by induction on kk. For θk<1\theta_k \lt 1 the induction step writes

∑i=1kθixi=(1−θk)∑i=1k−1θi1−θk xi+θkxk,\sum_{i=1}^k \theta_i x_i = (1 - \theta_k)\sum_{i=1}^{k-1} \frac{\theta_i}{1 - \theta_k}\, x_i + \theta_k x_k ,

a two-point combination of xkx_k and a combination of k−1k - 1 points, whose weights again sum to 1. For finitely many points in the plane it is the polygon a rubber band would form around them.

Is (1,1)(1, 1) in the convex hull of (0,0)(0, 0), (3,0)(3, 0) and (0,3)(0, 3)? The weights must satisfy 3θ2=13\theta_2 = 1, 3θ3=13\theta_3 = 1 and θ1+θ2+θ3=1\theta_1 + \theta_2 + \theta_3 = 1, so all three are 13\tfrac13, non-negative, and the answer is yes. For (2,2)(2, 2) the same equations give θ1=−13\theta_1 = -\tfrac13. The three equations have exactly one solution, since the matrix with columns (0,0,1)(0, 0, 1), (3,0,1)(3, 0, 1) and (0,3,1)(0, 3, 1) has determinant 9, so no non-negative weights exist and (2,2)(2, 2) is outside. The probability simplex is the convex hull of the standard basis vectors, and the duality note shows that the hard-margin SVM looks for the two nearest points of the convex hulls of its two classes.

Convex functions#

A function ff on a convex set is convex if every chord lies on or above its graph:

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

for all xx, yy and θ∈[0,1]\theta \in [0, 1]. It is strictly convex if the inequality is strict whenever x≠yx \ne y and 0<θ<10 \lt \theta \lt 1, and concave if −f-f is convex. Equivalently, the region above the graph, the epigraph {(x,t):t≥f(x)}\{(x, t) : t \ge f(x)\}, is a convex set. If t≥f(x)t \ge f(x) and s≥f(y)s \ge f(y), the chord inequality gives θt+(1−θ)s≥f(θx+(1−θ)y)\theta t + (1 - \theta)s \ge f(\theta x + (1 - \theta)y); conversely, the points (x,f(x))(x, f(x)) and (y,f(y))(y, f(y)) lie in the epigraph, and convexity of the epigraph is the chord inequality. The sublevel sets {x:f(x)≤β}\{x : f(x) \le \beta\} of a convex function are convex as well: if f(x)≤βf(x) \le \beta and f(y)≤βf(y) \le \beta, the chord bounds ff on the segment by β\beta. Convex sublevel sets do not make a function convex: ∣x∣\sqrt{\lvert x\rvert} has intervals as sublevel sets, yet its chord from 0 to 1 passes below the graph at 14\tfrac14, where 14<12\tfrac14 \lt \tfrac12.

By induction the inequality extends from two points to any convex combination, f(∑iθixi)≤∑iθif(xi)f(\sum_i \theta_i x_i) \le \sum_i \theta_i f(x_i), which is Jensen’s inequality: the step splits off the last point as for the convex hull, applies the two-point inequality, then the inequality for k−1k - 1 points. With the weights read as probabilities it says f(E[X])≤E[f(X)]f(\mathbb E[X]) \le \mathbb E[f(X)] for an XX with finitely many values. The same holds for integrals and general expectations whenever they exist (Boyd and Vandenberghe, 2004, Section 3.1.8). The VAE note bounds a log-likelihood with it; the logarithm is concave, so there the inequality flips.

For f(x)=x2f(x) = x^2 the gap between chord and graph is

θx2+(1−θ)y2−(θx+(1−θ)y)2=θ(1−θ)x2+θ(1−θ)y2−2θ(1−θ)xy=θ(1−θ)(x−y)2.\begin{aligned} &\theta x^2 + (1 - \theta) y^2 - \big(\theta x + (1 - \theta) y\big)^2 \\ &= \theta(1 - \theta)x^2 + \theta(1 - \theta)y^2 - 2\theta(1 - \theta)xy \\ &= \theta(1 - \theta)(x - y)^2 . \end{aligned}

The middle line uses θ−θ2=θ(1−θ)\theta - \theta^2 = \theta(1 - \theta) and (1−θ)−(1−θ)2=θ(1−θ)(1 - \theta) - (1 - \theta)^2 = \theta(1 - \theta).

It is never negative, so x2x^2 is convex, and it is positive whenever x≠yx \ne y and 0<θ<10 \lt \theta \lt 1, so x2x^2 is strictly convex. For x=0x = 0, y=2y = 2 and θ=12\theta = \tfrac12 the chord is at 22 and the graph at 11. Every norm is convex, by the triangle inequality and homogeneity, as for the balls above.

Checking chords pair by pair is impractical in many dimensions. For differentiable functions two tests use the derivatives instead.

The first-order condition: the tangent lies below#

For a convex function the chord from xx towards yy lies above the graph. As the chord shrinks to a point it turns into the tangent, so the tangent at any point lies below the whole graph. For a differentiable ff on an open convex set:

  f is convex  ⟺  f(y)≥f(x)+∇f(x)⊤(y−x)  for all x,y  \boxed{\; f \text{ is convex} \iff f(y) \ge f(x) + \nabla f(x)^\top (y - x) \ \text{ for all } x, y \;}
Figure

The tangent of a convex function lies below the graph

Tangents of a convex and a non-convex functionLeft, the exponential with three tangent lines below it and the gap 0.718 marked at x = 1. Right, a curve with two wells: the flat tangent at the bottom of the shallow right well passes above the bottom of the deeper left well; the region below the tangent is shaded.convex: tangents below the graph-2-11240.718f(x) = eˣnot convex: a tangent above the graph-11-11local −0.517global −1.5150.998 belowx⁴ − 2x² + x/2Tangents of a convex and a non-convex functionLeft, the exponential with three tangent lines below it and the gap 0.718 marked at x = 1. Right, a curve with two wells: the flat tangent at the bottom of the shallow right well passes above the bottom of the deeper left well; the region below the tangent is shaded.convex: tangents below the graph-2-11240.718f(x) = eˣnot convex: a tangent above the graph-11-11local −0.517global −1.5150.998 belowx⁴ − 2x² + x/2
Left, eˣ and its tangents at −1, 0 and 1, each below the graph; at x = 1 the graph is e − 2 = 0.718 above the tangent 1 + x. Right, the non-convex x⁴ − 2x² + x/2 of the opening: the flat tangent at its local minimum 0.930 (height −0.517) lies above the graph near −1.057, by up to 0.998 (shaded).

Fix xx and yy and take 0<θ≤10 \lt \theta \le 1. Convexity with the weights θ\theta and 1−θ1 - \theta bounds ff at the point x+θ(y−x)x + \theta(y - x) of the segment; subtracting f(x)f(x) and dividing by θ\theta gives a difference quotient:

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

As θ→0\theta \to 0 the left side tends to the directional derivative ∇f(x)⊤(y−x)\nabla f(x)^\top (y - x), by the chain rule for t↦f(x+t(y−x))t \mapsto f(x + t(y - x)) at t=0t = 0, and the inequality survives the limit. The converse is as short. Put z=θx+(1−θ)yz = \theta x + (1 - \theta)y, write the tangent bound at zz once towards xx and once towards yy, multiply the two inequalities by θ\theta and 1−θ1 - \theta, and add. The gradient terms cancel, because θ(x−z)+(1−θ)(y−z)=0\theta(x - z) + (1 - \theta)(y - z) = 0, and what remains is θf(x)+(1−θ)f(y)≥f(z)\theta f(x) + (1 - \theta) f(y) \ge f(z).

At x=0x = 0, exe^x has the tangent 1+y1 + y, so the bound says ey≥1+ye^y \ge 1 + y for every yy: at y=1y = 1, 2.718≥22.718 \ge 2. The opening function fails the test. At its local minimum x=0.930x = 0.930 the tangent is the horizontal line at height −0.517-0.517, and at x=−1.057x = -1.057 the graph is 0.9980.998 below it.

The bound is why convexity matters. Put ∇f(x∗)=0\nabla f(x^*) = 0 into it, and it reads f(y)≥f(x∗)f(y) \ge f(x^*) for every yy. For a differentiable convex function, a point with zero gradient is a global minimum.

The second-order condition: a positive semidefinite Hessian#

A function is convex exactly when it is convex along every line, because chords along a line are chords of ff. Restrict ff to the line through xx in direction vv, g(t)=f(x+tv)g(t) = f(x + tv). Its derivatives come from the chain rule:

g′(t)=∇f(x+tv)⊤v,g′′(t)=v⊤∇2f(x+tv) v.\begin{aligned} g'(t) &= \nabla f(x + tv)^\top v , \\ g''(t) &= v^\top \nabla^2 f(x + tv)\, v . \end{aligned}

In one variable, gg is convex exactly when g′′≥0g'' \ge 0. If g′′≥0g'' \ge 0, then g′g' never decreases, and the gap to the tangent at tt, g(s)−g(t)−g′(t)(s−t)=∫ts(g′(r)−g′(t)) drg(s) - g(t) - g'(t)(s - t) = \int_t^s \big(g'(r) - g'(t)\big)\,dr, is non-negative on both sides of tt: the tangent bound. Conversely, the tangent bound written at ss and at tt and added gives (g′(s)−g′(t))(s−t)≥0\big(g'(s) - g'(t)\big)(s - t) \ge 0, so g′g' is nondecreasing, and its derivative g′′g'', a limit of non-negative difference quotients, is ≥0\ge 0. Requiring g′′≥0g'' \ge 0 at every point and in every direction vv is exactly the condition on the Hessian. For a twice differentiable ff on an open convex set:

f is convex  ⟺  ∇2f(x)⪰0  for all x.f \text{ is convex} \iff \nabla^2 f(x) \succeq 0 \ \text{ for all } x .

Here A⪰0A \succeq 0 means that the symmetric matrix AA is positive semidefinite, v⊤Av≥0v^\top Av \ge 0 for all vv, as in the symmetric matrices note. If ∇2f(x)≻0\nabla^2 f(x) \succ 0 (positive definite) everywhere, ff is strictly convex: then g′′>0g'' \gt 0, g′g' is strictly increasing, the gap integral above is positive for s≠ts \ne t, and the converse argument of the tangent test, run with strict inequalities, gives strict chords. The converse fails: x4x^4 has f′′(0)=0f''(0) = 0, yet its derivative 4x34x^3 is strictly increasing, so the same argument makes it strictly convex.

The quadratic f(x)=12x⊤Ax+b⊤xf(x) = \tfrac12 x^\top A x + b^\top x with symmetric AA has the constant Hessian AA, so it is convex exactly when A⪰0A \succeq 0. For AA with rows (2,1)(2, 1) and (1,2)(1, 2) the eigenvalues are 1 and 3: convex. For rows (1,2)(1, 2) and (2,1)(2, 1) they are 3 and −1-1, and the direction v=(1,−1)v = (1, -1), with v⊤Av=−2v^\top A v = -2, exposes it. With b=0b = 0, f(±v)=−1f(\pm v) = -1, while the midpoint has f(0)=0f(0) = 0, above the chord.

The log-sum-exp lse⁡(z)=log⁡∑iezi\operatorname{lse}(z) = \log\sum_i e^{z_i}, the normalizer of the softmax, has gradient p=softmax⁡(z)p = \operatorname{softmax}(z) and Hessian diag⁡(p)−pp⊤\operatorname{diag}(p) - pp^\top, the Jacobian of pp; both are derived in the matrix calculus note. 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 the right side is the variance of a random variable that takes the value viv_i with probability pip_i. For z=(1,2,3)z = (1, 2, 3), p=(0.090,0.245,0.665)p = (0.090, 0.245, 0.665), and the Hessian has eigenvalues 00, 0.1190.119 and 0.3710.371; for v=(1,0,−1)v = (1, 0, -1) both sides equal 0.4240.424. The zero eigenvalue belongs to v=(1,1,1)v = (1, 1, 1): adding a constant to every logit changes nothing, so lse is convex but not strictly.

Operations that preserve convexity#

Most losses are not checked from scratch. They are built from convex pieces by operations that keep convexity, each a line of algebra from the chord definition (Boyd and Vandenberghe, EE364a, lecture 3):

  • a non-negative weighted sum ∑iwifi\sum_i w_i f_i of convex functions is convex: add their chord inequalities with the weights wiw_i. An average loss over training examples plus a convex penalty is convex;
  • a composition f(Ax+b)f(Ax + b) with an affine map is convex, because the affine map sends a segment to a segment. A convex loss of a linear model’s prediction is convex in the weights;
  • a pointwise maximum max⁡ifi\max_i f_i of convex functions is convex: its epigraph is the intersection of their epigraphs. The hinge max⁡(0,1−m)\max(0, 1 - m) is the maximum of two affine functions, and for symmetric XX, λmax⁡(X)=max⁡∥v∥=1v⊤Xv\lambda_{\max}(X) = \max_{\lVert v\rVert = 1} v^\top X v (the Rayleigh quotient), a maximum of functions linear in XX, is convex in XX. The epigraph argument works for any family, finite or not.

A convex nondecreasing function of a convex function is convex. If gg is convex and h:R→Rh: \mathbb R \to \mathbb R is convex and nondecreasing, then

h(g(θx+(1−θ)y))≤h(θg(x)+(1−θ)g(y))≤θ h(g(x))+(1−θ) h(g(y)).\begin{aligned} h\big(g(\theta x + (1 - \theta)y)\big) &\le h\big(\theta g(x) + (1 - \theta) g(y)\big) \\ &\le \theta\, h(g(x)) + (1 - \theta)\, h(g(y)) . \end{aligned}

The first step uses that hh is nondecreasing, the second that hh is convex. So eg(x)e^{g(x)} is convex whenever gg is.

A product, a difference, or a composition in the wrong order need not be convex. The functions xx and −x-x are convex, but their product −x2-x^2 is not, and (ab−1)2(ab - 1)^2, the squared error of the product of two weights, is the counterexample of the section on losses below.

Why every local minimum is global#

A point x∗x^* is a local minimum if f(x∗)≤f(x)f(x^*) \le f(x) for all xx in some ball ∥x−x∗∥≤r\lVert x - x^*\rVert \le r with r>0r > 0. For a convex ff on a convex set CC, a local minimum is a global one: f(x∗)≤f(y)f(x^*) \le f(y) for every y∈Cy \in C.

Suppose some y∈Cy \in C had f(y)<f(x∗)f(y) \lt f(x^*). Walk a small fraction θ\theta of the way from x∗x^* towards yy, to z=(1−θ)x∗+θyz = (1 - \theta)x^* + \theta y. Since ∥z−x∗∥=θ∥y−x∗∥\lVert z - x^*\rVert = \theta\lVert y - x^*\rVert, any θ∈(0,1]\theta \in (0, 1] with θ≤r/∥y−x∗∥\theta \le r/\lVert y - x^*\rVert puts zz inside the ball. The point zz is in CC because CC is convex, and

f(z)≤(1−θ)f(x∗)+θf(y)convexity<(1−θ)f(x∗)+θf(x∗)=f(x∗).\begin{aligned} f(z) &\le (1 - \theta) f(x^*) + \theta f(y) && \text{convexity} \\ &\lt (1 - \theta) f(x^*) + \theta f(x^*) = f(x^*) . \end{aligned}

So zz is inside the ball and strictly lower than x∗x^*, which contradicts local minimality. The same chord gives two more facts. The set of minimizers is convex: if xx and yy both attain the minimum value, the chord keeps ff at or below it on the segment. And a strictly convex function has at most one minimizer, since two distinct ones would make the midpoint strictly lower than both.

Least squares with two identical feature columns shows both. For a target of 1 and a feature equal to 1, the loss (w1+w2−1)2(w_1 + w_2 - 1)^2 is convex, and its minimizers form the whole line w1+w2=1w_1 + w_2 = 1, a convex set; gradient descent stops somewhere on it, always at the minimum value 0. Its gradient 2(w1+w2−1)(1,1)2(w_1 + w_2 - 1)(1, 1) never changes w1−w2w_1 - w_2, and with the step 14\tfrac14 the sum s=w1+w2s = w_1 + w_2 moves to s−14⋅4(s−1)=1s - \tfrac14 \cdot 4(s - 1) = 1 in one step. The penalty λ(w12+w22)\lambda(w_1^2 + w_2^2) adds 2λI2\lambda I to the Hessian, which has rows (2,2)(2, 2) and (2,2)(2, 2), and so makes it positive definite and the loss strictly convex. Setting both partial derivatives 2(w1+w2−1)+2λwi2(w_1 + w_2 - 1) + 2\lambda w_i to zero forces w1=w2=ww_1 = w_2 = w with 2(2w−1)+2λw=02(2w - 1) + 2\lambda w = 0, so the one minimizer is w1=w2=1/(2+λ)w_1 = w_2 = 1/(2 + \lambda), which is 0.4760.476 for λ=0.1\lambda = 0.1.

Strong convexity and smoothness#

Convexity says the function lies above its tangents. Two quantitative versions say by how much, at least and at most, and they set the speed of every algorithm in the next note.

A differentiable ff is μ\mu-strongly convex, with μ>0\mu \gt 0, if it lies above each tangent by at least a parabola:

f(y)≥f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2.f(y) \ge f(x) + \nabla f(x)^\top(y - x) + \frac\mu2 \lVert y - x\rVert^2 .

Equivalently, h(x)=f(x)−μ2∥x∥2h(x) = f(x) - \frac\mu2\lVert x\rVert^2 is convex. Its gradient is ∇f(x)−μx\nabla f(x) - \mu x, and its tangent test, after moving the μ\mu terms to the right, adds μ2(∥y∥2−∥x∥2−2x⊤(y−x))=μ2∥y−x∥2\frac\mu2\big(\lVert y\rVert^2 - \lVert x\rVert^2 - 2x^\top(y - x)\big) = \frac\mu2\lVert y - x\rVert^2 to the tangent of ff. For twice differentiable ff the Hessian test for hh reads ∇2f(x)−μI⪰0\nabla^2 f(x) - \mu I \succeq 0 everywhere: every eigenvalue of the Hessian is at least μ\mu.

A differentiable ff is LL-smooth if its gradient is LL-Lipschitz, ∥∇f(x)−∇f(y)∥≤L∥x−y∥\lVert\nabla f(x) - \nabla f(y)\rVert \le L\lVert x - y\rVert; for twice continuously differentiable ff this means −LI⪯∇2f(x)⪯LI-LI \preceq \nabla^2 f(x) \preceq LI. One way, ∇2f(x)v\nabla^2 f(x)v is the limit of (∇f(x+tv)−∇f(x))/t\big(\nabla f(x + tv) - \nabla f(x)\big)/t, whose norm is at most L∥v∥L\lVert v\rVert, so every eigenvalue lies in [−L,L][-L, L]. The other way, ∇f(y)−∇f(x)=∫01∇2f(x+t(y−x))(y−x) dt\nabla f(y) - \nabla f(x) = \int_0^1 \nabla^2 f(x + t(y - x))(y - x)\, dt, and a symmetric matrix with eigenvalues in [−L,L][-L, L] stretches no vector by more than LL. The gradient cannot change faster than LL per unit of distance, so the function cannot rise above its tangent faster than a parabola of curvature LL:

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

To prove it, let d=y−xd = y - x, xt=x+tdx_t = x + td, and write Δ\Delta for the gap f(y)−f(x)−∇f(x)⊤df(y) - f(x) - \nabla f(x)^\top d between the function and its tangent:

Δ=∫01(∇f(xt)−∇f(x))⊤d dt≤∫01∥∇f(xt)−∇f(x)∥ ∥d∥ dt≤∫01L t ∥d∥2 dt=L2∥d∥2.\begin{aligned} \Delta &= \int_0^1 \big(\nabla f(x_t) - \nabla f(x)\big)^\top d\, dt \\ &\le \int_0^1 \lVert\nabla f(x_t) - \nabla f(x)\rVert\, \lVert d\rVert\, dt \\ &\le \int_0^1 L\, t\, \lVert d\rVert^2\, dt = \frac L2 \lVert d\rVert^2 . \end{aligned}
  1. The fundamental theorem of calculus for t↦f(xt)t \mapsto f(x_t), whose derivative is ∇f(xt)⊤d\nabla f(x_t)^\top d; the constant ∇f(x)⊤d\nabla f(x)^\top d integrates to itself.
  2. The Cauchy–Schwarz inequality.
  3. The Lipschitz gradient, with ∥xt−x∥=t∥d∥\lVert x_t - x\rVert = t\lVert d\rVert.

This is the route of Bubeck (2015), Lemma 3.4. A function that is both strongly convex and smooth is squeezed between two parabolas around every tangent, of curvatures μ\mu and LL. Their ratio κ=L/μ≥1\kappa = L/\mu \ge 1 is the condition number of ff. For a quadratic it is λmax⁡(A)/λmin⁡(A)\lambda_{\max}(A)/\lambda_{\min}(A), the condition number of the matrix that the conditioning note studies from the numerical side.

For f(x)=12x⊤Axf(x) = \tfrac12x^\top A x with AA of rows (2,1)(2, 1) and (1,2)(1, 2), μ=1\mu = 1, L=3L = 3 and κ=3\kappa = 3. From x=0x = 0 to y=(1,0)y = (1, 0) the tangent is 0, f(y)=1f(y) = 1, and the two parabolas give 0.5≤1≤1.50.5 \le 1 \le 1.5.

How far from the minimum?#

Strong convexity turns a small gradient into a guarantee. Write the lower parabola at xx towards the minimizer x∗x^*, with f∗=f(x∗)f^* = f(x^*). A value is at least the minimum over all points, and the parabola f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2f(x) + \nabla f(x)^\top(y - x) + \frac\mu2\lVert y - x\rVert^2 is smallest where its gradient ∇f(x)+μ(y−x)\nabla f(x) + \mu(y - x) vanishes, at y−x=−∇f(x)/μy - x = -\nabla f(x)/\mu. There it equals f(x)−∥∇f(x)∥2/μ+∥∇f(x)∥2/(2μ)f(x) - \lVert\nabla f(x)\rVert^2/\mu + \lVert\nabla f(x)\rVert^2/(2\mu):

f∗≥f(x)+∇f(x)⊤(x∗−x)+μ2∥x∗−x∥2≥min⁡y(f(x)+∇f(x)⊤(y−x)+μ2∥y−x∥2)=f(x)−12μ∥∇f(x)∥2.\begin{aligned} f^* &\ge f(x) + \nabla f(x)^\top(x^* - x) + \frac\mu2\lVert x^* - x\rVert^2 \\ &\ge \min_{y} \Big( f(x) + \nabla f(x)^\top(y - x) + \frac\mu2\lVert y - x\rVert^2 \Big) \\ &= f(x) - \frac{1}{2\mu}\lVert\nabla f(x)\rVert^2 . \end{aligned}

Rearranged, f(x)−f∗≤∥∇f(x)∥2/(2μ)f(x) - f^* \le \lVert\nabla f(x)\rVert^2/(2\mu). This is the Polyak–Łojasiewicz inequality, and the next note turns it into the linear convergence rate of gradient descent. For the quadratic above at x=(1,1)x = (1, 1), f=3f = 3 and ∇f=(3,3)\nabla f = (3, 3), and the bound is 18/2=9≥318/2 = 9 \ge 3. The inequality can hold without convexity: Merkulov (2024) shows x2+3sin⁡2xx^2 + 3\sin^2 x, whose second derivative 2+6cos⁡2x2 + 6\cos 2x dips to −4-4. Karimi, Nutini and Schmidt (2016) state that it satisfies the inequality with μ=1/32\mu = 1/32 (Section 2.2) and build linear-rate proofs on the inequality alone (Theorem 1).

The ridge penalty λ∥w∥2\lambda\lVert w\rVert^2 adds 2λI2\lambda I to any Hessian, so a convex loss plus ridge is 2λ2\lambda-strongly convex. The regularization note uses the penalty for statistics; it also fixes the geometry. The logistic loss has Hessian 1nX⊤SX\frac1n X^\top S X with S=diag⁡(pi(1−pi))S = \operatorname{diag}(p_i(1 - p_i)), from the matrix calculus note. Since p(1−p)=14−(p−12)2≤14p(1 - p) = \tfrac14 - (p - \tfrac12)^2 \le \tfrac14, we get v⊤X⊤SXv≤14∥Xv∥2≤14λmax⁡(X⊤X)∥v∥2v^\top X^\top S X v \le \tfrac14\lVert Xv\rVert^2 \le \tfrac14\lambda_{\max}(X^\top X)\lVert v\rVert^2, so the loss is LL-smooth with L=λmax⁡(X⊤X)/(4n)L = \lambda_{\max}(X^\top X)/(4n). Far from the origin the predictions saturate: along w=t w0w = t\,w_0 every pi(1−pi)p_i(1 - p_i) with w0⊤xi≠0w_0^\top x_i \ne 0 tends to 0 as t→∞t \to \infty, so the smallest Hessian eigenvalue tends to 0, and without a penalty no μ>0\mu \gt 0 works everywhere.

Which machine learning losses are convex#

The tests above settle the standard losses of linear models, all in the weights ww for fixed data:

LossWhy it is convexStrongly convex?
least squares 1n∥Xw−y∥2\frac1n\lVert X w - y\rVert^2Hessian 2nX⊤X⪰0\frac2n X^\top X \succeq 0 (least squares)if XX has full column rank: v⊤X⊤Xv=∥Xv∥2>0v^\top X^\top X v = \lVert Xv\rVert^2 \gt 0 for v≠0v \ne 0
logistic 1n∑ilog⁡(1+e−yiw⊤xi)\frac1n\sum_i \log(1 + e^{-y_i w^\top x_i})convex function of an affine map (logistic regression)no, only with a penalty
softmax cross-entropylse of an affine map minus a linear termno: shifting all logits is flat
hinge 1n∑imax⁡(0,1−yiw⊤xi)\frac1n\sum_i \max(0, 1 - y_i w^\top x_i)maximum of affine functions (SVM)no; not differentiable either
ridge λ∥w∥2\lambda\lVert w\rVert^2, lasso λ∥w∥1\lambda\lVert w\rVert_1a squared norm, a normridge yes (2λ2\lambda), lasso no

A neural network composes linear maps with nonlinear activations, and its loss as a function of all the weights is not convex. The smallest example is a “network” with one hidden unit and no activation, y^=a b x\hat y = a\,b\,x, fitted to one example x=y=1x = y = 1:

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

Both (1,1)(1, 1) and (−1,−1)(-1, -1) fit exactly, with loss 0, but their midpoint (0,0)(0, 0) has loss 1, above the chord. The Hessian there has eigenvalues 22 and −2-2: the origin is a saddle point with zero gradient. The same sign symmetry, and the permutation symmetry of hidden units, holds in every network, as the deep learning course works out. Matrix factorization, kk-means and mixture models are non-convex for the same reason: a model with interchangeable parts has several equivalent minimizers and worse points between them. For these, the guarantees of the next note shrink to “the gradient becomes small”, and the starting point matters.

Where it breaks#

Convexity does not promise that a minimum exists. Logistic regression on one point x=1x = 1 with label y=1y = 1 has the loss f(w)=log⁡(1+e−w)f(w) = \log(1 + e^{-w}), convex and decreasing towards 0, a value no ww attains. Gradient descent with step 1 never stops moving:

Stepswwf(w)f(w)
102.262.269.9⋅10−29.9 \cdot 10^{-2}
1004.594.591.0⋅10−21.0 \cdot 10^{-2}
1,0006.916.911.0⋅10−31.0 \cdot 10^{-3}
10,0009.219.211.0⋅10−41.0 \cdot 10^{-4}

The weight grows like log⁡k\log k and the loss falls like 1/k1/k, as the fold derives. The same happens on any linearly separable data set: if yiw⊤xi>0y_i w^\top x_i \gt 0 for every ii, each term log⁡(1+e−t yiw⊤xi)\log(1 + e^{-t\,y_i w^\top x_i}) decreases as tt grows, so scaling up a separating ww always lowers the loss, which stays positive. A ridge penalty restores a minimizer. The penalized loss is at least λ∥w∥2\lambda\lVert w\rVert^2, so outside a large enough ball it exceeds its value at w=0w = 0; on that closed ball a continuous function attains its minimum.

Derivation: why the weight grows like log k

The step is wk+1=wk+1/(1+ewk)w_{k+1} = w_k + 1/(1 + e^{w_k}). Put uk=ewku_k = e^{w_k}, so u0=1u_0 = 1 and uk+1=uk e1/(1+uk)u_{k+1} = u_k\, e^{1/(1 + u_k)}. The weight only grows, so uk≥1u_k \ge 1 and the exponent τ=1/(1+uk)\tau = 1/(1 + u_k) lies in (0,12](0, \tfrac12]. From 1+τ≤eτ≤1+τeτ1 + \tau \le e^\tau \le 1 + \tau e^\tau, the tangent bound and the mean value theorem:

uk+1−uk≥uk1+uk≥12,uk+1−uk≤uk1+uk e1/2≤e1/2.\begin{aligned} u_{k+1} - u_k &\ge \frac{u_k}{1 + u_k} \ge \frac12 , \\ u_{k+1} - u_k &\le \frac{u_k}{1 + u_k}\, e^{1/2} \le e^{1/2} . \end{aligned}

So 1+k/2≤uk≤1+1.65 k1 + k/2 \le u_k \le 1 + 1.65\,k: wk=log⁡ukw_k = \log u_k is log⁡k\log k up to a bounded term, and the loss log⁡(1+1/uk)\log(1 + 1/u_k), which lies between 1/(2uk)1/(2u_k) and 1/uk1/u_k because x−x2/2≤log⁡(1+x)≤xx - x^2/2 \le \log(1 + x) \le x and x=1/uk≤1x = 1/u_k \le 1, falls like 1/k1/k.

Convexity also says nothing about speed: a convex function can be very flat or badly conditioned, and the next note shows that the number of gradient steps grows with κ\kappa. And the hinge and the L1 norm are convex but not differentiable at their kinks. There the tangent bound becomes a family of supporting lines, the subgradients, and gradient methods need their non-smooth variants.

What you should remember#

  • Convex set: contains every chord. Convex function: every chord lies above the graph.
  • Equivalent tests: the tangent lies below, f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top(y - x); or ∇2f⪰0\nabla^2 f \succeq 0 everywhere.
  • Sums, affine substitutions, maxima and convex nondecreasing outer functions keep convexity.
  • For a convex ff, every local minimum is global, and ∇f=0\nabla f = 0 certifies it.
  • μI⪯∇2f⪯LI\mu I \preceq \nabla^2 f \preceq LI, κ=L/μ\kappa = L/\mu, and f(x)−f∗≤∥∇f(x)∥2/(2μ)f(x) - f^* \le \lVert\nabla f(x)\rVert^2/(2\mu).
  • Linear models with standard losses are convex; neural networks, factorizations and mixtures are not.

Code#

The opening function, the chord test on three convex losses and one non-convex one, the tangent and Hessian tests, the parabola sandwich, the Polyak–Łojasiewicz bound, and logistic regression without a minimizer, with NumPy. Exact digits can differ slightly across platforms and library versions.

import numpy as np
rng = np.random.default_rng(0)

# the opening function f(x) = x^4 - 2x^2 + x/2 has two local minima; gradient descent finds the one near its start
f = lambda x: x**4 - 2 * x**2 + 0.5 * x
df = lambda x: 4 * x**3 - 4 * x + 0.5
crit = np.sort(np.roots([4, 0, -4, 0.5]).real)
print(crit.round(3), f(crit).round(3), (12 * crit**2 - 4).round(2))   # stationary points, values, f''
for x in (1.5, -1.5):
    x0 = x
    for _ in range(2000):
        x -= 0.05 * df(x)
    print(x0, round(x, 3), round(f(x), 3))
# [-1.057  0.127  0.93 ] [-1.515  0.032 -0.517] [ 9.42 -3.81  6.39]
# 1.5 0.93 -0.517 | -1.5 -1.057 -1.515

# the chord test f(t u + (1 - t) v) <= t f(u) + (1 - t) f(v) on 10,000 random pairs
X = rng.normal(size=(50, 3)); y = np.sign(rng.normal(size=50))
losses = {
    'least squares': lambda w: np.mean((X @ w - y) ** 2),
    'logistic': lambda w: np.mean(np.logaddexp(0, -y * (X @ w))),
    'hinge': lambda w: np.mean(np.maximum(0, 1 - y * (X @ w))),
    '(ab - 1)^2': lambda w: (w[0] * w[1] - 1) ** 2,
}
for name, L in losses.items():
    bad = 0
    for _ in range(10_000):
        u, v, t = rng.normal(size=3) * 2, rng.normal(size=3) * 2, rng.uniform()
        bad += L(t * u + (1 - t) * v) > t * L(u) + (1 - t) * L(v) + 1e-12
    print(name, bad)
# least squares 0 | logistic 0 | hinge 0 | (ab - 1)^2 1779

# (ab - 1)^2: two minimizers, a worse midpoint, and an indefinite Hessian there
g = losses['(ab - 1)^2']
print(g(np.array([1., 1, 0])), g(np.array([-1., -1, 0])), g(np.zeros(3)), np.linalg.eigvalsh([[0, -2], [-2, 0]]))
# 0.0 0.0 1.0 [-2.  2.]

# the tangent lies below: e^y >= 1 + y; at the local minimum of the opening function the flat tangent does not
print(np.exp(1.0) - 2, f(crit[0]) - f(crit[2]))
# 0.7182818284590451 -0.9980051329904565

# the Hessian of log-sum-exp is diag(p) - p p^T, a covariance matrix
z = np.array([1., 2, 3]); p = np.exp(z) / np.exp(z).sum()
H = np.diag(p) - np.outer(p, p); v = np.array([1., 0, -1])
print(p.round(4), np.linalg.eigvalsh(H).round(4), round(v @ H @ v, 4), round(p @ v**2 - (p @ v) ** 2, 4))
# [0.09   0.2447 0.6652] [-0.      0.1186  0.3709] 0.4244 0.4244

# mu and L of a quadratic, and the sandwich mu/2 |d|^2 <= f(x + d) - f(x) - grad^T d <= L/2 |d|^2
A = np.array([[2., 1], [1, 2]]); mu, L = np.linalg.eigvalsh(A)
q = lambda x: 0.5 * x @ A @ x
ok = 0
for _ in range(10_000):
    x, d = rng.normal(size=2), rng.normal(size=2)
    gap = q(x + d) - q(x) - (A @ x) @ d
    ok += mu / 2 * d @ d - 1e-12 <= gap <= L / 2 * d @ d + 1e-12
print(mu, L, L / mu, ok)
# 1.0 3.0 3.0 10000

# the Polyak-Lojasiewicz bound f(w) - f* <= |grad f(w)|^2 / (2 mu) for logistic loss + (lam/2)|w|^2, mu = lam
lam = 0.1
F = lambda w: np.mean(np.logaddexp(0, -y * (X @ w))) + lam / 2 * w @ w
G = lambda w: -X.T @ (y / (1 + np.exp(y * (X @ w)))) / len(y) + lam * w
w = np.zeros(3)
for _ in range(20):                                   # Newton's method finds the minimizer to machine precision
    p_ = 1 / (1 + np.exp(-(X @ w)))
    w -= np.linalg.solve((X.T * (p_ * (1 - p_))) @ X / len(y) + lam * np.eye(3), G(w))
Fs = F(w)
ratios = [(F(u) - Fs) / (G(u) @ G(u) / (2 * lam)) for u in rng.normal(size=(1000, 3)) * 3]
print(round(max(ratios), 3))                           # at most 1: the bound holds at every sampled point
# 0.9

# a non-convex function with the PL property: x^2 + 3 sin^2 x
t = np.linspace(-20, 20, 400_001); t = t[np.abs(t) > 1e-3]
print(round((2 + 6 * np.cos(2 * t)).min(), 3), round(((2 * t + 3 * np.sin(2 * t)) ** 2 / (2 * (t**2 + 3 * np.sin(t) ** 2))).min(), 3))
# -4.0 0.176: f'' dips to -4, yet f'^2 >= 2 * 0.176 * (f - f*) on the whole range

# the convex hull of (0,0), (3,0), (0,3): weights of (1,1) and of (2,2)
P = np.array([[0., 3, 0], [0, 0, 3], [1, 1, 1]])      # columns: the points, with a row of ones for sum = 1
print(np.linalg.solve(P, [1, 1, 1]).round(3), np.linalg.solve(P, [2, 2, 1]).round(3))
# [0.333 0.333 0.333] [-0.333  0.667  0.667]

# no minimizer: logistic loss log(1 + e^-w) on one separable point, gradient descent with step 1
w = 0.0
for k in range(1, 10_001):
    w += 1 / (1 + np.exp(w))
    if k in (10, 100, 1000, 10_000):
        print(k, round(w, 2), f'{np.log1p(np.exp(-w)):.1e}', end=' | ')
print()
# 10 2.26 9.9e-02 | 100 4.59 1.0e-02 | 1000 6.91 1.0e-03 | 10000 9.21 1.0e-04

Next#

Convexity says that the bottom of the bowl can be recognized. It does not say how to get there, or how many steps it takes. The next note proves how fast gradient descent converges on smooth and on strongly convex functions, with the condition number κ\kappa setting the speed, and derives Newton’s method from the quadratic model, which removes κ\kappa at the price of solving a linear system per step.

References#