Three projections of the same thing
Attention needs three different views of every position, and it gets them by multiplying the input by three learned matrices.
- : the input, : one row per position.
- : projections into the comparison space, .
- : projection into the output space, .
All three start from the identical input. What makes them different is only that the three matrices are trained separately.
What each one is for
The query is what this position is looking for. The key is what a position advertises about itself. The value is what it actually hands over if selected.
The comparison happens between queries and keys, and the thing returned is a mixture of values.
That split is the reason there are three and not two: what a position offers as a match key need not be what it contributes as content.
A pronoun might key on "I am a noun phrase" while its value carries the identity of who it refers to.
The shapes
Queries and keys must share a width, because they are dotted together. Values need not, because they are only ever summed.
In practice , where is the number of heads, so that concatenating the heads returns to the original width.
One layer, in order
- Take the input, one vector per position.
- Multiply by to get a query for every position.
- Multiply the same input by to get a key for every position.
- And by to get a value.
- Hand all three to the attention formula.
Where they come from decides the layer's name
If all three are projections of the same sequence, it is self-attention.
If the queries come from one sequence and the keys and values from another, it is cross-attention.
Nothing else changes. The formula, the matrices and the shapes are identical, and only the source of differs.