Step 1 of 5 · Reading · ~3 min
Read
Forward & Backward
Gradient Descent
A gradient tells you which way is uphill. Gradient descent takes a small step the other way, over and over. After this lesson your program fits a line to data by repeated steps, with a learning rate and a batch size you control.
The update rule
For every parameter p with gradient g and learning rate lr:
p <- p - lr * g
The gradient says how fast the loss grows as p grows. Subtract it, scaled by lr, and the loss drops, provided the step is small enough that the slope has not changed under your feet. One such step for every parameter at once is one update.
The learning rate decides everything
Minimise f(w) = (w - 3)^2, whose gradient is 2*(w - 3), starting from w = 0. Each update shrinks the distance to the minimum by the factor 1 - 2*lr:
| lr | factor | w after steps 1, 2, 3, 4 | verdict |
|---|---|---|---|
| 0.1 | 0.8 | 0.60, 1.08, 1.46, 1.77 | converges, slowly |
| 0.5 | 0.0 | 3.00, 3.00, 3.00, 3.00 | lands exactly on the minimum |
| 1.0 | -1.0 | 6, 0, 6, 0 | bounces forever |
| 1.1 | -1.2 | 6.6, -1.32, 8.18, -5.79 | diverges, each step is further away |
Real losses are not parabolas, but the lesson carries over: too small wastes time, too big overshoots, and the right value depends on how steeply the loss curves. When a loss goes up or turns to nan, suspect the learning rate first.
How much data per update
The gradient of a dataset's loss is the average of the per-example gradients. You choose how many examples to average before each update.
- Full batch: all
Nexamples per update. Smooth and stable, but one update per pass over the data. - Stochastic (batch size 1): one example per update. Many cheap, noisy updates.
- Mini-batch:
Bexamples (commonly 32 to 256) per update. The noise shrinks roughly like 1/sqrt(B) and the hardware stays busy. This is the default.
One pass over all the data is an epoch. With N = 10 and B = 4, an epoch makes three updates, on examples 1-4, 5-8 and 9-10. The last batch is smaller; its average must divide by its own size, 2.
Fitting a line
Model prediction = w*x + b with loss mean((prediction - y)^2). For a batch of m examples, with err_i = w*x_i + b - y_i, both gradients are averages:
dL/dw = 2 * sum(err_i * x_i) / m
dL/db = 2 * sum(err_i) / m
Both are computed from the current w and b, and only then are both updated. Start from w = 0, b = 0.
Common mistakes
- Dividing by the full batch size for the final, shorter batch.
- Updating
wand then computing the bias gradient from the neww. - Resetting
wandbat the start of each epoch, so only the last epoch counts. - Forgetting the
x_ifactor in the weight gradient. - Summing instead of averaging, which makes the effective learning rate grow with the batch size.
Your exercise
The input gives LR, EPOCHS, BATCH and the DATA points. The starter parses them, calls your train and prints the final w, b and the mean squared error over all data. Implement train, visiting the data in the given order.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…