Find a course

Module 3: How Networks LearnLesson 6 of 1115 min

Gradient Descent

See how gradient descent lowers the loss step by step, run the update rule by hand, and learn what happens when the learning rate is too small or too large.

Step 1 of 13Learn

Walking downhill in fog

Picture yourself on a mountainside in thick fog, trying to reach the valley floor. You can't see the valley, but you can feel the ground 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 a low point.

That is gradient descent, the algorithm that trains almost every neural network. The "position" is the current set of weights. The "height" is the loss: how wrong the network is on the training data. The "slope under your feet" is the gradient, which tells you how the loss changes if you nudge each weight. You can't see the whole loss surface, since with 100,000 weights it has 100,000 dimensions, but you can always compute the local slope.

Training starts from random weights, high up the mountain. Each step computes the gradient, moves the weights a little in the downhill direction, and checks the loss again. After thousands of steps, the network sits in a valley where its predictions are good. The rest of this lesson looks at how big each step should be and what can go wrong along the way.

Gradient descent on a U-shaped loss curve: a ball takes shrinking steps downhill toward the minimum at w = 3minimum (w = 3)lossweight w

Step 2 of 13Learn

The update rule with one weight

Start with the simplest case: a loss that depends on a single weight, L(w) = (w − 3)2. Its minimum is obviously at w = 3, but let's pretend we don't know that.

The derivative dL/dw = 2(w − 3) gives the slope at any point. At w = 0, the slope is −6: negative, meaning the loss goes down as w increases. So we should increase w. Gradient descent turns this into one rule:

w_new = w − η · dL/dw

Here η (the Greek letter eta) is the learning rate, a small positive number that sets the step size. The minus sign is the key: we move against the slope, downhill.

With η = 0.1, starting at w = 0:

  • Step 1: slope −6, so w = 0 − 0.1·(−6) = 0.6
  • Step 2: slope 2(0.6 − 3) = −4.8, so w = 0.6 + 0.48 = 1.08
  • Step 3: slope −3.84, so w = 1.08 + 0.384 = 1.464

The steps shrink automatically as w approaches 3, because the slope gets flatter near the bottom. With this loss and learning rate, each step removes 20% of the remaining distance to the minimum.

Step 3 of 13Practice · Work it out

For L(w) = (w − 3)2 with learning rate η = 0.25, what is w after one gradient descent step starting from w = 5?

Step 4 of 13Explore

Learning rate explorer

The curve is the loss L(w) = (w − 3)2. Choose a starting weight and a learning rate, and the readouts show where gradient descent lands. Try η = 0.5, 0.9, 1.0 and 1.1.

020406080100 -50510
w after 1 step
-2.6
w after 5 steps
0.706
w after 20 steps
2.919
Loss after 20 steps
0.0065

Step 5 of 13Learn

Choosing the learning rate

The learning rate is the single most important setting in training, and the explorer shows why.

  • Too small (η = 0.01 above): every step is tiny. The loss does go down, but after 20 steps you've covered only a third of the distance. On a real network, training would take far longer than necessary or stall on a flat stretch.
  • Just right: the loss falls quickly and smoothly. For this particular curve η = 0.5 lands exactly on the minimum in one step, but real loss surfaces are never this tidy.
  • Too large (η = 0.9): each step overshoots the minimum, landing on the other side. The weight zigzags back and forth and settles slowly.
  • Far too large (η above 1 here): each overshoot is bigger than the last. The loss explodes, and in a real framework you soon see "NaN" (not a number) in your logs.

In practice, learning rates between about 0.0001 and 0.1 are common, depending on the optimizer. Many training runs use a schedule: a short warm-up, then a learning rate that gradually decreases so the final steps can settle precisely into a valley. Optimizers like Adam go further and adapt the step size separately for every weight, which makes them forgiving and a popular default.

Step 6 of 13Chart

Loss per step for three learning rates

Gradient descent on L(w) = (w − 3)2, starting from w = 0, so the loss starts at 9.

Loss per step for three learning ratesLine chart with 7 categories: 0: η = 0.05 (too small) 9, η = 0.2 (good) 9, η = 1.05 (too large) 9; 1: η = 0.05 (too small) 7.29, η = 0.2 (good) 3.24, η = 1.05 (too large) 10.89; 2: η = 0.05 (too small) 5.9, η = 0.2 (good) 1.17, η = 1.05 (too large) 13.18; 3: η = 0.05 (too small) 4.78, η = 0.2 (good) 0.42, η = 1.05 (too large) 15.94; 4: η = 0.05 (too small) 3.87, η = 0.2 (good) 0.15, η = 1.05 (too large) 19.29; 5: η = 0.05 (too small) 3.14, η = 0.2 (good) 0.05, η = 1.05 (too large) 23.34; 6: η = 0.05 (too small) 2.54, η = 0.2 (good) 0.02, η = 1.05 (too large) 28.24.0102030loss0123456step
Computed exactly: each step multiplies the loss by (1 − 2η)2.
Loss per step for three learning rates
stepη = 0.05 (too small)η = 0.2 (good)η = 1.05 (too large)
0999
17.293.2410.89
25.91.1713.18
34.780.4215.94
43.870.1519.29
53.140.0523.34
62.540.0228.24

Step 7 of 13Practice · Estimate

For L(w) = (w − 3)2, which learning rate takes gradient descent from any starting point straight to the minimum in exactly one step?

0.3

Step 8 of 13Practice · Diagram

What each learning rate does on L(w) = (w − 3)2

Which learning rate never improves the loss at all, yet never blows up either?

Tap the shape that answers the question, then press Check.

What each learning rate does on L(w) = (w − 3)^2Scale: η = 0.01: crawls, η = 0.5: one step, η = 0.9: zig-zags in, η = 1.0: bounces forever, η = 1.1: blows up.Creeps in from one sideOvershoots but settlesDiverges00.20.40.60.811.20.01η = 0.01: crawls0.5η = 0.5: one step0.9η = 0.9: zig-zags in1η = 1.0: bouncesforever1.1η = 1.1: blows up
η = 0.01: crawls

Each step closes only 2% of the gap.

η = 0.5: one step

For this loss, w − 0.5·2(w − 3) = 3 from any start.

η = 0.9: zig-zags in

Jumps past the minimum each time, but by less and less.

η = 1.0: bounces forever

Jumps from w to 6 − w and back, never getting closer.

η = 1.1: blows up

Each jump overshoots further: the loss grows without limit.

Step 9 of 13Learn

Many weights: the gradient vector

A real network has thousands or millions of weights, and the loss depends on all of them. The gradient collects one partial derivative per weight into a vector: how much the loss changes if you nudge that one weight while holding the rest fixed. The gradient points in the direction of steepest increase, so gradient descent moves every weight a little in the opposite direction, all at the same time:

each weight w_i ← w_i − η · ∂L/∂w_i

The loss surface of a network isn't a simple bowl. It has flat plateaus, long narrow ravines and many low points. A local minimum is a point lower than everything nearby but not the lowest overall. Early researchers worried that networks would get stuck in poor local minima. In very high dimensions, it turns out, most points with zero slope are saddle points (downhill in some directions, uphill in others) rather than true traps, and many of the minima networks find are about equally good.

The noise in practical training helps here. Networks compute each gradient on a small random batch of examples rather than the whole dataset, so each step is slightly off. That jitter is enough to roll off saddle points and out of sharp, narrow dips.

Step 10 of 13Note

Step 11 of 13Practice · Code challenge · Python

Write gradient_descent(w, lr, steps) that minimizes L(w) = (w − 3)2. On each of the steps iterations, compute the gradient 2·(w − 3) and update w = w − lr·gradient. Return the final w rounded to 4 decimal places.

Real Python, running in a sandbox in your browser.
  • gradient_descent(0, 0.1, 1)
  • gradient_descent(0, 0.1, 2)
  • gradient_descent(-7, 0.5, 1)
  • gradient_descent(10, 0.1, 30)
  • gradient_descent(4, 1.5, 3)

Step 12 of 13Practice · What would you do?

What would you do?

Priya trains a network and sees the loss go 2.31, 2.10, 2.45, 3.80, 9.70, then NaN within the first 50 steps. What should she try first?

Step 13 of 13Practice · Choose one

At the current weights, the partial derivative of the loss with respect to one weight is +2.4. How does gradient descent change that weight?