AI Glossary / Generative AI and language / Intermediate
Attention
Definition
Attention is a neural-network mechanism that combines information using weights that depend on the current input or processing context. In a common form, a query is compared with keys, and the resulting weights determine a weighted sum of associated value vectors.
What it means
Different parts of an input can matter differently for a particular prediction. When translating a sentence, the useful source information can change as each output word is produced. Attention allows the model to form a context-dependent combination instead of relying on one unchanging summary.
The word is a mathematical analogy. It does not imply awareness, intention or human concentration. Attention weights describe part of a computation, not a model's conscious interests.
Attention also predates the Transformer architecture. Earlier neural translation systems used it to connect a decoder with source-sentence representations. Transformers made attention central to the interactions among representations throughout the model.
How it works
In query-key-value attention, each key is paired with a value. A compatibility function compares a query with the keys. A normalization step commonly converts those scores into nonnegative weights that sum to one, and the output combines the values using those weights.
Scaled dot-product attention uses query-key dot products divided by the square root of the key dimension before softmax. Learned projections create the relevant representations. Masks can exclude positions, such as padding or future tokens in causal language modeling.
Self-attention obtains queries, keys and values from the same sequence of representations. Cross-attention obtains queries from one stream and keys and values from another, such as a decoder consulting an encoder. Multiple heads can perform different learned combinations in parallel.
The output then passes through other network operations. A large attention weight does not, on its own, establish that an input caused the final prediction. Values, projections, residual connections and later layers also affect the result.
Three practical examples
- A translation system uses attention to combine source-sentence information while producing successive target tokens.
- A document model uses self-attention to relate representations of words appearing in different parts of its available context.
- A multimodal model uses an attention mechanism to combine one representation stream with information from another, depending on its architecture.
Think of it this way
Think of a researcher preparing an answer from several reference cards and giving each card a different contribution to a working summary. The contributions can change with the question.
The analogy stops at weighted information combination. Neural values are vectors, and their meanings need not correspond neatly to readable cards. A weight is not a certificate that a card is true or a complete explanation of the final answer.
Worked example
Combining values, not choosing a single winner
Suppose a teaching attention calculation produces weights 0.2, 0.3 and 0.5 for three scalar values 10, 20 and 40. The output is 0.2 times 10 + 0.3 times 20 + 0.5 times 40 = 28.
The mechanism has combined all three values. It has not simply returned the value 40 because that value received the largest weight. With vector values, the same weighted combination applies component by component.
If a mask excludes the third position, the remaining scores need to be normalized over the allowed positions. If their original relative proportions are preserved, the weights become 0.4 and 0.6, yielding 16. This illustrates why excluding a position changes the combination rather than merely hiding its displayed weight.
Illustrative case study
A fictional contract-analysis team, Fern Review, adds an attention visualization to a document model. Reviewers initially treat the darkest highlighted words as a definitive explanation of why the model assigned a category.
The technical team shows that the display represents one selected layer and head, while the final result combines many computations. It checks the model with controlled input changes and error analysis instead of relying only on the heatmap.
The interface is revised to describe the visualization as an inspection aid. The team retains evidence links for factual claims separately. Attention over a sentence does not establish that the sentence supports an extracted fact, and a plausible-looking heatmap does not establish decision correctness.
Common misconceptions and mistakes
Attention is not a database search or fact-checking service. It combines representations available within the computation; it does not automatically retrieve authoritative external evidence.
High weight is not synonymous with high causal importance. Research has demonstrated cases in which substantially different attention distributions produce similar predictions. Interpretation requires a defined method and supporting tests.
A large context window also does not guarantee effective use of every detail. Position, representation, training and computational constraints can affect performance. Dense attention's pairwise interactions can be expensive as sequence length grows, although implementation and architecture choices change practical costs.
Business use
Attention helps explain how many language and multimodal models combine context, but it should not serve as a procurement quality claim by itself. Evaluate the complete system on representative tasks and difficult examples.
When a product exposes attention visualizations, make their scope clear: which layer, head and computation they show. For operational explanations, combine suitable inspection methods with source evidence and error analysis. For long documents, measure whether critical information is used correctly rather than inferring reliability from the availability of attention.
People also ask
Is attention only used for text?
No. Similar mechanisms can combine image, audio and other representations.
Does self-attention mean a model is self-aware?
No. Self refers to the same input sequence supplying the representations used in the attention calculation.
Do the highest attention weights always identify the most important words?
Not necessarily. A weight is one component of a larger computation, and its relationship to the final outcome needs investigation.
Check your understanding
Attention assigns weights 0.25 and 0.75 to scalar values 4 and 12. Is its weighted output simply 12?
Answer: No. The weighted sum is 0.25 times 4 + 0.75 times 12 = 10. The larger weight does not turn the operation into a winner-takes-all selection.
Related terms
- P-value
- Softmax
- Self-attention
- Cross-attention
- Attention head
- Attention mask
- Transformer
- Model Interpretability
- Query
- key
- value
Learn next
Calculate a weighted sum and then work through a small query-key compatibility example. Compare self-attention and cross-attention by identifying the source of each vector. Finally, examine why an attention heatmap and a validated explanation of a prediction are different kinds of evidence.
More in Generative AI and language
- Adaptive sampling - Adaptive sampling is an approach in which information from earlier samples influences which samples are selected next or how sampling effort is allocated.
- Adaptive Synthetic Sampling - Adaptive Synthetic Sampling, usually abbreviated ADASYN, is an oversampling method for imbalanced classification.
- AI slop - AI slop is an informal, critical term for low-quality AI-generated content produced with an emphasis on quantity rather than usefulness, accuracy or...
- Attribute sampling - Attribute sampling is a decision-tree training technique that randomly selects a subset of input features to consider when choosing a split.
- Autoencoder - An autoencoder is a neural network trained to reconstruct an input from an internal representation of that input.
- Auxiliary Classifier GAN - An Auxiliary Classifier GAN, or AC-GAN, is a class-conditional generative adversarial network whose discriminator predicts both whether an example is real...
Sources
Sources: Vaswani and colleagues, Attention Is All You Need: https://arxiv.org/abs/1706.03762 Bahdanau, Cho and Bengio, Neural Machine Translation by Jointly Learning to Align and Translate: https://arxiv.org/abs/1409.0473 Hugging Face, attention masks and model terminology: https://huggingface.co/docs/transformers/main/en/glossary Jain and Wallace, Attention is not Explanation: https://aclanthology.org/N19-1357/
Previous: Attack Next: Attention coverage mechanism