How does self-attention work in a transformer?
hardAnswer
- For each token, produce a Query, Key, and Value vector.
- The output for token i is a weighted sum of all Values, where the weight is .
- Multi-head attention runs this in parallel with different projections and concatenates the results.
- Attention lets every token look at every other token — capturing long-range dependencies with complexity.
Check yourself — multiple choice
- Attention weights are computed as Q · V
- Weights are applied to V
- Attention only looks at neighboring tokens
- Multi-head means multiple layers stacked in series
That's the scaled dot-product attention formula.
#attention#transformers
Practise Deep Learning
214 interview questions in this topic.