AI from scratch · Calculus 5 · Stage 2: how a network learns
Convexity and constraints
Gradient descent only feels the ground under its feet, so it can get stuck in a shallow valley (chapter 2). For some losses that can't happen: they're shaped like a single bowl, and every way down leads to the one bottom. This lesson says exactly which shapes those are (convex ones), why linear and logistic regression have them and neural networks don't, and then asks a new question: what's the best point when you're not allowed to go everywhere? The answer explains PCA one more time, and why one kind of regularization sets weights exactly to zero.
1. A bowl, defined by a chord
Pick any two points on a curve and join them with a straight line, a chord. A function is convex when every chord stays on or above the curve: the curve never bulges up over a straight line between two of its own points. Move the two ends and look for red, where the chord dips below the curve.
\[ f(m) \le (1 - t)\,f(a) + t\,f(b) \]
\[ \text{for every point } m = (1 - t)\,a + t\,b \text{ between } a \text{ and } b \ (0 \le t \le 1) \]
The left side is the curve at the in-between point \(m\); the right side is the chord at that same point. In words: the function's value between two points is never above the straight-line average of its values there.
Predict, then check with the widget: \(|x|\) has a sharp corner at 0 and no derivative there. Is it convex?
- Yes: no chord ever dips below it
- No: a function needs a smooth bottom to be convex
A function is convex when it never rises above the straight line between two of its points: a bowl with no bumps, possibly with corners.
Why does a convex function have only one valley?
- Suppose \(a\) is the bottom of a valley (nothing nearby is lower), and somewhere else there's a lower point \(b\): \(f(b) < f(a)\).
- The chord from \(a\) to \(b\) slopes down from \(f(a)\) toward \(f(b)\), so right next to \(a\), the chord is already below \(f(a)\).
- Convexity says the curve is on or below the chord, so right next to \(a\) the curve is below \(f(a)\) too.
- That contradicts \(a\) being the bottom of a valley. So there's no lower point anywhere: every valley bottom of a convex function is the lowest point there is, and gradient descent with a sensible step can't get stuck in a bad one.
2. Testing for a bowl without drawing it
Calculus 3 measured how a curve bends with the second derivative. Convex means it never bends downward: \(f''(x) \ge 0\) everywhere. For a loss with many weights the bend is the Hessian, and "never bends down in any direction" means all its eigenvalues are \(\ge 0\) at every point (linear algebra 2: the eigenvalues are the curvatures along the main axes).
You rarely need to compute that, because convexity survives the ways losses are built:
- a sum of convex functions is convex (a loss is a sum over examples, plus a penalty);
- a convex function of a linear expression, \(f(Xw)\), is convex in \(w\) (a linear model feeds \(Xw\) into its loss);
- the largest of several convex functions is convex (the hinge loss is \(\max(0, 1 - z)\)).
Convex means curving up (or flat) in every direction at every point: the Hessian's eigenvalues are never negative. Sums, linear inputs and maxima keep it that way.
3. Which ML losses are bowls
| model and loss | convex in the weights? |
|---|---|
| linear regression, squared error | yes: a quadratic bowl, Hessian \(2X^\top X / n\) (linear algebra 1 solved it in one step) |
| logistic regression, cross-entropy | yes: \(-\ln \sigma(z)\) is convex (the widget's fourth curve) and \(z = Xw\) is linear |
| either of those plus \(\lambda \lVert w \rVert^2\) or \(\lambda \lVert w \rVert_1\) (the L1 norm, \(|w_1| + |w_2| + \dots\), section 6) | yes: sums of convex functions |
| a neural network with a hidden layer, any loss | no |
Why can't a network's loss be a bowl? Swap two hidden neurons, with all their incoming and outgoing weights. The network computes exactly the same thing, so the loss is the same, but the weights are a different point. Now walk in a straight line from one to the other: halfway, the two neurons are averaged into identical twins, and the network is worse. Same height at both ends, a bump in the middle, and a bowl can't have that.
Linear and logistic regression have bowl-shaped losses, so training finds the best answer. A network's loss has many equally good valleys (at least one per way of relabeling its neurons) with ridges between them, so it can't be a bowl.
Where it breaks: convex isn't the same as easy, and non-convex isn't hopeless
- A flat bottom. Convex allows a whole line of equally low points. Linear algebra 1's twin columns give exactly that: one lowest value, endless weights reaching it. Section 4 fixes it.
- A long, thin bowl. Still one valley, but if the curvatures differ by a factor of 10,000 (the condition number), gradient descent zigzags for a very long time. Convex guarantees where you end up, not how fast.
- Networks train anyway. Their losses aren't convex, yet gradient descent finds good weights reliably. The working explanation: the many valleys of a large network are mostly about equally good (the relabeled twins above are exactly equal), and in millions of dimensions most flat spots are saddles you roll off, not traps. That's an observed fact more than a proven theorem, which is why training still needs care (stage 4's "making nets train").
4. Making the bottom a single point
Adding \(\lambda \lVert w \rVert^2\) to a loss adds \(2\lambda\) to the curvature in every direction: the Hessian becomes \(H + 2\lambda I\), so every eigenvalue goes up by \(2\lambda\). A flat direction (eigenvalue 0) now curves up with \(2\lambda\), and a bowl that curves up in every direction has exactly one lowest point. That's why ridge regression always has a single answer, even with twin columns.
With twin columns, a squared-error loss has Hessian eigenvalues 5 and 0. You add \(0.1 \lVert w \rVert^2\). What's the smallest eigenvalue now?
An L2 penalty bends every direction upward by the same amount, turning a flat-bottomed bowl into one with a single lowest point.
5. The best point on a boundary
Sometimes you can't go everywhere: the weights must stay within a budget, a direction must have length 1, probabilities must add up to 1. Here the gray ellipses are the loss's contour lines around its unconstrained best point (drag the yellow dot), and you may only use weights inside the blue circle, \(\lVert w \rVert \le t\). The green dot is the best you can do.
best allowed point downhill, \(-\nabla L\) straight out of the boundary
Look at the two arrows at the green dot. Whenever the best point is on the circle, downhill points straight out of it: the two arrows line up. That's the Lagrange condition. For a boundary \(g(w) = 0\) (here \(g = \lVert w \rVert^2 - t^2\)), the best point has
\[ \nabla L(w) = \mu\, \nabla g(w) \quad \text{for some number } \mu \]
and the number \(\mu\) (here just a name for a number, not a mean), the Lagrange multiplier, says how much the best loss would improve per unit of extra budget: what the constraint costs you.
Why must downhill point straight out of the boundary?
- Stand at the best allowed point on the boundary, and split the downhill arrow \(-\nabla L\) into a part along the boundary and a part straight across it.
- If the part along the boundary weren't zero, you could slide a little along the boundary in that direction, stay allowed, and lower the loss (chapter 2: a small step with a downhill component goes down).
- So at the best point that part is zero: downhill points straight across the boundary, which is the direction of \(\nabla g\) (a gradient is perpendicular to its contour, and the boundary is a contour of \(g\)).
- Two arrows along the same line are multiples of each other: \(\nabla L = \mu\,\nabla g\). It must point outward, too, or stepping inward would help.
Here is a result you've met twice, derived a third way. PCA wants the direction \(u\) with the most variance \(u^\top \Sigma u\), subject to \(u\) having length 1, \(u^\top u = 1\). The gradients are \(2\Sigma u\) and \(2u\), and the Lagrange condition says \(2 \Sigma u = \mu \cdot 2u\), that is \(\Sigma u = \mu u\): the best direction is an eigenvector, and the variance it holds, \(u^\top \Sigma u = \mu\), is its eigenvalue. Linear algebra 2's answer, from nothing but "the arrows line up".
At the best point on a boundary, the loss's downhill arrow points straight out of it. That one picture gives Lagrange multipliers, and with them PCA.
6. Why L1 gives exact zeros
Switch the widget to the diamond: the budget is now \(\lVert w \rVert_1 = |w_1| + |w_2| \le t\), the L1 norm. Keep the budget below about 1.8 and watch the green dot: it sits exactly on the corner, where \(w_2 = 0\). Growing ellipses touch a diamond at a corner far more often than anywhere else, and corners lie on the axes, so some weights come out exactly zero. With the circle there are no corners, and the weights only shrink.
Penalizing \(\lambda \lVert w \rVert_1\) instead of setting a budget is the same thing seen from the other side (every budget \(t\) matches some \(\lambda\)). That's lasso regression, and its zeros make it a feature selector:
With a strong enough penalty, lasso keeps only body-mass index (bmi) and one blood measurement (s5): the two features that carry most of the signal. Ridge shrinks all ten and zeroes none.
L1's budget is a diamond with corners on the axes, and the loss usually touches it at a corner, so lasso sets weights exactly to zero. L2's circle has no corners, so ridge only shrinks.
Where it breaks: lasso's choices
- Correlated features. When two features carry the same information (the blood measurements
s1ands2are strongly correlated), lasso tends to keep one and drop the other more or less arbitrarily, and a slightly different sample can swap them. Don't read "lasso dropped it" as "it doesn't matter". Elastic net (a mix of both penalties) keeps correlated groups together. - The corner is hard to land on. \(|w|\) has a kink at 0, so plain gradient descent jitters around zero instead of stopping there. Libraries use coordinate descent or a "shrink and clip at zero" step, which lands exactly on 0. A neural network trained with an L1 penalty by plain gradient descent gets small weights, not exact zeros.
Why does the circle (L2) almost never give an exact zero?
- Because L2 is a weaker penalty than L1
- Because a smooth circle meets the ellipses where their directions match, which is almost never exactly on an axis; only a corner can "catch" them there
- Because the L2 penalty isn't convex
7. What to carry forward
- Convex: chords never dip below the curve. Then every valley bottom is the lowest point: gradient descent can't get trapped.
- Tests: \(f'' \ge 0\); in many dimensions, Hessian eigenvalues \(\ge 0\). Sums, linear inputs and maxima keep convexity.
- Linear and logistic regression are convex; networks aren't (relabeling neurons makes equal valleys with ridges between them), and train well anyway.
- \(\lambda \lVert w \rVert^2\) adds \(2\lambda\) to every curvature: one single best answer.
- On a boundary, the best point has \(\nabla L = \mu \nabla g\) (Lagrange). PCA's eigenvectors fall out of it.
- L1's diamond has corners on the axes: lasso gives exact zeros and selects features; ridge only shrinks.
All lessons: AI from scratch. Boyd and Vandenberghe's Convex Optimization is free online from Stanford; its first chapters go much further.
Last updated