研读论文《Attention Is All You Need》(12)

原文 23

3.5 Positional Encoding

Since our model contains no recurrence and no convolution, in order for the model to make use of the order of the sequence, we must inject some information about the relative or absolute position of the tokens in the sequence. To this end, we add “positional encodings” to the input embeddings at the bottoms of the encoder and decoder stacks. The positional encodings have the same dimension d m o d e l d_{model} dmodel as the embeddings, so that the two can be summed. There are many choices of positional encodings, learned and fixed [9].

翻译

3.5 位置编码

由于我们的模型不包含循环结构或卷积操作,为了让模型能够利用序列的顺序,我们必须注入一些信息,这些信息关乎序列中各个标记的相对位置或绝对位置。为此,我们在编码器和解码器堆栈底部的输入嵌入中添加了“位置编码”。这些位置编码的维度 d m o d e l d_{model} dmodel 与嵌入向量的维度相同,因此二者可以直接相加。位置编码有多种选择,可以是习得的,也可以是固定的[9]。

重点句子解析

  1. Since our model contains no recurrence and no convolution, in order for the model to make use of the order of the sequence, we must inject some information about the relative or absolute position of the tokens in the sequence.

【解析】

这是“原因状语从句+目的状语(不定式的复合结构)+主句”构成的复合句。

主句是“we must inject some information about…”。 其主干是we must inject some information. 句中的must inject是“助动词+动词原形”构成的谓语,谓语前后分别是主语和宾语。介词短语about…做后置定语,修饰information。其中,the relative or absolute做定语,修饰position;of the tokens做后置定语,也是修饰position;句尾的介词短语in the sequence修饰the tokens,做后置定语。

句首是since引导的原因状语从句,从句的主干“our model contains no recurrence and no convolution”属于主谓宾结构,其中,contains是谓语动词,前后分别是主语和宾语,只不过宾语是由and连接的两个并列名词短语,即:no recurrence and no convolution. 实际上,我们也可以把这个从句改写为:Since our model doesn’t contain any recurrence or convolution.

中间部分的“in order for the model to make use of the order of the sequence”属于不定式的复合结构做目的状语,我们可以把它概括为“in order for sb. to do sth.(为了让某人能够做某事)”。其中的the model就相当于sb.,是不定式动作的发出者(逻辑主语);不定式to make use of the order of the sequence就相当于to do sth, 其中的make use of是固定短语,意为“利用”;the order of the sequence表示“序列的顺序”。of the sequence是后置定语,修饰the order。

【参考翻译】

由于我们的模型不包含循环结构或卷积操作,为了让模型能够利用序列的顺序,我们必须注入一些信息,这些信息关乎序列中这些标记的相对位置或绝对位置。

原文 24

In this work, we use sine and cosine functions of different frequencies:
P E ( p o s , 2 i ) = sin ⁡ ( p o s / 10000 2 i / d m o d e l ) P E p o s , 2 i + 1 = cos ⁡ ( p o s / 10000 2 i / d m o d e l ) \begin{split} PE_{(pos,2i)}&=\sin(pos/10000^{2i/d_{model}}) \\PE_{pos,2i+1}&=\cos(pos/10000^{2i/d_{model}}) \end{split} PE(pos,2i)PEpos,2i+1=sin(pos/100002i/dmodel)=cos(pos/100002i/dmodel)
where p o s pos pos is the position and i i i is the dimension. That is, each dimension of the positional encoding corresponds to a sinusoid. The wavelengths form a geometric progression from 2 π 2π 2π to 10000 ⋅ 2 π 10000⋅2π 100002π. We chose this function because we hypothesized it would allow the model to easily learn to attend by relative positions, since for any fixed offset k , P E p o s + k k, PE_{pos+k} k,PEpos+k can be represented as a linear function of P E p o s P E_{pos} PEpos.

翻译

在这项工作中,我们采用不同频率的正弦与余弦函数:
P E ( p o s , 2 i ) = sin ⁡ ( p o s / 10000 2 i / d m o d e l ) P E p o s , 2 i + 1 = cos ⁡ ( p o s / 10000 2 i / d m o d e l ) \begin{split} PE_{(pos,2i)}&=\sin(pos/10000^{2i/d_{model}}) \\PE_{pos,2i+1}&=\cos(pos/10000^{2i/d_{model}}) \end{split} PE(pos,2i)PEpos,2i+1=sin(pos/100002i/dmodel)=cos(pos/100002i/dmodel)
其中, p o s pos pos 表示位置, i i i 表示维度。也就是说,位置编码的每个维度都对应一个正弦曲线。其波长构成从 2 π 2π 2π 10000 ⋅ 2 π 10000⋅2π 100002π 的几何级数。我们选择该函数是因为我们假设,模型可以借此轻松学习基于相对位置的注意力机制——对于任意固定偏移量 k k k P E p o s + k PE_{pos+k} PEpos+k 都可以表示为 P E p o s PE_{pos} PEpos 的线性函数。

重点句子解析

  1. We chose this function because we hypothesized it would allow the model to easily learn to attend by relative positions, since for any fixed offset k k k, P E p o s + k PE_{pos+k} PEpos+k can be represented as a linear function of P E p o s PE_{pos} PEpos.

【解析】

句子的结构是:主句+原因状语从句(because…)+原因状语从句(since…)。具体来说,We chose this function是主句,后边是because引导的原因状语从句,然后是since引导的另一个原因状语从句。其中,because从句本身又是包含了宾语从句的一个复合句,we hypothesized后边是省略了引导词that的宾语从句(that引导宾语从句时,不充当任何语法成分,常常可以省略)。宾语从句使用了allow sb./sth. to do (sth.)的结构,“attend by relative positions”表示“基于相对位置进行注意力分配”。attend的本意是“处理,照料”,此处可以活译为“进行注意力分配”;by的本意是“通过,凭借”,此处可以活译为“基于”。在since引导的原因状语从句中,“for any fixed offset k”是状语,引出针对的对象。其中for表示“对于”,offset表示“偏移量”; P E p o s + k PE_{pos+k} PEpos+k can be represented as…是从句的主体, 其中“be represented as…”意为“(被)表示为”;a linear function of P Epos中的介词短语“of P E p o s PE_{pos} PEpos”是后置定语,修饰a linear function。

【参考翻译】

我们选择该函数是因为我们假设,模型可以借此轻松学习基于相对位置的注意力机制——对于任意固定偏移量 k k k P E p o s + k PE_{pos+k} PEpos+k 都可以表示为 P E p o s PE_{pos} PEpos 的线性函数。

原文 25

We also experimented with using learned positional embeddings [9] instead, and found that the two versions produced nearly identical results (see Table 3 row (E)). We chose the sinusoidal version because it may allow the model to extrapolate to sequence lengths longer than the ones encountered during training.

翻译

我们还尝试了使用可习得的位置嵌入(learned positional embeddings)[9]作为替代方案,但发现这两种方法产生的结果几乎相同(参见表3第(E)行)。我们最终选择了正弦函数版本的位置编码,因为这种编码方式有可能使模型具备外推能力,以此类推地应用于训练时未曾遇到的更长序列。

在这里插入图片描述

### 关于Seq2Seq模型的学术论文 Seq2Seq(Sequence-to-Sequence)模型是一种用于处理序列数据的强大架构,广泛应用于自然语言处理领域中的各种任务,如机器翻译、文本摘要生成等。这类模型通常由编码器和解码器两部分组成,能够有效地捕捉输入序列与输出序列之间的依赖关系。 #### Seq2Seq模型的经典文献 一篇经典的Seq2Seq模型介绍文章是由Sutskever等人提出的神经网络翻译方法[^1]。在这篇文章中,作者们首次引入了基于循环神经网络(RNN)构建的编码器-解码器框架来解决机器翻译问题,并展示了这种方法相较于传统统计方法的优势所在。 随着技术的发展,后续的研究者不断对该结构进行了优化改进: - **注意力机制的应用**:Bahdanau et al. 提出了加入Attention Mechanism 的变体形式,使得模型可以更加聚焦于源端的重要位置从而提高性能表现。 - **Transformer 架构革新**:Vaswani et al. 发布了名为《Attention Is All You Need》的工作,在这篇论文里彻底摒弃掉了RNN/CNN组件而完全依靠自注意层搭建起了全新的Encoder-Decoder体系——即著名的Transformers,这标志着NLP进入了新时代。 对于希望深入了解Seq2Seq及其衍生版本的朋友来说,上述提及的一些开创性和里程碑式的成果是非常值得研读的对象。 ```python import torch.nn as nn class Encoder(nn.Module): def __init__(self, input_dim, emb_dim, hid_dim, n_layers, dropout): super().__init__() self.hid_dim = hid_dim self.embedding = nn.Embedding(input_dim, emb_dim) self.rnn = nn.LSTM(emb_dim, hid_dim, num_layers=n_layers, bidirectional=True, dropout=dropout) def forward(self, src): embedded = self.dropout(self.embedding(src)) outputs, (hidden, cell) = self.rnn(embedded) return hidden, cell class Decoder(nn.Module): def __init__(self, output_dim, emb_dim, hid_dim, n_layers, dropout): super().__init__() self.output_dim = output_dim self.hid_dim = hid_dim * 2 self.embedding = nn.Embedding(output_dim, emb_dim) self.rnn = nn.LSTM(emb_dim + hid_dim*2 , hid_dim*2, num_layers=n_layers, dropout=dropout) self.fc_out = nn.Linear(hid_dim * 2, output_dim) def forward(self, trg_token, hidden, context_vector): ... ``` 此代码片段展示了一个简单的双向LSTM作为编码器以及带有上下文向量连接方式的标准解码器实现方案的一部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CS创新实验室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值