Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Walking downhill in fog
Imagine standing on a foggy hillside and wanting to reach the lowest point. You can’t see the valley, but you can feel which way the ground slopes under your feet. So you take a step downhill, feel again, and repeat. That is gradient descent. Here the “hill” is how wrong the line is, and each step nudges the slope and intercept to make it less wrong. You can feel the slope using all the data (careful but slow), one point at a time (quick but wobbly), or a handful at a time (the usual compromise).
Onboard estimators and adaptive controllers update models as each new measurement arrives. That is stochastic gradient descent in disguise. The same loop, scaled up, trains every neural network.
- 1. Each gradient descent step moves the weights…
- 2. Stochastic gradient descent looks at…
- 3. In the demo, standardising x makes the error surface…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.A worked example by hand
Points (x, y): (−1, 1), (0, 2), (1, 3). Model ŷ = w₀ + w₁x, loss L = (1/2n)Σ(ŷ − y)². Start at w = (0, 0) with learning rate 0.5.
- Errors ŷ − y = (−1, −2, −3).
- Gradients:
- ∂L/∂w₀ = mean(error) = −2;
- ∂L/∂w₁ = mean(error·x) = (1 + 0 − 3)/3 = −2/3.
- Step: w₀ = 0 − 0.5·(−2) = 1, and w₁ = 0 − 0.5·(−2/3) = 1/3.
- Repeating: w₀ → 2 and w₁ → 1, which is the exact least-squares line y = 2 + x.
The steps in plain English:
- Start with any weights.
- Measure the error of every prediction (or of a random batch).
- Average error × input to get the direction of steepest increase.
- Move a small step the opposite way.
- Repeat until the error stops falling.
- 1. The gradient for the intercept is the average…
- 2. If the learning rate is too large…
- 3. One epoch means…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.Updates, step sizes and schedules
With L(w) = (1/2n)‖Xw − y‖²:
- the gradient is ∇L = (1/n)Xᵀ(Xw − y) and the Hessian is H = XᵀX/n;
- batch GD: w ← w − η∇L;
- SGD on example i: w ← w − η xᵢ(xᵢᵀw − yᵢ);
- mini-batch: the average over a batch B.
Convergence.
- Batch GD converges iff 0 < η < 2/λ_max(H).
- With η = 1/λ_max, the error contracts per step by about (1 − 1/κ), where κ = λ_max/λ_min is the condition number. So the number of steps grows like κ log(1/ε).
- SGD with constant η stalls at a noise floor proportional to η·(gradient variance). Decaying schedules such as ηₜ = η₀/(1 + γt) reach the optimum.
Feature scaling changes κ dramatically: standardise features before gradient methods.
for epoch in 1..E:
shuffle the examples
for each mini-batch B:
g ← (1/|B|) Σ_{i∈B} xᵢ (xᵢᵀw − yᵢ)
w ← w − η_t gfrom sklearn.linear_model import SGDRegressor
from sklearn.pipeline import make_pipeline; from sklearn.preprocessing import StandardScaler
model = make_pipeline(StandardScaler(), SGDRegressor(learning_rate="invscaling", eta0=0.01, max_iter=50)).fit(X, y)
model[-1].partial_fit(X_new, y_new) # online updates as data arriveHyperparameters: learning rate (the most important), batch size, schedule, number of epochs. Use it when data are huge or streaming, or as the building block of larger models. Prefer the closed form for small, dense problems.
- 1. Batch GD on least squares converges for η below…
- 2. Steps needed by GD grow roughly with…
- 3. Constant-step SGD ends…
Level 3: Advanced
★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.Analysis and variants
Convergence of batch GD. Write e = w − w*. Then e ← (I − ηH)e, and each eigen-direction shrinks by |1 − ηλᵢ|. The worst factor is minimised at η = 2/(λ_max + λ_min), giving the rate (κ − 1)/(κ + 1). Convergence requires |1 − ηλ_max| < 1, that is η < 2/λ_max.
SGD. The gradients are unbiased, with variance σ²_g/|B|. With step sizes satisfying Σηₜ = ∞ and Σηₜ² < ∞ (Robbins–Monro), SGD converges, and the suboptimality decays as O(1/t) for strongly convex problems. Averaging the iterates (Polyak–Ruppert) achieves the optimal statistical rate.
Cost. Batch GD costs O(np) per step; SGD costs O(p) per step and needs no full pass to make progress. For large redundant datasets, SGD reaches statistical accuracy after roughly one pass (Bottou and Bousquet, 2008).
Variants:
- momentum and Nesterov acceleration (√κ instead of κ);
- adaptive methods (AdaGrad, Adam);
- conjugate gradients (exact in p steps);
- variance-reduced SGD (SVRG, SAGA, linear convergence);
- the least-mean-squares (LMS, Widrow–Hoff) filter, which is SGD with batch 1 on streaming data.
Interview questions.
- Why shuffle each epoch? It avoids biased ordering effects and cycles, and the stochastic gradient stays unbiased within the epoch.
- Why does large-batch training need a larger learning rate? Less gradient noise allows bigger stable steps, up to the curvature limit 2/λ_max.
- 1. Best constant step for a quadratic with eigenvalues in [λ_min, λ_max]:
- 2. Robbins–Monro conditions require…
- 3. The LMS adaptive filter is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How much computation does each method need?
History. Cauchy proposed gradient descent in 1847. Robbins and Monro (1951) founded stochastic approximation. Widrow and Hoff’s 1960 LMS algorithm brought SGD to adaptive filtering. Bottou’s work in the 2000s explained why SGD wins for large-scale learning.
Exercise 1.
- With features on scales 50 and 0.01, L = 2 577: a stable step is tiny, and 300 batch steps leave the weights far from the optimum. A step slightly above 2/L diverges.
- After standardising (L = 1.01), batch GD matches the closed form to 10⁻⁶.
- Mini-batch SGD with decay reaches the optimal loss 0.1274, while constant-step SGD with batch 1 stays above it.
Exercise 2: examples processed and sequential steps to reach within 1% of the optimal loss. Setup: n = 4 000, 20 standardised features, best learning rate from a 7-value grid, at most 20 passes (300 for batches of 512 and above).
| Batch size | Independent features (κ ≈ 1.3): passes / steps | Correlated features, ρ = 0.95 (κ ≈ 450): passes / steps |
|---|---|---|
| 1 | 1.45 / 5 800 | 2.35 / 9 400 |
| 8 | 0.80 / 400 | 3.40 / 1 700 |
| 64 | 0.67 / 42 | 8.05 / 507 |
| 512 | 1.51 / 12 | 64.1 / 513 |
| 4 000 (batch GD) | 2.00 / 2 | not reached in 300 |
- On a well-conditioned problem: mini-batches of 8–64 need less than one pass over the data, while full-batch GD needs only two steps. The choice depends on whether data passes or sequential steps are the bottleneck (single CPU or parallel hardware).
- With κ ≈ 450, large batches pay the full condition-number penalty: about κ steps, so over 60 passes at batch 512 and more than 300 for batch GD. Small stochastic batches still get within 1% in 2–3 passes, because the poorly conditioned directions contribute little to the loss.
- This is why SGD with modest batches dominates large-scale learning, and why feature scaling still matters.
Gradient-based regression in AI, robotics and aerospace
- Adaptive filters: LMS and NLMS for vibration cancellation and channel equalisation.
- Online system identification: updating aerodynamic or battery models from streaming telemetry.
- Large-scale learning: the same loop trains every deep network (E sections).
- Embedded learning: SGD fits in a few lines of fixed-point code on microcontrollers.
References
- A. Cauchy (1847). “Méthode générale pour la résolution des systèmes d’équations simultanées.” Comptes Rendus 25.
- H. Robbins, S. Monro (1951). “A stochastic approximation method.” Annals of Mathematical Statistics 22(3).
- B. Widrow, M. E. Hoff (1960). “Adaptive switching circuits.” IRE WESCON Convention Record.
- L. Bottou, O. Bousquet (2008). “The tradeoffs of large scale learning.” NeurIPS.
- L. Bottou, F. E. Curtis, J. Nocedal (2018). “Optimization methods for large-scale machine learning.” SIAM Review 60(2).