Gradient Descent — Learning by Walking Downhill
On this page 10 headings
One complete sigmoid learning step
One example: input 2, target 1. Start with weight 0.5 and bias −1. Use loss ½(prediction − target)² and learning rate 0.5.
Forward pass: find the current prediction.
Differentiate the chosen loss.
The bias has the same derivative, without the input factor.
Subtract the learning rate times each gradient.
Check that the prediction moved toward the target.
What changed? Negative gradients make these parameters increase. The new prediction is closer to the target of 1.
Quick check: Why does subtracting a negative gradient increase a parameter?
Because −η(−g) = +ηg when η and g are positive.
Swipe long formulas, tables or code sideways to see the rest.
Start here Imagine you are on a foggy hill and want to reach the valley. You can’t see far, but you can feel which way the ground slopes under your feet. So you take a small step downhill, feel again, and repeat. That is gradient descent. This page shows why “downhill” means “opposite to the gradient”, using a short Taylor-series argument, and then works it out for the sigmoid neuron.
Find a better way to move on the error surface, so we reach the minimum quickly, without brute-force search.
Put all parameters in one vector: (start randomly).
We will move by a small change . To be careful, we move only a small amount (eta, the learning rate) in that direction:
The question: what is the right ? The answer comes from the Taylor series.
A Taylor series approximates a smooth function L(w) near a point using polynomials. Higher degree → better approximation.
- Linear approximation (degree 1):
- Quadratic approximation (degree 2): add the L″ term.
Close to , even the straight-line approximation is very good.
Everyday example On a small patch of a curved road, the road looks straight. The smaller the patch, the better a straight line fits it.
Write u. From the Taylor series (for many variables):
is small, so are tiny and can be dropped:
The move is good only if the new loss is smaller: , which means
How negative can it be? be the angle between u and :
Multiply by :
The change in loss is most negative when .e. .
Move opposite to the gradient:
where and , both evaluated at .
t ← 0; max_iterations ← 1000
w, b ← random
while t < max_iterations:
w ← w − η ∇w
b ← b − η ∇b
t ← t + 1Now we need and for our sigmoid neuron.
Model: . Take one point (x, y) and loss . (The ½ makes the 2 from the square cancel.)
Step 1 — outer part (chain rule):
Step 2 — derivative of the sigmoid with respect to w:
f(x) · (1 − f(x)) · x
(because .)
Result for one point:
- (same, but instead of x)
For two (or N) points, add them up:
import numpy as np
X = [0.5, 2.5]
Y = [0.2, 0.9]
def f(x, w, b): # sigmoid neuron
return 1 / (1 + np.exp(-(w*x + b)))
def error(w, b): # loss = 0.5 * sum of squared errors
err = 0.0
for x, y in zip(X, Y):
fx = f(x, w, b)
err += (fx - y) ** 2
return 0.5 * err
def grad_b(x, w, b, y):
fx = f(x, w, b)
return (fx - y) * fx * (1 - fx)
def grad_w(x, w, b, y):
fx = f(x, w, b)
return (fx - y) * fx * (1 - fx) * x
def do_gradient_descent():
w, b, eta, max_epochs = -2, -2, 1.0, 1000
for i in range(max_epochs):
dw, db = 0, 0
for x, y in zip(X, Y):
dw += grad_w(x, w, b, y)
db += grad_b(x, w, b, y)
w = w - eta * dw
b = b - eta * db| Point | f(x) | f(1 − f) | grad_w term | grad_b term | ||
|---|---|---|---|---|---|---|
| (0.5, 0.2) | −0.1526 | 0.0452 | −0.003446 | −0.006893 | ||
| (2.5, 0.9) | −0.8991 | 0.000910 | −0.002046 | −0.000818 |
- −0.005492
- −0.007711
- −1.99451
- −1.99229
The loss goes from 0.41581 to 0.41573 — a very small improvement, because the starting point is on a flat part of the surface (tiny gradients).
Lecture example — running it Running the full loop (computed in code): loss after 10 steps (still flat), after 100 steps , after 1000 steps with — the exact best answer. Slow at first on the flat region, then fast.
Later in the course we study gradient descent and its variants in much more detail (Week 4). For now we have an algorithm to learn the parameters of a sigmoid neuron.
In one line: Gradient descent repeatedly moves the parameters a small step opposite to the gradient, because that direction reduces the loss fastest.
Exam words: gradient descent, learning rate , Taylor series, linear approximation, gradient , angle , update rule, , epoch.
