Part D · Chapter 20 · The paper's model

20. Training as the paper did

Every weight of the model starts as noise and is nudged, step after step, until the translations come out right. This chapter plays our own training run of the paper's model, with the paper's recipe: Adam, the warm-up schedule, dropout and label smoothing.

Our model learning, from noise to a pattern
Playback speed

Our own recorded run of the paper's model, not the paper's: 4,000 steps on a laptop's processor. First the training loss falling, step by step. Then the learning rate following the paper's formula: a straight climb for the warm-up steps, then a slow fall. Then label smoothing: the target for the right word is not 100 % but a little less, the rest spread thinly over every other word. Last, one encoder–decoder head's map on our probe sentence at four moments of training, from even noise at step 0 to a sharp pattern.

The idea

A model learns from examples: an English sentence and its German translation. For each German word, the model gives its probabilities, and the loss measures how little probability it gave the right word: the minus logarithm of it, 0 for a certain right answer and large for a confident wrong one. Training nudges every weight a little in the direction that lowers the loss, over a batch of sentences at a time, and repeats.

The paper's recipe has four ingredients. The Adam optimiser decides how big each weight's nudge is. The learning rate scales all the nudges, and the paper's schedule warms it up from almost nothing, so that the early, chaotic steps do no damage, then lets it fall slowly. Dropout (chapter 14) switches values off at random. And label smoothing asks the model not to be completely sure: the target for the right word is 0.9 plus a sliver, and the remaining tenth is spread over all the other words.

Our run follows the same recipe at our size. Everything in the clip is a recording of it, made once with the training script; the browser trains nothing.

A worked example: our run

Our run took 4,000 steps of 128 sentence pairs and about 4 minutes on a laptop's processor. The training loss fell from 6.82 to 0.87; the loss on the right word alone, without the smoothing, from 6.86 to 0.11.

The learning rate rose for 800 steps to 0.0044, then fell. Encoder–decoder head 2.1's rows on the probe sentence put 14 % on their strongest word at step 0, evenly spread noise, then 90 % at step 500, 91 % at step 2,000 and 91 % at the end. The pattern is found early and then refined.

Label smoothing, as the paper reports, “hurts perplexity”. Our reruns of Table 3 show it plainly: without label smoothing, the held-out loss at the end is 0.0005 against 0.0632 with it, far lower. Yet the translation is no better: both translate every held-out sentence word for word by the end, and the run without smoothing gets there a little later, at step 200 on average against 187.

The formula

\text{lrate} = d_{\text{model}}^{-0.5} \cdot \min\!\left(\text{step\_num}^{-0.5},\ \text{step\_num} \cdot \text{warmup\_steps}^{-1.5}\right)
\text{target}_w = (1 - \epsilon_{ls})\,[w = \text{right}] + \frac{\epsilon_{ls}}{V}, \qquad \text{loss} = -\sum_w \text{target}_w \log p_w
\(\text{step\_num}\)
the training step: the rate rises in proportion to it during the warm-up
\(\text{warmup\_steps}\)
where the rise stops: 4000 in the paper, 800 in our run
\(d_{\text{model}}^{-0.5}\)
a smaller rate for a wider model: 512 in the paper, 64 in ours
\(\epsilon_{ls}\)
how much of the target is spread: 0.1 in the paper and in our run
\(V\)
the vocabulary: every word gets an equal share of the spread, 201 words in ours
\(p_w\)
the model's probability for word w; with the plain target, the loss is just −log of the right word's
Try this Change d_model and warmup_steps yourself and compare the curve with our recorded run.
Why this is true: training follows the slope of the loss

The loss is one number computed from every weight of the model. For each weight, calculus gives the slope: how much the loss would change if that weight moved a tiny bit. Backpropagation computes all those slopes at once, layer by layer from the output back to the input.

Plain gradient descent moves each weight a small step against its slope, scaled by the learning rate. Adam refines this: it keeps a running average of each weight's slopes (β₁ sets how long it remembers them) and of their squares (β₂), and divides one by the square root of the other, so every weight gets a step of a sensible size whatever the scale of its slope; ε only prevents a division by 0.

The residual paths of chapter 14 matter here: the slopes flow back along them straight to the early layers, which is part of why a deep stack can be trained at all.

Why it matters

Training is where the cost of a model is paid. The paper trained its base model on one machine with eight GPUs for about twelve hours; today's large models take thousands of GPUs for months, which is why so few organisations train them and so many rent them through an API.

Label smoothing is also a reminder that the loss is not the goal: a model can score a better loss and translate no better, as our rerun shows. That is why the paper judges its models by BLEU (chapter 19's translations, scored as section 6.1 describes).

Where it fails

  • Our task is easy. Our model translates almost every held-out sentence after a few hundred steps; the paper's models trained for 100,000 steps on real text. Our curves show the shape of training, not its scale.
  • The settings are not universal. The warm-up length, the learning rate and ε_ls were tuned for the paper's models; ours uses a shorter warm-up for a shorter run.
  • A sharper map is not proof of understanding. The head sharpens because aligning words lowers the loss on our data; on harder text the patterns heads learn are messier.
Remember Training lowers the loss, −log of the right word's probability, by nudging every weight with Adam; the paper's learning rate warms up and then decays, dropout and label smoothing keep the model from being overconfident. Label smoothing worsens the loss yet not the translations.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

The learning rate at step 400

With our settings (d_model = 64, warmup_steps = 800), what is the learning rate at step 400? Four decimals.

Your answer
Within 0.0001 of the answer

The smoothed target

With ε_ls = 0.1 and our 201 words, what is the target for the right word? Four decimals.

Your answer
Within 0.0001 of the answer

The paper's peak learning rate

The paper used d_model = 512 and warmup_steps = 4000. What is its highest learning rate, (d_model × warmup_steps)^−0.5? Five decimals.

Your answer
Within 0 of the answer

Which held-out loss is lower?

In our Table 3 reruns, which variant ends with the lower held-out loss: with label smoothing or without?

When does the head find its pattern?

At which recorded checkpoint does head 2.1's map first put more than 80 % on one word per row, on average?

In the paper

Section 5.1: the base models trained on WMT 2014 English–German, “about 4.5 million sentence pairs”, encoded with byte-pair encoding. Section 5.2: “We trained our models on one machine with 8 NVIDIA P100 GPUs”, the base models for 100,000 steps or 12 hours. Section 5.3: “We used the Adam optimizer with \(\beta_1 = 0.9\), \(\beta_2 = 0.98\) and \(\epsilon = 10^{-9}\)”, with the learning-rate formula above and warmup_steps = 4000. Section 5.4: residual dropout with \(P_{drop} = 0.1\), and label smoothing of \(\epsilon_{ls} = 0.1\): “This hurts perplexity, as the model learns to be more unsure, but improves accuracy and BLEU score.”

In the paper: section 5.1–5.4

Open the Theory tab
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.