Attention matches keys and mixes values
Query, Key and Value through one complete calculation
Queries match keys; the resulting weights mix values. One numerical example exposes the roles, shapes and limits.
Attention can combine information rather than returning one best matching item. Each query creates a different weighted mixture of the available values.
Start with dot products and embeddings and Softmax.
Three roles
A key and its value belong to the same item, but need not be the same vector or dimension. Without masks or dropout:
Softmax runs across the available items for each query.
One complete calculation
Choose q=[ln(3)], so d_k=1 and the scaling divisor is one. These are hand-picked vectors, not trained outputs.
| Item | Key | Value | Score | Softmax weight | |---|---|---|---|---:| | A | [1] | [10,0] | ln(3) | 0.75 | | B | [0] | [0,20] | 0 | 0.25 |
The exponentials are 3 and 1. Mix the values:
output = 0.75×[10,0] + 0.25×[0,20] = [7.5,5]
It returns neither A nor the number 0.75. Change only the query to [-ln(3)] and the weights reverse, giving [2.5,15].
What “self” means
In typical self-attention, the same input sequence X supplies all three roles through separate learned projections:
Same source sequence does not mean identical projections. The separation lets the model learn what determines a match separately from what information to pass on.
Shapes reveal the cost
| Part | Shape | |---|---| | Q | n × dk | | K | m × dk | | Scores | n × m | | V | m × dv | | Output | n × dv |
Dense self-attention with n items compares n² pairs. That counts comparisons; it does not prove an implementation materializes the full score matrix in memory.
Under simplifying assumptions of independent, zero-mean, unit-variance components, dot-product variance grows with dk. Dividing by sqrt(dk) moderates score scale before Softmax. It is not cosine normalization and guarantees no particular trained distribution.
Check yourself
Change only A's value from [10,0] to [100,0], holding Q and K fixed. The weights remain 0.75 and 0.25; the output becomes [75,5].
With the original values and a zero query both weights are 0.5 and the output is [5,10]. No single item was selected.
An attention weight is not a truth score or a complete explanation of the model's decision. Useful mixing still depends on learned representations and the surrounding network.