In the context of transformer models and attention mechanisms, particularly self-attention, the Query (Q), Key (K), and Value (V) vectors are fundamental. They are derived from the input vector embeddings through simple linear transformations.
Let's break down each component:
Suppose you have an input sequence of tokens, and each token i has an associated input embedding vector xi. The dimension of this embedding is often referred to as dmodel (e.g., 768 for BERT base).
1. Query (Q), Key (K), and Value (V) Vectors
For each input embedding vector xi, we calculate its corresponding query (qi), key (k), and value () vectors. This is done by multiplying by three different learnable weight matrices: , , and . These weight matrices are parameters of the model learned during training.
Let:
- xi∈Rdmodel be the input embedding vector for the -th token.
Often, dk (dimension of Q and K) and dv (dimension of V) are equal, and for multi-head attention, they are typically . For simplicity, let's assume .
The formulas for calculating Q, K, and V vectors for a single input embedding xi are:
-
Query vector for xi:
qi=x
()
Intuition:
- Query (Q): Represents "what I'm looking for" or "what information is relevant to this token."
- Key (K): Represents "what I have" or "what information this token can offer."
- Value (V): Represents "the actual information" that will be passed on if the key matches a query.
Example Analogy: Think of searching for a video on YouTube.
- Your search query is the Query (Q).
- The video titles, descriptions, and tags are the Keys (K).
- The actual video content itself is the Value (V).
You compare your query against the keys to find relevant videos (attention weights), and then you watch the content (value) of the most relevant ones.
For an entire input sequence of N tokens, we can represent these operations in matrix form:
Let:
- X∈RN×dmodel be the matrix of input embeddings, where each row is .
Then:
- Q=XWQ
- K=XWK
2. Attention Weights (or Attention Probabilities)
Attention weights quantify the relevance or importance of each key to a specific query. In self-attention, this means how much each token in the input sequence contributes to the representation of another token (or itself).
The process involves three main steps:
-
Calculate Similarity Scores (Dot Product): For each query qi, we calculate a similarity score with every key kj in the sequence. The dot product is commonly used for this.
In matrix form, for all queries and all keys:
3. Context Vectors (or Output of Attention Head)
Once the attention weights are calculated, they are used to compute a weighted sum of the Value vectors. This weighted sum forms the context vector for each query. The context vector for a given token effectively aggregates information from all other tokens in the sequence, weighted by their relevance (attention weights).
For each query qi, its corresponding context vector ci is calculated as:
In matrix form, for all context vectors:
- Context Vectors Matrix: C=AweightsV
(C∈. Each row is the context vector for the -th input token.)
This matrix C (or a linearly transformed version of it in multi-head attention) represents the output of the attention mechanism, containing the "contextualized" embeddings for each token in the input sequence. These context vectors are then typically passed to subsequent layers in the transformer, such as feed-forward networks.
Summary of the Flow:
- Input Embeddings X
- Linear Projections: X→Q,K,V using WQ
This entire process is the core of the self-attention mechanism, allowing models like Transformers to capture long-range dependencies and understand the relationships between different parts of an input sequence.