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.
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 and in a set, the points on the segment between them are with from to . At you are at , at at , and at 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 is convex when this holds for every pair of points:
The left side is the height of the curve at a point between and . 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 and is allowed.) Two numbers make it concrete. Take with and :
- At the point between them is . The curve is . The chord is . And .
- At the point is . The curve is . The chord is . And .
Now a curve that fails: , which has two valleys at and . Both ends of the chord have height , so the chord runs along the axis at height . The midpoint has , which is above the chord. The rule is false, so this 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 and every strictly between and , so “” becomes “”. 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. is concave exactly when is convex. and are concave.
- A function can be convex without being smooth. has a corner at , has no derivative there, and is still convex. The chord from to has height at the midpoint , where .
The chord rule is the two-point case of Jensen’s inequality from P1.D4. If equals with probability and with probability , then and , so the rule says for convex . 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 at every in an interval, then is convex there (P1.C8: the second derivative is the curvature, and means the curve bends upward or is straight).
- : . Convex.
- : . Convex.
- for : . Convex.
- : . Convex, and even strictly convex, although . So “ everywhere” is enough for strict convexity but not needed for it.
- : , which is negative on . Not convex.
- : , which is negative for . Not convex.
Several variables: the Hessian. If the Hessian matrix (P1.C8) is positive semidefinite at every point, then is convex. Positive semidefinite means that every eigenvalue is (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.
- : eigenvalues . Convex.
- The stretched bowl of P1.F3: eigenvalues . Convex.
- The P0.2 loss: eigenvalues and at every point (its Hessian is constant), both positive. Convex.
- The saddle : eigenvalues . Not convex.
- The Two valleys landscape of P1.F1: the Hessian is . At it is , positive, but at it is , with a negative eigenvalue. The landscape is bowl-shaped near each valley floor and still not convex.
The tangent test. A differentiable is convex exactly when its tangent line never rises above its graph: for all . For at the tangent is . At it gives , and . Hold on to this form: it explains the next section, because if the slope at is the tangent is flat, and the rule says that for every .
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 and and look at , a function of one number. The chord rule for between and is exactly the chord rule for between and . The second derivative of is , the curvature along that direction (P1.C8), so for every direction exactly when 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 linear map plus a shift”) keeps convexity: if is convex, so is .
- A maximum of convex functions is convex. For example .
Here is what they give. Take the loss of the line on the P0.2 points. For one example the error is an affine function of the parameters . Squaring it is a convex function () composed with an affine map, so it is convex. The mean squared error is a sum of such terms times , 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 with the matrix of inputs, and every matrix of the form is positive semidefinite (P1.B10).
The same rules cover logistic regression (P3.A7, preview). For an example with label the loss is with , an affine function of the parameters. As a function of , its second derivative is , which lies between and 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: is at and at but at . And composing with something that is not affine can break it. The tiny model of P1.F1 has loss , a square of the non-affine . It is not convex: both and have loss , but their midpoint has loss , 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, is convex.
1. Every local minimum is a global minimum. The proof is the chord rule. Suppose is a local minimum, so nothing close to is lower, and suppose some far point were lower: . Take the points on the segment from toward . The chord rule gives for every in . For small the point is as close to as you like, yet lower than . That contradicts “nothing close to is lower”. So no lower exists.
2. A point with zero gradient is a global minimum. If is differentiable and , the tangent test gives for every . On a convex function a flat point cannot be a hilltop or a saddle.
3. The set of minimizers is convex. If is the lowest value, the chord rule gives at every point between them , so it equals . The lowest points form a single connected piece: one point, a segment, or a flat floor, not two separate pits. And if is strictly convex there is at most one minimizer.
4. Gradient descent works. Suppose is convex and differentiable, has a minimizer, and its slope does not change faster than a rate (for the quadratic losses in this course, is the largest Hessian eigenvalue). Then gradient descent (P1.F3) with step size drives toward the minimum value from any starting point. For the P0.2 loss, and . P1.C8 found that for this quadratic the sharper limit is .
Two limits keep the picture honest. First, convex does not promise that a minimum exists. 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 times the slope”) from a start you choose. You do not need more of P1.F3 than that.
Things to try:
- Bowl. Drag the start anywhere. The end is 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 pairs of points (a grid of 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 does.)
- Double well (made up). Start at and then at : the ball goes to a different valley each time, and both valleys are equally low (), 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 it starts. Press Start exactly on the hilltop: the slope at is exactly zero, so the step is zero, and the ball stays put. Move the start to and it rolls to the right. The default chord from to fails, and the red bar marks how far the curve rises above it (by , at ).
- Two valleys, slice of P1.F1. This is the Two valleys landscape cut along . From with the ball ends at with after 33 steps. That is the local minimum, and the status says a lower one exists. From it ends at with after 30 steps, the global minimum. The strip changes colour at about , the hilltop that P1.F1 called the pass. Drag the start across it.
- Step size matters. On the same curve, raise . The left valley floor becomes unstable above about () and the right one above about (), so many starts end up hopping between valleys: at the ball started at does not settle, and at no start settles. At the other end, a very small such as 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 to behave (P1.F3), so the basin map belongs to a particular .
- Plateau and flat bend (made up). This curve is not convex (its chord test fails), yet it has only one minimum, at . At it has a flat bend: the slope is there but the curve keeps rising to the right. Start at and the ball reaches in 143 steps. Start at : the ball creeps toward and after 3000 steps it is still at , slowing down as the slope fades. It will not get past the bend. The strip is grey to the right of .
- 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 . Start at and the ball ends in the right valley after 136 steps. Press Start on the pass line: the ball slides along to the pass at 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 , 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 they are and . 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 the part grows by a factor 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 along the floor against across it (P1.C8). The product loss 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, and its swap , take the targets to be the network’s own predictions at , so both copies have loss . Their midpoint predicts , and instead of , and , a mean squared error of . 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 eigenvalue signs were an independent coin flip, the chance that all are positive would be : about for and about for . 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 one Newton step lands exactly on the saddle at from any start: the gradient is , the Hessian is , and the step gives . 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 . 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 with to a convex loss adds to the Hessian, which raises every eigenvalue by . Now every eigenvalue is at least : 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 added, the eigenvalues go from and to and . The ratio of largest to smallest falls from to , 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 is convex if and imply . A function on a convex set is convex if
and strictly convex if the inequality is strict for and . Equivalent conditions for a differentiable : for all ; and, for a twice differentiable , at every (all eigenvalues ). If everywhere, is strictly convex; the converse fails ().
-strongly convex: is convex, which for twice differentiable means . Then the minimizer exists and is unique.
For a convex : a local minimum is global; iff is a global minimizer; the set of minimizers is convex. For a convex whose gradient is -Lipschitz and which has a minimizer, gradient descent with satisfies .
Operations that preserve convexity: , with , , .
At a flat point of a twice differentiable : 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.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Expected output: , , and break the rule for pairs. breaks it for all , because is concave on all of . The double well breaks it for only pairs, since it curves down only for : a random test can miss a failure, so a passing test is weaker evidence than a failing one. The eigenvalues are and ; with the penalty and ; and and for the product loss at the origin.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
The output: starts , and end at with , the global minimum. Starts , and end at with , the local minimum. The hilltop is at , so starts and , only apart, end in different valleys.
Go deeper Strong convexity, and what the eigenvalues say about speed
A function is -strongly convex if its Hessian has every eigenvalue at least : it curves up by at least in every direction. It is -smooth if the eigenvalues are at most . For a quadratic both are just the smallest and largest eigenvalue. Strongly convex functions have one minimizer. For a quadratic, gradient descent with shrinks the distance to it by a factor of at most per step, so the ratio (the condition number) sets the speed. The P0.2 loss has ; with the L2 penalty above, . 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. has the lowest value approached but never reached (P1.F1). A convex function can also have a whole floor of minimizers. is convex, equals on all of , 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
Which statement is the chord rule for a convex function ?
The curve at a point between and lies on or below the chord. The reversed inequality describes a concave function. The additive rule describes a linear function, which is a very special convex function. Being non-negative has nothing to do with the shape: is convex and takes negative values.
Which of these functions is convex on all the real numbers?
Its second derivative is positive everywhere. The sine has negative second derivative on . The cube has , which is negative for negative (the chord from to is at height at the midpoint , where the curve is ). The negative square is concave.
At a flat point of a loss, the Hessian has eigenvalues and . What is the point?
The ground curves up in one direction and down in another, which is a saddle. A single negative eigenvalue at any point already rules out convexity. A minimum needs every eigenvalue positive, and a maximum needs every eigenvalue negative.
Gradient descent on a differentiable convex loss stops at a point where the gradient is zero. What follows?
The tangent is flat, so by the tangent test every other point is on or above it, which makes this point a lowest one. A convex function has no lower valley to find, and no saddles. The lowest value of a loss does not have to be zero (it is for the P0.2 loss).
A curve has exactly one local minimum. Is it convex?
The example has a single minimum yet bends downward between and , where is negative, so some chord lies below the curve. Having one valley is weaker than convexity. A function with one local minimum can still fail the chord test, and a convex curve can have one minimum, many (a flat floor) or none.
You add the penalty with to a convex loss. What is true of the new loss?
The penalty adds to the Hessian, which raises every eigenvalue by . Pulling the weights toward zero does not break convexity (a sum of convex functions is convex). The minimizer in general moves, because the penalty changes the objective (it stays only if it was already). The gradient of a loss with a unique minimizer is zero only at that point.
Exercises
- For , check the chord rule with , and .
Show answer
the midpoint is and . The chord at the midpoint is . And , as the rule requires. - Show with one chord that is not convex on all the real numbers.
Show answer
take , and . The midpoint is , where . The chord there is . The curve () is above the chord (), so the rule fails. (On , where , it is convex.) - Is convex?
Show answer
no. is at , negative near . The chord from to is at height (both ends have ), and is above it. - Decide whether is convex.
Show answer
the Hessian is , with eigenvalues and . Both are positive, so is strictly convex, and its only flat point is the global minimum. - Decide whether is convex, and classify the flat point at the origin.
Show answer
the Hessian is with eigenvalues and . One is negative, so is not convex, and the origin is a saddle. - Is convex? Strictly convex?
Show answer
is the absolute value of an affine function, which is convex, and is convex, so the sum is convex. It is strictly convex, because is strictly convex and adding a convex function keeps the chord strictly above the curve. - Let be convex, let be a local minimum, and suppose for some . Where does the contradiction come from?
Show answer
the points satisfy for every in , and gets as close to as you like when is small. So there are points arbitrarily close to that are lower, contradicting that is a local minimum. - For the loss , find two points with loss whose midpoint has a larger loss, and say what this proves.
Show answer
and both have , so loss . The midpoint is , with loss . The chord rule would need , so the loss is not convex. - The P0.2 loss has Hessian eigenvalues and . Add the penalty with . What are the new eigenvalues, and the largest safe step size of P1.C8?
Show answer
each eigenvalue rises by : and . The limit is (against before). The ratio of largest to smallest falls from to . - In the toy argument, each of Hessian eigenvalue signs is an independent coin flip. For , what is the chance that the flat point is a minimum, and what does the toy model fail to capture?
Show answer
. 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.