Okay, let's break down multi-head attention in Transformer networks.
1. The Foundation: Scaled Dot-Product Attention (The Building Block)
Before understanding multi-head, we need to understand the basic attention mechanism used within it, often called "Scaled Dot-Product Attention".
Goal: To calculate how relevant different parts of an input sequence are to a specific part (e.g., a word) we are currently processing.
Inputs: Three vectors derived from the input embeddings:
Query (Q): Represents the current word/token we're focusing on.
Key (K): Represents all the words/tokens in the sequence (including the current one) that we might want to pay attention to.
Value (V): Represents the actual information or content associated with each word/token in the sequence (corresponding to the Keys).
Process:
Calculate Scores: Compute the dot product between the Query (Q) vector and each Key (K) vector. This measures the similarity or compatibility between the current focus (Q) and each potential context word (K).
Scale: Divide the scores by the square root of the dimension of the Key vectors (dk). This prevents the dot products from becoming too large, which could push the softmax function into regions with very small gradients, hindering learning.
Softmax: Apply a softmax function to the scaled scores. This converts the scores into probabilities (positive values that sum to 1), representing the "attention weights". A higher weight means a word is more relevant.
Weighted Sum: Multiply each Value (V) vector by its corresponding attention weight and sum them up. This produces an output vector that is a weighted representation of the input sequence, emphasizing the parts deemed most relevant by the Q-K interaction.
2. Single-Head Attention
Single-head attention is simply applying the Scaled Dot-Product Attention mechanism described above just once.
You have one set of learned weight matrices to project the input embeddings into Q, K, and V.
You perform the attention calculation (dot product, scale, softmax, weighted sum) once.
Limitation: This single attention mechanism might focus on only one specific type of relationship or feature correlation within the sequence (e.g., maybe it learns to focus on adjacent words, or perhaps words with similar syntactic roles). It lacks the ability to capture diverse aspects of relationships simultaneously.
3. Multi-Head Attention
Multi-head attention enhances the basic attention mechanism by running the attention process multiple times in parallel, allowing the model to jointly attend to information from different representation subspaces at different positions.
The Idea: Instead of performing a single attention calculation, perform multiple attention calculations ("heads") in parallel. Each head learns different aspects of the relationships between words.
How it Works:
Linear Projections: Take the original Q, K, and V vectors (derived from the input embeddings). Instead of using them directly in one attention calculation, project them h times using h different, learned linear projection matrices (WiQ,WiK for head ). This creates sets of lower-dimensional Q, K, V vectors ().
How Multi-Head Differs from Single-Head Attention
Feature
Single-Head Attention
Multi-Head Attention
Attention Ops
One Scaled Dot-Product Attention
h Scaled Dot-Product Attentions (in parallel)
Q, K, V Sources
One set of Q, K, V matrices
h sets of Q, K, V projection matrices
Focus/Learning
Learns one type of relationship/pattern
Learns multiple, diverse types of relationships/patterns
Representation
Single representational subspace
Multiple representation subspaces combined
Output
Direct result of one attention calculation
Concatenation of head outputs, followed by projection
Benefit
Simpler
Captures richer context, more robust representation
Why is Multi-Head Better?
Diverse Focus: Each head can learn to focus on different parts of the sequence or different types of relationships (e.g., syntactic dependencies, semantic similarities, positional proximity). One head might capture long-range dependencies, while another focuses on local context.
Richer Representation: By combining the outputs of multiple heads, the model builds a more nuanced and comprehensive understanding of the input sequence compared to relying on a single attention mechanism.
Improved Performance: Empirically, multi-head attention significantly improves the performance of Transformer models on various tasks like machine translation, text summarization, and question answering.
In essence, multi-head attention allows the Transformer to attend to information more effectively by giving it multiple "perspectives" or "channels" through which to learn and process relationships within the data.
,
WiV
i
h
Qi,Ki,Vi
Typically, the dimension of these projected vectors is reduced (e.g., dmodel/h), where dmodel is the original embedding dimension and h is the number of heads.
Parallel Attention: Apply the Scaled Dot-Product Attention mechanism (as described in step 1) independently to each of these h projected sets (Qi,Ki,Vi). This results in h different output vectors (headi).
Concatenation: Concatenate the output vectors from all the heads: [head1,head2,...,headh].
Final Linear Projection: Apply one final linear projection (using a learned weight matrix WO) to the concatenated output. This mixes the information learned by the different heads and maps it back to the expected output dimension.