Sub topic 8
Minimizing the loss with gradient descent
This chapter assumes you know derivatives, the idea of a gradient, and the log loss from the previous chapter. It explains how gradient descent uses the gradient to find good weights. The next chapter works out the gradient of log loss itself.
The idea
Log loss is a function of the weights. Training means finding the weights that make it small. Logistic regression has no closed-form solution like the normal equation in linear regression, so we search for the minimum step by step.
The gradient points in the direction where the loss rises fastest. Moving the opposite way lowers the loss. Each step applies one update rule:
- Compute the gradient of the loss at the current weights.
- Subtract the gradient, scaled by a learning rate, from the weights.
- Repeat until the loss stops improving.
In symbols: w ← w − lr · ∇L(w).
Why it works well here
Log loss is convex for logistic regression. It has one basin and no separate local minima, so with a suitable learning rate gradient descent heads toward the global minimum. There is one exception. If the classes are perfectly separable, the weights grow without bound. A later chapter covers that case.
The learning rate
The learning rate lr sets the step size. This example minimizes (w - 3)², whose gradient is 2(w - 3), using three learning rates:
def descend(grad, w0, lr, steps):
w = w0
for _ in range(steps):
w = w - lr * grad(w)
return w
grad = lambda w: 2 * (w - 3)
for lr in (0.01, 0.1, 1.1):
print(lr, descend(grad, 0.0, lr, 20))The minimum is at 3. The results show three behaviors:
| Learning rate | Result after 20 steps | Behavior |
|---|---|---|
| 0.01 | about 1.0 | Too small. Progress is slow. |
| 0.1 | about 2.97 | Good. It converges steadily. |
| 1.1 | about -112 | Too large. Each step overshoots, and the error grows. |
Batch, stochastic, and mini-batch
The gradient is an average over training examples, so you can compute it from different amounts of data.
- Batch uses all examples per step. The gradient is exact, but each step is costly on large data.
- Stochastic (SGD) uses one example per step. Steps are cheap but noisy.
- Mini-batch uses a small group, such as 32 to 256 examples. It is the usual compromise.
The NumPy implementation in this tutorial uses full batches.
Common errors
- Loss goes up or becomes
nan. The learning rate is too large. Reduce it by a factor of 10. - Loss falls very slowly. The learning rate may be too small. Unscaled features can also cause this, because one feature dominates the gradient. The feature scaling chapter covers the fix.
- Stopping after a fixed number of steps without looking at the loss. Record the loss every so often and plot it. A healthy curve falls and then flattens.
Exercise
You run gradient descent on log loss and the loss alternates between large values and grows each step. What is the most likely cause, and what do you change first?
Show answer
The learning rate is too large, so each update overshoots the minimum and lands farther away. Reduce the learning rate, for example by a factor of 10, and check that the loss then decreases steadily.