Gradients
Collect two partial derivatives into a gradient, take one gradient descent step by hand, and see why a learning rate that is too large raises the loss.
Imagine adjusting two numbers in a model, w and b. These adjustable numbers are its parameters. You want to know which small changes might lower its error score.
Use a deliberately small error function, called a loss:
$$L(w,b) = (w-1)^2 + (b+2)^2$$
The loss is smallest at w = 1, b = −2, where both squares are zero. We can see the answer here; that lets us check the method we would use when the answer is harder to see.
Ask one parameter at a time
Start at w = 3, b = −1. The loss is (3 − 1)² + (−1 + 2)² = 5.
First hold b fixed and change w. We need its derivative, the loss's local rate of change as w moves. For a square u², that rate is 2u. Here u = w − 1, which changes by the same amount as w, so the rate is 2(w − 1) = 4.
For example, increasing w from 3 to 3.01 while leaving b at −1 raises the loss from 5 to 5.0401. The change, 0.0401, is close to the rate 4 multiplied by the input change 0.01.
Next hold w fixed and change b. The same calculation gives 2(b + 2) = 2. Each is a partial derivative: we changed one input while holding the other fixed.
Collect those two partial derivatives, in the same order as the parameters:
$$\nabla L(w,b) = [2(w-1),\;2(b+2)],\qquad \nabla L(3,-1)=[4,2]$$
This ordered list of rates is the gradient. An ordered list of numbers is also called a vector. The symbol ∇ means we are collecting rates of change, one per input. Compare equally small moves in different directions, using ordinary distance to measure their size. For a smooth loss, following a nonzero gradient gives the fastest local increase. At a zero gradient, it supplies no preferred direction.
To try decreasing the loss, take a small step in the opposite direction. With a step size of 0.25:
$$[w,b]_{\text{new}}=[3,-1]-0.25[4,2]=[2,-1.5]$$
The new loss is 1² + 0.5² = 1.25. This is gradient descent. Its step size is usually called the learning rate and written η.
A direction still needs a sensible step
The gradient describes nearby change. It does not tell us that every step along the opposite direction will improve the loss.
For the plot below, keep only the first term, L(w) = (w − 1)². Start at w = 3, where its derivative is 4. Predict what happens at η = 0.5, then move the slider. After that, try η = 1.2.
At 0.5 we land on w = 1. At 1.2 we jump past it to w = −1.8, and the loss rises from 4 to 7.84. Even with a correct gradient, a large step can make things worse.
Your turn: Return to the two-parameter loss. At w = 0, b = −2, the gradient is [−2, 0]. Where does one step with η = 0.25 take you? Does b change?
Real model losses are more complicated than this bowl. Zero gradient need not mean a minimum, and training often estimates gradients from a small batch of examples. Keep those limits in mind without needing to solve them yet.
Read next if curious: Deep Learning, §4.3 connects derivatives, gradients and optimization. Find the gradient descent update and match its symbols to the update you just calculated.
- A partial derivative measures the local rate as one input changes while the other independent inputs stay fixed.
- The gradient collects those partial derivatives in input order.
- For a smooth function, a nonzero gradient points toward the fastest local increase under ordinary Euclidean distance.
- Gradient descent subtracts learning rate × gradient from the current parameters.
- A large learning rate can raise the loss. A zero gradient need not identify a minimum.