Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
The attention mechanism explained: queries, keys and values, self-attention, multi-head attention, masking, and why it replaced recurrent networks.
Quick answer: Attention is a mechanism that lets a neural network decide, for each item it is processing, which other items are most relevant, and draw information from them in proportion. In text, each word computes a relevance score against every other word, turns the scores into weights that add up to 1, and builds a new representation of itself as a weighted mix of the others. It was a breakthrough because it gave models a direct connection between any two positions, however far apart, and because all of those connections can be computed in parallel.
Early neural translation systems had two parts. An encoder read the source sentence and squeezed it into one fixed-size vector. A decoder then wrote the translation from that vector alone.
That single vector was a bottleneck. A three-word sentence and a fifty-word sentence had to fit into the same space, and translation quality fell sharply as sentences got longer. The model was being asked to memorise the whole sentence before writing a word.
In 2014, Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio proposed a fix in Neural Machine Translation by Jointly Learning to Align and Translate. Instead of relying on one summary vector, let the decoder look back at all of the encoder's states each time it produces a word, and learn which ones to focus on. When writing the French word for "cat", the model attends mostly to "cat" in the English sentence.
A human translator works the same way: they glance back at the relevant part of the source, not recite it from memory.
In 2017, Attention Is All You Need took the idea further. Instead of attention between two sequences, apply it within one sequence: every token attends to every other token of the same text. This is self-attention, and the paper showed that a model built from it alone, with no recurrence, outperformed everything before it. That model was the transformer; see transformers explained.
Why does a word need to look at other words? Because meaning depends on context.
Self-attention lets each word's representation be reshaped by the words around it.
Each token starts as a vector (its embedding; see how embeddings work). From that vector, three new vectors are produced using learned weight matrices:
| Vector | Role | Analogy |
|---|---|---|
| Query | What this token is looking for | A search request |
| Key | What this token offers | A label on a folder |
| Value | The information this token passes on | The folder's contents |
Then, for each token:
The result is the token's new representation: its own content enriched with whatever it attended to.
In one line:
Attention(Q, K, V) = softmax(Q × Kᵀ / √d) × V
For "it" in the trophy sentence, the weights might come out something like:
| Token | Weight |
|---|---|
| trophy | 0.61 |
| suitcase | 0.22 |
| big | 0.09 |
| all others | 0.08 |
Nothing tells the model that pronouns refer to nouns. The query and key matrices are learned during training, and patterns like this emerge because they help predict text.
If this "compare a query against keys by similarity" sounds like search, that is no accident. It is a soft, differentiable lookup, and the same similarity idea drives vector databases.
One set of weights can capture only one kind of relationship at a time. So transformers run several attention calculations in parallel, each with its own query, key and value matrices. Each is a head.
Different heads learn different things. Researchers have found heads that track which noun a pronoun refers to, heads that link verbs to their subjects, and heads that attend to the previous or next word. Their outputs are joined together and mixed.
When a model is trained to predict the next token, it must not see the answer. A causal mask blocks attention from each position to all later positions by setting those scores to negative infinity before the softmax, which gives them a weight of zero.
Models that only need to understand text, such as BERT, leave the mask off and let every token see the whole input in both directions.
| Recurrent networks | Attention | |
|---|---|---|
| Distance between two words | Many steps; information fades | One step, at any distance |
| Processing | One word at a time | All positions at once |
| Training speed on modern hardware | Slow | Fast |
| Long documents | Poor | Good, within the context limit |
Every token attends to every other token. For a sequence of n tokens that is n × n scores.
| Sequence length | Attention scores per head per layer |
|---|---|
| 1,000 | 1,000,000 |
| 10,000 | 100,000,000 |
| 100,000 | 10,000,000,000 |
This quadratic growth is the main limit on how much text a model can consider at once, and a major reason why long prompts are slower and more expensive. Research into more efficient attention, and engineering tricks such as caching, aim to soften it.
Attention applied within a single sequence, so each token computes how relevant every other token in the same text is and updates its representation accordingly.
Three vectors derived from each token. A token's query is matched against all keys to get relevance weights, which are then used to blend the corresponding values.
Several attention calculations run in parallel with separate learned weights, so the model can track several types of relationship at once.
No. Attention is the mechanism. A transformer is a full architecture that stacks attention layers with feed-forward layers, normalisation and residual connections.
Attention answers one question for every token: what else here should I be paying attention to? Turning that into a learnable, parallel calculation removed the memory bottleneck of earlier models and connected every part of the input to every other. Jay Alammar's Illustrated Transformer is the best visual companion if you want to see it step by step.