Attention Is All You Need · Section 2

2 Background

Others had tried to make translation parallel with convolutions. Section 2 explains why that still left distant words far apart, and what self-attention is.

From the first word to the last

ConvS2S: 5 layers from the first word to the last

ByteNet: 3 layers from the first word to the last

Transformer: 1 layer from the first word to the last

The same sentence drawn three times, its words as dots in a row and each layer as a row above. In ConvS2S each layer lets a word reach k − 1 more words, so the path from the first word to the last climbs many layers. In ByteNet the reach multiplies by k each layer, so the path is shorter. In the Transformer one self-attention layer links the first word straight to the last. Change the number of words and watch the counts.

What this section says

Section 2 places the paper among its rivals. Three models had already replaced recurrence with convolutions, which compute every position at the same time: the Extended Neural GPU, ByteNet and ConvS2S. A convolution reads a small window of neighbouring words, k of them. To connect two words further apart, the signal must climb through several layers.

How many layers depends on the design, as the figure shows. ConvS2S stacks plain convolutions: each layer widens the window by k − 1 words, so the number of layers grows in proportion to the distance. ByteNet spaces its windows further apart at every layer (dilated convolutions), so the number grows with the logarithm of the distance. In the Transformer any two words are one step apart. The paper's argument (section 4): the shorter the path, the easier it is to learn a link such as a pronoun and its noun.

The section admits a cost: mixing all words with attention weights blurs detail, which multi-head attention counters (section 3.2.2). It then defines self-attention and claims the Transformer is the first translation model built on self-attention alone.

In the paper's words

“In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions, linearly for ConvS2S and logarithmically for ByteNet.”

Section 2 In plain words: In the convolutional models, connecting two words takes more steps the further apart they are: in proportion to the distance for ConvS2S, with its logarithm for ByteNet.

“Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence.”

Section 2 In plain words: Self-attention lets the words of one sentence look at each other to build a new representation of that same sentence.

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\) the number of words (tokens) in the sentence any length up to 10 tokens in training
\(k\) a convolution's window: how many neighbouring words one layer reads small; 3 in the figure not used: our model has no convolutions
\(O(n),\ O(\log_k n),\ O(1)\) how the path grows with n: in proportion, with the logarithm, not at all ConvS2S, ByteNet, Transformer the Transformer: always 1

Try it

Try this Section 4's Table 1 counts the same paths and the cost of each layer type: set n, d and k yourself.

How our miniature differs

The figure counts layers for an idealised sentence of 10 words and k = 3: ConvS2S needs 5 layers, ByteNet 3, the Transformer 1. The real ConvS2S and ByteNet have more details (gates, residual connections, several layers per block), which do not change how the path grows.

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.

Plain convolutions

With a window of k = 3, how many layers of plain convolutions does the first of 10 words need to reach the last?

Your answer
A whole number

Dilated convolutions

With k = 3, the reach of dilated convolutions triples with every layer: 3, 9, 27 words. How many layers until 27 words are all within reach?

Your answer
A whole number

How the path grows

How does the number of steps between two distant words grow in ConvS2S and in ByteNet?

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.