Attention Is All You Need, Explained

4 min read

Before 2017, the standard way to translate a sentence in NLP was a recurrent neural network. You read the source sentence word by word, turn each word into a vector, and fold it into a hidden state that is supposed to hold the meaning of the whole sentence. Then a decoder unfolds it, one word at a time, into the target language. Kilcher's walkthrough starts by showing why that design struggles, using a short example: "the cat eats the mouse", translated into German.

When the decoder is about to produce the German word for "eats", the only information it has is the last word it produced and the final hidden state. But "eats" entered the encoder several steps ago and had to travel through every hidden state since, mixed in with every other word, the word order, and the grammar, a long path for one hidden state to remember. This is the long-range dependency problem, and it is why RNN translation degrades on long sentences.

Attention is the fix. Let the decoder look back at the encoder's hidden states directly, at the exact step where the word it needs was encoded. Instead of everything flowing through one final vector, the decoder picks which parts of the input to pay attention to, right when it needs them. The path from "eats" to its translation becomes one step instead of many.

Then comes the bolder question. If attention works this well as an add-on, do we need the recurrence at all? The answer is the Transformer: no recurrence, just attention, over everything, all at once. The whole source sentence and the target produced so far go in together, and out comes the probability for the next word. Every step of producing a sentence is one training sample, with backpropagation running through that step only. Sequence-to-sequence used to be one sample per sentence. Now it is one sample per token, and everything happens in parallel.

The machinery has three kinds of attention. The encoder attends over the input sentence. The decoder attends over what it has produced so far. The third, the interesting one, joins the two: it combines the source sentence with the target produced so far. All three use the same addressing mechanism, built from keys, values, and queries. The encoder builds key-value pairs from the source sentence, the decoder builds a query from the target so far, and the dot product of the query with each key, run through a softmax and multiplied by the values, selects the key that lines up best. Kilcher's analogy is a table: values are attributes like a person's name, height, and weight, keys are the labels, and the query is what you want to know. Multi-head attention just means running many of these lookups at once.

Positional encoding is easy to miss. Without recurrence the network has no sense of order, so the paper adds a position signal built from sine waves at different frequencies, a continuous version of binary counting. Compare two words across all the waves and you can tell which came first and how far apart they are. It is not the core, but it gives the network a real boost.

Shorter paths are the whole story, the paper claims. Every step information flows through is a chance to lose it, so attention wins by cutting the path length down. The experiments back it up, and the code is public in TensorFlow's tensor2tensor repo. The Transformer went on to become the base of nearly all models in this blog, which is why this paper is worth reading in full.

Try this

bash
git clone https://github.com/tensorflow/tensor2tensor
curl -s https://arxiv.org/abs/1706.03762
pip install transformers

Sources