第03章:Scaled Dot-Product Attention——那个√d_k到底在防什么?
论文链接 :Attention Is All You Need (Vaswani et al., NIPS 2017)
本章对应 :Section 3.2.1, Footnote 4
核心困惑
为什么Attention公式里要除以d k \sqrt{d_k} d k ?不除会怎样?
这个d k \sqrt{d_k} d k 看起来不起眼,但它在防一个致命问题。如果你在面试时被问"Scaled Dot-Product Attention的Scaled是什么意思",答不出来就直接挂了。
完整的Attention公式是:
Attention ( Q , K , V ) = softmax ( Q K T d k ) V \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V Attention ( Q , K , V ) = softmax ( d k Q K T ) V
为什么是d k \sqrt{d_k} d k 而不是d k d_k d k 或者d k 2 d_k^2 d k 2 ?这背后有严格的数学推导。
前置知识补给站
1. 向量点积的几何意义
两个向量q q q 和k k k 的点积:
q ⋅ k = ∑ i = 1 d k q i k i = ∥ q ∥ ∥ k ∥ cos θ q \cdot k = \sum_{i=1}^{d_k} q_i k_i = \|q\| \|k\| \cos\theta q ⋅ k = ∑ i = 1 d k q i k i = ∥ q ∥∥ k ∥ cos θ
几何意义 :
点积衡量两个向量的相似度
点积越大,向量越相似(夹角越小)
点积可以是负数(夹角 > 90°)
2. 随机变量的方差
对于随机变量X X X :
Var ( X ) = E [ ( X − E [ X ] ) 2 ] = E [ X 2 ] − ( E [ X ] ) 2 \text{Var}(X) = E[(X - E[X])^2] = E[X^2] - (E[X])^2 Var ( X ) = E [( X − E [ X ] ) 2 ] = E [ X 2 ] − ( E [ X ] ) 2
方差的性质 :
Var ( a X ) = a 2 Var ( X ) \text{Var}(aX) = a^2 \text{Var}(X) Var ( a X ) = a 2 Var ( X )
如果X X X 和Y Y Y 独立:Var ( X + Y ) = Var ( X ) + Var ( Y ) \text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y) Var ( X + Y ) = Var ( X ) + Var ( Y )
3. Softmax函数的饱和区
Softmax函数:
softmax ( x i ) = e x i ∑ j e x j \text{softmax}(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}} softmax ( x i ) = ∑ j e x j e x i
饱和区问题 :
当某个x i x_i x i 远大于其他x j x_j x j 时,softmax ( x i ) ≈ 1 \text{softmax}(x_i) \approx 1 softmax ( x i ) ≈ 1 ,其他位置≈ 0 \approx 0 ≈ 0
此时梯度∂ softmax ∂ x j ≈ 0 \frac{\partial \text{softmax}}{\partial x_j} \approx 0 ∂ x j ∂ softmax ≈ 0 (对于所有j j j )
这叫"饱和",会导致梯度消失
论文精读:为什么需要缩放?
原论文的解释
Section 3.2.1 :
"We call our particular attention 'Scaled Dot-Product Attention'. The input consists of queries and keys of dimension d k d_k d k , and values of dimension d v d_v d v . We compute the dot products of the query with all keys, divide each by d k \sqrt{d_k} d k , and apply a softmax function to obtain the weights on the values."
Footnote 4 (关键):
"We suspect that for large values of d k d_k d k , the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients. To counteract this effect, we scale the dot products by 1 d k \frac{1}{\sqrt{d_k}} d k 1 ."
翻译成人话:
当d k d_k d k 很大时,点积的值会很大
大的点积会让Softmax进入饱和区
饱和区的梯度极小,导致梯度消失
除以d k \sqrt{d_k} d k 可以把点积的值控制在合理范围
但原论文没有证明"为什么是d k \sqrt{d_k} d k " 。我们来严格推导。
第一性原理推导:为什么是√d_k?
推导1:随机向量点积的方差
假设 (理想化数学模型):
q q q 和k k k 是d k d_k d k 维随机向量
q = x W Q q = xW^Q q = x W Q ,k = x W K k = xW^K k = x W K ,其中W Q W^Q W Q 和W K W^K W K 在初始化时独立
每个分量q i , k i q_i, k_i q i , k i 的均值为0,方差为1
q q q 的各分量不相关,k k k 的各分量不相关
注 :实践中这些假设通过LayerNorm近似满足。
目标 :计算点积q ⋅ k q \cdot k q ⋅ k 的方差。
推导 :
首先计算期望:
q ⋅ k = ∑ i = 1 d k q i k i E [ q ⋅ k ] = ∑ i = 1 d k E [ q i k i ] \begin{aligned} q \cdot k &= \sum_{i=1}^{d_k} q_i k_i \\ E[q \cdot k] &= \sum_{i=1}^{d_k} E[q_i k_i] \end{aligned} q ⋅ k E [ q ⋅ k ] = i = 1 ∑ d k q i k i = i = 1 ∑ d k E [ q i k i ]
在初始化时,W Q W^Q W Q 和W K W^K W K 独立初始化,因此对于固定的输入x x x ,q i q_i q i 和k i k_i k i 条件独立:
E [ q i k i ∣ x ] = E [ q i ∣ x ] ⋅ E [ k i ∣ x ] E[q_i k_i | x] = E[q_i | x] \cdot E[k_i | x] E [ q i k i ∣ x ] = E [ q i ∣ x ] ⋅ E [ k i ∣ x ]
由于投影后的向量均值为0(假设),因此:
E [ q ⋅ k ] = 0 E[q \cdot k] = 0 E [ q ⋅ k ] = 0
接下来计算方差:
Var ( q ⋅ k ) = E [ ( q ⋅ k ) 2 ] − ( E [ q ⋅ k ] ) 2 = E [ ( q ⋅ k ) 2 ] = E [ ( ∑ i = 1 d k q i k i ) 2 ] = E [ ∑ i = 1 d k q i 2 k i 2 + ∑ i ≠ j q i k i q j k j ] \begin{aligned} \text{Var}(q \cdot k) &= E[(q \cdot k)^2] - (E[q \cdot k])^2 \\ &= E[(q \cdot k)^2] \\ &= E\left[\left(\sum_{i=1}^{d_k} q_i k_i\right)^2\right] \\ &= E\left[\sum_{i=1}^{d_k} q_i^2 k_i^2 + \sum_{i \neq j} q_i k_i q_j k_j\right] \end{aligned} Var ( q ⋅ k ) = E [( q ⋅ k ) 2 ] − ( E [ q ⋅ k ] ) 2 = E [( q ⋅ k ) 2 ] = E ( i = 1 ∑ d k q i k i ) 2 = E i = 1 ∑ d k q i 2 k i 2 + i = j ∑ q i k i q j k j
对于i ≠ j i \neq j i = j 的交叉项,由于q q q 和k k k 来自不同的投影矩阵:
E [ q i k i q j k j ] = E [ ( q i q j ) ( k i k j ) ] = E [ q i q j ] ⋅ E [ k i k j ] E[q_i k_i q_j k_j] = E[(q_i q_j)(k_i k_j)] = E[q_i q_j] \cdot E[k_i k_j] E [ q i k i q j k j ] = E [( q i q j ) ( k i k j )] = E [ q i q j ] ⋅ E [ k i k j ]
如果q q q 的各分量不相关且均值为0,则E [ q i q j ] = Cov ( q i , q j ) + E [ q i ] E [ q j ] = 0 E[q_i q_j] = \text{Cov}(q_i, q_j) + E[q_i]E[q_j] = 0 E [ q i q j ] = Cov ( q i , q j ) + E [ q i ] E [ q j ] = 0 (i ≠ j i \neq j i = j )。同理E [ k i k j ] = 0 E[k_i k_j] = 0 E [ k i k j ] = 0 。因此交叉项期望为0。
对于对角项:
∑ i = 1 d k E [ q i 2 k i 2 ] = ∑ i = 1 d k E [ q i 2 ] E [ k i 2 ] ( q 和 k 独立) = ∑ i = 1 d k Var ( q i ) ⋅ Var ( k i ) (均值为0) = ∑ i = 1 d k 1 ⋅ 1 = d k \begin{aligned} \sum_{i=1}^{d_k} E[q_i^2 k_i^2] &= \sum_{i=1}^{d_k} E[q_i^2] E[k_i^2] \quad \text{(}q \text{和} k \text{独立)} \\ &= \sum_{i=1}^{d_k} \text{Var}(q_i) \cdot \text{Var}(k_i) \quad \text{(均值为0)} \\ &= \sum_{i=1}^{d_k} 1 \cdot 1 \\ &= d_k \end{aligned} i = 1 ∑ d k E [ q i 2 k i 2 ] = i = 1 ∑ d k E [ q i 2 ] E [ k i 2 ] ( q 和 k 独立 ) = i = 1 ∑ d k Var ( q i ) ⋅ Var ( k i ) ( 均值为 0) = i = 1 ∑ d k 1 ⋅ 1 = d k
因此:
Var ( q ⋅ k ) = d k \text{Var}(q \cdot k) = d_k Var ( q ⋅ k ) = d k
结论 :点积q ⋅ k q \cdot k q ⋅ k 的方差是d k d_k d k 。
标准差 :Var ( q ⋅ k ) = d k \sqrt{\text{Var}(q \cdot k)} = \sqrt{d_k} Var ( q ⋅ k ) = d k
归一化 :如果我们除以d k \sqrt{d_k} d k :
Var ( q ⋅ k d k ) = 1 d k Var ( q ⋅ k ) = 1 d k ⋅ d k = 1 \text{Var}\left(\frac{q \cdot k}{\sqrt{d_k}}\right) = \frac{1}{d_k} \text{Var}(q \cdot k) = \frac{1}{d_k} \cdot d_k = 1 Var ( d k q ⋅ k ) = d k 1 Var ( q ⋅ k ) = d k 1 ⋅ d k = 1
这就是为什么是d k \sqrt{d_k} d k :它把点积的方差归一化到1,无论d k d_k d k 多大。
推导2:Softmax饱和区的数值演示
问题 :为什么点积过大会导致Softmax饱和?
Softmax的梯度 :
∂ softmax ( x i ) ∂ x j = { softmax ( x i ) ( 1 − softmax ( x i ) ) if i = j − softmax ( x i ) softmax ( x j ) if i ≠ j \frac{\partial \text{softmax}(x_i)}{\partial x_j} = \begin{cases} \text{softmax}(x_i)(1 - \text{softmax}(x_i)) & \text{if } i = j \\ -\text{softmax}(x_i) \text{softmax}(x_j) & \text{if } i \neq j \end{cases} ∂ x j ∂ softmax ( x i ) = { softmax ( x i ) ( 1 − softmax ( x i )) − softmax ( x i ) softmax ( x j ) if i = j if i = j
数值示例 :
假设有3个位置,点积为[ x 1 , x 2 , x 3 ] [x_1, x_2, x_3] [ x 1 , x 2 , x 3 ] 。
情况1:点积适中(d k = 64 d_k=64 d k = 64 ,已缩放)
x = [ 1.0 , 0.5 , − 0.5 ] x = [1.0, 0.5, -0.5] x = [ 1.0 , 0.5 , − 0.5 ]
softmax ( x ) = [ 0.506 , 0.307 , 0.187 ] \text{softmax}(x) = [0.506, 0.307, 0.187] softmax ( x ) = [ 0.506 , 0.307 , 0.187 ]
∂ softmax ( x 1 ) ∂ x 1 = 0.506 × ( 1 − 0.506 ) = 0.250 \frac{\partial \text{softmax}(x_1)}{\partial x_1} = 0.506 \times (1 - 0.506) = 0.250 ∂ x 1 ∂ softmax ( x 1 ) = 0.506 × ( 1 − 0.506 ) = 0.250
梯度正常,可以学习。
情况2:点积过大(d k = 64 d_k=64 d k = 64 ,未缩放)
x = [ 8.0 , 4.0 , − 4.0 ] x = [8.0, 4.0, -4.0] x = [ 8.0 , 4.0 , − 4.0 ]
softmax ( x ) = [ 0.9997 , 0.0003 , 0.0000 ] \text{softmax}(x) = [0.9997, 0.0003, 0.0000] softmax ( x ) = [ 0.9997 , 0.0003 , 0.0000 ]
∂ softmax ( x 1 ) ∂ x 1 = 0.9997 × ( 1 − 0.9997 ) = 0.0003 \frac{\partial \text{softmax}(x_1)}{\partial x_1} = 0.9997 \times (1 - 0.9997) = 0.0003 ∂ x 1 ∂ softmax ( x 1 ) = 0.9997 × ( 1 − 0.9997 ) = 0.0003
梯度几乎为0,无法学习。
可视化对比 :
d k d_k d k 标准差 3 σ 3\sigma 3 σ 范围Softmax状态 梯度 64(已缩放) 1 [-3, 3] ✅ 正常 ~0.25 64(未缩放) 8 [-24, 24] ❌ 饱和 ~0.0003 512(未缩放) 22.6 [-68, 68] ❌ 极饱和 ~0.0
推导3:为什么不是其他缩放因子?
候选方案 :
除以 d k d_k d k :Var ( q ⋅ k / d k ) = 1 / d k \text{Var}(q \cdot k / d_k) = 1/d_k Var ( q ⋅ k / d k ) = 1/ d k ,方差随维度增加而消失,分布太集中。
除以 d k \sqrt{d_k} d k :Var ( q ⋅ k / d k ) = 1 \text{Var}(q \cdot k / \sqrt{d_k}) = 1 Var ( q ⋅ k / d k ) = 1 ,方差刚好稳定。
除以 log d k \log d_k log d k :Var ( q ⋅ k / log d k ) = d k / ( log d k ) 2 \text{Var}(q \cdot k / \log d_k) = d_k / (\log d_k)^2 Var ( q ⋅ k / log d k ) = d k / ( log d k ) 2 ,方差仍然随 d k d_k d k 快速增长。
只有d k \sqrt{d_k} d k 能把方差归一化到1 。
Scaled vs Unscaled的实验对比
原论文没有直接对比Scaled和Unscaled的实验(因为缩放的必要性可以通过数学推导证明)。但Table 3 row (B)的消融实验说明:在缩放了的情况下,d k d_k d k 的维度选择仍然重要。
原论文Table 3 row (B) :
d k = 16 d_k=16 d k = 16 : PPL 5.16, BLEU 25.1
d k = 32 d_k=32 d k = 32 : PPL 5.01, BLEU 25.4
d k = 64 d_k=64 d k = 64 (base): PPL 4.92, BLEU 25.8
解读 :
d k d_k d k 越大,效果越好(在缩放了的情况下)。
这说明更大的d k d_k d k 能提供更丰富的表示能力。
但如果不缩放,大d k d_k d k 会导致Softmax饱和,效果反而变差。
Dot-Product Attention vs Additive Attention
原论文提到了两种Attention机制:
1. Dot-Product Attention(原论文使用)
score ( q , k ) = q ⋅ k \text{score}(q, k) = q \cdot k score ( q , k ) = q ⋅ k
优点 :计算高效(矩阵乘法);并行性好。
缺点 :需要缩放(否则方差随d k d_k d k 增长)。
2. Additive Attention(Bahdanau et al., 2015)
score ( q , k ) = v T tanh ( W q q + W k k ) \text{score}(q, k) = v^T \tanh(W_q q + W_k k) score ( q , k ) = v T tanh ( W q q + W k k )
优点 :不需要缩放(tanh自带归一化);理论表达力强。
缺点 :计算慢(两次矩阵乘法 + tanh);参数量大。
结论 :缩放后的Dot-Product快且效果相当,是工业界的首选。
完整的Scaled Dot-Product Attention流程
计算相似度 :Q K T QK^T Q K T ,得到 n × m n \times m n × m 的score矩阵。
缩放 :除以 d k \sqrt{d_k} d k ,把方差归一化到 1。
Mask (可选):先Scale后Mask是工程习惯(Mask值通常为 − 1 0 9 -10^9 − 1 0 9 )。
归一化 :Softmax,把score转为概率分布。
加权求和 :用概率加权 V V V ,得到输出。
2026年的批判性视角
缩放因子的理论假设 :LayerNorm 是保证 q , k q, k q , k 符合均值0、方差1假设的关键。
其他缩放方案 :如 T5 使用的可学习缩放,或 ALiBi 使用的位置偏置。
Softmax 替代品 :ReLU Attention 或 Linear Attention。
确定性 vs 统计性 :相比 BatchNorm,除以 d k \sqrt{d_k} d k 是确定性的,在推理时更稳定。
面试追问清单
为什么Attention要除以d k \sqrt{d_k} d k ? (提示:点积方差推导)
如果不除以d k \sqrt{d_k} d k 会怎样? (提示:Softmax饱和、梯度消失)
证明:若 q i , k i ∼ ( 0 , 1 ) q_i, k_i \sim (0, 1) q i , k i ∼ ( 0 , 1 ) ,则 Var ( q ⋅ k ) = d k \text{Var}(q \cdot k) = d_k Var ( q ⋅ k ) = d k 。 (提示:展开并利用独立性)
Dot-Product 相比 Additive Attention 的优势? (提示:计算效率)
LayerNorm 在这里起到了什么作用? (提示:维持分布假设)
下一章预告 :第04章将深入拆解Multi-Head Attention,回答"八个头,八个视角,还是八份低秩分解?"