|
|
@@ -429,11 +429,11 @@ class PositionWiseFeedForward(nn.Module):
|
|
|
|
|
|
位置编码的核心思想是,为输入序列中的每一个词元嵌入向量,都额外加上一个能代表其绝对位置和相对位置信息的“位置向量”。这个位置向量不是通过学习得到的,而是通过一个固定的数学公式直接计算得出。这样一来,即使两个词元(例如,两个都叫 `agent` 的词元)自身的嵌入是相同的,但由于它们在句子中的位置不同,它们最终输入到 Transformer 模型中的向量就会因为加上了不同的位置编码而变得独一无二。原论文中提出的位置编码使用正弦和余弦函数来生成,其公式如下:
|
|
|
|
|
|
-$$ PE_{(pos,2i)}=\sin\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right) $$,
|
|
|
+$$PE_{(pos,2i)}=\sin\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right),$$
|
|
|
|
|
|
-$$PE_{(pos,2i+1)}=\cos\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)$$,
|
|
|
+$$PE_{(pos,2i+1)}=\cos\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)$$
|
|
|
|
|
|
-其中:
|
|
|
+,其中:
|
|
|
|
|
|
- $pos$ 是词元在序列中的位置(例如,$0$,$1$,$2$,...)
|
|
|
- $i$ 是位置向量中的维度索引(从 $0$ 到 $d_{\text{model}}/2$)
|