1 Introduction
Why the paper wanted to get rid of recurrence: a recurrent network reads one word per step, and those steps cannot run in parallel.
Top: a recurrent network reads the sentence one word at a time, each step folding a word into one hidden state, h, while a counter shows the steps it takes. Bottom: attention. The asking word (“it”, or “She”) reaches every word of the sentence in a single step, and the arrows thicken by how much weight one head of the paper's model really gives each word.
What this section says
In 2017 the best translation models were recurrent networks, in particular two kinds called LSTM and GRU. A recurrent network reads a sentence one word at a time and carries everything it has read in a single vector, its hidden state: step t needs the state of step t − 1. The clip above shows those steps, then attention linking every word at once.
The introduction makes two points. First, the steps of a recurrent network must happen one after another inside each sentence, so a long sentence cannot be split across a GPU's thousands of cores; tricks had made each step cheaper, but the chain of steps remained. Second, attention was already used to connect words however far apart they are, but almost always on top of a recurrent network.
The paper's proposal follows: keep attention, drop the recurrence. Its payoff is speed: the introduction says the Transformer reached a new best translation quality after twelve hours of training on eight GPUs (section 5.2).
In the paper's words
“This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples.”
“In this work we propose the Transformer, a model architecture eschewing recurrence and instead relying entirely on an attention mechanism to draw global dependencies between input and output.”
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 |
|---|---|---|---|
| \(h_t\) | the hidden state after reading position t: everything a recurrent network remembers | computed from h_{t−1} and the word at t | not used: our models have no recurrence |
| \(t\) | a position in the sentence, counted from the start | any position | any position; our training sentences have up to 10 tokens |
| \(\text{LSTM, GRU}\) | two kinds of recurrent network, the state of the art in 2017 | the models the paper replaces | only pictured in the clip |
Try it
How our miniature differs
The recurrent half of the clip is a picture of the steps, not a trained recurrent model: the lab trains only transformers. The attention half uses the real weights of encoder head 2.3 of our model.
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 bottleneck
Why can a recurrent network not process the words of one sentence in parallel?
Counting steps
How many sequential steps does a recurrent network need to read a sentence of 10 tokens?
Attention before the Transformer
According to the introduction, how was attention almost always used before?