MIT 6.790 — Lecture 10

Stochastic Gradient Descent and Estimation Error Bounding

§ Stochastic Gradient Descent

One issue with gradient descent is that it's slow to compute when the function being minimized is the average of nn loss functions across nn data points, like so, because computing the gradient ∇f(wt)\nabla f(w_t) becomes an O(n)O(n) task.

f(w):=1n∑i=1nfi(w).f(w) := \frac{1}{n} \sum_{i = 1}^n f_i(w).

Stochastic gradient descent sacrifices accuracy for speed like so:

Definition. To compute wt+1w_{t + 1} from wtw_t in stochastic gradient descent, sample i∼Unif{1,…,n}i \sim \mathrm{Unif}\{1, \dots, n\} and compute gt+1=∇fi(wt)g_{t + 1} = \nabla f_i(w_t). Then set wt+1←wt−ηt+1gt+1w_{t + 1} \leftarrow w_t - \eta_{t + 1} g_{t + 1}; the point is that gt+1g_{t + 1} is an unbiased estimate for ∇f(wt)\nabla f(w_t), since E[gt+1]=∇f(wt)\mathbb{E}[g_{t + 1}] = \nabla f(w_t).

Remark. If you want to decrease the variance of the approximation gt+1≈∇f(wt)g_{t + 1} \approx \nabla f(w_t), you can also set gt+1g_{t + 1} to be the average of ∇fi(wt)\nabla f_i(w_t) across bb randomly-sampled indices ii.

Importantly, the loss f(wt)f(w_t) may increase on some iterations of SGD, even for arbitrarily small step sizes ηt\eta_t.

We conclude our discussion on SGD with the statement of a theorem, without proof.

Theorem. (SGD Convergence) Suppose ff is convex with minimizer w∗w^* and satisfies ∥∇f(w)∥≤B\|\nabla f(w)\| \leq B for all ww. Suppose:

Then SGD with step size ηt:=DBt\eta_t := \frac{D}{B\sqrt{t}} yields:

E[f(w‾t)−f(w∗)]≤DB⋅[2+log⁡t2t]    where    w‾t:=∑s=1tηsws−1∑s=1tηs.\mathbb{E}\left[ f(\overline{w}_t) - f(w^*) \right] \leq DB \cdot \left[ \frac{2 + \log t}{2\sqrt{t}} \right] ~~~ \text{ where } ~~~ \overline{w}_t := \frac{\sum_{s = 1}^t \eta_s w_{s - 1}}{\sum_{s = 1}^t \eta_s}.

Remark. The 6.790 notes claim the RHS should have a coefficient of DB2DB^2 rather than DBDB, but this is incorrect by dimensional analysis alone. The details here, for 6.790's sake, are not too important regardless.

§ Estimation Error Bounding

Recently, we've been restricting ourselves to specific classes of hypotheses (regression, parametric models). Let's consider machine learning at a much more general level; what can we say regardless of what the hypothesis class H\mathcal{H} looks like?

Definition. For any hypothesis h∈Hh \in \mathcal{H}, there are two types of errors (or risks).

Can we upper-bound the estimation error between the expected error and the empirical error?

Theorem. (Estimation Error Bound) Assume L(x,y)∈[0,1]\mathcal{L}(x, y) \in [0, 1] and pick some δ∈(0,1)\delta \in (0, 1). Then with probability 1−δ1 - \delta, the following is true for all hypotheses h∈Hh \in \mathcal{H}:

∣R^(h)−R(h)∣≤log⁡∣H∣+log⁡(2/δ)2n.\left|\hat{\mathcal{R}}(h) - \mathcal{R}(h)\right| \leq \sqrt{\frac{\log|\mathcal{H}| + \log(2/\delta)}{2n}}.

Note that the assumption L(x,y)∈[0,1]\mathcal{L}(x, y) \in [0, 1] makes sense for classification problems, say. More generally, one can achieve a similar bound up to a constant factor for models with bounded loss functions.

Remark. It should make sense that the RHS grows as ∣H∣|\mathcal{H}| grows. The reason why is that larger ∣H∣|\mathcal{H}| yield higher rates of overfitting, in which case the empirical error becomes a less accurate estimation of the expected error.

One might question what happens in the case of linear regression, where H\mathcal{H} is an infinitely-sized hypothesis class, with one hypothesis for each of the infinitely many parameters θ∈Rd\theta \in \mathbb{R}^d. The trick, then, is to replace R\mathbb{R} with, say, the set of 6464-bit floating point numbers. This way, ∣H∣≈264d|\mathcal{H}| \approx 2^{64d} and log⁡∣H∣≈64dlog⁡2\log |\mathcal{H}| \approx 64d \log 2, and the RHS doesn't blow up.

We'll now provide a proof of this theorem. Expect the proof to be “simplistic”—after all, it has to work no matter what H\mathcal{H} is!

Proof: The proof will rely on a statistical fact known as Hoeffding's Inequality, which we state below without proof.

Hoeffding's Inequality. If U1,…,Un∈[0,1]U_1, \dots, U_n \in [0, 1] are sampled (i.i.d.) from the same distribution P\mathbb{P}, then:

P(∣Uˉn−μ∣≥t)≤2exp⁡(−2nt2).\mathbb{P}\left( \left|\bar{U}_n - \mu\right| \geq t \right) \leq 2 \exp\left(-2nt^2\right).

Note that Uˉn\bar{U}_n is the average of the {Ui}i=1n\{U_i\}_{i = 1}^n, and μ\mu is the expected value μ=E[Ui]\mu = \mathbb{E}[U_i] (the same for all ii).

Directly applying Hoeffding's Inequality with Ui:=L(h(xi),yi)U_i := \mathcal{L}(h(x_i), y_i) yields:

P(∣R^(h)−R(h)∣≥t)≤2exp⁡(−2nt2).\mathbb{P}\left( \left|\hat{\mathcal{R}}(h) - \mathcal{R}(h)\right| \geq t \right) \leq 2\exp\left(-2nt^2\right).

Then the union bound across all h∈Hh \in \mathcal{H} yields:

P(∣R^(h)−R(h)∣≥t  for some h∈H)≤2∣H∣exp⁡(−2nt2).\mathbb{P}\left( \left|\hat{\mathcal{R}}(h) - \mathcal{R}(h)\right| \geq t ~ \text{ for some } h \in \mathcal{H} \right) \leq 2|\mathcal{H}|\exp\left(-2nt^2\right).

Plugging in t=log⁡∣H∣+log⁡(2/δ)2nt = \sqrt{\frac{\log|\mathcal{H}| + \log(2/\delta)}{2n}} now directly yields the promised bound; you can check this in your head.   ■\blacksquare

We conclude with one more similar-looking theorem.

Definition. We establish the notation h^:=arg min⁡h∈HR^(h)\hat{h} := \argmin_{h \in \mathcal{H}} \hat{\mathcal{R}}(h) and h∗:=arg min⁡h∈HR(h)h^* := \argmin_{h \in \mathcal{H}} \mathcal{R}(h).

Clearly h∗h^* is preferred to h^\hat{h}, but training data and ERM can only get us to h^\hat{h}. How do h∗h^* and h^\hat{h} compare?

Theorem. (ERM Risk Bound) Assuming L(x,y)∈[0,1]\mathcal{L}(x, y) \in [0, 1], for all δ∈(0,1)\delta \in (0, 1) we have with probability 1−δ1 - \delta that:

R(h^)−R(h∗)≤2⋅log⁡∣H∣+log⁡(2/δ)2n.\mathcal{R}(\hat{h}) - \mathcal{R}(h^*) \leq 2 \cdot \sqrt{\frac{\log |\mathcal{H}| + \log\left(2 / \delta\right)}{2n}}.

Proof: The point is the following:

R(h^)−R(h∗)=[R(h^)−R^(h^)]⏟bound #1+[R^(h^)−R^(h∗)]⏟≤ 0+[R^(h∗)−R(h∗)]⏟bound #2.\mathcal{R}(\hat{h}) - \mathcal{R}(h^*) = \underbrace{\left[ \mathcal{R}(\hat{h}) - \hat{\mathcal{R}}(\hat{h}) \right]}_{\text{bound \#1}} + \underbrace{\left[ \hat{\mathcal{R}}(\hat{h}) - \hat{\mathcal{R}}(h^*) \right]}_{\leq \ 0} + \underbrace{\left[ \hat{\mathcal{R}}(h^*) - \mathcal{R}(h^*) \right]}_{\text{bound \#2}}.

The RHS has two bracketed expressions marked “bound #1” and “bound #2” that can each be upper-bounded using the “Estimation Error Bound” theorem. Summing these two upper bounds yields the promised result.   ■\blacksquare