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?

The definition only compares the curve with straight lines, so kinks are fine. That matters: the L1 penalty \(|w|\) and the hinge loss of support vector machines both have corners and are both 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?
  1. 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)\).
  2. 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)\).
  3. Convexity says the curve is on or below the chord, so right next to \(a\) the curve is below \(f(a)\) too.
  4. 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:

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 lossconvex in the weights?
linear regression, squared erroryes: a quadratic bowl, Hessian \(2X^\top X / n\) (linear algebra 1 solved it in one step)
logistic regression, cross-entropyyes: \(-\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 lossno

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

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?

Each goes up by \(2\lambda = 0.2\): the eigenvalues become 5.2 and 0.2. The flat direction now curves up a little, enough to pick one answer: the one that splits the weight evenly between the twins, since that has the smallest \(\lVert w \rVert^2\).

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?
  1. 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.
  2. 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).
  3. 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\)).
  4. 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

Why does the circle (L2) almost never give an exact zero?

On a smooth boundary the touching point moves continuously as the loss changes, and landing exactly on an axis would be a coincidence. A corner has a whole range of "straight out" directions, so it stays the answer for a whole range of losses: that's why lasso's zeros are exact and stable.

7. What to carry forward

All lessons: AI from scratch. Boyd and Vandenberghe's Convex Optimization is free online from Stanford; its first chapters go much further.

Last updated