Attention Is All You Need · Section 5.4

5.4 Regularisation

Two tricks that keep the model from memorising its training data: dropout switches values off at random, and label smoothing makes the target a little less certain.

Residual dropout, live

During training, dropout switches off a random share of the numbers flowing through the model, a different share every time. The model cannot rely on any one number, so it learns patterns that survive losing a few: it overfits less.

Mask seed 2017

Dropout with P_drop = 10 % on 16 numbers of the word dog: the input, which values survive, and the rescaled output 14 of 16 values kept. input kept? output ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✗ ✓ ✓ ✗ ✓ ✓ ✓ ✓
12345678… 8 moresum of all 16
input−0.93−0.380.430.25−1.000.54−0.41−0.41…−7.02
output−1.03−0.420.470.28−1.110.60−0.45−0.46…−6.97
average of 1,000 masks−0.91−0.380.430.26−0.980.55−0.41−0.41…−6.93

This mask drops 2 of 16 values and multiplies the rest by 1.111. Over 1,000 masks the average differs from the input by at most 0.04.

Why rescale, and why switch it off?

Dropping a share p of the values makes the vector smaller on average. Multiplying the survivors by 1 / (1 − p) puts the average back: with p = 0.1, the nine in ten that survive are multiplied by about 1.11. The last row shows the average over 1,000 different masks: it comes back to the input, give or take chance.

When the model translates, there is nothing to learn, and we want the same answer every time, so dropout is off: every value passes unchanged. Because of the rescaling during training, the values the next layer sees have the same size in both cases.

In the paper (5.4)

“We apply dropout to the output of each sub-layer, before it is added to the sub-layer input and normalized. In addition, we apply dropout to the sums of the embeddings and the positional encodings in both the encoder and decoder stacks. For the base model, we use a rate of P_drop = 0.1.”

On its own page: Dropout

A vector's values switched off at random with probability p, the rest scaled up by 1/(1 − p) so the average stays the same, with a new random mask each time; and why dropout is switched off when the model translates.

What this section says

A model with many weights can learn its training sentences by heart and then do worse on new ones: overfitting. Regularisation is anything that makes that harder. The section's heading announces three types; the text describes residual dropout and label smoothing.

Residual dropout: during training, the output of every sub-layer has a random 10 % of its values set to 0 (P_drop = 0.1) before it is added to the sub-layer's input and normalised; so do the sums of embeddings and positional encodings at the bottom of both stacks. The model cannot rely on any single value. When translating, dropout is off.

Label smoothing: the target for each position is not 100 % on the right token but 1 − ε_ls on it, with ε_ls = 0.1 spread evenly over the whole vocabulary. The model learns to be a little unsure. The paper notes the trade: perplexity, a measure of how surprised the model is by the right token, gets worse, but accuracy and BLEU get better.

In the paper's words

“We apply dropout [33] to the output of each sub-layer, before it is added to the sub-layer input and normalized.”

Section 5.4 In plain words: Dropout acts on each sub-layer's output, before the residual addition and the normalisation.

“This hurts perplexity, as the model learns to be more unsure, but improves accuracy and BLEU score.”

Section 5.4 In plain words: Label smoothing makes the model less sure, which raises perplexity but improves accuracy and BLEU.

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
\(P_{drop}\) the share of values dropout switches off, in training only 0.1 (base), 0.3 (big; 0.1 for English–French) 0.1
\(\epsilon_{ls}\) the share of the target spread over all tokens 0.1 0.1
\(\text{perplexity}\) how surprised the model is by the right token: e to the loss; lower is surer per word piece, reported in Table 3 not reported; we report the held-out loss

Try it

Label smoothing, live

When the model learns that “dog” is “Hund”, the plain target says: probability 1 for Hund, 0 for every other word. Label smoothing asks for a little less certainty: it keeps most of the target on the right word and spreads a small share ε over the whole vocabulary.

The training target

  1. Hund 91.7 %
  2. Katze 1.7 %
  3. Vogel 1.7 %
  4. Pferd 1.7 %
  5. Ball 1.7 %
  6. Buch 1.7 %

The loss of three predictions

Predictionp(Hund)Loss, smoothed targetLoss, plain target
Sure and right97.4 %0.4770.027
Unsure30.9 %1.2481.175
Sure and wrong0.2 %5.8776.027
Exactly the smoothed target91.7 %0.4210.087

The three predictions are made up to show the idea; the losses are computed from them. The last row is the prediction that matches the smoothed target exactly: the lowest loss training can reach.

Our model, trained with and without it

Our modelHeld-out lossPerplexityFirst step at 95 %
With label smoothing (ε_ls = 0.1)0.06321.0652187 (180–200)
Without label smoothing0.00051.0005200 (180–220)

Without smoothing, the held-out loss falls almost to 0: the model is nearly certain of every right word. With smoothing it stays near 0.06, because the model was trained never to be quite sure. Yet both first translate 95 % of held-out sentences at about the same step, and both translate them right. A worse loss, the same translations: that is the paper's point.

Why make a model less sure?

With a plain target, the model can always lower its loss by being even more certain, pushing its scores further apart. That habit makes it overconfident about wrong words too, and it learns little about which other words were nearly right. With a smoothed target, being too certain costs loss, so the model keeps some probability for close alternatives, which tends to help the beam search and the final translation.

Perplexity is e to the power of the loss: roughly how many words the model is hesitating between. Smoothing raises it on purpose, so it is a poor guide to a smoothed model's quality; BLEU, which scores the translations themselves, is the better one.

In the paper (5.4)

“During training, we employed label smoothing of value ε_ls = 0.1. This hurts perplexity, as the model learns to be more unsure, but improves accuracy and BLEU score.”

On its own page: Label smoothing

The training target spread with ε, and what it does to the loss of a confident and an unsure prediction; then our model trained with and without it.

How our miniature differs

We reran both on our miniature (Table 3 rows D, three seeds each). Without dropout the held-out loss ends at 0.1135 against 0.0632: dropout helps on our task too. Without label smoothing it ends far lower, at 0.0005, yet the model translates no better and first reaches 95 % of the held-out sentences later, at step 200 against 187: lower loss, no better translations, as the paper says.

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 smoothed target

With ε_ls = 0.1 spread evenly over our 201 tokens, how much of the target does the right token get? Give four decimals.

Your answer
Within 0.0001 of the answer

Dropout's rescaling

With P_drop = 0.1, by how much are the values that stay multiplied, so the average is unchanged? Give three decimals.

Your answer
Within 0.0005 of the answer

Why perplexity gets worse

Why does label smoothing hurt perplexity yet improve BLEU?

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.