Attention: Select the Information That Matters Now
When answering a question about a paragraph, you do not give every word equal attention.
Attention: Select the Information That Matters Now explained visually
When answering a question about a paragraph, you do not give every word equal attention. English captions are optional and start off. The complete transcript follows the written lesson.
What you will learn
When answering a question about a paragraph, you do not give every word equal attention.
Compare a query with available information
When answering a question about a paragraph, you do not give every word equal attention. An attention mechanism lets a model form a context-dependent combination of available representations. It compares a query with keys, converts the comparison scores into weights, and combines the corresponding values.
Form a weighted combination
The query represents what the current computation is looking for. Keys describe how available items can be matched, and values carry the information to combine. In common scaled dot-product attention, scores are normalized with softmax so that the weights form a distribution over the available positions. Masks can exclude positions, such as future tokens in an autoregressive language model or padding in a batch.
Interpret weights cautiously
Attention makes it easier to connect distant parts of a sequence without passing all information through one recurrent state. But the weights are part of a learned computation, not automatically a complete explanation of the model's decision. Their interpretation depends on the architecture and context. Evaluate the full output and use controlled tests before claiming that a high weight proves a particular causal reasoning process.
Worked example
Suppose attention assigns weights 0.2 and 0.8 to scalar values ten and twenty. Their weighted combination is eighteen. The larger weight gives the second value more influence in this operation, but that single calculation does not explain every later layer or the final prediction.
Quick check: test your understanding
Try it yourself: Why must a next-token predictor mask future positions during training?
Check the reasoning
Otherwise it could use the answer or later information that will not be available when generating the next token.
Does a large attention weight alone prove why a model made its final decision?
No. Other layers, representations, and interactions also contribute.
Further reading
Dive into Deep Learning — Aston Zhang, Zachary C. Lipton, Mu Li, and Alexander J. Smola.
Deep Learning — Ian Goodfellow, Yoshua Bengio, and Aaron Courville.
Original ToolsInVision teaching examples. Editorial approach · Suggest a correction
Accessible referenceLesson 76 transcript231 words · Video transcript · Matches the published narration and captionsOpen transcript
When someone asks a question about a paragraph, you usually look back at the parts that help answer it. An attention mechanism gives a model a numerical way to combine relevant information.
There are three ideas to keep separate: queries, keys, and values. A query represents what the current computation is looking for. Keys provide information for matching. Values contain the information that will be combined.
The model compares a query with the keys and produces scores. A common attention method uses softmax to turn those scores into weights. It then takes a weighted combination of the values.
Let's make that concrete. Suppose the values are ten and twenty, and their weights are zero point two and zero point eight. Ten times zero point two gives two. Twenty times zero point eight gives sixteen. Add them, and the result is eighteen.
The second value has more influence in this calculation because it received a larger weight. That does not mean the weight completely explains the model's final decision. Other layers and interactions still matter.
Attention can connect distant positions without forcing all information through one recurrent state. Masks also let us exclude positions. For example, a model predicting the next word should not look ahead at future words during training.
The useful idea is selective combination. The model learns which available information to combine for the computation it is doing now.