On the path Attention paperAdam, with a learning rate that rises for the first 4,000 steps and then falls.
From gradient to step
Training computes, for every weight, the gradient of the loss: which way and how steeply the loss rises. An optimiser decides how far to move each weight against it. Plain gradient descent takes the same fraction of every gradient, the learning rate η: w ← w − η g. Computed on a random batch rather than all the data, it is stochastic gradient descent.
Two problems follow. Gradients from one batch to the next are noisy and zig-zag; and weights differ widely in how large their gradients are, so one learning rate is too large for some and too small for others.
Momentum and Adam
Momentum moves along a running average of past gradients instead of the last one, which smooths the zig-zag. Adam (Kingma and Ba, 2015) keeps two running averages per weight: m of the gradient and v of its square, and divides one by the square root of the other.
g is the weight’s gradient on this batch; β1 and β2, close to 1, set how long the averages remember; m̂ and v̂ are m and v corrected for starting at 0 (divided by 1 − βt after t steps); ε keeps the division safe.
Dividing by √v̂, the typical size of the weight’s own gradient, gives every weight a step of about η in size, whatever the scale of its gradients: Adam gives each weight its own learning rate.
The learning rate over time
The learning rate is rarely kept fixed. Early on the averages are poor and the weights random, so large steps can throw training off; a warm-up raises the rate from near 0 over the first steps. Later, smaller steps settle the weights; the rate is lowered, a decay.
On the path Attention paper
In the paper. The Transformer was trained with Adam, β1 = 0.9, β2 = 0.98 and ε = 10−9, and this learning rate (section 5.3):
With dmodel = 512 and warmup = 4,000 the rate rises linearly to about 0.0007 at step 4,000, then falls as 1/√step.
The base model trained for 100,000 steps, about 12 hours on 8 NVIDIA P100 GPUs (0.4 seconds per step); the big model for 300,000 steps, 3.5 days.
Test yourself
What problem does dividing by √v̂ solve?
Weights whose gradients are of very different sizes: dividing by each weight’s typical gradient size gives every weight a step of similar size, as if each had its own learning rate.
Why warm up the learning rate at the start?
At first the weights are random and Adam’s averages rest on few gradients, so large steps can throw training off; a small rate that grows avoids that.
At which step does the paper’s learning rate peak, and at what value?
At step 4,000, at 512−0.5 · 4000−0.5 ≈ 0.0007.
Where it leads
Kept in this browser only, never sent.
Paths through it
- Attention paperstep 14 of 17Follow this path