← All notes · ML revision (DA5400)

Lecture 31 · Regression: computational considerations

Gradient Descent and Stochastic Gradient Descent

The closed form $w^\star=(XX^\top)^{+}Xy$ needs a $d\times d$ matrix inverse, which costs about $d^3$ operations, too slow for millions of features. Gradient descent reaches the same $w^\star$ by repeated small steps, with no inverse. When the number of data points $n$ is also huge, stochastic gradient descent uses a small random batch per step.

Source: this lecture on YouTube (≈25 min). Previous: L30 · Next: L32.

1 · The problem: the inverse is expensive

Inverting the $d\times d$ matrix $XX^\top$ takes roughly $O(d^3)$ time, as hard as computing PCA's eigenvectors. With a billion features, it's out of reach. But $w^\star$ is the minimiser of an unconstrained function, so general-purpose minimisation tools also find it.

2 · Gradient descent: walk downhill

The gradient points in the direction where the function increases fastest. So step the opposite way, scaled by a step size $\eta_t$:

$$w_{t+1}=w_t-\eta_t\,\nabla f(w_t).$$

For linear regression, $\nabla f(w)=2XX^\top w-2Xy$, so:

$$w_{t+1}=w_t-\eta_t\big(2XX^\top w_t-2Xy\big)\qquad\text{(no inverse anywhere)}$$ The 2 can be absorbed into $\eta_t$ (or avoided by putting a ½ in front of the error). This works even with millions of features.

3 · When n is huge too: stochastic gradient descent

Each gradient step still involves $XX^\top$, which is built from all $n$ points. With hundreds of millions of points, even one gradient is costly. SGD's idea: pretend a small random batch is the whole dataset for one step.

for t = 1 … T:
  sample k points uniformly at random  (k = batch size, e.g. 100)
  wt+1 = wt − ηt · ( 2 X̃X̃⊤wt − 2 X̃ỹ )  (X̃: d × k matrix of the sampled points)
output the average of the iterates: w̄ = (1/T) Σt wt

Fitting $y \approx b + m\,x$ to 200 noisy points (true $b = 1$, $m = 2$). The shading is the average squared error over $(b, m)$; darker means lower. Both methods start from the same point and take 60 steps. (Errors are averaged rather than summed, which only rescales the step size.)

gradient descent (all 200 points per step)SGD path (k points per step)SGD average of iteratesw* (closed form)
–
GD final error
–
SGD last iterate
–
SGD average
–
best possible (w*)

4 · Summary of the trade-offs

MethodMain costGood when
Closed form $(XX^\top)^+Xy$forming $XX^\top$ and inverting it: $O(nd^2+d^3)$small $d$: one exact answer
Gradient descenteach step uses all $n$ points; no inverselarge $d$, moderate $n$
SGDeach step uses only $k$ pointslarge $d$ and large $n$. The everyday workhorse.

Key takeaways

  1. The closed form needs an $O(d^3)$ inverse, impractical for huge $d$.
  2. Gradient descent $w_{t+1}=w_t-\eta\nabla f(w_t)$ avoids the inverse. The squared error is convex, so it reaches the global minimum.
  3. SGD uses a random batch of $k$ points per step, making each step cheap even for huge $n$. Average the iterates for the final answer.
  4. The batch size trades cost per step against noise and the number of steps needed.