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.
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.
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.”
“Another way of describing this is as two convolutions with kernel size 1.”
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\) | 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
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.
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.
The paper's network
With d_model = 512 and d_ff = 2048, how many weights does one feed-forward network have, biases included?
The ReLU
What does the ReLU, max(0, z), give for z = −1.5?
Position-wise
Which statement about the feed-forward network's weights is right?