AI术语词典

A comprehensive multilingual AI terminology dictionary

Definition

多头注意力通过并行运行多次标准注意力机制(使用不同的学习到的线性投影)来扩展标准注意力机制。这使得模型能够联合关注来自不同位置的不同表示子空间的信息。

Summary

Transformer模型中的一种机制,允许模型同时关注来自不同表示子空间的信息。

Key Concepts

Use Cases

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