Part D · Chapter 17 · The paper's model

17. The encoder

The encoder reads the whole English sentence at once and, layer by layer, turns each word into a vector that carries what the word means in this sentence. Its output is all the decoder will know about the English.

The English, up the encoder
Sentence
Playback speed

The encoder half of the paper's Figure 1, redrawn, with the chosen sentence at the bottom. The tokens are embedded and given their positions; then each of our N = 2 layers lights up in turn: its self-attention, drawn as the four heads' heat maps (a row for each word asking, a column for each word looked at, darker for more weight), then Add & Norm and the feed-forward network. Last, the encoder's output: one vector per token, of which the first eight numbers are shown.

The idea

The encoder is the left half of Figure 1: the stack of chapter 16 with the English sentence going in. Its job is to read. Every word starts as its embedding plus its position, a vector that knows the word but not the sentence. Each layer lets every word look at every other word (that is self-attention: the queries, keys and values all come from the same sentence) and then think about what it gathered.

Nothing is hidden from the encoder: the whole English sentence is there from the start, so a word may look forwards as well as backwards. No mask, unlike the decoder of chapter 10. After N layers each word's vector has been shaped by the words around it: “it” has picked up something of the noun it stands for.

What comes out is one vector per English token. The encoder does not translate anything; it hands these vectors to the decoder, which will consult them for every German word it writes.

A worked example: “it” finds “dog”

“Anna has a small dog . She likes it .” enters as 11 tokens, the full stop and the closing </s> included. In the first layer, “it” looks most at “it” (24 % of its attention, averaged over the four heads): mostly at itself and its neighbours.

In the second layer, it looks most at “dog” (32 %). One head of that layer does most of the work: the head gallery names it the head that links a pronoun to its noun. The encoder's output for “it” begins [0.54, −0.24, 0.78, −0.31, …]; the decoder will read all 11 vectors.

The formula

x^{(0)} = \sqrt{d_{\text{model}}}\,E_{\text{English}} + PE, \qquad x^{(l)} = \operatorname{EncoderLayer}_l\big(x^{(l-1)}\big), \qquad \text{memory} = x^{(N)}
\operatorname{SelfAttention}(x) = \operatorname{MultiHead}(Q{=}x,\ K{=}x,\ V{=}x)
\(E_{\text{English}}\)
the English tokens' rows of the shared embedding matrix (chapter 15)
\(PE\)
the sine and cosine positions (chapter 12)
\(\operatorname{EncoderLayer}_l\)
self-attention and the feed-forward network, each with Add & Norm (chapters 11, 13 and 14)
\(Q{=}x,\ K{=}x,\ V{=}x\)
self-attention: the queries, keys and values all come from the same words
\(\text{memory}\)
the encoder's output, one vector per English token, which every decoder layer reads

Why it matters

Reading a whole input at once, with every word able to look at every other, is what the encoder showed was possible, and it is how today's models read your prompt: all its tokens in parallel, in one pass, before the first word of the answer. That pass is why a long prompt is processed much faster per token than the answer is written.

Encoder models such as BERT keep only this half of the design, for tasks that read text without writing it; GPT-style models keep only a decoder (chapter 22). The encoder–decoder pair of the paper suits tasks with a clear input and output, such as translation.

Where it fails

  • Every word looks at every word. The cost of self-attention grows with the square of the sentence's length (chapter 21); a ten-word sentence is cheap, a whole book is not.
  • The output is all the decoder gets. If the encoder's vectors lose something, the decoder cannot recover it from the English.
  • Heat maps are a partial view. They show where each head looked, not what it took away; the vectors after the feed-forward network carry that.
Remember The encoder reads the whole sentence at once: embeddings plus positions, then N layers of unmasked self-attention and feed-forward. Its output, one vector per English token, is what every decoder layer reads.

Exercises

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

Where “it” looks in layer 2

In the clip's first sentence, which word does “it” look at most in the encoder's second layer, averaged over the heads?

How many vectors?

How many vectors does the encoder hand to the decoder for “Anna has a small dog . She likes it .”?

Your answer
A whole number

The paper's heat maps

The paper's encoder has N = 6 layers of h = 8 heads. How many self-attention heat maps would its encoder have for one sentence?

Your answer
A whole number

Is the encoder masked?

Does the encoder's self-attention hide later words from earlier ones?

In the paper

Section 3.1 describes the encoder as a stack of N = 6 identical layers, each with a multi-head self-attention and a feed-forward network. Section 3.2.3 says what its self-attention sees: “In a self-attention layer all of the keys, values and queries come from the same place, in this case, the output of the previous layer in the encoder. Each position in the encoder can attend to all positions in the previous layer of the encoder.”

In the paper: section 3.1, 3.2.3

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.