diff --git a/Book/3.实现注意力机制.md b/Book/3.实现注意力机制.md index 0255f90..acd129f 100644 --- a/Book/3.实现注意力机制.md +++ b/Book/3.实现注意力机制.md @@ -304,3 +304,37 @@ Sum: tensor(1.) > + **概率解释**:Softmax 输出可以解释为“模型对每个类别的信心”,使得输出直观可理解。 > + **与交叉熵的结合**:Softmax 与交叉熵损失函数结合效果特别好,可以直接将模型预测的概率分布与真实标签比较,从而更快收敛,效果更好。 +现在我们已经计算出了归一化的注意力权重,接下来可以执行图 3.10 所示的最后一步:通过将嵌入后的输入 token x(i) 与相应的注意力权重相乘,再将所得向量求和,来计算上下文向量 z(2)。 + + + +如图 3.10 所示,上下文向量 z(2)  是通过所有输入向量的加权和计算得到的。这一过程涉及将每个输入向量与其对应的注意力权重相乘: + +```python +query = inputs[1] # 2nd input token is the query +context_vec_2 = torch.zeros(query.shape) +for i,x_i in enumerate(inputs): + context_vec_2 += attn_weights_2[i]*x_i +print(context_vec_2) +``` + +结算结果如下: + +``` +tensor([0.4419, 0.6515, 0.5683]) +``` + +在接下来的部分,我们将把串行计算上下文向量的过程优化为并行计算所有输入token的上下文向量。 + + + +### 3.2 为所有输入的 token 计算注意力权重 + +在前一节中,我们计算了第二个输入元素的注意力权重和上下文向量,如图 3.11 中的高亮行所示。现在,我们将扩展该计算,以对所有输入计算注意力权重和上下文向量。 + + + +我们沿用之前的三个步骤(如图 3.12 所示),只是对代码做了一些修改,以计算所有的上下文向量,而不仅仅是第二个上下文向量 z(2)。 + + + diff --git a/Image/chapter3/figure3.10.png b/Image/chapter3/figure3.10.png new file mode 100644 index 0000000..201a367 Binary files /dev/null and b/Image/chapter3/figure3.10.png differ diff --git a/Image/chapter3/figure3.11.png b/Image/chapter3/figure3.11.png new file mode 100644 index 0000000..0320abd Binary files /dev/null and b/Image/chapter3/figure3.11.png differ diff --git a/Image/image3.12.png b/Image/image3.12.png new file mode 100644 index 0000000..d37e415 Binary files /dev/null and b/Image/image3.12.png differ diff --git a/Image/image3.9.png b/Image/image3.9.png deleted file mode 100644 index 78612d6..0000000 Binary files a/Image/image3.9.png and /dev/null differ