The production of Query (Q), Key (K), and Value (V) sequences in attention mechanisms involves linear transformations of input embeddings or hidden states. Here's a structured breakdown:
1. Input Representation
- The input sequence consists of token embeddings (or hidden states from a previous layer), denoted as X=[x1,x2,...,xn], where each xi∈Rdmodel.
2. Learned Projections
- Three learnable weight matrices are used:
- WQ∈Rdmodel×d
3. Purpose of QKV
- Query (Q): Represents the current token's interest (what it seeks in other tokens).
- Key (K): Represents the relevance of other tokens to the current one.
- Value (V): Encodes the information to be aggregated based on attention weights.
4. Attention Computation
- Attention weights are computed as:
Attention(Q,K,V)=softmax(d
5. Multi-Head Attention
- In multi-head attention, multiple sets of WQ,WK,WV matrices are used in parallel, projecting inputs into different subspaces. The resulting QKV sequences are concatenated and linearly transformed to maintain dimensionality.
6. Key Properties
- Dimensionality: dk (dimension of keys/queries) and dv (dimension of values) are typically smaller than d to reduce computation.
Example
For an input sequence of 3 tokens (x1,x2,x3), the QKV sequences are:
- Q=[x1WQ,x
These sequences are then used to compute attention weights and outputs, enabling the model to dynamically focus on relevant tokens.