Attention Is All You Need · Section 6.2

6.2 Model variations

Which parts of the design matter? The paper changes one thing at a time and measures the result, and we did the same on our miniature.

Table 3, rerun on our miniature

The paper's Table 3 changes one thing at a time in its base model (the number of heads, the size, dropout, label smoothing, the positions) and reports what each change does. We did the same on our tiny model, three times each with different random starts, and recorded what happened.

Paper's row Our variant First step at 95 % Held-out loss What the paper found
Base Base: 4 heads, d_k 16, dropout 0.1, label smoothing 0.1, sine positions 187 (180–200) 0.0632 The reference every other row is compared with.
(A) 1 head, d_k 64 227 (180–260) 0.0658 One head is clearly worse; too many heads are worse again.
(A) 16 heads, d_k 4 207 (200–220) 0.0643 One head is clearly worse; too many heads are worse again.
(C) d_model 32 240 (240–240) 0.0699 Bigger models are better.
(C) d_model 128 180 (160–220) 0.0644 Bigger models are better.
(D) No dropout 193 (160–240) 0.1135 Dropout helps a lot against overfitting.
(D) No label smoothing 200 (180–220) 0.0005 Label smoothing hurts perplexity but improves accuracy and BLEU (section 5.4).
(E) Learned positions instead of sines 193 (180–200) 0.0637 Learned positions give nearly identical results to the sines.

Each variant trained 800 steps (warm-up 200) with seeds 2017, 2018, 2019. “First step” is the first measurement, every 20 steps, at which it translated 95 % of 200 held-out sentences word for word: the mean of the three seeds, with the range in brackets. “Held-out loss” is the plain loss (no smoothing) on those sentences at the end, the mean of the three seeds.

Held-out loss as training goes

Held-out loss during training, mean of three seeds On a logarithmic scale, for the variants: base, D-ls0, D-drop0. 1e−4 0.001 0.01 0.1 1 10 200 400 600 800 step Held-out loss
  • Base
  • (D) No label smoothing
  • (D) No dropout

What our reruns can and cannot say

Our task is easy: every variant ends up translating nearly all held-out sentences. So instead of the final BLEU the paper compares, we measure how fast each variant gets there, and how sure it ends up of the right words. Differences of 20 steps are within what a different random start does, as the ranges show.

Three results stand out. Without label smoothing, the held-out loss falls almost to zero, while every smoothed variant stays near 0.06: smoothing keeps the model unsure on purpose (row D, and the label-smoothing tool). Without dropout, the held-out loss is the highest of all: the model overfits a little, the paper's reason for dropout. And learned positions (row E) do as well as the sine waves, as the paper found.

In the paper (6.2)

“While single-head attention is 0.9 BLEU worse than the best setting, quality also drops off with too many heads.”

On its own page: Table 3 explorer

Our reruns of the paper's Table 3 rows on the tiny model, three seeds each: fewer and more heads (A), a smaller and a bigger model (C), no dropout and no label smoothing (D), learned positions (E). For each, the step at which it first translated 95 % of our held-out sentences and its held-out loss, next to the base model, with the loss curves.

What this section says

Table 3 starts from the base model and changes one setting per row, measuring perplexity and BLEU on the English–German development set (newstest2013), with beam search but no checkpoint averaging. The base row scores 25.8 BLEU there; the big model, the last row, 26.4.

  • (A) Heads: h from 1 to 32 with d_k = d_v = 512 / h, so the work stays the same (section 3.2.2). One head is clearly worse; too many heads also lose a little.
  • (B) Key size: a smaller d_k hurts, which suggests that judging compatibility is not easy and a better function than the dot product might help.
  • (C) Size: more layers, wider vectors and wider feed-forward networks are better, as expected.
  • (D) Regularisation: without dropout the model overfits; label smoothing, as section 5.4 said, worsens perplexity but not BLEU.
  • (E) Positions: learned positions instead of sines give nearly the same result (section 3.5).

The method is the lesson for Daniel: when a design has many parts, change one at a time against a fixed baseline, and report what moved.

In the paper's words

“While single-head attention is 0.9 BLEU worse than the best setting, quality also drops off with too many heads.”

Section 6.2 In plain words: One head scores 0.9 BLEU below the best setting, and too many heads are worse too.

“We further observe in rows (C) and (D) that, as expected, bigger models are better, and dropout is very helpful in avoiding over-fitting.”

Section 6.2 In plain words: Bigger models do better, and dropout clearly helps against overfitting.

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
\(N,\ d_{\text{model}},\ d_{ff}\) layers, vector size and feed-forward width: rows (C) 6, 512, 2048 (base) 2, 64, 256
\(h,\ d_k,\ d_v\) heads and their size: rows (A) and (B) 8, 64, 64 4, 16, 16
\(P_{drop},\ \epsilon_{ls}\) dropout and label smoothing: rows (D) 0.1, 0.1 0.1, 0.1
\(\text{PPL}\) perplexity per word piece on the development set; lower is better 4.92 (base) not measured; we use the held-out loss
\(\text{params}\) the number of weights, in millions 65 (base), 213 (big) about a quarter of a million

Try it

Rows (A) by hand: switch heads off
Sentence
Encoder layer
The word that asks (the query)
Layer 2, head 1 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna90 %has40 %23 %a53 %44 %small52 %39 %dog96 %.14 %13 %34 %12 %She91 %likes31 %39 %10 %it13 %37 %33 %.15 %16 %30 %13 %</s>12 %22 %17 %15 %10 %

From “it” it looks most at “.” (37.4 %).

Layer 2, head 2 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna42 %42 %has32 %17 %20 %a35 %13 %10 %13 %small54 %15 %dog14 %11 %41 %.17 %28 %She12 %13 %30 %34 %likes24 %27 %30 %it16 %18 %19 %.17 %30 %</s>15 %12 %18 %13 %

From “it” it looks most at “</s>” (18.6 %).

Layer 2, head 3 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna85 %has35 %13 %a99 %small21 %48 %dog15 %15 %19 %13 %15 %.14 %22 %17 %12 %She11 %43 %likes17 %25 %18 %it88 %.16 %16 %15 %12 %</s>13 %14 %10 %15 %10 %21 %

From “it” it looks most at “dog” (87.9 %).

Layer 2, head 4 Each English token (row) looks at every English token (column), before and after it.Annahasasmalldog.Shelikesit.</s>Anna21 %11 %11 %12 %has18 %15 %15 %37 %a91 %small58 %31 %dog32 %35 %17 %.15 %24 %She16 %10 %18 %22 %15 %10 %likes18 %18 %24 %15 %it25 %14 %34 %.17 %24 %</s>37 %13 %11 %13 %

From “it” it looks most at “dog” (34.3 %).

Playback speed

The layer's attention output for “it”, first 8 of 64 numbers
Token12345678… 56 more
All heads on0.450.120.62−0.710.15−0.171.11−0.06…
As switched now0.450.120.62−0.710.15−0.171.11−0.06…

Every head is on: switch one off to see how much the output changes.

The heads of one attention layer of our model as heat maps on your sentence. Switch a head off and the layer's output, recomputed without it, changes: how much each head contributes.

How our miniature differs

Our task is easy: every variant ends up translating our held-out sentences almost perfectly, so BLEU cannot rank them. We compare instead the step at which each first translates 95 % of them, averaged over three seeds: the base model at step 187; one head 227, sixteen heads 207; d_model 32 at 240, d_model 128 at 180. The pattern echoes the paper's, one head and a smaller model slower, a bigger model faster, but on a task far easier than WMT, so it is a sketch, not a replication.

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 base row

What BLEU does the base model score on the development set in Table 3?

Your answer
Within 0.05 of the answer

One head

According to section 6.2, how does single-head attention compare?

Our fastest variant

Among our reruns of rows (A) and (C), which variant first translated 95 % of the held-out sentences earliest?

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.