To comprehend the transformative capabilities of Transformers, it's crucial to examine their fundamental components. These components exemplify the architectural ingenuity of Transformers and underscore their efficiency in handling intricate language tasks. In this section, we will explore the pivotal elements that constitute the Transformer model, specifically focusing on the self-attention mechanism, position-wise feed-forward networks, and layer normalization.
At the core of the Transformer lies the self-attention mechanism. This component enables the model to weigh the significance of different words in a sentence relative to each other, allowing for a nuanced understanding of context. Unlike previous models that heavily relied on recurrent structures, self-attention provides a mechanism to capture dependencies across entire sequences simultaneously.
Key Operation: Scaled Dot-Product Attention
The self-attention mechanism operates primarily using scaled dot-product attention. This attention function processes an input matrix to produce an output that highlights the relevant features of the input sequence. The process involves three core steps:
Query, Key, and Value Vectors: For each input token, we derive three vector representations: the Query (Q), Key (K), and Value (V). These vectors are obtained through learned linear transformations.
import torch
import torch.nn as nn
d_k = 64 # Dimension of Key
d_v = 64 # Dimension of Value
input_dim = 512 # Dimension of input embeddings
query = nn.Linear(input_dim, d_k)
key = nn.Linear(input_dim, d_k)
value = nn.Linear(input_dim, d_v)
Attention Scores: Compute the attention scores by taking the dot product of the Query with all Keys, followed by a scaling factor:
Attention(Q,K,V)=softmax(dkQKT)VThis step ensures that the model focuses more on relevant tokens, effectively enabling it to capture long-range dependencies.
Softmax Normalization: Apply a softmax function to obtain weights that sum to one, thus highlighting the importance of each token in the context.
Visualization of the self-attention mechanism, showing how different input tokens are assigned varying attention weights.
To enhance the model's ability to focus on different positions, the Transformer employs multi-head attention. This involves running several self-attention operations in parallel, each with different linear projections, and then concatenating their outputs.
num_heads = 8 # Number of attention heads
multi_head_attention = nn.MultiheadAttention(embed_dim=input_dim, num_heads=num_heads)
Illustration of multi-head attention, where different attention heads focus on different aspects of the input.
Following the self-attention mechanism, each layer of the Transformer includes a position-wise feed-forward network. This network consists of two linear transformations with a ReLU activation in between:
Layer-wise Transformation: The input from the self-attention layer is transformed through two fully connected layers.
class PositionwiseFeedForward(nn.Module):
def __init__(self, input_dim, ffn_dim=2048):
super(PositionwiseFeedForward, self).__init__()
self.fc1 = nn.Linear(input_dim, ffn_dim)
self.fc2 = nn.Linear(ffn_dim, input_dim)
self.relu = nn.ReLU()
def forward(self, x):
return self.fc2(self.relu(self.fc1(x)))
feed_forward = PositionwiseFeedForward(input_dim=input_dim)
Non-Linearity and Dimensionality Expansion: The first linear transformation increases the dimensionality, allowing for more complex representations, while the second reduces it back to the input size.
Diagram illustrating the position-wise feed-forward network, which applies two linear transformations with a ReLU activation in between.
Each sub-layer within the Transformer is followed by layer normalization and a residual connection. These elements are crucial for stabilizing the training process and ensuring efficient gradient flow:
Residual Connections: Introduce shortcuts that allow the gradient to bypass certain layers, thus mitigating the vanishing gradient problem.
class TransformerLayer(nn.Module):
def __init__(self, input_dim):
super(TransformerLayer, self).__init__()
self.self_attention = multi_head_attention
self.feed_forward = feed_forward
self.norm1 = nn.LayerNorm(input_dim)
self.norm2 = nn.LayerNorm(input_dim)
def forward(self, x):
# Self-attention + Layer Norm
attention_output, _ = self.self_attention(x, x, x)
x = self.norm1(x + attention_output)
# Feed-forward + Layer Norm
ff_output = self.feed_forward(x)
x = self.norm2(x + ff_output)
return x
Layer Normalization: Applied after each residual connection to maintain stable activations.
Diagram illustrating the layer normalization and residual connections within a Transformer layer.
Finally, because Transformers lack recurrent structures, they require a mechanism to incorporate positional information. Positional encodings are added to the input embeddings to provide a sense of order.
These encodings use sinusoidal functions to ensure that the model can easily learn to attend by relative positions, a crucial aspect for ensuring the model recognizes the sequential nature of language.
By integrating these components, the Transformer achieves its unparalleled ability to process language data efficiently and accurately. The collaboration of these mechanisms allows for handling complex dependencies and contextual nuances, setting the stage for the advanced applications explored in subsequent chapters.
© 2025 ApX Machine Learning