On the path Attention paperOne network reads the input and another writes the output: the shape of every translation model, the Transformer included, and why recurrence was its bottleneck.
Input and output of different shapes
Some tasks turn one sequence into another of a different length: a German sentence into an English one, a recording into its words, a long article into a short summary. No position of the output lines up with a fixed position of the input. An encoder-decoder network splits such a task in two. An encoder reads the whole input and turns it into a representation, a set of vectors. A decoder then writes the output from that representation, one element at a time.
The decoder writes autoregressively: each element it writes is fed back in, so the choice of the next one can depend on everything written so far. It starts from a start mark, written <s>, and stops when it writes an end mark. At each step it gives a probability to every possible next element, usually with a softmax over a vocabulary of tokens, and one of them is chosen.
The same split works beyond text. An autoencoder encodes an input into a small code and decodes the input back from it; a U-Net encodes an image into coarse features and decodes them into a map of the same image, one label per pixel. This page follows the sequence case, where the idea was developed for machine translation.
The first neural translators
In 2014 two groups built translators from two recurrent networks. Sutskever, Vinyals and Le at Google used two LSTMs of four layers each; Cho and colleagues in Montreal used the gated cell now called the GRU. The encoder reads the source sentence word by word. Its final state, a single vector usually called c, the context, is handed to the decoder, which starts from it and writes the translation word by word, each step taking the word it wrote before.
On English to French, five such networks together scored 34.8 BLEU, above a phrase-based statistical system on the same test (33.3). But the design has a narrow point: everything the decoder knows about the source sentence must pass through c, a vector of fixed size, whether the sentence has five words or fifty. Cho and colleagues found that their translations got much worse as sentences grew longer. Sutskever’s group found a trick that shows where the trouble lies: feeding the source sentence in reversed, so that its first words, which the decoder needs first, were the last the encoder read and the freshest in c. That alone raised one of their models from 25.9 to 30.6 BLEU.
Attention to the input
In 2014 Bahdanau, Cho and Bengio removed the narrow point. Their encoder keeps all its states, one per source word, and at each step the decoder decides afresh which of them to read. It compares its own current state with every encoder state, turns the comparison scores into weights with a softmax, and takes the weighted sum of the encoder states as its context for this step. Writing “Katze”, it can lean on the state of “cat”; writing “Matte”, on that of “mat”. The weights are learned with the rest of the network, from translations alone; nobody tells the model which words correspond.
With attention the quality no longer collapsed on long sentences, and the weights themselves became readable: drawn as a table of target words against source words, they trace an alignment, mostly along the diagonal, with jumps where the two languages order their words differently. This mechanism, a step reading many vectors with weights it computes itself, is attention. Where the reader and the vectors it reads come from two different sequences, as here, it is called cross-attention.
Training and writing
An encoder-decoder is trained on pairs, an input and its correct output. During training the decoder is not fed its own guesses but the correct previous words, a practice called teacher forcing, and at every step it is scored on how much probability it gave the correct next word (the cross-entropy). So the training signal reaches every position of the output in one pass, even though writing will later go word by word.
When the trained model writes, it has no correct words to lean on. Taking the most probable word at each step (greedy decoding) is fast, but an early choice that looked best can lead into a poor sentence. Beam search keeps the few best partial sentences, the beam, at every step, extends each by every word, and keeps the best few of the results, ranked by their total log-probability; at the end it returns the best complete sentence. Beams of four to ten are common in translation.
On the path Attention paper
Why recurrence was the bottleneck. By 2017 every strong translator was an encoder-decoder with attention, and the attention worked well. What remained slow was the recurrence on both sides. The encoder still read the source one word at a time, so a sentence of n words cost n steps in order, and the decoder did the same with the target during training, although teacher forcing gives it every correct word at once. And within the encoder, what one word says still had to travel through the states in between to reach a distant word.
Two groups had replaced recurrence with convolutions, which compute all positions at once: ByteNet at DeepMind and ConvS2S at Facebook. But a convolution sees only a few neighbours, so linking two distant words takes a stack of layers: the paper notes that the number of operations needed grows with the distance, linearly for ConvS2S and logarithmically for ByteNet. The Transformer keeps the encoder-decoder shape and the cross-attention between them, and replaces the recurrence inside each half with attention of each sequence to itself, self-attention. The paper compares the three kinds of layer for a sequence of n positions with vectors of width d (its Table 1; k is the convolution’s width):
| Layer | Work per layer | Steps in order | Longest path between two positions |
|---|---|---|---|
| Self-attention | n2 · d | 1 | 1 |
| Recurrent | n · d2 | n | n |
| Convolutional | k · n · d2 | 1 | logk n |
(Each entry grows in proportion to the expression given.) Self-attention needs one step whatever the length, and links any two positions directly; its cost grows with n2, which for sentences shorter than the width d is still less than the recurrent layer’s.
Test yourself
Why can the decoder not write the whole output at once, even after the encoder has read the whole input?
Each output word depends on the words before it: which word fits fourth depends on the first three. So at writing time the decoder must choose them in order, feeding each choice back in.
Why did reversing the source sentence help the first LSTM translators?
Everything passed through the encoder’s final state. Reversed, the first source words, which the decoder needs first, were the last the encoder read and so the best kept in that state, and the gap between each source word and the target word it matches became shorter at the start of the sentence.
With attention, what does the decoder read at each step instead of the single vector c?
A weighted sum of all the encoder’s states, one per source word, with weights it computes at that step by comparing its own state with each of them and passing the scores through a softmax.
Training uses teacher forcing; writing uses beam search. Why the difference?
In training the correct previous words are known, so the decoder is fed them and every position is trained at once. When writing, the decoder only has its own earlier choices, so it must search: beam search keeps several promising partial sentences instead of committing to the most probable word at each step.
Where it leads
Kept in this browser only, never sent.
Paths through it
- Attention paperstep 3 of 17Follow this path