Phase II — Unconstrained Optimization | Week 4 | 2.5 hours ⚡ ENRICHMENT SGD, Adam, and friends — ML’s workhorses, briefly visiting robotics.
While warehouse robots primarily use deterministic optimization (Ceres, OSQP), stochastic methods appear in two key areas: (1) online parameter estimation where sensor data arrives as a stream and parameters must be updated incrementally, and (2) learning-based components like terrain classifiers or neural SLAM modules. Understanding SGD/Adam also builds intuition for adaptive step size selection.
Note: SGD/Adam are primarily ML tools. Robotics optimization rarely uses stochastic methods. Treat this as enrichment — skim if time-constrained, deep-dive if working on learning-based robotics.
Given $f(x) = \frac{1}{N}\sum_{i=1}^N f_i(x)$, replace the full gradient with a stochastic estimate:
where $i_k$ is randomly sampled.
Key property: $\mathbb{E}[\nabla f_{i_k}(x)] = \nabla f(x)$ — unbiased estimator.
Convergence rate: $O(1/\sqrt{k})$ for convex, $O(1/k)$ for strongly convex with decaying $\alpha_k = O(1/k)$.
Use a batch $B_k$ of size $b$:
Variance reduction: $\text{Var}[\text{estimate}] = \frac{\sigma^2}{b}$.
Trade-off: larger $b$ → lower variance but more compute per step.
AdaGrad — adapt per-parameter:
where $G_{k,j} = \sum_{t=1}^k g_{t,j}^2$.
RMSProp — exponential moving average instead of full history:
Adam — RMSProp + momentum with bias correction:
Default: $\beta_1 = 0.9$, $\beta_2 = 0.999$, $\varepsilon = 10^{-8}$, $\alpha = 10^{-3}$.
| Schedule | Formula | When |
|---|---|---|
| Constant | $\alpha_k = \alpha_0$ | Simple, but won’t converge to exact minimum |
| Step decay | $\alpha_k = \alpha_0 \cdot \gamma^{\lfloor k/s \rfloor}$ | Every $s$ steps, decay by $\gamma$ |
| Inverse | $\alpha_k = \alpha_0 / (1 + dk)$ | Theoretically optimal for convex |
| Cosine annealing | $\alpha_k = \frac{\alpha_0}{2}(1 + \cos(\pi k / T))$ | Popular in deep learning |
| Warm-up + decay | Linearly increase, then decay | Transformer training |
# See: code/week04/stochastic_methods.py
Implement SGD for $f(x) = \frac{1}{N}\sum_i f_i(x)$ where each $f_i$ is a single data point’s contribution.
Implement mini-batch GD with configurable batch size. Show variance reduction as batch size increases.
Generate $N = 10000$ samples, compare full GD, SGD, and mini-batch ($b = 32, 128, 512$) on linear regression.
Implement constant, $1/k$, and cosine annealing schedules. Show their effect on convergence.
Implement Adam optimizer with bias correction. Compare to SGD on a nonconvex toy problem.
Problem: Implement Stochastic Variance-Reduced Gradient (SVRG): periodically compute the full gradient, then use it to reduce variance of stochastic updates. Show that SVRG converges linearly (like GD) while using stochastic updates (like SGD).
Solution: SVRG outer loop: compute $\tilde{\mu} = \nabla f(\tilde{x})$ at a reference point. Inner loop: $g_k = \nabla f_{i_k}(x_k) - \nabla f_{i_k}(\tilde{x}) + \tilde{\mu}$. The correction term $-\nabla f_{i_k}(\tilde{x}) + \tilde{\mu}$ ensures $\mathbb{E}[g_k] = \nabla f(x_k)$ with variance → 0 as $x_k \to \tilde{x}$. Converges linearly for strongly convex $f$.
Problem: Construct a simple 1D problem where Adam with default parameters converges to the wrong point (non-convergence of Adam, Reddi et al., 2018). Then implement AMSGrad (the fix) and show it converges correctly.
Solution: The classic counter-example alternates gradients: $g_k = C$ for $k \mod 3 \neq 1$ and $g_k = -\sqrt{C}$ otherwise, with $C > 1$. The running average in Adam can make the effective update direction wrong. AMSGrad fixes this by taking $\hat{v}_k = \max(\hat{v}_{k-1}, v_k)$ — it never decreases the denominator, ensuring convergence.
Problem: Simulate an online wheel calibration scenario: encoder ticks and true distances arrive in a stream. Implement an SGD-based online estimator for wheel radius. Compare to batch least squares (computing periodically with all data). Show that SGD with averaging converges to the same answer.
Solution: Model: $d_i = \pi r \cdot \text{ticks}_i + \varepsilon_i$. Loss: $\frac{1}{2}(d_i - \pi r \cdot \text{ticks}_i)^2$. SGD update: $r \leftarrow r - \alpha_k \cdot \text{ticks}_i \cdot \pi \cdot (d_i - \pi r \cdot \text{ticks}_i)$. With Polyak averaging ($\bar{r}_k = \frac{1}{k}\sum r_j$), this converges to the batch LS solution. Practical benefit: handles non-stationary wheel wear by weighting recent data more.