What it is used for
Attention is used so a model reads its input with context instead of word by word blindly. The same word can mean different things depending on the words around it, and attention is how the model weighs which surrounding words most determine the meaning of the word being processed. Because every position weighs every other position, the model does not have to wait for the sequence to be read one step at a time; the computation can be parallelized, and the source paper records that this approach required significantly less time to train than the best earlier models.
Inside the source paper, the proven use is machine translation: turning English sentences into German and into French on the WMT 2014 benchmark, plus one task of breaking English sentences into their grammatical parts. After that paper, the architecture resting on attention became the foundation of large language models, the models trained on very large amounts of text to write and answer questions, so this mechanism now runs behind almost every AI assistant people use every day.
How to use it
The way attention works can be read as four steps. First, every word is turned into three vectors, lists of numbers, matching its three roles: query, key, and value. Second, one word's query is matched against the keys of all words by multiplying them; the larger the product, the better the match. Third, all those match scores are scaled and turned into weights that add up to one, so the best matching words receive the largest share. Fourth, the values of all words are mixed according to those weights, and that mixture is the output for the word being processed.
A single round of attention weighs from only one angle. The source paper therefore runs it multi-head: eight parallel rounds, each weighing with a different numerical projection, whose results are then combined. With many heads, the model can attend jointly to different things, for example one head weighing the relation between an actor and an action while another head weighs the relation between a pronoun and the word it stands for.
The mechanism is used in three ways inside the Transformer. In the encoder, the part that reads the input, all three roles come from the same sequence; this form is called self-attention, because every word weighs the words of its own sentence. In the decoder, the part that writes the output, there is attention whose queries come from the output being written while its keys and values come from the input, so every output word weighs the whole input sentence. The decoder also uses masked self-attention: the word being written may only weigh earlier output words, never peek at later ones.
One thing attention itself does not provide is order. The mechanism weighs all words at once without knowing which came first. Because the source architecture uses neither recurrence nor convolutions, the paper adds a position marker to every word at the start of processing, a sine wave pattern that differs for every position, so the model still knows the shape of its sentence.
Practitioner level
For practitioners, the exact formulation is called scaled dot-product attention. The weights are computed by multiplying queries and keys, dividing by the square root of the key dimension, written as the square root of dk, and applying softmax, the function that turns a set of numbers into weights that add up to one. The division is not decoration: without it, at large dimensions the products grow too large and softmax enters regions where its change is almost zero, which hampers learning.
The standard figures from the source paper: its base model uses a model dimension of 512, eight parallel heads, and each head works at dimension 64 for keys and values. Its measured evidence: a BLEU score of 28.4 on English to German translation, improving over the previous best results including ensembles by more than 2 points, and 41.8 on English to French as a new single model record. BLEU is an automatic score that compares machine translation against reference human translation. The model was trained for 3.5 days on eight GPUs, the graphics processing chips commonly used to speed up AI computation.
Expert level
At the expert level, attention is read as a function mapping a query and a set of key and value pairs to an output, with the output a weighted sum of the values and the weights computed by a compatibility function of the query with the corresponding key. The source paper chooses dot-product attention over additive attention, in which compatibility is computed by a small feed-forward network, for speed and space efficiency through optimized matrix multiplication, while noting that additive attention can outperform unscaled dot-product attention at large dimensions.
The three applications in section 3.2.3 of the source paper remain the taxonomy in use today: encoder-decoder attention, encoder self-attention, and masked decoder self-attention, with the mask implemented inside the scaled attention by setting the values of illegal connections to negative infinity before the softmax, preserving the autoregressive property. The sinusoidal positional encoding of section 3.5 was chosen after a learned version produced nearly identical results, and the authors hypothesized that the sinusoidal form would let the model attend to relative positions more easily.
Limits and risks
Attention weights are not an explanation. A weight shows how much one word influenced the computation for another word inside the model, not a human-readable reason for why the model answered as it did. Reading a weight map as proof of the reason behind an answer is a conclusion the source paper does not provide.
The scope of this article is also limited and stated as it is. The formulation covered here is the one written in the source paper in 2017, and this article rests on that single primary paper alone. Models today use many derivatives and adjustments of this formulation, and those differences lie outside this article's scope. The results in the source paper are also bound to the tasks and scale of 2017: two translation tasks and one parsing task, not evidence for every language task.
