Multi-Head Attention

term_id: multi_head_attention

Category: basic_concepts

Definition

Multi-Head Attention breidt het standaard attention-mechanisme uit door deze meerdere keren parallel uit te voeren met verschillende geleerde lineaire projecties. Dit stelt het model in staat gezamenlijk informatie te benaderen uit verschillende representatieruimtes.

Summary

Een mechanisme in transformermodellen dat het model in staat stelt gelijktijdig informatie uit verschillende representatiesubruimten te benaderen.

Key Concepts

  • Self-Attention (Zelfattention)
  • Lineaire Projecties
  • Concatenatie

Use Cases

  • Natural Language Processing (NLP)
  • Machinevertaling
  • Afbeeldingsclassificatie met Vision Transformers

Code Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import torch.nn as nn

class MultiHeadAttention(nn.Module):
    def __init__(self, d_model, num_heads):
        super().__init__()
        self.num_heads = num_heads
        self.d_k = d_model // num_heads
        self.W_q = nn.Linear(d_model, d_model)
        self.W_k = nn.Linear(d_model, d_model)
        self.W_v = nn.Linear(d_model, d_model)
        self.W_o = nn.Linear(d_model, d_model)

    def forward(self, x):
        # Simplified forward pass logic
        pass