1. Multi-Head Attention
Multi-Head Attention is a widely adopted extension of the attention mechanism in the Transformer model. It captures different attention distributions in various subspaces of the input sequence by running multiple independent attention mechanisms in parallel, thereby comprehensively capturing the various semantic associations present in the sequence.

In Multi-Head Attention, the input sequence is first transformed through three different linear transformation layers to obtain Query, Key, and Value. These transformed vectors are then divided into several “heads,” with each head having its own independent Query, Key, and Value matrices. For each head, a Scaled Dot-Product Attention operation is performed, which is:

Finally, the outputs of all heads are concatenated and passed through a linear layer to fuse them, resulting in the final attention output vector.
Through this approach, Multi-Head Attention can process the input sequence from different perspectives in parallel, enhancing the model’s ability to understand and capture complex dependencies. In practice, Multi-Head Attention significantly improves the performance of Transformer models in natural language processing and other sequence data processing tasks.
2. Why Use Multiple Attention Heads
1. Parallel Processing of Multiple Attention Patterns: Each attention head uses different linear transformations, meaning they can learn different feature associations from various subspaces of the input sequence. This allows the model to simultaneously focus on different aspects of the input sequence, such as grammatical structure, semantic roles, and topic transitions.
2. Increased Learning Capacity and Expressiveness: With multiple attention heads, the model can learn richer contextual information, with each head possibly focusing on different features of the input. These features, when combined, provide a more comprehensive understanding and processing of the input sequence.
3. Improved Model Performance: Experiments have shown that the Multi-Head Attention mechanism often leads to performance improvements compared to single-head attention. This is because the model can capture the diversity of data by processing and integrating results from multiple attention heads in parallel, enhancing its understanding and generalization capabilities for complex sequence tasks.
3. Multi-Head Self-Attention
Multi-Head Self-Attention is a type of Multi-Head Attention, both belonging to the applications of attention mechanisms in deep learning, especially in the Transformer model in natural language processing (NLP).
3.1 Is Self-Attention Q=K=V?

3.2 Differences Between Multi-Head Self-Attention and Multi-Head Attention
1. Application Scenarios:
Multi-Head Attention is not limited to self-attention scenarios; it can be applied to any form of attention mechanism, including but not limited to cross-sequence attention, such as attention from one sequence to another.
Multi-Head Self-Attention specifically refers to the multi-head processing of the attention mechanism within the same sequence, capturing the complex dependencies among elements within the sequence, commonly seen in the encoder and decoder of Transformers.
2. Focus of Functionality:
Multi-Head Attention can consider various types of associations simultaneously, regardless of whether they involve interactions among elements within the same sequence.
Multi-Head Self-Attention particularly emphasizes the self-referential characteristics of the sequence, meaning that each position in the sequence can view the entire sequence and adjust its representation accordingly.
In summary, Multi-Head Attention is a general term, and when applied to the sequence itself, it becomes Multi-Head Self-Attention. Both aim to enhance the model’s expressive capacity and ability to capture complex patterns by processing multiple attention perspectives in parallel, but Multi-Head Self-Attention specifically targets the contextual information mining of the sequence itself.
For reference, click the link at the bottom left to read the original text. For academic sharing only; if there is infringement, please delete immediately.
Edited / Garvey
Reviewed / Fan Ruiqiang
Verified / Garvey
Click below
Follow us