5.3 Optimizer
How the weights were updated: the Adam optimiser, and a learning rate that warms up and then cools down, in one formula.
The paper's formula (3) as a curve over the training steps: it rises in a straight line during the warm-up and falls as one over the square root of the step after it. Change d_model and warmup_steps and watch the peak move; our recorded run is drawn beside it.
What this section says
Training lowers the loss by nudging every weight a little against its slope, again and again. The learning rate is the size of each nudge. The paper used the Adam optimiser, which keeps for every weight a running average of its recent slopes (β₁ = 0.9) and of their squares (β₂ = 0.98), and divides one by the other, so each weight gets a step suited to it; ε = 10⁻⁹ only keeps that division from being by zero.
The schedule has two phases. For the first warmup_steps steps the second term is the smaller, so the rate grows in a straight line: big steps at the start, when the weights are random, would throw training off course. After that the first term takes over and the rate falls as one over the square root of the step, so the model settles. The two terms meet exactly at step warmup_steps, the peak. The factor d_model⁻⁰·⁵ makes the rate smaller for wider models. The paper used warmup_steps = 4000.
In the paper's words
“This corresponds to increasing the learning rate linearly for the first \(warmup\_steps\) training steps, and decreasing it thereafter proportionally to the inverse square root of the step number.”
Quoted from Vaswani et al., Attention Is All You Need, arXiv:1706.03762v7(opens in a new tab). The figures on this page are redrawn, not copied.
The notation
| Symbol | Meaning | In the paper | Our miniature |
|---|---|---|---|
| \(\beta_1,\ \beta_2\) | how much Adam's running averages of the slopes and of their squares remember | 0.9, 0.98 | 0.9, 0.98 |
| \(\epsilon\) | a tiny number that keeps Adam from dividing by zero | 10⁻⁹ | 10⁻⁹ |
| \(warmup\_steps\) | the steps over which the rate rises; the peak | 4000 | 800 |
| \(step\_num\) | the current training step, from 1 | up to 100,000 (base) | up to 4,000 |
| \(d_{\text{model}}^{-0.5}\) | scales the whole curve down for wider models | 512⁻⁰·⁵ | 64⁻⁰·⁵ |
Try it
How our miniature differs
We train for 4,000 steps, so our warm-up is shorter: 800 steps, with the same formula and the same Adam settings. With d_model = 64 our peak rate is 0.0044; the paper's, with 512 and 4000, is 0.0007.
Prepared in the Course by
Check yourself
Three questions a reader of this section should now be able to answer. The lab checks your answer against the paper and our models.
The paper's peak
With d_model = 512 and warmup_steps = 4000, what is the learning rate at step 4000? Give four decimals.
The shape
How does the paper's learning rate change over training?
After the peak
After the peak, the rate falls as one over the square root of the step. At step 16,000, what fraction of the peak at step 4000 is it?