Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsOptimization

Convex vs. non-convex

P1.F230 min●●○ IntermediateReviewed Dec 1, 2026

Why this matters for machine learning

Convexity decides whether training is easy or risky. On a convex loss, every low point is the lowest point, so gradient descent cannot get trapped. On a non-convex loss, where you end depends on where you start.

  • The squared-error loss of linear regression is convex, so the best line is found reliably, whether by a formula or by gradient descent. (P3.A2: Linear regression)
  • The loss of logistic regression is convex too, which is why a simple classifier can be trained without worrying about the starting point. (P3.A7: Logistic regression)
  • A neural network’s loss is not convex, so the random starting weights matter, and that is why initialization gets its own lesson. (P4.12: Weight initialization)
  • Adding an L2 penalty to a convex loss makes it strictly convex, which gives a single best answer even when the data alone do not. (P3.A6: Regularization: L1 and L2)

Before this: The Hessian, curvature and Taylor approximation, What is optimization?

After this lesson you can

  • State the chord rule for a convex function and use it to test small examples
  • Test convexity with the second derivative (one variable) and the Hessian (several variables)
  • Prove that on a convex function every local minimum is global, and say what else convexity guarantees
  • Recognize the linear and logistic regression losses as convex, and say why a neural-network loss is not
  • Describe local minima, saddle points, plateaus and basins, and predict where gradient descent ends from a given start
  • Say what is known and what is not about saddles and local minima in large networks

Put a marble anywhere inside a round bowl and let go. It rolls to the bottom, and the bottom is the same place every time. Now put the marble on a hilly field with two valleys. It rolls into one of them, and which one depends on where you let go.

Training a model is a marble rolling on a loss landscape (P1.F1). So the shape of the landscape decides how much trouble training will be. Convex landscapes are the bowl: one low region, no traps. Non-convex landscapes are the hilly field: valleys, passes and flat patches. This lesson gives you a test that tells the two apart, shows what the bowl guarantees, and then lets you roll a ball and watch what the hilly field does.

Convex sets: no dents

Start with shapes, because the word comes from there. A set of points is convex if the straight segment between any two of its points stays inside the set.

ABconvexABnot convex
Left: an oval is a convex set, because the segment between any two points inside it stays inside. Right: an L-shaped region is not convex. The segment between the two marked points leaves the shape, and the part outside is drawn in red.

A disc, a square, a straight line and the whole plane are convex. A ring, a crescent and the letter L are not: each has a dent, and a segment can cross the dent. For a\mathbf{a} and b\mathbf{b} in a set, the points on the segment between them are λa+(1−λ)b\lambda\mathbf{a}+(1-\lambda)\mathbf{b} with λ\lambda from 00 to 11. At λ=1\lambda=1 you are at a\mathbf{a}, at λ=0\lambda=0 at b\mathbf{b}, and at λ=12\lambda=\tfrac12 at the midpoint. That mix is the only formula you need.

Convex functions: the chord lies above the graph

Take a curve and two points on it. The straight line segment joining them is the chord. If the chord never dips below the curve between the two points, the curve is bowl-shaped there. A function ff is convex when this holds for every pair of points:

f(λx+(1−λ)y)≤ λf(x)+(1−λ)f(y)for all x,y, 0≤λ≤1\begin{gathered} f\big(\lambda x+(1-\lambda)y\big) \\ \le\ \lambda f(x)+(1-\lambda)f(y) \\ \text{for all } x, y,\ 0\le\lambda\le 1 \end{gathered}

The left side is the height of the curve at a point between xx and yy. The right side is the height of the chord at the same place. (A function is only called convex when its domain is a convex set, such as an interval, so that every point between xx and yy is allowed.) Two numbers make it concrete. Take f(x)=x2f(x)=x^2 with x=−1x=-1 and y=3y=3:

  • At λ=12\lambda=\tfrac12 the point between them is 11. The curve is f(1)=1f(1)=1. The chord is 12(1)+12(9)=5\tfrac12(1)+\tfrac12(9)=5. And 1≤51\le 5.
  • At λ=14\lambda=\tfrac14 the point is 14(−1)+34(3)=2\tfrac14(-1)+\tfrac34(3)=2. The curve is f(2)=4f(2)=4. The chord is 14(1)+34(9)=7\tfrac14(1)+\tfrac34(9)=7. And 4≤74\le 7.

Now a curve that fails: f(x)=(x2−1)2f(x)=(x^2-1)^2, which has two valleys at x=−1x=-1 and x=1x=1. Both ends of the chord have height 00, so the chord runs along the axis at height 00. The midpoint x=0x=0 has f(0)=1f(0)=1, which is above the chord. The rule 1≤01\le 0 is false, so this ff is not convex. One failing pair is enough.

Some words that go with this:

  • Strictly convex: the chord is strictly above the curve for every pair x≠yx\ne y and every λ\lambda strictly between 00 and 11, so “≤\le” becomes “<<”. x2x^2 is strictly convex. A straight line is convex but not strictly, because its chord lies on it.
  • Concave: the opposite, the chord lies on or below the curve. ff is concave exactly when −f-f is convex. ln⁡x\ln x and −x2-x^2 are concave.
  • A function can be convex without being smooth. ∣x∣|x| has a corner at 00, has no derivative there, and is still convex. The chord from −1-1 to 33 has height 1+32=2\tfrac{1+3}{2}=2 at the midpoint x=1x=1, where ∣x∣=1|x|=1.

The chord rule is the two-point case of Jensen’s inequality from P1.D4. If XX equals xx with probability λ\lambda and yy with probability 1−λ1-\lambda, then E[f(X)]=λf(x)+(1−λ)f(y)\mathbb{E}[f(X)]=\lambda f(x)+(1-\lambda)f(y) and f(E[X])=f(λx+(1−λ)y)f(\mathbb{E}[X])=f(\lambda x+(1-\lambda)y), so the rule says E[f(X)]≥f(E[X])\mathbb{E}[f(X)]\ge f(\mathbb{E}[X]) for convex ff. P1.D4 stated it for any random variable; the chord rule is the version you can draw.

Tests you can run

Drawing chords checks pairs one at a time. Calculus gives faster tests.

One variable: the second derivative. If f′′(x)≥0f''(x)\ge 0 at every xx in an interval, then ff is convex there (P1.C8: the second derivative is the curvature, and f′′≥0f''\ge 0 means the curve bends upward or is straight).

  • x2x^2: f′′=2>0f''=2>0. Convex.
  • exe^x: f′′=ex>0f''=e^x>0. Convex.
  • −ln⁡x-\ln x for x>0x>0: f′′=1/x2>0f''=1/x^2>0. Convex.
  • x4x^4: f′′=12x2≥0f''=12x^2\ge 0. Convex, and even strictly convex, although f′′(0)=0f''(0)=0. So “f′′>0f''>0 everywhere” is enough for strict convexity but not needed for it.
  • sin⁡x\sin x: f′′=−sin⁡xf''=-\sin x, which is negative on (0,π)(0,\pi). Not convex.
  • (x2−1)2(x^2-1)^2: f′′=12x2−4f''=12x^2-4, which is negative for ∣x∣<0.577|x|<0.577. Not convex.

Several variables: the Hessian. If the Hessian matrix H\mathbf{H} (P1.C8) is positive semidefinite at every point, then ff is convex. Positive semidefinite means that every eigenvalue is ≥0\ge 0 (P1.B12), which says the surface curves up, or stays flat, in every direction. The words “at every point” matter. A single point with a good Hessian shows only that the ground is bowl-shaped right there.

  • x2+y2x^2+y^2: eigenvalues 2,22, 2. Convex.
  • The stretched bowl x2+10y2x^2+10y^2 of P1.F3: eigenvalues 2,202, 20. Convex.
  • The P0.2 loss: eigenvalues 11.0911.09 and 0.240.24 at every point (its Hessian is constant), both positive. Convex.
  • The saddle x2−y2x^2-y^2: eigenvalues 2,−22, -2. Not convex.
  • The Two valleys landscape of P1.F1: the Hessian is diag(12x2−4, 2)\mathrm{diag}(12x^2-4,\ 2). At x=1x=1 it is diag(8,2)\mathrm{diag}(8,2), positive, but at x=0x=0 it is diag(−4,2)\mathrm{diag}(-4,2), with a negative eigenvalue. The landscape is bowl-shaped near each valley floor and still not convex.

The tangent test. A differentiable ff is convex exactly when its tangent line never rises above its graph: f(y)≥f(x)+f′(x)(y−x)f(y)\ge f(x)+f'(x)(y-x) for all x,yx, y. For x2x^2 at x=1x=1 the tangent is 1+2(y−1)1+2(y-1). At y=3y=3 it gives 55, and f(3)=9≥5f(3)=9\ge 5. Hold on to this form: it explains the next section, because if the slope at xx is 00 the tangent is flat, and the rule says that f(y)≥f(x)f(y)\ge f(x) for every yy.

Go deeper Why a line test is enough for several variables

A function of several variables is convex exactly when its restriction to every straight line is convex. Pick two points a\mathbf{a} and b\mathbf{b} and look at g(t)=f(a+t(b−a))g(t)=f(\mathbf{a}+t(\mathbf{b}-\mathbf{a})), a function of one number. The chord rule for ff between a\mathbf{a} and b\mathbf{b} is exactly the chord rule for gg between t=0t=0 and t=1t=1. The second derivative of gg is (b−a)⊤H(b−a)(\mathbf{b}-\mathbf{a})^\top\mathbf{H}(\mathbf{b}-\mathbf{a}), the curvature along that direction (P1.C8), so g′′≥0g''\ge 0 for every direction exactly when H\mathbf{H} is positive semidefinite. This is why the Hessian test works, and why the chord test in the widget below, which looks along one line, is a faithful picture of the general case.

Building convex functions

Checking every formula from scratch would be tiring. A few rules let you build convex functions from simple ones:

  • A sum of convex functions is convex, and so is a nonnegative multiple of one.
  • Composing with an affine map (a function of the form Aθ+b\mathbf{A}\boldsymbol{\theta}+\mathbf{b}, “a linear map plus a shift”) keeps convexity: if gg is convex, so is f(θ)=g(Aθ+b)f(\boldsymbol{\theta})=g(\mathbf{A}\boldsymbol{\theta}+\mathbf{b}).
  • A maximum of convex functions is convex. For example ∣x∣=max⁡(x,−x)|x|=\max(x,-x).

Here is what they give. Take the loss of the line y^=wx+b\hat{y}=wx+b on the P0.2 points. For one example the error wxi+b−yiwx_i+b-y_i is an affine function of the parameters (w,b)(w,b). Squaring it is a convex function (u2u^2) composed with an affine map, so it is convex. The mean squared error is a sum of such terms times 13\tfrac13, so it is convex. This works for any data and any number of features, which is why the mean squared error of a linear model is always convex in its parameters. Its Hessian is 23A⊤A\tfrac23\mathbf{A}^\top\mathbf{A} with A\mathbf{A} the matrix of inputs, and every matrix of the form A⊤A\mathbf{A}^\top\mathbf{A} is positive semidefinite (P1.B10).

The same rules cover logistic regression (P3.A7, preview). For an example with label 11 the loss is −ln⁡σ(z)-\ln\sigma(z) with z=w⋅x+bz=\mathbf{w}\cdot\mathbf{x}+b, an affine function of the parameters. As a function of zz, its second derivative is σ(z)(1−σ(z))\sigma(z)\big(1-\sigma(z)\big), which lies between 00 and 0.250.25 and is positive. So each term is convex, and the sum over the training set is too. (P1.D10 derived this loss as a negative log-likelihood.)

The rules do not work in reverse. A difference of convex functions, a product, or a minimum of convex functions need not be convex: min⁡(x2,(x−2)2)\min(x^2,(x-2)^2) is 00 at x=0x=0 and at x=2x=2 but 11 at x=1x=1. And composing with something that is not affine can break it. The tiny model y^=w1w2x\hat{y}=w_1w_2x of P1.F1 has loss (w1w2−2)2(w_1w_2-2)^2, a square of the non-affine w1w2−2w_1w_2-2. It is not convex: both (1,2)(1,2) and (−1,−2)(-1,-2) have loss 00, but their midpoint (0,0)(0,0) has loss 44, so the chord rule fails. Two weights multiplied are the smallest picture of why stacking layers can destroy convexity.

Why convexity matters

Four facts make convex landscapes friendly. In each, ff is convex.

1. Every local minimum is a global minimum. The proof is the chord rule. Suppose xx is a local minimum, so nothing close to xx is lower, and suppose some far point yy were lower: f(y)<f(x)f(y)<f(x). Take the points zt=(1−t)x+tyz_t=(1-t)x+ty on the segment from xx toward yy. The chord rule gives f(zt)≤(1−t)f(x)+tf(y)<f(x)f(z_t)\le(1-t)f(x)+tf(y)<f(x) for every tt in (0,1](0,1]. For small tt the point ztz_t is as close to xx as you like, yet lower than f(x)f(x). That contradicts “nothing close to xx is lower”. So no lower yy exists.

2. A point with zero gradient is a global minimum. If ff is differentiable and ∇f(x)=0\nabla f(\mathbf{x})=\mathbf{0}, the tangent test gives f(y)≥f(x)+0⋅(y−x)=f(x)f(\mathbf{y})\ge f(\mathbf{x})+\mathbf{0}\cdot(\mathbf{y}-\mathbf{x})=f(\mathbf{x}) for every y\mathbf{y}. On a convex function a flat point cannot be a hilltop or a saddle.

3. The set of minimizers is convex. If f(a)=f(b)=mf(a)=f(b)=m is the lowest value, the chord rule gives ff at every point between them ≤m\le m, so it equals mm. The lowest points form a single connected piece: one point, a segment, or a flat floor, not two separate pits. And if ff is strictly convex there is at most one minimizer.

4. Gradient descent works. Suppose ff is convex and differentiable, has a minimizer, and its slope does not change faster than a rate LL (for the quadratic losses in this course, LL is the largest Hessian eigenvalue). Then gradient descent (P1.F3) with step size η≤1/L\eta\le 1/L drives f(xk)f(\mathbf{x}_k) toward the minimum value from any starting point. For the P0.2 loss, L=11.09L=11.09 and 1/L=0.091/L=0.09. P1.C8 found that for this quadratic the sharper limit is η<2/L=0.18\eta<2/L=0.18.

Two limits keep the picture honest. First, convex does not promise that a minimum exists. exe^x is convex and has none. The separable logistic loss of P1.F1 is convex, and its weights are pushed to infinity. Second, convexity says nothing about speed. A very stretched convex bowl is easy to solve in principle and slow for plain gradient descent (P1.F3, P1.F5).

Roll a ball

Now the other side. The widget below runs gradient descent (P1.F3: repeat “subtract η\eta times the slope”) from a start you choose. You do not need more of P1.F3 than that.

f(x) = (x² − 1)² + 0.3x (the Two valleys landscape of P1.F1 along y = 0)

−1.5−1−0.500.511.5−101234xf(x)GLH
ends at the global minimum ends at a worse (local) minimum does not end at a minimum not finished after 3000 steps (still moving)

The strip along the bottom shows where the ball would end if it started there, for η = 0.05. Markers: G global minimum, L local minimum, H hilltop (maximum), B flat bend, S saddle (the pass). The ring is the start, the dot is the ball.

Settled at a local minimum, x = 0.96, where f = 0.294, after 33 steps. It is not the lowest point: the global minimum is lower (f = −0.305). The slope is 0 here, so gradient descent cannot tell.

start x = 1.4
end x = 0.96
end value f = 0.294
steps = 33
which minimum: local minimum, not the lowest
curvature at the end, f″ = 7.06 (positive: curves up)

Chord test: is the curve always below the straight line between two of its points?

The curve rises above the chord at x = 0, by 0.81. One pair that fails is enough to prove the curve is not convex.

Gradient descent from a start you choose. The ring is the start (drag it, tap the curve, or use the slider or arrow keys), the dot is the ball and the blue line is its path. The strip along the bottom is the basin map: green where a start ends at the global minimum, red hatching where it ends at a worse minimum, grey dots where it does not end at a minimum, blue lines where it is still moving after 3000 steps. The chord test below the readouts draws a chord between two points of the curve.

Things to try:

  1. Bowl. Drag the start anywhere. The end is x=0x=0 wherever you start, the strip is green all along, and the chord test passes for every pair. Press Search for a failing pair: it checks about 1,6001{,}600 pairs of points (a grid of 6161 points) and finds none. That is what “no traps” looks like. (One passing pair would prove nothing, but the search over many pairs plus the second derivative f′′=2f''=2 does.)
  2. Double well (made up). Start at −0.5-0.5 and then at 0.50.5: the ball goes to a different valley each time, and both valleys are equally low (f=0f=0), so both are global minima. The strip is green all the way across, because both valleys are global minima; which valley the ball ends in still depends on which side of x=0x=0 it starts. Press Start exactly on the hilltop: the slope at 00 is exactly zero, so the step is zero, and the ball stays put. Move the start to 0.010.01 and it rolls to the right. The default chord from −1-1 to 11 fails, and the red bar marks how far the curve rises above it (by 11, at x=0x=0).
  3. Two valleys, slice of P1.F1. This is the Two valleys landscape cut along y=0y=0. From x=1.4x=1.4 with η=0.05\eta=0.05 the ball ends at x≈0.960x\approx 0.960 with f≈0.294f\approx 0.294 after 33 steps. That is the local minimum, and the status says a lower one exists. From x=−0.5x=-0.5 it ends at x≈−1.036x\approx-1.036 with f≈−0.305f\approx-0.305 after 30 steps, the global minimum. The strip changes colour at about x=0.075x=0.075, the hilltop that P1.F1 called the pass. Drag the start across it.
  4. Step size matters. On the same curve, raise η\eta. The left valley floor becomes unstable above about 0.230.23 (2/f′′=2/8.872/f''=2/8.87) and the right one above about 0.280.28 (2/7.062/7.06), so many starts end up hopping between valleys: at η=0.25\eta=0.25 the ball started at 1.41.4 does not settle, and at 0.30.3 no start settles. At the other end, a very small η\eta such as 0.010.01 is slow: the colours show where a start would end, and the plateau curve then shows a fourth colour for starts that are still moving after 3000 steps. Gradient descent also needs a small enough η\eta to behave (P1.F3), so the basin map belongs to a particular η\eta.
  5. Plateau and flat bend (made up). This curve is not convex (its chord test fails), yet it has only one minimum, at x=0x=0. At x=1x=1 it has a flat bend: the slope is 00 there but the curve keeps rising to the right. Start at 0.50.5 and the ball reaches 00 in 143 steps. Start at 1.51.5: the ball creeps toward x=1x=1 and after 3000 steps it is still at x≈1.003x\approx1.003, slowing down as the slope fades. It will not get past the bend. The strip is grey to the right of 11.
  6. 2-D map of P1.F1. The whole landscape seen from above, with contour lines. The colours show the same split, now as left and right halves divided near x≈0.075x\approx0.075. Start at (1.4,0.8)(1.4,0.8) and the ball ends in the right valley after 136 steps. Press Start on the pass line: the ball slides along yy to the pass at x≈0.075x\approx 0.075 and stops there after 138 steps. It is a flat point, not a minimum. Press Start exactly at the pass: it stays put.

Non-convex landscapes

When convexity fails, four things can happen. You met each of them in the widget.

Local minima. A valley floor that is higher than another one. Gradient descent stops there, because the slope is zero and nothing in the update says that a lower valley exists. The status line of the widget reports it.

Basins of attraction. The set of starting points that lead to a given minimum is its basin. On the two-valley curve the basins are the two sides of the hilltop, and the strip colours show them. A basin is not a fixed property of the landscape: it also depends on the step size, because a big step can jump over a hill, so the boundary can move and, for large η\eta, the picture becomes erratic.

Saddle points. A flat point where the ground curves up in some directions and down in others. The Hessian there has eigenvalues of both signs (P1.C8). For x2−y2x^2-y^2 they are 22 and −2-2. A ball that starts exactly on the ridge that leads to a saddle stays on it. A ball that starts a tiny bit off the ridge slides away, though slowly at first: P1.F3 showed that from (1,0.001)(1,0.001) the yy part grows by a factor 1.21.2 per step and leaves after 38 steps. In the widget, the pass of the two-valley map is a saddle.

Plateaus and flat valleys. A region where the slope is tiny, so the steps are tiny too, and training looks stuck although it has not reached a minimum. The flat bend of the Plateau curve is one: a ball approaching it from one side slows down for as long as you run it. A flat valley is a long, nearly level floor. The P0.2 loss has one, with curvature 0.240.24 along the floor against 11.0911.09 across it (P1.C8). The product loss (w1w2−2)2(w_1w_2-2)^2 of P1.F1 has a floor that is exactly flat along a whole curve, with infinitely many equally low minima.

Non-convex does not always mean many minima. The Plateau curve has exactly one. Convexity is a stronger property than having a single valley: it forbids hilltops and flat bends as well.

The same copy twice

Neural networks have a built-in reason to be non-convex. As P1.F1 showed, swapping two hidden units gives a network with the same function and so the same loss. Suppose a loss were convex. Then the midpoint of two minimizers would have to be a minimizer too (fact 3 above). For the two-unit network of P1.F1, (a1,w1,a2,w2)=(1.5,0.7,−0.4,2.0)(a_1,w_1,a_2,w_2)=(1.5,0.7,-0.4,2.0) and its swap (−0.4,2.0,1.5,0.7)(-0.4,2.0,1.5,0.7), take the targets to be the network’s own predictions at x=−1,0.5,2x=-1,0.5,2, so both copies have loss 00. Their midpoint (0.55,1.35,0.55,1.35)(0.55,1.35,0.55,1.35) predicts −0.961-0.961, 0.6470.647 and 1.0901.090 instead of −0.521-0.521, 0.2000.200 and 0.9280.928, a mean squared error of 0.1400.140. The midpoint is worse than both ends, which breaks the chord rule, so this loss is not convex.

What happens in many dimensions

A picture with two parameters can mislead about a network with millions. Some work on high-dimensional problems argues that saddle points matter more than local minima there (Dauphin and co-authors, 2014, “Identifying and attacking the saddle point problem in high-dimensional non-convex optimization”). Here is the toy version of the argument. A flat point is a minimum only if every Hessian eigenvalue is positive. If each of nn eigenvalue signs were an independent coin flip, the chance that all are positive would be 2−n2^{-n}: about 0.0010.001 for n=10n=10 and about 8×10−318\times10^{-31} for n=100n=100. Real Hessians are not coin flips, so treat that as an intuition, not a measurement. What the paper itself argues, from statistical physics, random matrix theory and experiments, is that the deeper difficulty in high dimensions comes from the many saddle points, which sit among high-error plateaus that slow learning and can look like a local minimum, and that local minima with much higher error than the global minimum are, in their analysis, less of a problem than often thought. That is evidence from models and experiments, not a theorem about every network.

Why do large networks train anyway? This is a research question, and the honest answer is partly open. Several things are believed to help: a network with many parameters has many different good minima, not one fragile one, and the randomness in training (P1.F4) tends to shake the ball off saddles, which helps. Careful starting weights (P4.12) and the optimizers of P4.13 matter in practice. None of this is a guarantee, and the same landscape can still trap a badly chosen setup, such as a learning rate that is too large or weights that start too small.

Go deeper Two ways a saddle can fool a method

Gradient descent can wait at a saddle because the slope is nearly zero there, and the escape direction grows only slowly at first, as in the 38 steps of P1.F3. Methods that jump straight to “the nearest flat point” are worse. Newton’s method (P1.F6) solves “gradient equals zero” and does not care whether that point is a minimum, a maximum or a saddle. On x2−y2x^2-y^2 one Newton step lands exactly on the saddle at (0,0)(0,0) from any start: the gradient is (2x,−2y)(2x,-2y), the Hessian is diag(2,−2)\mathrm{diag}(2,-2), and the step −H−1∇f-\mathbf{H}^{-1}\nabla f gives (x,y)−(x,y)=(0,0)(x,y)-(x,y)=(0,0). Noise in the gradient (P1.F4) and momentum (P1.F5) help in different ways, and the following lessons say how.

Convexity in machine learning

  • Linear regression (P3.A2): the mean squared error is convex, with Hessian 23A⊤A\tfrac23\mathbf{A}^\top\mathbf{A}. Every local minimum is global, and gradient descent with a small enough step finds the best line from any start. The P0.2 landscape of P1.F1, one long valley with one lowest point, is this case.
  • Logistic regression (P3.A7): the negative log-likelihood is convex. It may have no minimizer when the classes can be separated perfectly (P1.F1).
  • L2 regularization (P3.A6, P1.D11): adding the penalty λ2∥θ∥2\tfrac{\lambda}{2}\lVert\boldsymbol{\theta}\rVert^2 with λ>0\lambda>0 to a convex loss adds λI\lambda\mathbf{I} to the Hessian, which raises every eigenvalue by λ\lambda. Now every eigenvalue is at least λ>0\lambda>0: the loss is strongly convex, hence strictly convex, with exactly one minimizer, even if the unpenalized loss had none or many. For the P0.2 loss with 12(w2+b2)\tfrac12(w^2+b^2) added, the eigenvalues go from 11.0911.09 and 0.240.24 to 12.0912.09 and 1.241.24. The ratio of largest to smallest falls from 4646 to 9.89.8, so gradient descent has an easier time too (P1.F3). The minimizer in general moves: a penalty changes the question as well as making it easier.
  • Neural networks (P4.3 and after): with hidden layers the loss is not convex, for the reason above: weights from different layers multiply, and the same function can be written with many weight settings. The starting weights and the random order of the data change where training ends (P4.12, P1.F4).
  • Convex relaxations: when a problem is hard because it is non-convex, one can sometimes replace it by a nearby convex one that can be solved reliably. Counting mistakes is hard to optimize (P1.F1); a convex stand-in such as the logistic loss is the same idea in spirit.

Intuition

A convex landscape is a bowl: it has one bottom, and any marble rolled in from any side ends there. A non-convex landscape is a field with hills. The chord is a string stretched between two points of the ground. On a bowl the string hangs above the ground. On a hilly field there are pairs of points whose string passes through a hill, which is exactly the failing chord.

Where the picture breaks: a bowl in two dimensions can be stretched very long and narrow and is still convex, but a marble then takes a long zig-zagging time to settle (P1.F3). And a landscape with one valley is not always convex: a flat bend can sit on the way down.

Math

A set CC is convex if a,b∈C\mathbf{a},\mathbf{b}\in C and λ∈[0,1]\lambda\in[0,1] imply λa+(1−λ)b∈C\lambda\mathbf{a}+(1-\lambda)\mathbf{b}\in C. A function f:C→Rf:C\to\mathbb{R} on a convex set is convex if

f(λx+(1−λ)y)≤λf(x)+(1−λ)f(y),\begin{gathered} f\big(\lambda\mathbf{x}+(1-\lambda)\mathbf{y}\big) \\ \le\lambda f(\mathbf{x})+(1-\lambda)f(\mathbf{y}), \end{gathered}

and strictly convex if the inequality is strict for x≠y\mathbf{x}\ne\mathbf{y} and 0<λ<10<\lambda<1. Equivalent conditions for a differentiable ff: f(y)≥f(x)+∇f(x)⊤(y−x)f(\mathbf{y})\ge f(\mathbf{x})+\nabla f(\mathbf{x})^\top(\mathbf{y}-\mathbf{x}) for all x,y\mathbf{x},\mathbf{y}; and, for a twice differentiable ff, ∇2f(x)⪰0\nabla^2 f(\mathbf{x})\succeq 0 at every x\mathbf{x} (all eigenvalues ≥0\ge 0). If ∇2f≻0\nabla^2 f\succ 0 everywhere, ff is strictly convex; the converse fails (x4x^4).

mm-strongly convex: f−m2∥⋅∥2f-\tfrac m2\lVert\cdot\rVert^2 is convex, which for twice differentiable ff means ∇2f⪰mI\nabla^2 f\succeq m\mathbf{I}. Then the minimizer exists and is unique.

For a convex ff: a local minimum is global; ∇f(x∗)=0\nabla f(\mathbf{x}^*)=\mathbf{0} iff x∗\mathbf{x}^* is a global minimizer; the set of minimizers is convex. For a convex ff whose gradient is LL-Lipschitz and which has a minimizer, gradient descent with η≤1/L\eta\le 1/L satisfies f(xk)−f∗≤∥x0−x∗∥22ηkf(\mathbf{x}_k)-f^*\le\dfrac{\lVert\mathbf{x}_0-\mathbf{x}^*\rVert^2}{2\eta k}.

Operations that preserve convexity: f1+f2f_1+f_2, cfc f with c≥0c\ge 0, f(Ax+b)f(\mathbf{A}\mathbf{x}+\mathbf{b}), max⁡(f1,f2)\max(f_1,f_2).

At a flat point of a twice differentiable ff: all Hessian eigenvalues positive gives a strict local minimum, all negative a local maximum, mixed signs a saddle, and a zero eigenvalue leaves the question open (P1.C8).

Code

First, the chord rule tested on random pairs for six curves. Then the Hessian eigenvalues of three losses.

Try it: chord tests and Hessian eigenvalues

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Expected output: x2x^2, ∣x∣|x|, exe^x and −ln⁡x-\ln x break the rule for 00 pairs. sin⁡x\sin x breaks it for all 20002000, because sin⁡\sin is concave on all of [0.2,3][0.2,3]. The double well x4−x2x^4-x^2 breaks it for only 3030 pairs, since it curves down only for ∣x∣<0.41|x|<0.41: a random test can miss a failure, so a passing test is weaker evidence than a failing one. The eigenvalues are 0.240.24 and 11.0911.09; with the penalty 1.241.24 and 12.0912.09; and −4-4 and 44 for the product loss at the origin.

Try it: where does gradient descent end?

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The output: starts −1.5-1.5, −0.5-0.5 and 0.070.07 end at −1.0356-1.0356 with f=−0.3054f=-0.3054, the global minimum. Starts 0.080.08, 0.50.5 and 1.51.5 end at 0.96010.9601 with f=0.2941f=0.2941, the local minimum. The hilltop is at 0.07540.0754, so starts 0.070.07 and 0.080.08, only 0.010.01 apart, end in different valleys.

Go deeper Strong convexity, and what the eigenvalues say about speed

A function is mm-strongly convex if its Hessian has every eigenvalue at least m>0m>0: it curves up by at least mm in every direction. It is LL-smooth if the eigenvalues are at most LL. For a quadratic both are just the smallest and largest eigenvalue. Strongly convex functions have one minimizer. For a quadratic, gradient descent with η=1/L\eta=1/L shrinks the distance to it by a factor of at most 1−m/L1-m/L per step, so the ratio L/mL/m (the condition number) sets the speed. The P0.2 loss has L/m=11.09/0.24≈46L/m=11.09/0.24\approx 46; with the L2 penalty above, 12.09/1.24≈9.812.09/1.24\approx 9.8. That is why a penalty can make training faster as well as better defined (P1.F3 has the zig-zag picture).

Go deeper Convex but no minimizer, and a flat floor

Convexity guarantees that a local minimum is global, not that one exists. exe^x has the lowest value 00 approached but never reached (P1.F1). A convex function can also have a whole floor of minimizers. f(x)=max⁡(0, ∣x∣−1)f(x)=\max(0,\,|x|-1) is convex, equals 00 on all of [−1,1][-1,1], and so has infinitely many minimizers, forming a convex set as fact 3 says. Strict convexity removes the flat floor, and strong convexity (an L2 penalty) removes both problems.

Misconceptions

Check yourself

Check your understanding

  1. Which statement is the chord rule for a convex function ff?

  2. Which of these functions is convex on all the real numbers?

  3. At a flat point of a loss, the Hessian has eigenvalues 33 and −0.5-0.5. What is the point?

  4. Gradient descent on a differentiable convex loss stops at a point where the gradient is zero. What follows?

  5. A curve has exactly one local minimum. Is it convex?

  6. You add the penalty λ2∥θ∥2\tfrac{\lambda}{2}\lVert\boldsymbol{\theta}\rVert^2 with λ>0\lambda>0 to a convex loss. What is true of the new loss?

Exercises

  1. For f(x)=x2f(x)=x^2, check the chord rule with x=0x=0, y=4y=4 and λ=12\lambda=\tfrac12.
    Show answerthe midpoint is 22 and f(2)=4f(2)=4. The chord at the midpoint is 12(0)+12(16)=8\tfrac12(0)+\tfrac12(16)=8. And 4≤84\le 8, as the rule requires.
  2. Show with one chord that f(x)=x3f(x)=x^3 is not convex on all the real numbers.
    Show answertake x=−2x=-2, y=0y=0 and λ=12\lambda=\tfrac12. The midpoint is −1-1, where f=−1f=-1. The chord there is 12(−8)+12(0)=−4\tfrac12(-8)+\tfrac12(0)=-4. The curve (−1-1) is above the chord (−4-4), so the rule fails. (On x≥0x\ge 0, where f′′=6x≥0f''=6x\ge 0, it is convex.)
  3. Is f(x)=x4−2x2f(x)=x^4-2x^2 convex?
    Show answerno. f′′=12x2−4f''=12x^2-4 is −4-4 at x=0x=0, negative near 00. The chord from −1-1 to 11 is at height −1-1 (both ends have f=−1f=-1), and f(0)=0f(0)=0 is above it.
  4. Decide whether f(x,y)=x2+xy+y2f(x,y)=x^2+xy+y^2 is convex.
    Show answerthe Hessian is (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix}, with eigenvalues 33 and 11. Both are positive, so ff is strictly convex, and its only flat point (0,0)(0,0) is the global minimum.
  5. Decide whether f(x,y)=x2+3xy+y2f(x,y)=x^2+3xy+y^2 is convex, and classify the flat point at the origin.
    Show answerthe Hessian is (2332)\begin{pmatrix}2&3\\3&2\end{pmatrix} with eigenvalues 55 and −1-1. One is negative, so ff is not convex, and the origin is a saddle.
  6. Is f(x)=∣x−1∣+x2f(x)=|x-1|+x^2 convex? Strictly convex?
    Show answer∣x−1∣|x-1| is the absolute value of an affine function, which is convex, and x2x^2 is convex, so the sum is convex. It is strictly convex, because x2x^2 is strictly convex and adding a convex function keeps the chord strictly above the curve.
  7. Let ff be convex, let xx be a local minimum, and suppose f(y)<f(x)f(y)<f(x) for some yy. Where does the contradiction come from?
    Show answerthe points zt=(1−t)x+tyz_t=(1-t)x+ty satisfy f(zt)≤(1−t)f(x)+tf(y)<f(x)f(z_t)\le(1-t)f(x)+tf(y)<f(x) for every tt in (0,1](0,1], and ztz_t gets as close to xx as you like when tt is small. So there are points arbitrarily close to xx that are lower, contradicting that xx is a local minimum.
  8. For the loss (w1w2−2)2(w_1w_2-2)^2, find two points with loss 00 whose midpoint has a larger loss, and say what this proves.
    Show answer(1,2)(1,2) and (−1,−2)(-1,-2) both have w1w2=2w_1w_2=2, so loss 00. The midpoint is (0,0)(0,0), with loss (0−2)2=4(0-2)^2=4. The chord rule would need 4≤04\le 0, so the loss is not convex.
  9. The P0.2 loss has Hessian eigenvalues 11.0911.09 and 0.240.24. Add the penalty λ2(w2+b2)\tfrac{\lambda}{2}(w^2+b^2) with λ=2\lambda=2. What are the new eigenvalues, and the largest safe step size 2/λmax⁡2/\lambda_{\max} of P1.C8?
    Show answereach eigenvalue rises by 22: 13.0913.09 and 2.242.24. The limit is 2/13.09=0.1532/13.09=0.153 (against 0.1800.180 before). The ratio of largest to smallest falls from 4646 to 5.85.8.
  10. In the toy argument, each of nn Hessian eigenvalue signs is an independent coin flip. For n=10n=10, what is the chance that the flat point is a minimum, and what does the toy model fail to capture?
    Show answer2−10=1/1024≈0.0012^{-10}=1/1024\approx 0.001. Real Hessians are not independent coin flips: eigenvalues of a loss are correlated, and low-loss flat points are more likely to have positive ones. The calculation is an intuition for why saddles can outnumber minima, not a measurement.

Next: P1.F3 explains the update rule you just used in the widget, step size included. P1.F4 asks what noisy gradients do to saddles and valleys, P1.F5 adds momentum, P1.F6 uses curvature (with the saddle warning above), and P1.F7 brings in constraints, where convex sets return.

Key takeaways

  • A set is convex if the straight segment between any two of its points stays inside. A function is convex if its chord lies on or above its graph: f(λx+(1−λ)y)≤λf(x)+(1−λ)f(y)f(\lambda x+(1-\lambda)y)\le\lambda f(x)+(1-\lambda)f(y) for λ\lambda between 00 and 11.
  • Tests: f′′≥0f''\ge 0 everywhere in one variable; a positive semidefinite Hessian (all eigenvalues ≥0\ge 0) at every point in several variables; the tangent never lies above the graph. x2x^2, ∣x∣|x|, exe^x and −ln⁡x-\ln x are convex; sin⁡x\sin x and (x2−1)2(x^2-1)^2 are not.
  • Sums, nonnegative multiples, maxima and composition with an affine map keep convexity. That is why the mean squared error of a linear model is convex for any data.
  • On a convex function every local minimum is a global minimum, the minimizers form a convex set, and a point with zero gradient is a global minimum. For a smooth convex loss that has a minimizer, gradient descent with a small enough step size drives the loss toward its minimum value.
  • Strictly convex means at most one minimizer. Convex does not mean a minimizer exists: exe^x has none.
  • A non-convex landscape can have local minima, saddle points, plateaus and flat valleys. Which valley a ball ends in depends on its start; the set of starts that lead to one minimum is its basin.
  • Saddle points have a Hessian with eigenvalues of both signs. Work on high-dimensional problems (for example Dauphin and co-authors, 2014) argues that saddles are a bigger obstacle than bad local minima. It is evidence from models and experiments, not a theorem about every network.
  • Linear and logistic regression are convex; neural networks with hidden layers are not, so initialization and randomness matter. An L2 penalty adds strong convexity.
Found a mistake? Tell us