Gradient Descent: How Models Learn
See how gradient descent walks downhill on a loss surface, how the learning rate controls convergence, and how batches make training scale.
Welcome back!
Step 1 of 14Learn
Walking downhill in fog
Picture yourself on a hillside in thick fog, trying to reach the lowest point of the valley. You can't see the valley, but you can feel the slope under your feet. A sensible strategy: feel which way the ground slopes down most steeply, take a step that way, and repeat. Eventually you reach the bottom.
That is gradient descent, the optimization method behind most of machine learning, from linear regression to giant neural networks.
- The hillside is the loss surface: the loss plotted against every possible value of the parameters.
- Your position is the current parameter values.
- The slope under your feet is the gradient: the vector of partial derivatives of the loss with respect to each parameter. It points uphill, in the direction of steepest increase.
- So each step moves against the gradient.
The update rule for a single parameter w:
w_new = w − η × (dLoss/dw)
where η (the Greek letter eta) is the learning rate, a small positive number that sets the step size.
Take a tiny example. Suppose the loss is L(w) = (w − 3)², a bowl with its minimum at w = 3. Its derivative is 2(w − 3). Start at w = 5 with η = 0.25:
- Gradient = 2(5 − 3) = 4. New w = 5 − 0.25 × 4 = 4.
- Gradient = 2(4 − 3) = 2. New w = 4 − 0.25 × 2 = 3.5.
- Gradient = 2(0.5) = 1. New w = 3.5 − 0.25 = 3.25.
Each step halves the distance to the minimum. Notice that steps shrink automatically as the slope flattens near the bottom.
Step 2 of 14Diagram
One training loop
Tap a shape with to see more.
- Predict
Run the current weights on a batch of examples.
- Measure loss
How wrong were the predictions, e.g. MSE.
- Compute gradient
The slope of the loss for every weight: which way is uphill.
- Step downhill
w = w − η × gradient.
Step 3 of 14Practice · Work it out
The loss is L(w) = (w − 3)², with derivative 2(w − 3). Starting at w = 7 with learning rate 0.1, what is w after one gradient descent step?
Step 4 of 14Explore
One step on a loss bowl
The curve is the loss L(w) = (w − 3)². Choose a starting point w₀ and a learning rate η. The readouts show where one and two gradient steps land. Try η = 0.1, 0.5, 0.9 and 1.1 and see when steps converge smoothly, jump straight to the minimum, zigzag, or blow up.
- w after 1 step
- 7
- w after 2 steps
- 6.2
- Loss after 2 steps
- 10.24
Step 5 of 14Learn
The learning rate: the most important dial
The learning rate η is a hyperparameter: you choose it, and training does not learn it. It has an outsized effect.
- Too small: each step barely moves. Training converges eventually but may need a huge number of iterations, wasting time and compute.
- About right: the loss falls quickly and settles near the minimum.
- Too large: steps overshoot the minimum. The parameter zigzags from one side of the valley to the other; if η is large enough, each overshoot is bigger than the last and the loss explodes to infinity, often appearing in code as
NaN(not a number).
On the bowl L(w) = (w − 3)², you can see this exactly. Each step multiplies the distance from the minimum by (1 − 2η). With η = 0.1 the distance shrinks by 20% per step. With η = 0.5 it becomes zero in one step. With η between 0.5 and 1, the sign flips each step, a zigzag that still converges. With η greater than 1, the distance grows every step and training diverges.
Real loss surfaces are not perfect bowls, so practitioners use heuristics:
- Try values spaced by factors of about 10, such as 0.1, 0.01 and 0.001, and watch the loss curve.
- Use a learning rate schedule that starts larger and decreases over time.
- Use adaptive optimizers such as Adam, which adjust the step size for each parameter automatically based on recent gradients. They are the default for most deep learning.
Plotting the training loss after every epoch is the simplest diagnostic: a smooth decline is healthy, a flat line suggests η is too small, and a jagged or rising curve suggests it is too large.
Step 6 of 14Chart
Loss per step on L(w) = (w − 3)², starting at w = 7
Exact values: each step multiplies (w − 3) by (1 − 2η), so the loss is multiplied by (1 − 2η)².
| Item | η = 0.01 (too small) | η = 0.3 (about right) | η = 1.1 (too large) |
|---|---|---|---|
| Step 0 | 16 | 16 | 16 |
| 1 | 15.37 | 2.56 | 23.04 |
| 2 | 14.76 | 0.41 | 33.18 |
| 3 | 14.17 | 0.07 | 47.78 |
| 4 | 13.61 | 0.01 | 68.8 |
| 5 | 13.07 | 0 | 99.07 |
Step 7 of 14Practice · Code challenge
For the loss L(w) = (w − 3)², whose derivative is 2(w − 3), write step(w, lr) that returns the new value of w after one gradient descent step with learning rate lr.
step(5, 0.25)step(0, 0.5)step(3, 0.1)step(11, 0.25)
Step 8 of 14Learn
Batches, epochs and local minima
Computing the exact gradient requires looking at every training example. With millions of examples, one step becomes very slow. Three variants trade accuracy of each step for speed:
- Batch gradient descent uses the whole dataset for every step. Accurate but slow on large data.
- Stochastic gradient descent (SGD) uses a single random example per step. Very fast and noisy; the path wanders but trends downhill.
- Mini-batch gradient descent uses a small random batch, often 32 to 512 examples. It balances speed and stability, uses hardware such as GPUs efficiently, and is the standard in practice. People often call it SGD anyway.
An epoch is one full pass through the training data. With 10,000 examples and a batch size of 100, one epoch is 100 steps.
Local minima and saddle points. For linear regression with MSE, the loss is a perfect bowl (it is convex), so gradient descent always finds the single global minimum. Neural network loss surfaces are not convex: they have many valleys, flat plateaus and saddle points. In practice, for large networks, many of the minima found are about equally good, and the noise from mini-batches helps the optimizer escape flat regions and poor spots.
Feature scaling matters here. If one feature ranges up to 5,000 and another up to 5, the loss surface becomes a long, narrow valley. Gradient descent zigzags across the narrow direction and crawls along the long one. Standardizing features makes the valley rounder, so the same learning rate works for every direction and training converges far faster.
Step 9 of 14Compare
Batch vs. stochastic vs. mini-batch
| Aspect | Batch | Stochastic (SGD) | Mini-batch |
|---|---|---|---|
| Examples per step | All | 1 | 32 to 512 |
| Steps per epoch (50,000 rows) | 1 | 50,000 | 200 at batch 250 |
| Path | Smooth | Very noisy | Mildly noisy |
| Speed on big data | Slow | Fast but erratic | Fast, uses GPUs well |
| Used in practice | Small datasets | Rarely pure | The default |
Step 10 of 14Practice · Work it out
You train on 50,000 examples with a mini-batch size of 250 for 8 epochs. How many gradient descent steps (parameter updates) occur?
Step 11 of 14Practice · Match the pairs
Match each variant or term to its description.
Tap an answer, then tap the place where it belongs (or drag it there).
- Batch gradient descent
- Stochastic gradient descent
- Mini-batch gradient descent
- Epoch
- Convex loss
Step 12 of 14Note
Step 13 of 14Practice · Choose one
Your training loss decreases very slowly and is still falling steadily after 500 epochs, with no zigzagging. What is the most likely issue?
Step 14 of 14Practice · True or false
For linear regression with mean squared error loss, gradient descent with a suitable learning rate will reach the global minimum rather than getting stuck in a local one.
Lesson complete!