AI 1 分钟阅读
Attention 机制的数学直觉
Self-Attention 公式
Transformer 的核心是 Scaled Dot-Product Attention:
其中:
- — Query 矩阵
- — Key 矩阵
- — Value 矩阵
- — Key 的维度
为什么需要缩放
当 较大时, 的方差会随维度增长,导致 softmax 进入梯度极小的饱和区。除以 起到归一化作用。
代码实现
import torch
import torch.nn.functional as F
def self_attention(Q, K, V):
d_k = Q.size(-1)
scores = torch.matmul(Q, K.transpose(-2, -1)) / (d_k ** 0.5)
attn = F.softmax(scores, dim=-1)
return torch.matmul(attn, V)
这是一个简单的示例,展示了 Attention 的核心计算过程。