列向量写法一般在数学课本上使用
设
那么打分:
\[ s_{ij} = \frac{q_i^\top k_j}{\sqrt{d_k}} \quad (\text{标量}) \]
Softmax 权重:
\[ \alpha_{ij} = \frac{\exp(s_{ij})}{\sum_{t=1}^n \exp(s_{it})} \]
输出:
\[ o_i = \sum_{j=1}^n \alpha_{ij} v_j \quad \in \mathbb{R}^{d_v \times 1} \]
矩阵式:
\[ O = \operatorname{Softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V \]
其中
行向量写法在工程上更常见
设
那么打分:
\[ s_{ij} = \frac{q_i k_j^\top}{\sqrt{d_k}} \quad (\text{标量}) \]
Softmax 权重:
\[ \alpha_{ij} = \frac{\exp(s_{ij})}{\sum_{t=1}^n \exp(s_{it})} \]
输出:
\[ o_i = \sum_{j=1}^n \alpha_{ij} v_j \quad \in \mathbb{R}^{1 \times d_v} \]
矩阵式同样成立:
\[ O = \operatorname{Softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V \]
只是这里的
\(Q = \begin{bmatrix} q_1 \\ q_2 \\ \vdots \\ q_n \end{bmatrix} \in \mathbb{R}^{n \times d_k}\), 每一行是一个 query(行向量)。
列向量写法更贴近数学教科书,点积写作 \(q_i^\top k_j\)。
行向量写法更贴近实际工程框架(NumPy/PyTorch),点积写作 \(q_i k_j^\top\)。
矩阵公式 \(O = \text{Softmax}(QK^\top / \sqrt{d_k})V\) 在两种习惯下都完全一致。