Module 3 — Attention, Intuitively
The bigram model of Module 1 has a fundamental limitation: it conditions on only the immediately preceding token. Natural language exhibits dependencies that span far longer distances. Predicting the final word of "the keys to the cabinet were…" requires conditioning on "keys" — many tokens back — rather than on the nearer noun "cabinet," to determine the correct number agreement ("were" rather than "was"). The mechanism that enables a model to condition on tokens at arbitrary distances and to assign each a learned weight is attention — the central architectural mechanism of every modern LLM.
The formal question addressed by attention
When predicting the next token, attention computes the answer to: for each previously observed token, what weight should it receive in the current prediction? It assigns each previous token a non-negative weight in \( [0, 1] \), with the weights summing to 1. A large weight indicates that the corresponding token is highly relevant to the current prediction; a weight near zero indicates that the token contributes negligibly.
Identifying the relevant context
The model is predicting the highlighted blank below. First identify which earlier word the prediction most directly depends on, then display the attention weights to compare with your assessment.
This activity needs JavaScript. The lesson below still covers everything.
Sharp versus diffuse attention distributions
Attention is not categorical. The same relevance scores can produce a sharp attention distribution concentrated on a single token, or a diffuse distribution spread across many tokens — determined by the temperature parameter of the softmax that converts scores to weights (softmax is the step that turns a set of raw relevance scores into positive weights that sum to 1 — the next module builds it up from scratch). Adjust the temperature parameter to observe the weights concentrate or spread, and the weighted "context" vector the model passes forward change accordingly.
# a relevance score for each earlier token, then softmax to get weights scores = query @ keys.T # how well each token matches what we need weights = softmax(scores) # positive, sum to 1 — the bars you saw context = weights @ values # a weighted blend of the earlier tokens
This is the complete attention operation. Module 4 develops the construction of query, keys, and values in detail — but the operation is always: compute scores, apply softmax to obtain weights, compute a weighted sum of values.
Check your understanding
Answer a short set of questions on attention.
This activity needs JavaScript.