Skip to content
intermediate Phase 3 · Calculus and Optimization Foundations

Gradient Descent Fundamentals

Step down the loss surface by updating parameters against the gradient.

50m
0 problems
Topic Progress 0%

The Update Rule: w Minus Learning Rate Times Gradient

All of training, from linear regression to a large language model, is one line repeated. At each step, take the gradient of the loss with respect to every parameter, multiply by a small positive number called the learning rate, and subtract: w_new = w_old - learning_rate * gradient. The gradient points uphill, so subtracting walks downhill, and repeating the step from new positions traces a path down the loss surface toward a minimum.

The rule deserves a slow read because every symbol carries a decision. The gradient comes from backpropagation and encodes the surface's local slope. The learning rate encodes how much you trust that local slope: the gradient's promise holds in a neighborhood, and the step size decides how far you travel outside it. The subtraction encodes descent rather than ascent; gradient ascent, used to maximize log-likelihood objectives, flips the sign and is otherwise identical. And the update applies to every parameter at once, which is what lets a single expression govern a model with millions of weights.

Work a concrete trajectory. Loss L(w) = (w - 4)^2 with gradient 2(w - 4), learning rate 0.1, starting at w = 0:

step 0: w=0.0    grad=2(0-4)=-8.0    w_new = 0 - 0.1(-8.0) = 0.8
step 1: w=0.8    grad=-6.4           w_new = 1.44
step 2: w=1.44   grad=-5.12          w_new = 1.952
... converges toward w=4, each step covering 80% of the remaining distance

That geometric decay is not an accident of this example. For a quadratic, gradient descent contracts the distance to the minimum by a fixed factor per step, (1 - 2learning_ratecurvature), so progress is fastest far away and slows asymptotically near the optimum. It also foreshadows the learning-rate story: if the factor's magnitude exceeds one, the distance grows instead of shrinking, which is divergence in one line. The same arithmetic works with a vector of weights unchanged: replace the scalar gradient with the gradient vector, and every weight moves simultaneously by its own slope, which is why one update expression trains a network with millions of parameters.

def gradient_descent(lr, steps=50, w0=0.0):
    w = w0
    trace = [w]
    for _ in range(steps):
        grad = 2 * (w - 4)
        w = w - lr * grad
        trace.append(w)
    return trace

print(gradient_descent(0.1)[-1])    # ~4.0, converged
print(gradient_descent(1.1)[-1])    # oscillates and diverges

The choice of starting point also matters in practice, not just in toy examples. A poor initialization (all-zero weights producing a dead ReLU, or a transformer with an unscaled random init) can place the loss in a region where gradients are near-zero, and no learning rate rescues the run. That is why frameworks ship He or Xavier initialization for neural networks and why transformers use warmup: start small, verify gradients flow, then ramp up.

Two practical properties complete the picture. The update is computed once per pass over the data in its classical form, which you will meet as batch gradient descent shortly. And the rule is oblivious to anything but the current gradient: it carries no memory of previous steps, a limitation that momentum, the next phase's topic, exists to repair. For interviews, being able to run three steps of this arithmetic by hand, and to say why the step shrinks near the optimum, demonstrates that you understand the mechanism rather than the slogan.

Learning Rate: Too Large Diverges, Too Small Crawls

The learning rate is the single most consequential hyperparameter in training, and its failure modes are symmetric in opposite directions. Too large, and each step overshoots the minimum, lands on the opposite slope with an even steeper gradient, and the oscillation grows until the loss becomes NaN. Too small, and the model crawls: each step makes negligible progress, training takes unaffordable epochs, and you risk stopping before reaching any useful minimum. Between them lies a workable range that differs per problem, per optimizer, and per architecture.

The mechanism is visible in the quadratic picture. On a loss with curvature c, the distance to the minimum contracts by a factor of (1 - 2lrc) per step. That factor goes negative when lr exceeds 1/c, which is the oscillation threshold, and its magnitude exceeds one when lr exceeds 2/c, which is outright divergence. The practical translation: high-curvature directions, sharp valleys, tolerate smaller learning rates than flat ones, and a single scalar lr must satisfy the sharpest direction in the surface, which is exactly why ill-conditioned losses train slowly. In multiple dimensions, this is not a hypothetical: real loss surfaces have Hessian eigenvalues spanning orders of magnitude, and the largest one caps the rate for every direction simultaneously.

There is also a subtle interaction with the batch statistics you will meet next. When the gradient is estimated from a noisy minibatch, the effective step includes a random component proportional to the batch's gradient variance; a learning rate that is stable on the full-batch gradient can still diverge on noisy minibatches if the variance times the rate exceeds the curvature's stabilizing pull. This is why batch-size changes often require re-tuning the learning rate in the opposite direction.

def run(lr, w0=0.0, steps=30):
    w = w0
    for _ in range(steps):
        w = w - lr * 2 * (w - 4)
    return w

for lr in [0.01, 0.1, 0.5, 0.9, 1.1]:
    print(lr, round(run(lr), 3))
# 0.01 -> 2.94 (crawling), 0.1 -> 4.0, 0.5 -> 4.0 (edge),
# 0.9 -> oscillates near 4, 1.1 -> grows without bound

Schedules soften the choice. Rather than one constant rate, decay it over training: step decay cuts it by a factor at fixed epochs, cosine annealing sweeps it from a high start to near zero over the run, and warmup ramps it up from tiny values over the first steps so early noisy gradients cannot blow up freshly initialized weights. In practice most production runs use a schedule, and the constant-rate experiment serves as a diagnostic baseline rather than a shipping configuration. Schedules also interact with momentum and adaptive rates, which effectively rescale the step per parameter; a rate that diverges bare may train cleanly once momentum smooths the oscillation, so tune one knob at a time and record what you changed.

Empirical starting points matter in interviews. Classical linear models with normalized features often train stably at 0.01 to 0.1. SGD on neural networks commonly starts near 0.01 or 0.05 with momentum; Adam defaults to 0.001 and tolerates less tuning. These numbers are anchors, not answers: feature scaling changes the curvature, so the same architecture may need a different rate after standardization, and a rate tuned for one batch size may need rescaling for another, a relationship the next topics quantify. The diagnostic loop is always the same: plot the loss, and if it oscillates, halve the rate; if it crawls, multiply it; if it decays smoothly with a shrinking slope, leave the rate alone and spend your effort elsewhere.

Batch Gradient Descent: Stable Steps at Full-Dataset Cost

The update rule needs a gradient, and where that gradient comes from defines the variant of gradient descent you are running. Batch gradient descent computes it from every training example: sum the per-example gradients over all n samples, average them, and take one step. The step is the exact gradient of the mean loss, so it is deterministic and stable, and it costs a full pass over the data per update.

Formally, the batch gradient is (1/n) times the sum over examples of each example's gradient, which matrix form computes as one product: for squared error it is X^T(Xw - y) scaled, the same expression the normal equation solves in closed form. Every step requires reading all n rows, so a dataset with 10 million examples and a model with a 50-millisecond forward pass takes hours per step, and the total step count to convergence may be in the thousands. That arithmetic is the entire motivation for the sampling-based variants, and it is worth saying plainly: on large data, the full pass, not the arithmetic per example, is the bottleneck.

import numpy as np

def batch_gradient_descent(X, y, lr=0.01, steps=200):
    w = np.zeros(X.shape[1])
    history = []
    for _ in range(steps):
        grad = (2 / len(y)) * X.T @ (X @ w - y)   # full-dataset gradient
        w = w - lr * grad
        history.append(np.mean((X @ w - y) ** 2))
    return w, history

What batch descent buys with its cost is predictability. Each step moves in the true descent direction of the mean loss, so the loss decreases monotonically for a sufficiently small rate, and there is no sampling noise to tune around. On convex losses, the classic result is convergence: with an appropriately decaying rate, the iterates approach the global optimum, and for strongly convex quadratic losses the convergence rate is linear in the number of steps. This is why ordinary least squares, ridge regression, and logistic regression can be trained with full-batch optimization and reported as convex problems with unique solutions, and why a full-batch run is the reference implementation you write first to sanity-check a new model.

What it does not buy is speed, and on non-convex surfaces the deterministic path is not even obviously superior: a smooth path can settle into a saddle point that a noisy path would escape, a fact that motivates the stochastic variants entirely. The naming also matters for interviews: strict textbook gradient descent is batch; the minibatch version that every deep-learning framework implements by default reads the name loosely, computing gradients over small sampled subsets and standing between full-batch stability and single-example noise. That middle variant, and the shuffling discipline it requires, is the next topic.

A hybrid technique worth knowing for interviews is gradient accumulation: when the dataset exceeds GPU memory, you split a logical batch into microbatches, compute each microbatch's gradient, sum them, and only call the optimizer step once you have accumulated the equivalent of the full batch. This gives you the stability of large-batch training with the memory footprint of small-batch training, at the cost of one backward pass per microbatch before a single weight update.

Quiz

1. With L(w) = (w - 4)^2, learning rate 0.1, and w = 0, what is the next iterate?

Question 1 options

2. On a loss with curvature c, gradient descent's distance to the minimum contracts by (1 - 2*lr*c) per step. What does this predict as lr grows?

Question 2 options

3. What does one step of batch gradient descent cost, and what does it guarantee on a convex loss?

Question 3 options

4. Why is a deterministic full-batch path not automatically the best training strategy on a deep non-convex network?

Question 4 options

Flashcards

Question

State the gradient descent update rule and what each symbol encodes.

Answer

w_new = w_old - learning_rate * gradient(L, w). The gradient encodes the local uphill direction from backpropagation, the learning rate encodes how far that local estimate is trusted, and the subtraction turns ascent into descent. Repeating it traces a path down the loss surface; gradient ascent flips the sign for maximization objectives.

Question

On a quadratic, how does the error to the minimum evolve per step, and what does that imply about step size?

Answer

The distance to the optimum contracts by a fixed factor (1 - 2*lr*curvature) per step, so progress is geometric: fast far away, asymptotically slow near the minimum. If the factor's magnitude exceeds one (lr above 2/curvature) the iterates diverge; negative factors oscillate, which is why sharp curvature caps the usable learning rate.

Question

What are the two failure modes of the learning rate and their standard remedies?

Answer

Too large: steps overshoot the minimum, oscillation grows, loss diverges to NaN; remedy is halving the rate or clipping. Too small: training crawls through unaffordable epochs and may stop short; remedy is a larger rate or a schedule. Production runs usually use decay (step, cosine) or warmup rather than one constant value.

Question

What does batch gradient descent compute per step, at what cost, and with what guarantee?

Answer

The exact mean-loss gradient by averaging per-example gradients over all n samples, computable as one matrix product; each step costs a full pass over the data. With a suitable rate it descends monotonically and, on convex losses, converges to the global optimum, but the per-step cost makes it impractical for very large datasets.

Question

Why can a deterministic full-batch trajectory reach a worse solution than a noisy one on a non-convex loss?

Answer

Saddle points have exactly zero gradient, so a noiseless trajectory can stall there or crawl along flat regions; stochastic sampling noise perturbs iterates off saddles and through shallow barriers, often landing in better minima. This is why minibatch SGD generalizes differently from full-batch optimization on deep networks.

Revision Notes

Key Takeaways

  • 1. Gradient descent is one line repeated: subtract learning rate times gradient, where the gradient supplies direction, the learning rate supplies trust distance, and the subtraction makes it descent
  • 2. On quadratic losses the distance to the minimum contracts by (1 - 2*lr*curvature) per step, giving geometric convergence, oscillation past 1/c, and divergence past 2/c
  • 3. The learning rate's failure modes mirror each other: overshoot oscillation versus crawling progress, and production training almost always uses a decay or warmup schedule instead of a constant
  • 4. Batch gradient descent takes the exact mean-loss gradient at full-dataset cost per step, buying determinism and convex convergence at the price of impractical step counts on large data
  • 5. On non-convex surfaces the deterministic path can stall at zero-gradient saddles, which is the core argument for the stochastic variants that follow

Interview Tips

  • • "Run two steps of gradient descent at the board" — compute the gradient, multiply by the rate, subtract; then state the geometric contraction factor, which shows you know why the steps shrink
  • • "How do you choose the learning rate?" — start from anchors (0.01-0.1 for scaled classical models, Adam at 0.001), diagnose from the loss curve (oscillate: halve; crawl: double), and prefer schedules in production
  • • "Why doesn't full-batch training win on deep networks?" — per-step cost scales with dataset size, and the noiseless path stalls at saddles; add that stochastic noise aids escape and acts as a regularizer
  • • "What happens to the learning rate when you multiply the batch size by 8?" — the gradient estimate's variance drops, so the same effective step quality needs discussion: linear scaling (larger batches tolerate proportionally larger rates) is the standard heuristic, with warmup to avoid early instability

Cheat Sheet

Gradient Descent Fundamentals

Update rule: w_new = w_old - lr * grad(L, w)

L(w) = (w-4)^2, grad = 2(w-4), lr = 0.1, w0 = 0
0 -> 0.8 -> 1.44 -> 1.952 ... -> 4 (covers 80% of remaining gap per step)

Quadratic dynamics: error contracts by (1 - 2lrcurvature) per step

lr vs curvature Behavior
0 < 1-2*lrc < 1 healthy geometric convergence
factor negative (lr > 1/c) oscillating, alternating sides
factor

Failure signatures and fixes:

  • oscillating loss curve -> halve lr or add decay schedule
  • crawling loss (tiny progress per epoch) -> raise lr or lengthen warmup
  • NaN after a few steps -> lr too high for current feature scale; rescale or clip

Batch GD: exact mean gradient per step = full pass over n rows (one matrix product); deterministic, monotonically decreasing for small lr, converges on convex losses; too expensive for huge datasets.

Schedules: step decay, cosine annealing, linear warmup (protects fresh weights from early noisy gradients).

Saddle point point: exact gradients vanish there, so noiseless paths can stall; noisy paths escape, motivating SGD on non-convex surfaces.

Continue Learning