Self-attention
Attention lets one token assemble information from other positions. Each output is a weighted mixture that depends on the current input.
Open this chapter with experiments and audio
What are queries, keys and values?
Each input position has a vector. Three learned linear projections turn it into a query, a key and a value. For one position, take its query and compare it with the keys of all positions it may read. The resulting scores determine how heavily each value contributes to its output. One projection learns which information helps locate a relevant position. Another determines what information is passed on from that position.
The projection matrices contain trained parameters. The attention weights computed using a specific sentence are temporary activations. Change the sentence and you can change those weights without making any update to the checkpoint.
Work through two positions
Choose a query [1, 0] and two keys [2, 0] and [0, 2]. Their dot products are 2 and 0. Divide by the square root of the key width, here √2, to keep score sizes manageable. Applying softmax to [1.414, 0] gives approximately [0.804, 0.196]. These weights add up to one.
Suppose the first value is [1, 3] and the second [4, 2]. Multiply the first vector by 0.804 and the second by 0.196, then add matching components. The result is roughly [1.587, 2.804]. This is the actual output of this one query for those chosen values. A higher weight passes along more of a value, rather than choosing one entire token.
Which positions may contribute?
In a decoder trained to predict the next token, position two must not read position three. Otherwise the correct answer is already part of its input. A causal mask removes future positions from the softmax by setting their scores to negative infinity before normalization. Position two can then read positions one and two. It still predicts the following token.
Choose the middle token in the mask below and trace its row. Switch to a bidirectional encoder and observe which additional columns become available.
A causal mask removes only future positions. An early token can still influence a later one, even though the reverse connection is blocked. With bidirectional attention the representation of the early token may itself use later information.
| Query position | Allowed keys |
|---|---|
| 1 | 1 |
| 2 | 1, 2 |
| 3 | 1, 2, 3 |
Reproduce the weighted mixture
import math scores = [2 / math.sqrt(2), 0] exp_scores = [math.exp(s - max(scores)) for s in scores] weights = [v / sum(exp_scores) for v in exp_scores] values = [[1, 3], [4, 2]] out = [sum(w * row[j] for w, row in zip(weights, values)) for j in range(2)] print(weights, out)
Real models use many queries simultaneously. Multiple heads use separate projections and their outputs are combined. A high attention weight tells you about one mixing operation in one head. By itself it cannot establish why the full model chose a word. Later layers, value vectors and residual paths also contribute.
What happens if the two scores are equal and both keys are allowed?
Softmax gives each value weight 0.5. The output lies halfway between the two value vectors.
A weight becomes meaningful when you inspect the value it multiplies
One head reads two earlier positions with weights 0.8 and 0.2. Their value vectors are [2, 0] and [0, 5]. The returned vector is [1.6, 1]. Even the smaller weight contributes an entire coordinate. Swap only the value vectors and the result becomes [0.4, 4]. The attention map would look identical.
The weights follow from the query at the reading position and the keys at permitted positions. The values determine the information carried forward. A projected mixture gets added to the receiving position’s residual vector. A large weight alone therefore cannot tell us why the final output token won.
What happens if both value vectors become identical?
Any mix whose weights sum to one returns that same vector. The displayed weights can move substantially while this head’s output stays fixed.
Follow two actual positions through query, key and value →
Sources · Vaswani et al., 2017