Attention Is All You Need · Section 3

3 Model architecture

The whole Transformer on one page: an encoder that reads the input and a decoder that writes the output, one token at a time. Figure 1 is the map the rest of section 3 fills in.

Figure 1, redrawn and live

This is the transformer as the paper draws it: the encoder reads the English on the left, the decoder writes the German on the right, both from the bottom up. Click any box to see what it computes for your sentence, or press Play to watch a whole translation pass through it.

Watch a whole translation step by step

English sentence

The model's German: Anna hat einen kleinen Hund . Sie mag ihn .

Playback speed
The paper's Figure 1 redrawn: the encoder on the left and the decoder on the right, each box a button that shows its numbers. N× ×N N = 2 N = 2 Inputs Input Embedding + Positional Encoding Multi-Head Attention Add & Norm Feed Forward Add & Norm Outputs (shifted right) Output Embedding + Positional Encoding Masked Multi-Head Attention Add & Norm Multi-Head Attention Add & Norm Feed Forward Add & Norm Linear Softmax Output Probabilities
After Figure 1 of Vaswani et al. (2017), redrawn.

Multi-Head Attention

In the encoder, paper section 3.2.2

Every English token looks at every other one through four heads at once, mixes in what it finds, and the heads' results are joined and projected back to 64 numbers.

Trace step enc.0.self.out: 64 numbers per token
Token12345678… 56 more
Anna−0.290.61−0.56−0.10−0.09−0.460.35−0.12…
has−0.110.32−0.35−0.06−0.04−0.300.28−0.07…
a−0.560.39−0.43−0.370.05−0.550.81−0.33…
small−0.780.09−0.46−0.54−0.17−0.690.62−0.44…
dog−0.320.41−0.49−0.160.13−0.470.25−0.10…
.−0.45−0.29−0.32−0.35−0.54−0.110.23−0.20…
She−0.140.51−0.43−0.03−0.01−0.290.25−0.10…
likes−0.230.35−0.44−0.08−0.15−0.380.32−0.12…
it−0.580.17−0.13−0.390.06−0.350.49−0.28…
.−0.80−0.640.17−0.52−0.480.100.01−0.36…
</s>−0.850.08−0.63−0.69−0.41−0.840.83−0.47…
On its own page: The paper's Figure 1, live

The paper's Figure 1 redrawn in the lab's style: the encoder on the left and the decoder on the right, each read from the bottom up. Every box is a button: click it to see what it computes for your sentence on our model, with the layer when the box sits inside the stack of N. Play lights the boxes in the order a translation computes them.

What this section says

Most translation models of 2017 had the same overall shape, and the Transformer keeps it. The encoder turns the input sentence, n tokens, into n vectors, one per token, each of d_model numbers. The decoder then writes the output one token at a time. It is auto-regressive: to write the next token it reads all the tokens it has written so far, as well as the encoder's vectors.

Figure 1 shows both halves. Read each from the bottom: tokens become vectors (Input Embedding), positions are added (Positional Encoding), then a block repeated N times. The encoder's block has two parts, Multi-Head Attention and Feed Forward, each followed by Add & Norm. The decoder's block has three, because it also looks at the encoder's output. At the top, Linear and Softmax turn the decoder's vector into a probability for every word in the vocabulary.

The rest of section 3 explains the boxes one by one: the stacks (3.1), attention (3.2), the feed-forward network (3.3), embeddings and the softmax (3.4) and the positional encoding (3.5).

In the paper's words

“At each step the model is auto-regressive [10], consuming the previously generated symbols as additional input when generating the next.”

Section 3 In plain words: The decoder writes one token at a time and reads everything it has written so far before writing the next.

“The Transformer follows this overall architecture using stacked self-attention and point-wise, fully connected layers for both the encoder and decoder, shown in the left and right halves of Figure 1, respectively.”

Section 3 In plain words: Both halves are stacks of self-attention and per-position feed-forward layers: the encoder on the left of Figure 1, the decoder on the right.

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
\((x_1, \ldots, x_n)\) the input: n tokens of the English sentence byte-pair pieces of English words of English
\(\mathbf{z} = (z_1, \ldots, z_n)\) the encoder's output: one vector per input token n vectors of 512 numbers n vectors of 64 numbers
\((y_1, \ldots, y_m)\) the output the decoder writes, m tokens, one at a time German or French German
\(N\) how many times each half repeats its block 6 2

Try it

Try this Watch a whole translation pass through Figure 1, one sub-layer at a time, with the calculation beside the diagram.

How our miniature differs

Our miniature has the same boxes in the same order, with N = 2 instead of 6 and vectors of 64 numbers instead of 512. For “Anna has a small dog . She likes it .” the encoder produces 11 vectors and the decoder writes the German in 11 steps, one per token including the end mark.

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.

Which half writes

Which half of Figure 1 writes the German, one token at a time?

Auto-regressive

When the decoder writes its next token, what does it read?

Our N

How many layers does each half of our miniature stack (N)?

Your answer
A whole number
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.