The core mechanism of Transformers
Gain a deep understanding of the Attention mechanism, Encoder-Decoder architecture, feed-forward networks, and residual connections, and master how Transformers work.
Attention mechanism: the soul of Transformers
The Attention mechanism is the core of the Transformer, enabling the model to "attend" to different parts of the input sequence and understand the relationships between them.
Self-Attention: self-attention
Self-Attention allows each position in a sequence to directly attend to all other positions:
Multi-Head Attention: Multi-Head Attention
Multiple attention heads work in parallel to understand information from different angles:
- • Parallel Computing: multiple heads compute simultaneously without interfering with each other
- • Different perspectives: each head focuses on different relations (syntax, semantics, position, etc.)
- • Information fusion: Concatenates the outputs of multiple heads and then applies a linear transformation
- • Expressiveness: The multi-head mechanism greatly enhances the model's expressive power
Positional Encoding
The Attention mechanism itself does not contain positional information and needs to be injected through positional encoding:
- • Sinusoidal positional encoding: Use sin/cos functions to generate positional encodings
- • Learnable positional encoding: Models such as BERT use learnable embeddings
- • Relative position encoding: focus on relative position rather than absolute position
- • Limitations: Fixed positional encoding struggles with very long sequences (e.g., 1M+ tokens)
Highlighter theory: an intuitive way to understand Attention
The metaphor of marking key points with a highlighter helps explain how the Attention mechanism works.
Explain by analogy
Imagine you’re reading an article and highlighting the important parts with a highlighter:
Practical example
Encoder-Decoder architecture
The original Transformer architecture includes an Encoder and a Decoder, but many variants have emerged in practical applications.
Encoder
Understand the input sequence and generate an intermediate representation:
- • Self-Attention: Understand the internal relationships within the input sequence
- • Feed-Forward: nonlinear transformation
- • Output: encoded representation of each position
- • App: understanding tasks such as BERT, RoBERTa, etc.
Decoder
Generate the target sequence based on the Encoder output:
- • Masked Self-Attention: only the generated portion is visible
- • Cross-Attention: Focus on the Encoder output
- • Generate: generate the target sequence token by token
- • App: generative tasks such as GPT, T5
Practical application variants
GPT(Decoder Only)
Architecture: Decoder only, remove the Cross-Attention layer
Features: autoregressive generation, generating text from left to right
App: Text generation, conversation, code generation
BERT(Encoder Only)
Architecture: Encoder only, bidirectional understanding
Features: See the entire input sequence at once, with bidirectional context
App: Text classification, question answering, named entity recognition
T5(Encoder-Decoder)
Architecture: Complete Encoder-Decoder structure
Features: A unified framework where all NLP tasks are converted to text-to-text
App: Various tasks such as translation, summarization, and Q&A
Feedforward networks and residual connections
Feed-Forward Network, Residual Connection, and Layer Normalization are key components for stable Transformer training.
Feed-Forward Network (FFN)
The feedforward network performs nonlinear transformations independently at each position:
- • Structure: A two-layer fully connected network with an activation function in between (usually ReLU or GELU)
- • Role: enhance the model's nonlinear expressiveness
- • Features: Each position is processed independently, allowing parallel computation
- • Parameter: FFN usually accounts for most of the model's parameters (e.g., 66% in GPT-3)
Residual Connection (residual connection)
Add the input directly to the output, solving the vanishing gradient problem in deep networks:
- • Formula:output = input + sublayer(input)
- • Role: Provide a "highway" for gradient propagation
- • Effect: enables the model to train deeper networks (e.g., GPT-3 has 96 layers)
- • Source: Learn from the success of ResNet
Layer Normalization
Normalize the output of each layer to stabilize the training process:
- • Position: after Attention and FFN, before the residual connection
- • Role: Stable activation-value distributions accelerate training convergence
- • Effect: allows the use of a larger learning rate, making training more stable
- • Variant: Pre-LN (normalization before the sublayer) vs Post-LN (normalization after the sublayer)
Complete structure of the Transformer Block
Multiple Transformer Blocks are stacked to form a deep network, and each Block contains the structure above
Learning outcomes
After completing this chapter, you will:
- 1Gain a deep understanding of the mathematical principles of the Attention mechanism (Query, Key, Value) and its computation process
- 2Learn how Multi-Head Attention understands information from multiple perspectives, and the role of positional encoding
- 3Understand the architectural differences and use cases of GPT (Decoder only) and BERT (Encoder only)
- 4Understand the roles of Feed-Forward Network, Residual Connection, and Layer Normalization