Attention Is All You Need · Section 3.3

3.3 Position-wise feed-forward networks

The second sub-layer of every layer: a small two-layer network that each word passes through on its own. It holds most of a layer's weights.

Widen, cut, narrow
Sentence
Playback speed

One word's vector after the attention of the encoder's first layer: 64 numbers drawn as bars, up for positive and down for negative. W₁ widens it to 256 numbers; the ReLU sets every negative one to 0, and the clip counts them; W₂ narrows the rest back to 64. Last, the same two matrices on every word of the sentence, with how many of each word's 256 numbers the ReLU set to 0.

What this section says

After attention has mixed information between the words, every layer passes each word's vector through a small feed-forward network, on its own: no word looks at any other here. The network is two linear transformations with a ReLU between them. The first widens the vector from d_model to d_ff numbers, the ReLU turns every negative number into 0, and the second narrows it back to d_model.

\mathrm{FFN}(x) = \max(0,\, xW_1 + b_1)\,W_2 + b_2 \qquad (2)

Position-wise means the same two matrices are used for every position of the sentence, as the clip shows with every word in turn; each layer has its own pair. The paper adds that this is the same as two convolutions with a window of one word: a convolution slides the same weights along the sentence, and with a window of one it sees one word at a time.

The sizes: d_model = 512 in and out, d_ff = 2048 inside, four times wider. With biases that is about 2.1 million weights per network, about twice the four matrices of the attention sub-layer before it.

In the paper's words

“In addition to attention sub-layers, each of the layers in our encoder and decoder contains a fully connected feed-forward network, which is applied to each position separately and identically.”

Section 3.3 In plain words: Every layer also has a small feed-forward network, applied to each word on its own, with the same weights for every word.

“Another way of describing this is as two convolutions with kernel size 1.”

Section 3.3 In plain words: The same network can be described as two convolutions whose window is one word wide.

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\) one word's vector, coming from the attention sub-layer 512 numbers 64 numbers
\(W_1,\ b_1\) the first linear layer: widens the vector 512 × 2048, and 2048 biases 64 × 256, and 256 biases
\(\max(0,\cdot)\) the ReLU: keeps positive numbers, turns negative ones into 0 ReLU ReLU
\(W_2,\ b_2\) the second linear layer: narrows the vector back 2048 × 512, and 512 biases 256 × 64, and 64 biases
\(d_{ff}\) numbers inside the network, between the two layers 2048 256

Try it

Try this A linear layer is a matrix product: multiply a row of numbers by a small matrix, cell by cell, in the matrix calculator.

How our miniature differs

Our networks have d_ff = 256, four times our d_model of 64, as in the paper: 33,088 weights each, in each of our 4 layers.

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 paper's network

With d_model = 512 and d_ff = 2048, how many weights does one feed-forward network have, biases included?

Your answer
A whole number

The ReLU

What does the ReLU, max(0, z), give for z = −1.5?

Your answer
A whole number

Position-wise

Which statement about the feed-forward network's weights is right?

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.