Gradient Descent & Optimization Intuition
Walk downhill on a loss surface with step size, batches, and local-minima intuition.
In plain words
The computer repeatedly takes a small step downhill on the error until the error stops getting smaller.
Think of it like…
Walking downhill in fog by feeling the slope: each step goes in the steepest downhill direction. Steps too long and you overshoot the valley and bounce between its walls; too short and you inch along for hours; with a heavy ball (momentum) you roll through small bumps.
Where you’ve seen it
Minimise f(w) = (w − 3)² (derivative 2(w − 3)) starting at w = 0. With η = 0.1: w₁ = 0 − 0.1 × 2 × (−3) = 0.6; w₂ = 0.6 − 0.1 × 2 × (0.6 − 3) = 1.08; w₃ = 1.464; each step closes 20% of the remaining distance to 3. With η = 1.1 the first step jumps to 6.6, then −1.32, then 8.18: the distance to the minimum grows each time, so the iteration diverges. With η = 0.5 it lands exactly on 3 in one step. For a real model, a loss curve that rises or becomes NaN usually means the learning rate is too high.
Words you’ll hear
- Learning rate (η)
- Step size multiplier on the gradient; the most important hyperparameter.
- Mini-batch
- A small random subset of data used to estimate the gradient at each step.
- Momentum
- Accumulates past gradients so updates keep direction and smooth noise.
- Convex vs non-convex
- One global minimum vs many local minima and saddle points.
Visual diagram
Theory & deep dive
The algorithm and the learning rate
At each iteration compute the gradient of the loss at the current parameters and move a small step in the opposite direction. For a quadratic like f(w) = (w − 3)², each step multiplies the error by (1 − 2η): η = 0.1 shrinks it by 20% per step, η = 0.5 solves it in one step, and η > 1 makes |1 − 2η| > 1 so the error grows (divergence). Real losses have different curvature in different directions: in a long narrow valley (typical when features have very different scales) a learning rate that is stable across the steep direction is painfully slow along the shallow one. Standardising features reshapes the valley into a rounder bowl and speeds convergence a lot.
Practical guidance: start with a modest η (1e-3 for Adam, 1e-2 to 1e-1 for plain SGD on standardised data), try a few values on a log scale, monitor training and validation loss, and use learning-rate schedules (step decay, cosine decay, warm-up) to take large steps early and small steps late. Stop when validation loss stops improving (early stopping), which also regularises the model.
w = 0.0; for _ in range(50): grad = 2*(w - 3); w -= 0.1*grad converges towards 3 (error shrinks by 0.8 each step: 3 × 0.8⁵⁰ ≈ 4e-5).
Batches, momentum, Adam and non-convex landscapes
Batch gradient descent uses all n rows per step: exact but expensive. SGD uses one row (very noisy); mini-batches (32–1,024 rows) balance noise, speed and hardware efficiency, and the noise even helps escape shallow local minima. One epoch is a pass through the data; with 100,000 rows and batch size 100 there are 1,000 updates per epoch. Momentum keeps a running average of gradients (v ← βv + ∇L; w ← w − ηv with β ≈ 0.9) to damp oscillations and speed progress along consistent directions. RMSProp and Adam adapt step sizes per parameter using running averages of gradients and squared gradients, which makes them robust defaults for deep learning (Adam's usual η = 0.001).
In convex problems (linear and logistic regression with standard losses) any minimum is global, so a suitable η converges to the same answer. In non-convex problems (neural networks) there are many local minima and saddle points; in high dimensions most problematic points are saddles, and good solutions are reached through noise, momentum, careful initialisation and normalisation rather than by finding the one true minimum. Diagnose training from the loss curve: smooth decrease is healthy; spikes or NaN suggest a too-high η or bad data; a plateau suggests too-small η, vanishing gradients or a saturated activation; a widening gap between training and validation loss indicates overfitting.
100,000 rows, batch size 100: 1,000 steps per epoch; 20 epochs = 20,000 updates. Doubling the batch size halves the steps per epoch, and often lets you raise the learning rate somewhat.
How it works, step by step
Step 1 of 41. Prepare data and loss
Standardise features, define a differentiable loss and initialise parameters sensibly.
- Scaling
- speeds convergence
When to use it & trade-offs
What it is
Gradient descent minimises a loss by repeatedly stepping against its gradient: w ← w − η ∇L(w), where η is the learning rate. Variants differ in how much data each step uses: batch (all rows, accurate but slow), stochastic (SGD) (one row, noisy but cheap) and mini-batch (a small chunk, the practical default). Modern optimisers add momentum and adaptive step sizes (RMSProp, Adam). The loss surface may be convex (a single bowl, as in linear and logistic regression) or non-convex (many valleys and saddle points, as in neural networks), where the goal is a good-enough minimum rather than the global one.
Why it matters: Gradient descent trains almost every modern model, and most training failures trace back to it: a learning rate too large makes the loss oscillate or explode, too small makes training crawl, unscaled features create stretched valleys that slow convergence, and noisy mini-batches need tuning. Knowing the dynamics lets you diagnose loss curves and set sensible hyperparameters instead of guessing.
When to reach for it
- Training linear, logistic and neural models
- Fitting any differentiable objective
- Hyperparameter tuning of learning rates and batch sizes
When to avoid or be careful
- Unscaled features with plain gradient descent
- A learning rate chosen once and never tuned
- Ignoring validation loss while training loss falls
Gradient-descent variants
Strengths: Exact gradient; smooth descent.
Watch out: Slow per step on large data.
Strengths: Fast, hardware-friendly; noise helps generalisation.
Watch out: Needs learning-rate tuning.
Strengths: Adaptive per-parameter steps; robust to scaling.
Watch out: Can generalise slightly worse than tuned SGD in some tasks.
Interview signals & numbers to know
When this topic comes up
- “The loss went to NaN”
- “Training is very slow”
- “Batch vs mini-batch vs stochastic”
- Adam vs SGD
- Local minima and saddle points
Benchmarks & rules of thumb
- 0.6 → 1.08 → 1.464
- iterates for η = 0.1 in the example
- 1 − 2η
- error factor per step for (w − 3)²
- 1e-3
- common Adam learning rate
Common mistakes & follow-ups
Common interview mistakes
- Raising the learning rate to speed up a loss that is already oscillating.
- Training linear models on raw features with wildly different ranges.
- Stopping when training loss is lowest instead of validation loss.
Likely interviewer follow-ups
- What happens if the learning rate is too high or too low?
- How do mini-batches differ from full-batch gradient descent?
- Why does Adam often work better out of the box?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Gradient Descent & Optimization Intuition with data science interview Q&A and hands-on practice.