Attention Mechanism
\n\nThe Attention Mechanism is an important technique in deep learning that mimics the attention allocation method in human visual and cognitive processes. Just as you unconsciously focus your attention on keywords when reading, the attention mechanism allows neural networks to dynamically focus on the most relevant parts of the input data.\n\nBasic Concepts
\n\nThe core idea of the attention mechanism is: **dynamically allocate different weights based on the importance of different parts of the input to the current task**. This weight allocation is not fixed, but dynamically calculated based on the context.\n\nMathematical Expression
\n\nThe attention mechanism can usually be expressed as:\n\nAttention(Q, K, V) = softmax(QK^T/βd_k)V\n\nWhere:\n\n* Q (Query): The query item for which the output currently needs to be calculated\n* K (Key): The key used to match with the query item\n* V (Value): The actual value corresponding to the key\n* d_k: The dimension of the key, used to scale the dot product result\n\nWhy Do We Need Attention Mechanism?
\n\n1. **Solve long-distance dependency problems**: Traditional RNNs struggle to capture relationships between distant words\n2. **Parallel computing capability**: Compared to the sequential processing of RNNs, attention can be computed in parallel\n3. **Interpretability**: Attention weights can intuitively show the focus of the model\n\n\n\n
Self-Attention Mechanism
\n\nSelf-attention is a special form of the attention mechanism that allows each element in the input sequence to establish a connection with all other elements in the sequence.\n\nWorking Principle
\n\n1. For each element in the input sequence, calculate its similarity score with all elements\n2. Use the softmax function to convert these scores into weights (between 0-1)\n3. Use these weights to perform a weighted sum of the corresponding values to get the output\n\nExample
\n\n# Simplified self-attention implementation example\n\nimport torch\n\nimport torch.nn.functional as F\n\ndef self_attention(query, key, value):\n\n scores = torch.matmul(query, key.transpose(-2, -1)) / (query.size(-1) ** 0.5)\n\n weights = F.softmax(scores, dim=-1)\n\nreturn torch.matmul(weights, value)\n\nAdvantages of Self-Attention
\n\n1. **Global context awareness**: Each position can directly access information from all positions in the sequence\n2. **Position independence**: Does not rely on sequence order, suitable for processing various structured data\n3. **Efficient computation**: Compared to the O(n) complexity of RNNs, self-attention can be computed in parallel\n\n\n\n
Multi-Head Attention
\n\nMulti-head attention is an extension of self-attention that executes the attention mechanism multiple times in parallel and then concatenates the results.\n\nStructural Composition
\n\n1. **Multiple attention heads**: Usually uses 8 or more parallel attention heads\n2. **Linear transformation layers**: Each head has its own Q, K, V transformation matrices\n3. **Concatenation and output**: The outputs of each head are concatenated and passed through a linear layer\n\nAdvantages of Multi-Head Attention
\n\n1. **Capture different relationships**: Each head can learn to focus on relationships in different aspects\n2. **Enhanced representation capability**: Has stronger feature extraction capability than single-head attention\n3. **Stable training**: The combination of multiple heads can reduce the model's dependence on specific patterns\n\nExample
\n\n# Multi-head attention implementation example\n\nclass MultiHeadAttention(nn.Module):\n\ndef __init__ (self, d_model, num_heads):\n\nsuper(). __init__ ()\n\nself.d_model= d_model\n\nself.num_heads= num_heads\n\nself.d_k= d_model // num_heads\n\nself.W_q= nn.Linear(d_model, d_model)\n\nself.W_k= nn.Linear(d_model, d_model)\n\nself.W_v= nn.Linear(d_model, d_model)\n\nself.W_o= nn.Linear(d_model, d_model)\n\ndef forward(self, query, key, value):\n\n batch_size = query.size(0)\n\n# Apply linear transformation and split into heads\n\n Q =self.W_q(query).view(batch_size, -1,self.num_heads,self.d_k)\n\n K =self.W_k(key).view(batch_size, -1,self.num_heads,self.d_k)\n\n V =self.W_v(value).view(batch_size, -1,self.num_heads,self.d_k)\n\n# Compute attention\n\n scores = torch.matmul(Q, K.transpose(-2, -1)) / (self.d_k ** 0.5)\n\n weights = F.softmax(scores, dim=-1)\n\n output = torch.matmul(weights, V)\n\n# Concatenate multi-head outputs and return\n\n output = output.transpose(1,2).contiguous().view(batch_size, -1,self.d_model)\n\nreturn self.W_o(output)\n\n\n\n
Applications of Attention Mechanism in NLP
\n\nThe attention mechanism has become a core component of modern NLP systems, especially in the Transformer architecture.\n\nMain Application Scenarios
\n\n1. **Machine Translation**:\n\n * Classic Seq2Seq with Attention model\n * Allows the model to focus on the most relevant parts of the source sentence when generating each target word\n\n2. **Text Summarization**:\n\n * Identifies key information in the original text through attention weights\n * Generative summarization models use self-attention to capture global relationships in long documents\n\n3. **Question Answering Systems**:\n\n * Cross-attention between questions and documents\n * Helps the model locate text segments relevant to the question\n\n4. **Language Models**:\n\n * GPT series models use masked self-attention\n * Allows each word to attend to all preceding words\n\nPractical Case: Attention in BERT
\n\nBERT (Bidirectional Encoder Representations from Transformers) is a typical representative using the attention mechanism:\n\n1. **Bidirectional self-attention**: Considers both left and right contexts simultaneously\n2. **12/24-layer Transformer**: Stacks multi-head attention layers\n3. **Pre-training tasks**: Learns general representations through masked language model and next sentence prediction tasks\n\nExample
\n\n# Using the HuggingFace Transformers library to call BERT\n\nfrom transformers import BertModel, BertTokenizer\n\ntokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\n\n model = BertModel.from_pretrained('bert-base-uncased')\n\ninputs = tokenizer("Hello, my dog is cute", return_tensors="pt")\n\n outputs = model(**inputs)\n\n# Retrieve attention weights\n\n attention = outputs.attentions# Contains attention weights for each layer\n\n\n\n
Variants and Extensions of Attention Mechanism
\n\n1. Scaled Dot-Product Attention
\n\n* Introduces a scaling factor (βd_k) to prevent softmax saturation\n* High computational efficiency, suitable for large-scale applications\n\n2. Additive Attention
\n\n* Uses a single-layer feed-forward network to compute the compatibility function\n* Suitable for cases where the dimensions of the query and key are different\n\n3. Local Attention
\n\n* Only focuses on a subset of the input, reducing computational complexity\n* Balances global attention and computational efficiency\n\n4. Sparse Attention
\n\n* Only calculates attention weights for certain positions\n* Such as the sliding window attention adopted by Longformer\n\n\n\n
Practical Exercises
\n\nExercise 1: Implement Basic Attention Mechanism
\n\nExample
\n\nimport torch\n\nimport torch.nn as nn\n\nimport torch.nn.functional as F\n\nclass SimpleAttention(nn.Module):\n\ndef __init__ (self, hidden_size):\n\nsuper(SimpleAttention,self). __init__ ()\n\nself.attention= nn.Linear(hidden_size,1)\n\ndef forward(self, encoder_outputs):\n\n# encoder_outputs: [batch_size, seq_len, hidden_size]\n\n attention_scores =self.attention(encoder_outputs).squeeze(2)# [batch_size, seq_len]\n\n attention_weights = F.softmax(attention_scores, dim=1)\n\n context_vector = torch.bmm(attention_weights.unsqueeze(1), encoder_outputs)# [batch_size, 1, hidden_size]\n\nreturn context_vector.squeeze(1), attention_weights\n\nExercise 2: Visualize Attention Weights
\n\nExample
\n\nimport matplotlib.pyplot as plt\n\nimport seaborn as sns\n\ndef plot_attention(attention_weights, source_tokens, target_tokens):\n\n plt.figure(figsize=(10,8))\n\n sns.heatmap(attention_weights,\n\n xticklabels=source_tokens,\n\n yticklabels=target_tokens,\n\n cmap="YlGnBu")\n\n plt.xlabel("Source Tokens")\n\n plt.ylabel("Target Tokens")\n\n plt.title("Attention Weights Visualization")\n\n plt.show()\n\n# Usage Example\n\n source =["The","cat","sat","on","the","mat"]\n\n target =["Le","chat","s'est","assis","sur","le","tapis"]\n\n attention = torch.rand(7,6)# Simulated attention weights\n\n plot_attention(attention,
YouTip