Chapter 11 Optimization

We haven’t yet really talked in this course about any optimization algorithms to actually implement our learning algorithms ERM or SRM.

Recall our usual decomposition (1.5)

L𝒟⁢(𝒜⁢(Z))−L∗⏟excess error≤L𝒟⁢(𝒜⁢(Z))−infh∗∈ℋL𝒟⁢(h∗)⏟estimation error+infh∗∈ℋL𝒟⁢(h∗)−L∗⏟approximation error,

and that we’ve often done as in (1.6) that

L𝒟⁢(ERMℋ⁡(Z))−L𝒟⁢(h∗)=L𝒟⁢(ERMℋ⁡(Z))−LZ⁢(ERMℋ⁡(Z))⏟≤suphL𝒟⁢(h)−LZ⁢(h)+LZ⁢(ERMℋ⁡(Z))−LZ⁢(h∗)⏟≤0+LZ⁢(h∗)−L𝒟⁢(h∗)⏟small by Hoeffding.

What about if we try to get the ERM, but we don’t exactly achieve it, instead getting 𝒜⁢(Z) ? We can then do

L𝒟⁢(𝒜⁢(Z))−L𝒟⁢(h∗)=L𝒟⁢(𝒜⁢(Z))−LZ⁢(𝒜⁢(Z))⏟≤suphL𝒟⁢(h)−LZ⁢(h)+LZ⁢(𝒜⁢(Z))−LZ⁢(ERMℋ⁡(Z))⏟optimization error+LZ⁢(ERMℋ⁡(Z))−LZ⁢(h∗)⏟≤0+LZ⁢(h∗)−L𝒟⁢(h∗)⏟small by Hoeffding.

Today we’ll study this optimization error a bit.

By far the most common optimization algorithm used in machine learning is (stochastic) gradient descent and its variants. We’ll cover the foundational ideas, but there is a lot more in this area, and generally, learning theory and optimization are becoming much more integrated. For much much more about optimization, some good resources are graduate courses by Michael Friedlander (CPSC 536M) and Mark Schmidt (CPSC “5XX”, to eventually be offered as an actual class), the books of [BV04, NW06, BUB15], and the survey of [GG23]. Chapter 14 of [SB14] also gives an approachable account of projected stochastic subgradient descent, which generalizes what we’re talking about here.

11.1 Gradient descent

Gradient descent tries to find minw⁡f⁢(w) for some function f , such as LZ⁢(fw) . Here w should be some parameter vector, for example the flattened vector of all parameters in a network; most of what we’ll talk about works for w in a Hilbert space.

We start at some initial point w0 , often either 0 or a sample from, say, 𝒩⁢(0,σ2⁢I) . We then update according to the rule

wt+1=wt−ηt⁢∇f⁢(wt);

the scalar ηt>0 is known as either the “learning rate” or the “step size,” although note that it’s not actually the size of the step since ∥wt+1−wt∥=ηt⁢∥∇f⁢(wt)∥ .

One way to motivate this is to say that we should only “trust” the gradient direction locally, and then should re-check it regularly. Another way is to notice that this update actually minimizes the local quadratic approximation given by 111If instead of 12⁢η⁢∥w−wt∥2 we use 12⁢(w−wt)⁢∇2f⁢(wt)⁢(w−wt) , i.e. the second-order Taylor expansion, this is called Newton’s method. Each step of Newton’s method often improves your loss much more than gradient descent, but each step is also much more computationally expensive.

g⁢(w)=f⁢(wt)+⟨∇f⁢(wt),w−wt⟩+12⁢η⁢∥w−wt∥2.

This approximation is useful in that if f is 1η -strongly convex, then g will be a global lower bound for f (proposition C.3). Even if not, though, it’ll be an okay approximation to a lower bound locally, since it’s the first-order Taylor expansion plus a term that says “don’t go too far.’

We repeat this until we decide to stop, after T steps, and then return a result: this might be wT (the “last iterate”), w¯=1T⁢∑t=1Twt (the “average iterate”), wt^ for t^∈arg⁢mint∈[T]⁡f⁢(wt) (the “best iterate”), the best iterate according to a validation set, or some other scheme.

We’ll usually assume for now that ηt is some constant η , independent of the data, and that we optimize for a fixed number of steps T , also chosen independently of the data. In practice, other schemes are probably better; for instance, it’s often better to use a backtracking scheme to adaptively choose ηt , or to otherwise have some kind of learning rate schedule that decreases over time. In practice, we also probably do set some upper bound T on the optimization time, but will frequently stop sooner if it seems like we’re done.

11.2 β -smooth functions

A common assumption in optimization is that the target function is β -smooth:

Definition 11.1.
222Note that this is not what analysts mean when they say a “smooth function” (i.e. infinitely differentiable).

We say a function f is β -smooth if it is differentiable everywhere, and its gradient ∇f is β -Lipschitz.

Proposition 11.2.

If f is twice-differentiable, it is β -smooth iff for all w in the interior of its domain, all eigenvalues of the Hessian of f at x have absolute value at most β : −β⁢I⪯∇2f⁢(w)⪯β⁢I .333The notation A⪰0 means “is positive-semi-definite”; A⪰B means that A−B is positive-semi-definite.

Proof.

When f is twice-differentiable and β -smooth, we have by Taylor’s theorem that for any vector δ ,

∇f⁢(w+δ)=∇f⁢(w)+∇2f⁢(w)⁢δ+∥δ∥2⁢ξ,

where ξ is an error vector depending on δ but with ∥ξ∥≤C for some constant C . Now, eigendecompose ∇2f⁢(w) into eigenvalues λi and corresponding orthonormal eigenvectors vi , so we can write ∇2f⁢(w)⁢δ=∑iλi⁢⟨vi,δ⟩⁢vi . Also let δ=t⁢v for some ∥v∥=1 . Then we have that

∇f⁢(w+t⁢v)−∇f⁢(w) =t⁢∑iλi⁢⟨vi,v⟩⁢vi+t2⁢ξ.

Now suppose that ∇f is β -Lipschitz, meaning that the norm of this expression is at most β⁢t , or equivalently

∥∑iλi⁢⟨vi,v⟩⁢vi+t⁢ξ∥≤β.

Choosing v=vi , this becomes ∥λi⁢vi+t⁢ξ∥=λi2+t⁢⟨ξ,vi⟩+t2⁢∥ξ∥2≤β ; taking t→0 and noting that while ξ can depend on t , its norm is bounded for all t , this implies that we must have |λi|≤β .

For the other direction, suppose that each |λi|≤β . Then, via Cauchy-Schwarz,

∥∑iλi⁢⟨vi,v⟩⁢vi∥2≤∑iλi2⁢⟨vi,v⟩2≤∑iλi2≤β2;

thus ∥∇f⁢(w+t⁢v)−∇f⁢(w)−t2⁢ξ∥≤t⁢β . Rewriting this we get

∥∇f⁢(w+t⁢v)−∇f⁢(w)t−t⁢ξ∥≤β;

taking the limit as t→0 , this becomes that the directional derivative of ∇f at w in the direction v has norm at most β . Integrating along the line from w to any w′ , as in the proof of lemma 4.4, thus shows that ∥∇f⁢(w′)−∇f⁢(w)∥≤β⁢∥w′−w∥ as desired. ∎

Proposition 11.3.

Suppose f is β -smooth. Then for any w and w′ such that the line segment from w to w′ is in its domain,

|f⁢(w′)−f⁢(w)−⟨∇f⁢(w),w′−w⟩|≤12⁢β⁢∥w−w′∥2:

its deviation from its tangent planes is upper-bounded by a quadratic.

Proof.

By the Lagrange form of Taylor’s theorem, we have that

f⁢(w′)=f⁢(w)+⟨∇f⁢(w),w′−w⟩+12⁢⟨w′−w,∇2f⁢(q)⁢(w′−w)⟩

for some q on a line segment between w and w′ . By proposition 11.2, we know that the eigenvalues of ∇2f⁢(q) all have absolute value at most β , and thus the absolute value of the last term is at most 12⁢β⁢∥w′−w∥2 . ∎

Lemma 11.4 (Descent lemma).

Let w+=w−η⁢∇f⁢(w) for a β -smooth f ; then

f⁢(w)−f⁢(w+)≥η⁢(1−12⁢η⁢β)⁢∥∇f⁢(w)∥2.

Thus if 0<η<2/β and ∇f⁢(w)≠0 , we have f⁢(w+)<f⁢(w) .

Proof.

By proposition 11.3, we have

f⁢(w+) ≤f⁢(w)+⟨∇f⁢(w),w+−w⟩+12⁢β⁢∥w+−w∥2
=f⁢(w)−η⁢⟨∇f⁢(w),∇f⁢(w)⟩+12⁢β⁢∥−η⁢∇f⁢(w)∥2
=f⁢(w)−η⁢(1−12⁢η⁢β)⁢∥∇f⁢(w)∥2.

When 0<η<2/β , η⁢(1−η⁢β/2)>0 . The claim follows. ∎

So, this means that gradient descent with a small-enough learning rate is a “descent method”: each step decreases the objective, unless ∇f⁢(wt)=0 for some t , in which case wt+1=wt and we’re stuck forever. If we never hit such a point, then f⁢(wt) must strictly decrease forever.

Suppose infwf⁢(w)≥a for some finite a , for example LZ with a bounded loss. Then we necessarily have f⁢(wt)→f∞≥a , called the monotone convergence theorem; then since f⁢(wt)−f⁢(wt+1)→0 , the descent lemma implies ∥∇f⁢(wt)∥→0 . So, with an f bounded below, the objective values converge and the gradients vanish. If the iterates themselves converge, their limit is therefore a stationary point; the iterates might not converge, but they’ll stilly do so in a way where the gradient vanishes. The latter case happens e.g. for logistic regression with separable data: no finite w achives zero logistic loss, but we can get closer and closer to zero loss by letting ∥wt∥→∞ but wt/∥wt∥→w∗ where w∗ achieves zero training errors (0-1 loss).

For convex functions, any stationary point – one with ∇f⁢(w)=0 – is a global min. This is why convex optimization is nice! But for nonconvex functions, we can only say that it’s a stationary point: it might be a local but non-global minimizer, or a saddle point, or if we’re really unlucky a local max.

11.3 Aside: convex functions

For convex functions in particular (with a slightly smaller learning rate), we can turn the descent lemma into a proof of gradient descent convergence.

Theorem 11.5.

Let f be a convex, β -smooth function. Begin with w0 and then let wt+1=wt−η⁢∇f⁢(wt) , for some 0<η≤1/β . Let w∗ be any minimizer of f , and let w¯S=1S⁢∑t=T−S+1Twt be the average of the last S iterates; particular examples include w¯1=wT and w¯T=1T⁢∑t=1Twt . For any 1≤S≤T , it holds that

f⁢(w¯S)−f⁢(w∗)≤12⁢η⁢T⁢∥w0−w∗∥2.
Proof.

We’re going to want to bound the improvement in each step of gradient descent: f⁢(wt)−f⁢(wt+1) . By convexity (in particular, proposition C.3), we have that

f⁢(wt)−f⁢(w∗)≤⟨wt−w∗,∇f⁢(wt)⟩.

To see what that right-hand side is, we’ll use

∥wt−η⁢∇f⁢(wt)−w∗∥2=∥wt−w∗∥2−2⁢η⁢⟨wt−w∗,∇f⁢(wt)⟩+η2⁢∥∇f⁢(wt)∥2,

and rearrange into

f⁢(wt)−f⁢(w∗)≤⟨wt−w∗,∇f⁢(wt)⟩=12⁢η⁢[∥wt−w∗∥2−∥wt+1−w∗∥2]+η2⁢∥∇f⁢(wt)∥2.

Now we can use the descent lemma to deal with that last term: since η≤1/β , 1−12⁢η⁢β≥12 , and lemma 11.4 becomes

f⁢(wt)−f⁢(wt+1)≥12⁢η⁢∥∇f⁢(wt)∥2.

We’ve therefore obtained

f⁢(wt)−f⁢(w∗)≤12⁢η⁢[∥wt−w∗∥2−∥wt+1−w∗∥2]+f⁢(wt)−f⁢(wt+1),

which we can simplify to

f⁢(wt+1)−f⁢(w∗)≤12⁢η⁢[∥wt−w∗∥2−∥wt+1−w∗∥2]

or equivalently

f⁢(wt)−f⁢(w∗)≤12⁢η⁢[∥wt−1−w∗∥2−∥wt−w∗∥2].

This holds for each t from 1 to T ; let’s then take the average of them all, obtaining

1T⁢∑t=1Tf⁢(wt)−f⁢(w∗) ≤12⁢η⁢T⁢∑t=1T[∥wt−1−w∗∥2−∥wt−w∗∥2]
=12⁢η⁢T⁢[∥w0−w∗∥2−∥w1−w∗∥2+∥w1−w∗∥2−⋯−∥wT−w∗∥2]
=12⁢η⁢T⁢[∥w0−w∗∥2−∥wT−w∗∥2]
≤12⁢η⁢T⁢∥w0−w∗∥2.

Notice that by the descent lemma, f⁢(wt) is weakly decreasing; thus

1S⁢∑t=T−S+1Tf⁢(wt)≤1T⁢∑t=1Tf⁢(wt)≤f⁢(w∗)+12⁢η⁢T⁢∥w0−w∗∥2.

But by Jensen’s inequality (theorem C.8), since f is convex,

f⁢(w¯S)=f⁢(1S⁢∑t=T−S+1Twt)≤1S⁢∑t=T−S+1Tf⁢(wt).∎

Much faster rates are available if f is smooth and strongly convex.

11.3.1 Aside: SGD non-convex convergence

The analysis above can be pretty-easily extended to SGD; see e.g. Chapter 14 of [SB14] or the recent survey of [GG23]. It can be generalized further, though more complicatedly, to show that even SGD eventually reaches a stationary point, even for non-convex functions:

Proposition 11.6 (Corollary 1 of [KR23]).

Let f be β -smooth, with infxf⁢(x)≥finf>−∞ . Let g^t∣xt be independent such that 𝔼[g^t∣xt]=∇f⁢(xt) and

𝔼[∥g^t∥2∣xt]≤2⁢A⁢(f⁢(xt)−finf)+B⁢∥∇f⁢(xt)∥2+C

for some A,B,C≥0 . Fix ε>0 , and pick η=min⁡{1β⁢A⁢T,1β⁢B,ε2⁢β⁢C} . Initialize stochastic gradient descent at x0 , with δ0=f⁢(x0)−finf , and xt+1=xt−η⁢g^t . As long as T≥12⁢δ0⁢βε2⁢max⁡{B,12⁢δ0⁢Aε2,2⁢Cε2} , it holds that min1≤t≤T⁢𝔼[∥∇f⁢(xt)∥]≤ε .

That is, the best iterate achieves ε -stationarity (in expectation) with 𝒪⁡(1/ε4) steps. The assumption on g^t is satisfied for example if the g^t have a bounded variance, or if we choose g^t as the gradient of LZ′ for Z′⊆Z chosen randomly and the loss being Lipschitz, or various other settings.

11.4 Are deep networks β -smooth?

Is f⁢(w)=LZ⁢(hw) for hw a class of deep networks β -smooth?

Consider the very simple network

hW,v⁢(x)=⟨v,σ⁢(W⁢x)⟩,

where σ is itself β -smooth. Then the square loss for a single data point is

f⁢(W,v)=(v𝖳⁢σ⁢(W⁢x)−y)2=v𝖳⁢σ⁢(W⁢x)⁢σ⁢(W⁢x)𝖳⁢v−2⁢y⁢σ⁢(W⁢x)𝖳⁢v+y2,

and we have

∇vf⁢(W,v) =2⁢(σ⁢(W⁢x)𝖳⁢v−y)⁢σ⁢(W⁢x)⁢ 444If this is unfamiliar, try looking at individual partial derivatives to see that they line up.
∇v2f⁢(W,v) =2⁢σ⁢(W⁢x)⁢σ⁢(W⁢x)𝖳.

The Jacobian with W is more annoying, since we’d have to flatten W and reshape and stuff. 555Autodiff is nice…. But the overall Hessian of f with respect to its input parameters will have ∇v2f as a block in it, and so its largest eigenvalue will depend on W : if σ is the ReLU or something similar, then large values of W will result in much larger Hessians. Thus the loss is only going to be fully β -smooth if you bound the set of possible W s, but for any particular parameters it’s going to be “locally” smooth.

Notice that the descent lemma doesn’t actually need a global upper bound on the smoothness, just along the path from xt to xt+1 . So, intuitively, we should roughly expect (stochastic) gradient descent to reach a stationary point of the loss as long as ∇2f doesn’t blow up, i.e. in typical situations as long as none of the parameters blows up. (All of this also requires that σ itself be β -smooth; ReLU is not.)

Aside: edge of stability

So, if we’re optimizing a deep network with a fixed learning rate η , whether the descent lemma applies or not – whether gradient descent is “stable” or not – depends on whether η<2β , or more relevantly β<2η , for the “local” value of β . 666The “local β ” might be >max(∇2f(xt),∇2f(xt+1) : you might go through a sharper point on the way. Consider f⁢(x)=|x| on the reals: f′′⁢(x)=0 for all x≠0 , but the descent lemma might not apply when you switch signs, since you go through 0 which has “infinite second derivative.” We can roughly get this local value of β by just checking the largest eigenvalue of ∇2f⁢(xt) , and see whether it stays in a “stable” regime or not.

[CKL+21] demonstrated that in fact, optimization typically exhibits “progressive sharpening” where β increases up to 2/η , then hovers around there on the “edge of stability” (also see [FOX23]). [DNL23] proposed a mechanism for how this happens, based on Taylor expansions of the training process.

11.5 Is a stationary point enough?

One model we can look at is deep linear nets, f⁢(x)=wd⁢Wd−1⁢⋯⁢W2⁢W1⁢x . These are just linear models, but they’re nonconvex and hierarchical and so exhibit some of the same behaviour as regular deep nets. It’s reasonable to expect that, generally speaking, if something doesn’t work on deep linear nets, it won’t work on deep nonlinear nets either.

To see that they’re nonconvex: consider just a depth two model on scalars, f⁢(x)=v⁢w⁢x for v,w∈ℝ . Consider square loss with the training set Z=((1,1)) . Then LZ⁢(f)=(v⁢w−1)2 , whose minimizers are

{(v,w):v⁢w=1}={(v,1/v):v≠0}.

But this is not a convex set: it’s a line in ℝ2 with the point (0,0) cut out of it. The set of minimizers of convex functions must be convex, so therefore LZ is not convex.

It turns out that for deep linear nets:

  • •

    Fortunately, all local minima in deep linear nets are global minima ([KAW16, Lv18]).

  • •

    Unfortunately, stationary points can also be saddle points – including potentially “bad” saddles with λmin⁢(∇2f)=0 even though they’re not local minima. (For example, x3 has a saddle point like this at x=0 ; they can be even worse in high dimensions.)

  • •

    Fortunately, in general, gradient descent almost surely converges to local minimizers, not saddles (or local maxes) ([LSJ+16]).

  • •

    Unfortunately, doing so can take exponential time ([DJL+17]).

  • •

    Fortunately, this doesn’t happen for deep linear networks, under some conditions ([ACG+19a]).

Unfortunately, there are bad local minima in nonlinear networks. For a very simple example, consider the network h:ℝ→ℝ given by h⁢(x)=ReLU⁡(w⁢x) , where w∈ℝ ; use square loss with a single example, (1,1) . Then the loss is

ℓ⁢(hw,(1,1))={(w−1)2w≥01w≤0.

Any negative input is a (non-strict) local min (since f⁢(w)≥f⁢(v) for all v in a neighbourhood of w ), but it’s not a global min (since f⁢(1)=0 ). Thus, if you start gradient descent with a negative w , it’s just stuck. In fact, bad (strict) local minima can appear for almost any activation function ([DLS20]), and with more units, the loss landscape has such points almost all the time.

But, do bad local minima exist for realistic networks, with realistic data? Even if they do, does SGD find them? Moreover, even if I find a good local min of LZ , does that imply I get a good L𝒟 for realistic networks?