How does self-attention work?
Quick-fire answer from the Awesome Generative AI Guide (MIT)
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Self-attention is a mechanism that enables models to weigh the importance of different parts of the input data relative to each other. In the context of transformers, it allows every output element to be computed as a weighted sum of a function of all input elements, enabling the model to focus on different parts of the input sequence when performing a specific task. The self-attention mechanism involves three main steps:
Query, Key, and Value Vectors: For each input element, the model generates three vectors—a query vector, a key vector, and a value vector—using learnable weights.
Full worked answer
Self-attention is a mechanism that enables models to weigh the importance of different parts of the input data relative to each other. In the context of transformers, it allows every output element to be computed as a weighted sum of a function of all input elements, enabling the model to focus on different parts of the input sequence when performing a specific task. The self-attention mechanism involves three main steps:
Query, Key, and Value Vectors: For each input element, the model generates three vectors—a query vector, a key vector, and a value vector—using learnable weights.
Attention Scores: The model calculates attention scores by performing a dot product between the query vector of one element and the key vector of every other element, followed by a softmax operation to normalize the scores. These scores determine how much focus or "attention" each element should give to every other element in the sequence.
Weighted Sum and Output: The attention scores are used to create a weighted sum of the value vectors, which forms the output for each element. This process allows the model to dynamically prioritize information from different parts of the input sequence based on the
What a good answer covers
- Self-attention is a mechanism that enables models to weigh the importance of different parts of the…
- In the context of transformers, it allows every output element to be computed as a weighted sum of a…
- Query, Key, and Value Vectors
- Attention Scores: The model calculates attention scores by performing a dot product between the query…
- These scores determine how much focus or "attention" each element should give to every other element in…
Key terms: Query, Key, and Value Vectors · Attention Scores · Weighted Sum and Output
In the glossary: Attention
Quick-fire answer from the Awesome Generative AI Guide (MIT) · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep