Attention Is All You Need · Section 5.3

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 learning-rate formula, live

Training nudges every weight a little after each batch; the learning rate is how big that nudge is. The paper does not keep it fixed: it starts tiny, grows for a warm-up period, then shrinks slowly. Change the two settings and watch the curve.

The learning rate step by step for your settings, the paper's and ours The rate peaks at step 4000: 0.000699. 0 0.001 0.002 0.003 0.004 0 5000 1e4 1.5e4 2e4 step learning rate peak at 4000
  • yours (512, 4000)
  • the paper (512, 4000)
  • our model (64, 800)

The rate peaks at step 4000: 0.000699.

stepYoursPaper (512, 4000)Ours (64, 800)
10.0000000.0000000.000006
1000.0000170.0000170.000552
8000.0001400.0001400.004419
4,0000.0006990.0006990.001976
20,0000.0003130.0003130.000884

Our recorded training run

Our model trained for 4000 steps with d_model 64 and warmup_steps 800, in 239 seconds on one laptop core. The loss is how surprised the model is by the right German word: it falls fastest while the rate is high. The training loss includes label smoothing, so it cannot reach 0; the plain loss shows how sure the model became.

The learning rate of our recorded training run The rate rises to 0.004419 at step 800, then falls. 0 0.001 0.002 0.003 0.004 1000 2000 3000 4000 step learning rate
The loss of our recorded training run The training loss falls to 0.865, the plain loss to 0.113. 0 2 4 6 1000 2000 3000 4000 step loss
  • training loss (with label smoothing)
  • plain loss

The formula, symbol by symbol

lrate = d_model−0.5 · min(step_num−0.5, step_num · warmup_steps−1.5)

step_num counts the training steps. For the first warmup_steps steps the second term is the smaller one, so the rate rises in a straight line; after that the first term takes over and the rate falls as one over the square root of the step. The peak is exactly at step warmup_steps. d_model^−0.5 scales the whole curve down for bigger models, whose weights react more to each nudge.

Why warm up? At the start the weights are random and the model's early gradients point in poor directions; big steps then would throw it off. A short, gentle start lets the optimiser (Adam) collect reliable estimates first.

In the paper (5.3)

Adam with β1 = 0.9, β2 = 0.98 and ε = 10^−9, and warmup_steps = 4000. With d_model = 512 the peak rate is about 0.0007, reached after 4,000 steps; our model's peak, with 64 and 800, is about 0.0044.

On its own page: Learning-rate schedule

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.

lrate = d_{\text{model}}^{-0.5}\cdot\min\!\left(step\_num^{-0.5},\ step\_num\cdot warmup\_steps^{-1.5}\right) \qquad (3)

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.”

Section 5.3 In plain words: The learning rate climbs in a straight line during the warm-up, then falls as one over the square root of the step.

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

The symbols of this section, in the paper and in our model
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

Try this See our recorded run's loss fall as the learning rate warms up and cools down, and one head sharpen from noise into a pattern, in chapter 20.

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.

Your answer
Within 0.0001 of the answer

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?

Your answer
Within 0.001 of the answer
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.