Gradient descent: walking downhill

The algorithm that trains almost every model in machine learning. Feel the slope, take a step downhill, repeat.

Lesson · 18 min

In this lesson you will

  • Explain gradient descent as repeatedly stepping in the downhill direction of the cost
  • Read the update rule w := w − α · dJ/dw and say what each part does
  • Explain why the steps shrink automatically near the minimum
  • State the update rules for both w and b in linear regression

You now know what we want: the ww and bb at the bottom of the cost bowl. With two parameters you can find it by eye. With a million you can’t, and you can’t try every combination either. We need a method that finds the bottom without seeing the whole landscape. That method is gradient descent, and it trains nearly every model in this course, from this straight line up to large neural networks.

The idea: lost on a foggy hill

Imagine standing on a hillside in thick fog. You can’t see the valley, but you can feel the ground under your feet. A sensible strategy: feel which way slopes down most steeply, take a small step that way, and repeat. Eventually you reach the bottom.

Gradient descent does exactly this on the cost landscape:

  1. Start with some guess for the parameters (often just w=0w = 0, b=0b = 0).
  2. Measure the slope of the cost at that point.
  3. Take a small step downhill.
  4. Repeat until the steps become tiny: you’ve arrived.

The update rule

To keep things simple, hold bb fixed for a moment and look at JJ as a curve in ww. The slope of that curve at the current ww is the derivative ∂J∂w\frac{\partial J}{\partial w}. You don’t need calculus to follow it: it’s just “how much JJ changes if you nudge ww a little”. One step of gradient descent is:

w:=w−α∂J∂ww := w - \alpha \frac{\partial J}{\partial w}

The symbol :=:= means “replace ww with”. Each part has a job:

  • The slope tells you the direction. If the slope is negative (the curve goes down to the right), subtracting it increases ww, moving right, downhill. If it’s positive, ww decreases. Either way you head downhill.
  • α\alpha (alpha), the learning rate, is a small positive number like 0.10.1 that controls how big each step is. The next lesson is all about choosing it.

Here’s a neat bonus: near the bottom the curve flattens, so the slope gets close to zero and the steps shrink automatically, even though α\alpha stays the same. Gradient descent slows down as it arrives.

Roll the ball

The ball sits on J(w)J(w) for our six houses, with bb fixed at 0.50.5. The straight line touching the curve is the tangent: its steepness is the slope. Press Step to take one update and watch the numbers in the update rule change; press Run to keep going. The small chart shows the line through the data improving as the ball rolls.

Loading the interactive graphic…

Here is the cost curve for our six houses, with the ball at w equals zero. We want to get to the bottom.

Try starting the ball on the right side of the bowl (drag the start-ww slider past the bottom). The slope is now positive, so the same rule moves ww to the left. One rule works from both sides.

Both parameters at once

For the full model we update ww and bb together, each using its own slope. For the squared-error cost these slopes work out to:

∂J∂w=1m∑i=1m(fw,b(x(i))−y(i))x(i)∂J∂b=1m∑i=1m(fw,b(x(i))−y(i))\frac{\partial J}{\partial w} = \frac{1}{m} \sum_{i=1}^{m} \left( f_{w,b}(x^{(i)}) - y^{(i)} \right) x^{(i)} \qquad \frac{\partial J}{\partial b} = \frac{1}{m} \sum_{i=1}^{m} \left( f_{w,b}(x^{(i)}) - y^{(i)} \right)

(This is where the 12\frac{1}{2} in the cost paid off: it cancelled the 2 that comes down from the square.) Then, repeating until convergence:

w:=w−α∂J∂wb:=b−α∂J∂bw := w - \alpha \frac{\partial J}{\partial w} \qquad b := b - \alpha \frac{\partial J}{\partial b}

One important detail: compute both slopes using the current ww and bb, and only then update both. This is called a simultaneous update. If you updated ww first and used the new ww to compute bb‘s slope, you’d be mixing two different points on the landscape.

Because each step uses all mm training examples, this version is called batch gradient descent. Later you’ll meet faster variants that use a handful of examples per step.

Check yourself

Self-check · 4 questions

Gradient descent

1. In the update w:=w−α∂J∂ww := w - \alpha \frac{\partial J}{\partial w}, if the slope at the current ww is negative, what happens to ww?
2. Why do the steps get smaller near the minimum, even with a fixed learning rate?
3. What does a simultaneous update of ww and bb mean?
4. At w=1w = 1, b=0b = 0, the slope is ∂J∂w=−4\frac{\partial J}{\partial w} = -4 and α=0.1\alpha = 0.1. What is the new ww?