What is self-attention in neural networks?
Self-attention is an operation that updates each element of a sequence by taking a weighted average of all the elements, where the weights measure how relevant each one is. Every token produces a query, a key, and a value vector. The query is compared with every key by a dot product, the scores are scaled and passed through softmax, and the resulting weights mix the value vectors into a new, context-aware representation of the token.
Self-attention is the core operation of Transformers, so it runs inside every large language model, BERT-style encoder, vision transformer, and speech model such as Whisper. It lets a model link a pronoun to its noun, a verb to its subject, or an image patch to a related patch anywhere in the input, in a single step. Its masked (causal) form is what allows language models to be trained on all positions of a text at once.
Key ideas
- Each token is projected into a query, a key, and a value vector.
- Attention scores are dot products between one token's query and every token's key.
- Scores are divided by √d_k so softmax does not saturate as dimensions grow.
- The output for a token is the softmax-weighted average of all value vectors.
- A causal mask sets future positions to −∞ so a token sees only itself and earlier tokens.
- The score matrix is not symmetric, because queries and keys use different weights.
Short takes on this topic
Common questions
What is the difference between self-attention and cross-attention?
In self-attention the queries, keys, and values all come from the same sequence, so a sentence attends to itself. In cross-attention the queries come from one sequence and the keys and values from another, for example a translation decoder attending to the encoded source sentence. The math is identical; only the inputs differ.
What is the difference between additive and dot-product attention?
Additive attention, used by Bahdanau et al., scores a query and key with a small feed-forward network: v · tanh(W_1 q + W_2 k). Dot-product attention, used by Luong et al. and in Transformers, scores them with q · k. The dot-product form is much faster because all scores come from one matrix multiplication, and scaling by √d_k fixes its problem with large dimensions.
Do attention weights explain a model's predictions?
Only partly. Attention maps show where information was gathered in one layer, but later layers, value vectors, and residual connections also shape the output. Studies such as Jain and Wallace (2019) found that very different attention patterns can lead to the same prediction, so attention is a useful hint, not a full explanation.
Is self-attention only used for text?
No. Vision transformers apply it to image patches, speech models to audio frames, and graph attention networks to the neighbors of each node. Any input that can be split into a set of vectors can use it, as long as position or structure is supplied separately.
Related explainers
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- Multi-Head Attention — Parallel attention heads learning different patterns
- Cross-Attention — Attending across sequences and modalities
- Positional Encoding — How transformers know word order
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
References
- Vaswani et al. (2017), "Attention Is All You Need", NeurIPS
- Luong, Pham & Manning (2015), "Effective Approaches to Attention-based Neural Machine Translation"
- Dao et al. (2022), "FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness"
- Jain & Wallace (2019), "Attention is not Explanation"
Last updated
Spotlight