4 Why self-attention
The paper's argument for its design, in one table: what a self-attention layer costs against a recurrent or a convolutional one, on three counts.
The paper's Table 1 with your own numbers: for self-attention, recurrent, convolutional and restricted self-attention layers, the work per layer, the sequential operations and the maximum path length, as formulas and as values for the n, d, k and r you set. A chart shows the work per layer as the sentence grows and where self-attention overtakes recurrence.
What this section says
Section 4 compares self-attention with the layers it replaces on three counts, which the paper calls desiderata. Work per layer: self-attention compares every pair of the n positions, about n² · d operations; a recurrent layer multiplies a state of d numbers by a d × d matrix at each position, about n · d². Sequential operations: steps that must wait for each other, n for recurrence, a constant for the others. Maximum path length: how many steps a signal needs between the two furthest positions, the count of section 2.
Self-attention wins the last two outright. On the first it wins while n is smaller than d, which the paper notes is usually so for sentences in translation, with d = 512 and word pieces. For very long inputs it suggests restricting attention to a neighbourhood of r positions, which makes the work r · n · d and the path n / r (the last row of Table 1).
Convolutions, the third row, cost k times more than recurrence per layer and need a stack of layers to connect distant positions. The section closes with a side benefit: attention weights can be looked at, and many heads seem to follow the structure of the sentence (the appendix).
In the paper's words
“As noted in Table 1, a self-attention layer connects all positions with a constant number of sequentially executed operations, whereas a recurrent layer requires \(O(n)\) sequential operations.”
“As side benefit, self-attention could yield more interpretable models.”
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 |
|---|---|---|---|
| \(n\) | the sequence length | a sentence of word pieces | up to 10 tokens in training |
| \(d\) | the representation size, d_model | 512 | 64 |
| \(k\) | a convolution's kernel size: how many neighbours it reads | any | not used |
| \(r\) | the neighbourhood of restricted self-attention | a suggestion for future work | not used |
| \(O(\cdot)\) | how a cost grows with the sizes, constants left out | Table 1 | the same |
Try it
The chosen sentence through three kinds of layer. A recurrent layer reads one word per step, passing a state along, and the counter adds a step for each word; the first word reaches the last only through every state in between. Convolutions with a window of three words connect neighbours, and each further layer widens what a word can see by two, until the first and the last word meet. Self-attention connects every word to every other in one step. Last, the paper's Table 1 filled in for this sentence's n and the d chosen above, and whether self-attention costs more than recurrence.
How our miniature differs
With our d = 64, self-attention costs less work per layer than recurrence up to sentences of 64 tokens, and more from 65 on; the paper's d = 512 moves that point to 513. Our sentences have at most 10 tokens, far below either.
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 three counts
Which three counts does section 4 compare the layer types on?
Where self-attention costs more
With d = 512, from which sequence length n does a self-attention layer do more work than a recurrent one?
Restricted self-attention
With n = 1000 and a neighbourhood of r = 100, what is the maximum path length n / r of restricted self-attention?