Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Learn from one example at a time
Batch methods look at all the data before each update. Stochastic gradient descent (SGD) looks at one example, nudges the weights a little to do better on it, and moves on. This is cheap, works on endless data streams, and keeps adapting if the world changes. The loss you choose decides what kind of classifier you end up with: an SVM (hinge loss), logistic regression (log loss), or a robust hybrid (modified Huber).
| loss | value | slope |
|---|---|---|
| 0–1 | 0.000 | 0.000 |
| hinge (SVM) | 0.500 | -1.000 |
| log (logistic) | 0.684 | -0.545 |
| modified Huber | 0.063 | -0.250 |
Telemetry arrives as a stream, often faster than it can be stored, and what counts as “normal” drifts as hardware ages. An SGD-trained linear classifier updates in microseconds per sample, needs only the current weights in memory, and can track slow changes: ideal for on-board or ground-segment monitoring at scale.
- 1. Each SGD update uses…
- 2. SGD with hinge loss trains…
- 3. In the demo, which losses are exactly zero beyond margin 1?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Two SGD steps by hand
Use hinge loss, no penalty, step η = 0.5, starting at w = (0, 0) with no intercept.
| Example | x | y | Margin m = y·w·x | Hinge slope | Update | New w |
|---|---|---|---|---|---|---|
| 1 | (1, 2) | +1 | 0 | −1 (m below 1) | w + 0.5·(+1)(1, 2) | (0.5, 1) |
| 2 | (2, 0) | −1 | −1 | −1 | w + 0.5·(−1)(2, 0) | (−0.5, 1) |
With log loss, the slope at m = 0 is −1/(1 + e⁰) = −0.5, so the first step would be half as large: w = (0.25, 0.5).
With an L2 penalty λ, every step first shrinks w by the factor (1 − ηλ), as weight decay.
The steps in plain English:
- Standardise the features. SGD is very sensitive to scale.
- Choose the loss (hinge, log or modified Huber), the penalty (L2, L1 or elastic net) and its strength.
- Shuffle the data. For each example, compute the margin, then step against the loss gradient and apply weight decay.
- Decrease the step size over time, for example η_t = 1/(λ(t + t₀)), or keep it constant to follow drifting data.
- Optionally average the iterates for a smoother final model. Stop after a few passes or when validation loss stops improving.
- 1. Hinge slope at margin 2?
- 2. Log-loss slope at margin 0?
- 3. An L2 penalty makes each step…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.The algorithm
Objective: minimise (1/n)Σ ℓ(yᵢ(wᵀxᵢ + b)) + λR(w), with R = ½‖w‖², ‖w‖₁ or the elastic net.
SGD update for example i: w ← (1 − η_tλ)w − η_t ℓ′(mᵢ) yᵢxᵢ (for L2). For L1, apply a soft-threshold or a truncation step.
Losses in the margin m = y·f(x):
| Loss | ℓ(m) | Model | Probabilities |
|---|---|---|---|
| hinge | max(0, 1 − m) | linear SVM | no |
| log | log(1 + e⁻ᵐ) | logistic regression | yes (σ(f)) |
| modified Huber | (max(0, 1 − m))² if m ≥ −1, else −4m | smooth SVM-like, robust | yes, (clip(f, −1, 1) + 1)/2 |
| squared hinge | max(0, 1 − m)² | L2-SVM | no |
| perceptron | max(0, −m) | perceptron | no |
Learning-rate schedules:
- “optimal” (Bottou): η_t = 1/(λ(t + t₀)), with t₀ chosen so the first steps are of order 1;
- inverse scaling: η₀/t^p;
- constant: η₀;
- adaptive: reduce η when the loss stalls.
Averaging (Polyak–Ruppert): report w̄ = mean of the iterates. This reduces noise and attains optimal statistical rates on stationary data.
from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
clf = make_pipeline(StandardScaler(), SGDClassifier(loss="modified_huber", penalty="l2", alpha=1e-4,
learning_rate="optimal", average=True, max_iter=20, tol=1e-4))
clf.fit(X, y); clf.predict_proba(X_new) # available for loss="log_loss" and "modified_huber"
clf[-1].partial_fit(X_batch, y_batch, classes=[0, 1]) # online / out-of-core updatesUse it when the data are large or streaming, the features are sparse and high-dimensional, or you must update continuously. Prefer batch solvers (liblinear, L-BFGS) on small or medium data, where they are more precise and need no step-size tuning.
- 1. Which losses give probability estimates in SGDClassifier?
- 2. partial_fit is used for…
- 3. Iterate averaging mainly reduces…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Convergence and trade-offs
Rates for convex losses:
- with step size ∝ 1/√t, SGD’s expected suboptimality is O(1/√t);
- for λ-strongly convex objectives with η_t = 1/(λt), it is O(log t/(λt)), and O(1/(λt)) with suffix averaging (Rakhlin, Shamir and Sridharan, 2012);
- Pegasos (Shalev-Shwartz et al., 2007) is exactly this for the hinge loss: SVM training in time independent of n.
Large-scale learning (Bottou and Bousquet, 2008). Total error = approximation + estimation + optimisation error. With a fixed time budget, SGD’s cheap, imprecise steps over more data beat precise optimisation on less data. Optimising far beyond the statistical noise level is wasted effort.
Robustness of the losses. Hinge, log and modified Huber grow linearly for very negative margins, so each badly wrong example has bounded influence. Squared hinge and squared loss grow quadratically, so mislabelled points can dominate. Modified Huber combines a quadratic region near the margin (smooth, like squared hinge) with linear tails (robust).
Stationary versus drifting data.
- On a fixed distribution, decaying steps plus averaging converge to the optimum.
- Under drift, the target moves, and a decaying step eventually stops tracking it. Averaging is worse still, because it remembers old boundaries.
- A constant step size gives an exponentially weighted memory of about 1/(ηλ_eff) examples. The step size trades tracking speed against noise (exercise 2).
Interview questions.
- Why standardise for SGD? One step size serves every coordinate, so poorly scaled features make it diverge in some directions and crawl in others.
- Hinge or log loss? Hinge gives sparse support vectors and margins but no probabilities. Log gives probabilities and smooth gradients everywhere.
- 1. Pegasos is SGD on…
- 2. Under concept drift, iterate averaging…
- 3. Which loss lets mislabelled points dominate?
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Classifying a drifting telemetry stream
History.
- Robbins and Monro (1951) introduced stochastic approximation.
- Widrow and Hoff (1960) used SGD with squared loss (LMS, ADALINE).
- Polyak and Juditsky (1992) introduced iterate averaging.
- Zhang (2004) and Bottou (2010) analysed SGD for large-scale linear classifiers.
- Shalev-Shwartz et al. (2007) introduced Pegasos for SVMs.
- Bottou and Bousquet (2008) argued for SGD in large-scale learning.
- Vowpal Wabbit and scikit-learn’s SGDClassifier made online linear learning standard.
Exercise 1. n = 2000, 5 features, noisy linear truth. Averaged SGD reaches training accuracy 0.930 (hinge), 0.932 (log) and 0.932 (modified Huber). On the log loss it gets within 0.0002 of the batch gradient-descent objective in 8 passes. With t₀ = 10 instead (huge first steps), the averaged objective gap was about 36 times larger, because the early iterates poisoned the average.
Exercise 2: drifting telemetry. Setup: a stream of 12 000 samples with 10% label noise (accuracy ceiling 0.90), log-loss SGD with prequential evaluation (predict, then update). In the drifting case, the true boundary rotates 90° over the stream. The table shows accuracy over the last quarter of the stream.
| Schedule | Stationary | Drifting |
|---|---|---|
| Decaying η = 1/(λ(t + t₀)) | 0.874 | 0.884 |
| Decaying + averaging | 0.894 | 0.727 |
| Constant η = 0.005 | 0.892 | 0.858 |
| Constant η = 0.05 | 0.879 | 0.887 |
| Constant η = 0.5 | 0.849 | 0.857 |
- On a stationary stream, averaging is essentially optimal (0.894 against a ceiling of 0.90).
- Under drift, averaging is by far the worst (0.727). It averages boundaries from the whole history, which no longer apply.
- A constant step trades tracking against noise. η = 0.005 is too slow to follow the rotation, η = 0.5 is too noisy, and η = 0.05 is best.
- The “optimal” decaying schedule with a small λ (10⁻³) still had steps of about 0.08 at the end of the stream, so it kept tracking. With a larger λ it would freeze.
SGD classifiers in AI, robotics and aerospace
- Streaming telemetry monitoring: online classifiers that adapt as hardware ages.
- Large-scale logs and text: linear models on millions of sparse features (hashing trick plus SGD).
- Deep learning: every neural network is trained by SGD variants (E3). The schedule and averaging lessons carry over.
- On-board learning: tiny memory footprint, with one update per measurement.
References
- H. Robbins, S. Monro (1951). “A stochastic approximation method.” Annals of Mathematical Statistics 22(3).
- B. T. Polyak, A. B. Juditsky (1992). “Acceleration of stochastic approximation by averaging.” SIAM Journal on Control and Optimization 30(4).
- T. Zhang (2004). “Solving large scale linear prediction problems using stochastic gradient descent algorithms.” ICML.
- S. Shalev-Shwartz, Y. Singer, N. Srebro (2007). “Pegasos: primal estimated sub-gradient solver for SVM.” ICML.
- L. Bottou, O. Bousquet (2008). “The tradeoffs of large scale learning.” NeurIPS.