MIT 6.790 — Lecture 9

Gradient Descent

§ Gradient Descent

Recall from Lecture 8 that logistic regression requires minimizing the following (logistic) loss:

L(w)=∑i=1nlog⁡2[1+exp⁡(−y(i)w⊤x(i))].\mathcal{L}(w) = \sum_{i = 1}^n \log_2\left[ 1 + \exp\left(-y^{(i)}w^{\top}x^{(i)}\right) \right].

Unfortunately, unlike with linear regression, there is no closed-form expression for the minimizing parameter of logistic loss. But because logistic loss is convex, we can apply gradient descent to find the global minimum. (Recall from Lecture 8 that we can say “global minimum” rather than “local minimum” because of convexity.)

Definition. Let f ⁣:Rn→Rf \colon \mathbb{R}^n \to \mathbb{R} be a differentiable function. In gradient descent (GD), we start with some w0∈Rnw_0 \in \mathbb{R}^n and proceed via:

wt+1←wt−η∇f(wt).w_{t + 1} \leftarrow w_t - \eta \nabla f(w_t).

Note the hyperparameter η>0\eta > 0, which we call the step size. The hope is that {wt}t=0∞\{w_t\}_{t = 0}^{\infty} converges to a local minimizer of ff.

§ Stationary Points

Gradient descent stops at a value w∗∈Rnw^* \in \mathbb{R}^n where ∇f(w∗)=0\nabla f(w^*) = 0; we call such a point w∗w^* a stationary point. We can think of stationary points as being classifiable into two categories:

This is kind of irrelevant, though; gradient descent only converges to saddle points for a measure-zero set of initial points w0w_0.

For a further discussion of how the Hessian relates to saddle points and whatnot, see Lecture 16 and Lecture 19 of 18.701 (specifically the subsections linked above).

§ Lower-Bounding GD Efficiency

For one-dimensional functions f ⁣:R→Rf \colon \mathbb{R} \to \mathbb{R}, we can upper-bound the number of steps required to get close to a stationary point.

Theorem. (One-Dimensional Descent Lemma) Suppose f ⁣:R→Rf \colon \mathbb{R} \to \mathbb{R} is twice differentiable, and ∣f′′(w)∣≤β|f''(w)| \leq \beta for all ww. Then if we set η=1β\eta = \frac{1}{\beta}, the iterates of gradient descent satisfy:

f(wt+1)−f(wt)≤−12βf′(wt)2.f(w_{t + 1}) - f(w_t) \leq -\frac{1}{2\beta} f'(w_t)^2.

In particular, each step of gradient descent produces an improvement of at least 12βf′(wt)2\frac{1}{2\beta} f'(w_t)^2.

Proof: We won't provide a rigorous proof. The rough idea is to use the quadratic Taylor approximation.

f(wt+1)≈f(wt)+δf′(wt)+δ22f′′(wt)  where δ=−ηf′(wt).f(w_{t + 1}) \approx f(w_t) + \delta f'(w_t) + \frac{\delta^2}{2}f''(w_t) ~ \text{ where } \delta = -\eta f'(w_t).

Plugging in δ=−ηf′(wt)\delta = -\eta f'(w_t) and rearranging yields:

f(wt+1)−f(wt)≈−ηf′(wt)2[1−η2f′′(wt)]≤−12βf′(wt)2    assuming η=1β and ∣f′′(w)∣≤β.f(w_{t + 1}) - f(w_t) \approx - \eta f'(w_t)^2 \left[ 1 - \frac{\eta}{2} f''(w_t) \right] \leq -\frac{1}{2\beta} f'(w_t)^2 ~~~ \text{ assuming } \eta = \frac{1}{\beta} \text{ and } |f''(w)| \leq \beta.

That's the promised bound, with many computations omitted. This proof, of course, lacks rigor in that everything carries an ≈\approx symbol due to the Taylor approximation.   ■\blacksquare

An immediate corollary of the Descent Lemma is the following.

Theorem. (One-Dimensional GD Efficiency) Suppose, in the setting of the Descent Lemma, that f(w0)−min⁡wf(w)=Mf(w_0) - \min_w f(w) = M. If gradient descent yields {wt}t=0∞\{w_t\}_{t = 0}^{\infty}, then for any ϵ>0\epsilon > 0, there is some T≤2βMϵ2T \leq \frac{2 \beta M}{\epsilon^2} for which ∣f′(wT)∣<ϵ|f'(w_T)| < \epsilon.

Remark. Note that none of the reasoning here carries an assumption that ff is convex.

For some reason, 6.790 does not state the multivariate versions of these theorems, even though their statements and proofs are nearly identical to the one-dimensional case. We state them here for completeness.

Theorem. (Multivariate Descent Lemma) Suppose f ⁣:Rn→Rf \colon \mathbb{R}^n \to \mathbb{R} is twice differentiable, and −βI⪯∇2f(w)⪯βI-\beta I \preceq \nabla^2 f(w) \preceq \beta I for all ww. Then if we set η=1β\eta = \frac{1}{\beta}, the iterates of gradient descent satisfy:

f(wt+1)−f(wt)≤−12β∥∇f(wt)∥2.f(w_{t + 1}) - f(w_t) \leq -\frac{1}{2\beta} \|\nabla f(w_t)\|^2.

In particular, each step of gradient descent produces an improvement of at least 12β∥∇f(wt)∥2\frac{1}{2\beta} \|\nabla f(w_t)\|^2.

Remark. The ordering A⪯BA \preceq B is the Loewner order on symmetric matrices: A⪯BA \preceq B if and only if B−AB - A is positive semidefinite.

Theorem. (Multivariate GD Efficiency) Suppose, in the setting of the Descent Lemma, that f(w0)−min⁡wf(w)=Mf(w_0) - \min_w f(w) = M. If gradient descent yields {wt}t=0∞\{w_t\}_{t = 0}^{\infty}, then for any ϵ>0\epsilon > 0, there is some T≤2βMϵ2T \leq \frac{2 \beta M}{\epsilon^2} for which ∥∇f(wT)∥<ϵ\|\nabla f(w_T)\| < \epsilon.

§ Linear Regression Convergence Rate

Suppose we use gradient descent to find the minimum in simple least-squares linear regression. Then an overly large step size can cause gradient descent to fail to converge!

This often happens when the level sets of ff are elliptically-shaped, rather than spherically-shaped.

Let's investigate how this behavior appears specifically in the case of least-squares linear regression.

Theorem. (Least-Squares Gradient Descent) Suppose we use gradient descent to find a w∗∈Rdw^* \in \mathbb{R}^d such that, given training data X∈Rn×dX \in \mathbb{R}^{n \times d} and y∈Rny \in \mathbb{R}^n, the loss f(w):=∥Xw−y∥2f(w) := \|Xw - y\|^2 is minimized. Then the sequence {wt}t=0∞\{w_t\}_{t = 0}^{\infty} satisfies:

wt−w∗=(Id−ηH)t(w0−w∗),w_t - w^* = (I_d - \eta H)^t (w_0 - w^*),

where H:=∇2f(w)=2X⊤XH := \nabla^2 f(w) = 2X^{\top}X is the Hessian matrix (notably, independent of ww).

Proof: It's just plain computation. First, compute the relevant derivatives.

∇f(w)=2X⊤(Xw−y)    and    ∇2f(w)=2X⊤X.\nabla f(w) = 2X^{\top}(Xw - y) ~~~ \text{ and } ~~~ \nabla^2 f(w) = 2X^{\top}X.

Then note the minimizer w∗w^* is known to satisfy (see Lecture 23 of 18.650) the equation X⊤Xw∗=X⊤yX^{\top}Xw^* = X^{\top}y, so:

wt+1−w∗=[wt−2η(X⊤Xwt−X⊤y)⏟= η∇f(wt)]−w∗=(Id−2ηX⊤X)(wt−w∗).w_{t + 1} - w^* = \left[w_t - \underbrace{2 \eta \left( X^{\top}X w_t - X^{\top}y \right)}_{ = \ \eta \nabla f(w_t) }\right] - w^* = \left(I_d - 2 \eta X^{\top}X\right)(w_t - w^*).

Recalling H=2X⊤XH = 2X^{\top}X and applying induction finishes the job.   ■\blacksquare

For very large tt, the effect of (Id−ηH)t(I_d - \eta H)^t can be summarized by its eigenvalues. Say λ1≥⋯≥λd>0\lambda_1 \geq \dots \geq \lambda_d > 0 are the eigenvalues of HH. Then we can make the following observations.

  1. The eigenvalues of Id−ηHI_d - \eta H are precisely {1−ηλi}i=1d\left\{1 - \eta \lambda_i\right\}_{i = 1}^d.

  2. To ensure convergence, we require all eigenvalues to be in (−1,1)(-1, 1).

    • The largest eigenvalue λ1\lambda_1 gives the tightest bound here; the constraint 1−ηλ1∈(−1,1)1 - \eta \lambda_1 \in (-1, 1) implies η≲1λ1\eta \lesssim \frac{1}{\lambda_1}, roughly.

  3. However, smaller values of η\eta lead to slower convergence, as 1−ηλi1 - \eta \lambda_i is further from 00 when η\eta is smaller.

    • The smallest eigenvalue λd\lambda_d gives the slowest convergence here. For η=1λ1\eta = \frac{1}{\lambda_1}, the decay is:

      ∥wt−w∗∥2≤(1−λdλ1)2t∥w0−w∗∥2.\|w_t - w^*\|^2 \leq \left(1 - \frac{\lambda_d}{\lambda_1}\right)^{2t} \|w_0 - w^*\|^2.

We can summarize this whole story like so.

Definition. Let the condition number κ\kappa of a matrix HH be κ:=λmax(H)λmin(H)\kappa := \frac{\lambda_{\mathrm{max}}(H)}{\lambda_{\mathrm{min}}(H)} (the ratio of its largest eigenvalue to its smallest).

Theorem. (Least-Squares Convergence Rate) Gradient descent for least-squares linear regression, with step size η=1λmax(H)\eta = \frac{1}{\lambda_{\mathrm{max}}(H)}, converges to the global minimum at an exponential rate, described below.

∥wt−w∗∥2≤(1−1κ)2t∥w0−w∗∥2.\|w_t - w^*\|^2 \leq \left(1 - \frac{1}{\kappa}\right)^{2t} \|w_0 - w^*\|^2.

This also explains why elliptically-shaped level sets yield slower convergence: they have a larger condition number.