The bottleneck that attention removed
In 2014 Sutskever, Vinyals and Le, and Cho et al., translated sentences with two recurrent networks: an encoder read the source sentence into one fixed-length vector, and a decoder generated the translation from that vector, word by word. Every sentence, short or long, had to fit through it, and Bahdanau, Cho and Bengio pointed out that such encoder–decoders did worse as sentences grew longer. Their fix lets the decoder look back. At every output step it scores every source position, takes a softmax-weighted average of their states and reads that context vector along with its own state. This soft lookup is attention, the mechanism the transformer kept.
The model on this page follows Bahdanau et al. at a small scale: a bidirectional GRU encoder, additive attention and a GRU decoder, 5,724,100 parameters, trained here to translate English into French on 33,485 Tatoeba sentence pairs, short sentences that begin I am, he is, you are and so on.
The encoder
The encoder embeds each English word and runs two GRUs over the sentence, one forward and one backward. A GRU is a gated recurrent cell with no separate memory: a reset gate decides how much of the old state the candidate may read, and an update gate how much of the old state to keep (biases left out):
The annotation of word stacks the forward state after reading up to on the backward state after reading from the end back to , , 128 numbers each, so it describes the word in the context of the whole sentence. The annotations are computed once; the decoder reads them at every step.
Additive attention
At output step the previous decoder state is compared with every annotation by a small network, a softmax turns the scores into weights, and the weighted average of the annotations is the context:
does not depend on , so it is computed once per sentence; the page calls it the key and the annotation it returns. The Step panel spells out the scores, the weights and the context for the selected output word. Everything is differentiable, so the attention is learned from the translation loss alone.
The Alignment panel stacks the weights of every output word into a matrix, one row per French word and one column per English word. Nobody gave the model an alignment. Bahdanau et al. found such matrices close to word alignments, including reordered phrases such as European Economic Area → zone économique européenne. They need not be one-to-one, though: every annotation carries the rest of the sentence, so a word can be translated while its row rests on a neighbour.
Decoding
The decoder GRU reads the previous French word and the context to update its state, , and a linear layer on with a softmax gives the next word over a 5,700-word French vocabulary. The page decodes greedily, taking the most likely word each time until the end-of-sentence token. He's almost as tall as me. becomes il est presque aussi grand que moi ., which matches a Tatoeba translation; I'm working on his car. becomes je travaille sur le wagon de voiture ., which does not.
Training
Training uses teacher forcing: the decoder is fed the reference translation's previous word rather than its own guess, and the loss is the cross-entropy of every reference word. The run used Adam at learning rate 0.001, batches of 64 and 12 epochs on the CPU (6 minutes). The held-out loss was lowest after epoch 6 (1.115) and rose to 1.237 by epoch 12 while the training loss kept falling, the usual sign of overfitting a small data set. Greedy output equals a Tatoeba translation for 25 % of the 1,219 held-out sentences. That is a strict test: a correct translation worded differently does not count.
From this attention to the transformer's
The table at the end of the page lines this model up against the transformer. Luong, Pham and Manning (2015) replaced the small scoring network with a dot product between decoder and encoder states. Vaswani et al. (2017) scaled it, , projected separate queries, keys and values, ran several heads in parallel and, above all, dropped the recurrence. In self-attention every position of a sentence attends to every other at once, and word order enters through position encodings instead of the GRUs. The softmax-weighted read of this page survived unchanged; the Attention Explainer's multi-head attention tab continues from here.
Papers
- Neural Machine Translation by Jointly Learning to Align and Translate Additive attention over a bidirectional encoder's annotations, the model this tab trains.
- Sequence to Sequence Learning with Neural Networks An LSTM encoder–decoder that translates through one fixed-length vector.
- Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation The RNN encoder–decoder and the gated recurrent unit (GRU) both networks here are built from.
- Bidirectional recurrent neural networks Running one recurrent network forward and one backward, as the encoder does.
- A Learning Algorithm for Continually Running Fully Recurrent Neural Networks Real-time recurrent learning, with teacher forcing: feeding the target rather than the network's own output.
- Effective Approaches to Attention-based Neural Machine Translation Dot-product and other scoring functions, and global against local attention.
- Attention Is All You Need The transformer: scaled dot-product self-attention with several heads and no recurrence.