Lifestyle
What Is Attention? How Models Decide Which Tokens to Refer To
Attention compares queries with keys to compute weights for each position in a sequence, then takes a weighted average of values to form a new representation. From 2014 translation alignment to the 2017 Transformer, examples explain Q, K, V, self-attention, cross-attention and multi-head attention. It also examines standard attention's quadratic cost growth with length and why attention weights cannot directly explain a model's reasons.
About 9 min read

Contents
Attention is a computation: when processing a position in a sequence, it scores the relevance of every position available to it, including itself in self-attention, converts the scores into weights summing to 1, then takes a weighted average of those positions' content to form a new representation. “Attention” is a name for mathematical weighting, not conscious human focus. It lets two distant words be compared directly within a single layer, at the cost of standard attention's computation growing roughly with the square of sequence length.
This article moves from translation alignment to Q, K, V, self-attention and multi-head attention. It also explains why weights cannot directly be read as “the model's reasons” and how length relates to cost. Information is current as of 2026-10; material labeled “example” was devised for teaching and has not been empirically tested.
From translation alignment to attention alone
In 2014, Bahdanau et al. worked on machine translation. The encoder–decoder architectures common at the time first compressed the entire source sentence into a fixed-length vector, then generated the translation. Suspecting this was a bottleneck, they instead had the decoder, whenever it generated a target word, score the representations at each source position, convert the scores into weights, and take a weighted average to produce a context vector specific to that step. For scoring, they trained, together with the translation model, a small feedforward neural networkWhat Is a Neural Network? A Function Built from Layers and Weights That Learns from DataA neural network is a function of layered simple units: each computes a weighted sum, usually followed by a nonlinear transformation, with weights adjusted from data. A hand-calculated two-switch hallway light example explains forward passes, loss, backpropagation and gradient descent, distinguishes training from inference and parameter count from neuron count and capability, and shows how neural networks relate to deep learning and Transformer.Read the full article. In the paper's English–French translation experiments, the baseline architecture's performance declined markedly as sentences grew longer. The alignment-based version, trained on sentences up to 50 words long, showed no decline even on sentences longer than 50 words. This result applies to the paper's experimental setting.
In 2017, Vaswani et al. noted that attention was usually combined with recurrent networks at the time. They proposed an approach relying entirely on attention, without recurrence or convolution: the Transformer architectureWhat Is the Transformer Architecture (Transformer)?Transformer is a neural network architecture using attention for sequence relations, a basis for many language and multimodal models. Event notices' pronouns and word order show how self-attention, position data, feed-forward networks and masks cooperate, contrasting the original encoder–decoder with later variants. Learn how models use context, why attention weights are neither human attention nor full decision explanations, and why long sequences still incur compute and verification costs.Read the full article. The paper defines attention as mapping a query and a set of key-value pairs to an output. The output is a weighted sum of the values, with weights computed by a compatibility function between the query and each key, measuring how well they match.
Q, K, V: compare, weight, mix
Each position, usually a token in a language model, is transformed through three sets of learned matrices into a query, key and value vector. Roughly speaking, the query is “what I am looking for,” the key is “how I can be matched,” and the value is “the content I supply when selected.” All three consist of learned numerical values; nobody labels words as subjects or pronouns in advance.
- Take the dot product of the query with each position's key to obtain scores.
- Divide the scores by the square root of the key dimension. The original paper hypothesized that this prevents large dot products at high dimensions from making softmax gradients too small.
- Apply softmax to obtain nonnegative weights that sum to 1.
- Take a weighted average of the values at each position to obtain a new representation.
Example (for teaching; tokenization is illustrative and no real model was measured): “Xiaoya put the suitcase in the trunk because it was too heavy.” When processing “it,” suppose the weights are suitcase 0.55, trunk 0.25, Xiaoya 0.10 and all other words combined 0.10, summing to 1. The new representation of “it” mixes the values, with the suitcase contributing the most.
If the weights for suitcase and trunk were swapped, the new representation would lean toward the trunk, and later layers might associate “too heavy” with the wrong object. A real model's weights must be extracted to know what they are, and they are only mixing proportions, not the final answer.
Read the full description
Using “it” as the query, compare it with the keys for Xiaoya, suitcase, trunk and other words, then apply softmax to obtain weights. The example weights are 0.10, 0.55, 0.25 and 0.10 respectively, summing to 1. Mix the values at each position according to these weights to obtain a new representation of “it,” with the suitcase's content contributing the most. The weights are a teaching example, not measurements.
Self-attention and cross-attention
The difference lies in where the queries, keys and values come from; the computation is the same. In self-attention, all three come from the same sequence. In cross-attention, called encoder-decoder attention in the original paper, queries come from the decoder, while keys and values come from the encoder's output. Bahdanau et al.'s mechanism also has the decoder consult the source text, but scores it using a small network rather than dot products. The decoder's self-attention additionally uses a mask so that each position can see only itself and preceding positions.
What multi-head attention does
Multi-head attention uses separately learned projections to transform queries, keys and values into h sets of smaller vectors. Each set performs attention in parallel, after which the results are concatenated and projected back to the original dimension. The original paper's base configuration used 8 heads, 64 dimensions per head and a model dimension of 512, with total computation similar to a single full-dimensional head. The authors' rationale was that multiple heads let the model access information from different representation subspaces and different positions simultaneously; with only one head, weighted averaging inhibits this.
This does not mean each head has a fixed semantic responsibility. The paper's English–German translation ablation experiments, holding total computation fixed, showed that a single head performed worse than the best configuration, while too many heads also reduced quality. An appendix figure plots the weights of two heads in encoder layer 5, out of 6 layers, processing the English pronoun “its.” The authors said these heads “appear” to relate to pronoun reference. However, this was an observation about a particular model, layer and sentence, with no guarantee that it would recur in another model.
Attention weights are not the model's reasons
Weights are often plotted as heatmaps, seemingly saying, “The model answered this way because of these words.” Jain and Wallace (NAACL 2019) tested this intuition using bidirectional LSTMs with attention on English text classification, question answering and natural language inference. They found that attention weights from recurrent encoders had weak and inconsistent correlations with two other measures of word importance: gradients and changes in output after removing a word. Holding other parameters fixed, they could often construct alternative weights that emphasized different positions yet produced almost unchanged predictions. They argued that standard attention should not be treated as a meaningful explanation.
Wiegreffe and Pinter (EMNLP 2019) responded with a double-negative title. They found the correlation experiments persuasive, but disputed the latter part of the argument: the conclusion depends on the definition of “explanation.” Requiring an explanation to faithfully reflect the model's computation differs from merely requiring it to be understandable and plausible. Counterexamples also cannot be constructed by changing only the weights while ignoring the rest of the model. Instead, they trained the weights jointly with the entire model to find “adversarial” weights emphasizing different positions. They then fixed those weights and supplied them to a simple diagnostic model without access to context. On three of four English binary classification datasets, performance was markedly worse than with the originally learned weights. On the remaining dataset, performance was comparable, which the authors attributed to that dataset's characteristics. They therefore argued that prior work had not established that attention was useless for explanation. However, they also confirmed that such adversarial weights could indeed be found for some classification tasks, though they were less extreme than those found by the earlier method.
Both papers primarily studied recurrent models with attention and English-language tasks. Neither directly tested today's large Transformer language models, so neither conclusion can be directly extrapolated to them. A cautious interpretation is that weights describe the proportions used to mix content in a particular head of a particular layer, with many layers still to follow. A single weight map is insufficient to establish the causal basis of an answer.
The cost of longer sequences and the context window
Standard self-attention pairs every position with every position. For n tokens, there are n times n scores; doubling the length roughly quadruples the number of pairs. Table 1 in the original paper, excerpted below, compares several layer types, with n denoting sequence length and d representation dimension. Self-attention's computation grows with the square of n, but the maximum path length between any two positions is constant: however far apart two words are, they can be compared directly in a single layer. In a recurrent layer, the path length grows with n.
| Layer type | Computation per layer | Maximum path length between any two positions |
|---|---|---|
| Self-attention | O(n²·d) | O(1) |
| Recurrent layer | O(n·d²) | O(n) |
| Self-attention restricted to r nearby positions | O(r·n·d) | O(n/r) |
This is one reason computation and memory requirements rise as input grows longer, though implementations differ. The FlashAttention paper likewise notes that self-attention has quadratic time and memory complexity with respect to length, and proposes an “exact” algorithm: mathematically, it computes the same attention, but speeds it up by reducing reads and writes to GPU memory. Actual speed therefore cannot be predicted from the quadratic formula alone. Fitting within the window also does not mean the information will be used well. In experiments on multi-document question answering and key-value retrieval, Liu et al. (TACL 2024) observed that performance was often markedly worse when relevant information appeared in the middle of a long input rather than at the beginning or end. See also context window.
For token-by-token generation, Hugging Face's official documentation explains that a KV cache can store the keys and values of previous tokens, so each step computes only those of the new token. The tradeoff is memory usage that grows linearly with sequence length.
To explore related terms, start with the AI Terms Index.
Found this guide helpful? Share it with someone who needs it
Lifestyle
Agentic Retrieval-Augmented Generation (Agentic RAG): When to Search Again
Agentic RAG lets models make retrieval decisions, choosing sources, rewriting queries or searching further based on the question and available evidence. Using a concrete LangGraph workflow and a comparison of venue-use conditions, this article contrasts it with fixed RAG and explains stopping criteria, permissions, query logs and cost control. Learn to judge whether a task needs dynamic exploration or a predictable fixed workflow is enough.
Lifestyle
Graph Retrieval-Augmented Generation (GraphRAG): Understanding a Document Collection Through Relationships
GraphRAG uses entities and relationships in retrieval to answer questions about cross-document connections and overall themes. Drawing on Microsoft's original paper and official docs, this article explains entity extraction, community summaries, and local and global search, using local event records to show why different questions need different paths. It also shows how missing links, mistaken name merges, and distorted summaries affect answers: a pretty graph is not verified fact.
Lifestyle
Knowledge Graphs: Making Entities and Relationships Clear
Knowledge graphs organize entities and relationships to trace who took part in what, where and when through links. Using community events, this article covers entity identity, relationship direction, sources and time; compares Resource Description Framework (RDF) and property graphs; and explains why graphs are not equivalent to visualizations or reliable facts. Learn when to build one and how it works with tables, vector search and graph retrieval-augmented generation (GraphRAG).
Lifestyle
Hybrid Search: Balancing Exact Terms and Meaning
Hybrid search merges full-text, vector and other retrieval results to cover exact codes and natural-language needs. An equipment-manual query explains candidate retrieval, deduplication, rank fusion and reranking, including how Reciprocal Rank Fusion (RRF) merges results by rank. Hybrid search is not always more accurate. Learn to test quality with code, paraphrase and no-answer queries and spot biases from permissions, filters and duplicate content.
Sources
- Bahdanau, Cho, Bengio: Neural Machine Translation by Jointly Learning to Align and Translate (arXiv:1409.0473, ICLR 2015)Checked:
- Vaswani et al.: Attention Is All You Need (arXiv:1706.03762)Checked:
- Jain, Wallace: Attention is not Explanation (arXiv:1902.10186)Checked:
- ACL Anthology: Attention is not Explanation (NAACL 2019, N19-1357)Checked:
- Wiegreffe, Pinter: Attention is not not Explanation (arXiv:1908.04626)Checked:
- ACL Anthology: Attention is not not Explanation (EMNLP 2019, D19-1002)Checked:
- Dao et al.: FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (arXiv:2205.14135)Checked:
- Liu et al.: Lost in the Middle: How Language Models Use Long Contexts (arXiv:2307.03172, TACL 2024)Checked:
- Hugging Face Transformers official documentation: How caching works (KV cache)Checked:
Compiled from the references and verification dates listed above; fares, schedules, product features, and terms can change, so please confirm the latest details on the relevant official website before travelling, booking, or purchasing.