add third chapter

This commit is contained in:
skindhu 2024-10-31 10:37:25 +08:00
parent 5c0646ae8c
commit ce615bd123
5 changed files with 34 additions and 0 deletions

View File

@ -304,3 +304,37 @@ Sum: tensor(1.)
> + **概率解释**Softmax 输出可以解释为“模型对每个类别的信心”,使得输出直观可理解。
> + **与交叉熵的结合**Softmax 与交叉熵损失函数结合效果特别好,可以直接将模型预测的概率分布与真实标签比较,从而更快收敛,效果更好。
现在我们已经计算出了归一化的注意力权重,接下来可以执行图 3.10 所示的最后一步:通过将嵌入后的输入 token x<sup>(i)</sup> 与相应的注意力权重相乘,再将所得向量求和,来计算上下文向量 z<sup>(2)</sup>
<img src="../Image/chapter3/figure3.10.png" width="75%" />
如图 3.10 所示,上下文向量 z<sup>(2) </sup> 是通过所有输入向量的加权和计算得到的。这一过程涉及将每个输入向量与其对应的注意力权重相乘:
```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 中的高亮行所示。现在,我们将扩展该计算,以对所有输入计算注意力权重和上下文向量。
<img src="../Image/chapter3/figure3.11.png" width="75%" />
我们沿用之前的三个步骤(如图 3.12 所示),只是对代码做了一些修改,以计算所有的上下文向量,而不仅仅是第二个上下文向量 z<sup>(2)</sup>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

BIN
Image/image3.12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 249 KiB