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.
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.”
“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.”
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
| 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
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.
Prepared in the Course by
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)?