Attention Is All You Need · Section 3.1

3.1 Encoder and decoder stacks

What is inside each half of Figure 1: identical layers stacked N times, every sub-layer wrapped in a residual connection and layer normalisation.

The stacks, built up in layers
Sentence
The paper's model, built up in layers Anna has a small dog . She likes it . </s> Multi-Head Attention Add & Norm Feed Forward Add & Norm 1One layer

Encoder layer 1 of 2: “it” looks most at “it” (24.4 %, averaged over the four heads), and the layer changes its vector by 77.8 %.

The output of this layer for “it”, 49,728 weights in the layer
Token12345678… 56 more
it0.22−0.390.93−0.07−0.41−1.400.020.73…

First one encoder layer, then the encoder's N layers, then the decoder's, each layer a button. Your sentence lights the layer you pick: for each word, the word it looks at most, how much the layer changed its vector, and the first numbers of the vector it passes up.

What this section says

The encoder is N identical layers, 6 in the paper. Each has two sub-layers: multi-head self-attention, then a small feed-forward network applied to every position. Identical means the same design, not the same numbers: each layer learns its own weights.

Around each sub-layer sits a residual connection followed by layer normalisation: the sub-layer's input x is added back to its output, and the sum is rescaled. The paper writes it as one formula, LayerNorm(x + Sublayer(x)). To make the addition possible, every sub-layer and the embeddings produce vectors of the same size, d_model.

The decoder is also N identical layers, with a third sub-layer in the middle: attention over the encoder's output (section 3.2.3). Its self-attention is masked so that a position cannot look at later positions. Together with feeding the decoder its own output shifted one place to the right, this means the prediction for position i depends only on the tokens before i.

In the paper's words

“The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network.”

Section 3.1 In plain words: Each encoder layer has two parts: multi-head self-attention, then a small feed-forward network applied to every position.

“We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.”

Section 3.1 In plain words: In the decoder, self-attention is changed so that no position can look at the positions after it.

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\) identical layers in each stack 6 2
\(d_{\text{model}}\) numbers in every vector between sub-layers, and in the embeddings 512 64
\(x\) a sub-layer's input: one vector per token n × 512 n × 64
\(\mathrm{LayerNorm}(x + \mathrm{Sublayer}(x))\) the output of every sub-layer: the residual sum, normalised after every sub-layer (post-normalisation) the same

Try it

Add, then normalise
Sentence
Playback speed

One word's vector x in the encoder's first layer, drawn as 64 bars, and the attention sub-layer's output for it. The two take separate paths, x around the sub-layer like a bypass, and are added. Layer normalisation then subtracts the sum's mean, divides by its spread, and applies the learned γ and β: the result is exactly the model's output. Last, dropout, as in training only: about one value in ten of the sub-layer's output is switched off before the add.

How our miniature differs

Our stacks have N = 2 layers of vectors of 64 numbers. The layers hold 231,936 of our model's 244,800 weights; the rest is the one shared embedding matrix (section 3.4). Today's GPT-style models usually normalise before each sub-layer instead of after it; the paper and our miniature normalise after.

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.

Counting sub-layers

How many sub-layers does one decoder layer have?

Your answer
A whole number

The decoder's extra sub-layer

What does the decoder's third sub-layer do?

All the paper's sub-layers

In the paper's base model (N = 6), how many sub-layers are there in the encoder and the decoder together?

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.