Attention Is All You Need
Vaswani A , Shazeer N , Parmar N ,et al.Attention Is All You Need[J].arXiv, 2017.DOI:10.48550/arXiv.1706.03762.
注意力就是你所需要的
Abstract
The dominant sequence transduction models are based on complex recurrent or convolutional neural networks in an encoder-decoder configuration. The best performing models also connect the encoder and decoder through an attention mechanism. We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train.
Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task, improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training costs of the best models from the literature. We show that the Transformer generalizes well to other tasks by applying it successfully to English constituency parsing both with large and limited training data.
主流的序列转换模型普遍基于复杂的循环神经网络或卷积神经网络,并采用编码器-解码器架构。性能最优异的模型通常还通过注意力机制连接编码器与解码器。我们提出了一种全新的简单网络架构——Transformer,该架构完全基于注意力机制,彻底摒弃了循环与卷积结构。在两个机器翻译任务上的实验表明,该模型在翻译质量上表现更优,同时具有更强的并行化能力,并且所需训练时间显著减少。
我们的模型在 WMT 2014 英德翻译任务中取得了 28.4 的 BLEU 分数,相比现有最佳结果(包括集成模型)提升了超过 2 个 BLEU 分数。在 WMT 2014 英法翻译任务中,我们的模型在 8 块 GPU 上训练 3.5 天后,建立了一个新的单模型最优 BLEU 分数——41.8,其训练成本仅为文献中最佳模型所需成本的一小部分。我们还通过将该模型成功应用于英语成分句法分析任务(包括大量训练数据和有限训练数据两种场景),证明了 Transformer 能够很好地泛化至其他任务。
Introduction
Recurrent neural networks, long short-term memory and gated recurrent neural networks in particular, have been firmly established as state of the art approaches in sequence modeling and transduction problems such as language modeling and machine translation. Numerous efforts have since continued to push the boundaries of recurrent language models and encoder-decoder architectures.
Recurrent models typically factor computation along the symbol positions of the input and output sequences. Aligning the positions to steps in computation time, they generate a sequence of hidden states
Attention mechanisms have become an integral part of compelling sequence modeling and transduction models in various tasks, allowing modeling of dependencies without regard to their distance in the input or output sequences. In all but a few cases, however, such attention mechanisms are used in conjunction with a recurrent network.
In this work we propose the Transformer, a model architecture eschewing recurrence and instead relying entirely on an attention mechanism to draw global dependencies between input and output. The Transformer allows for significantly more parallelization and can reach a new state of the art in translation quality after being trained for as little as twelve hours on eight P100 GPUs.
循环神经网络(RNN),尤其是长短期记忆网络(LSTM)和门控循环神经网络,已被牢固确立为序列建模与转换任务(如语言建模和机器翻译)中的主流SOTA方法。此后,众多研究不断推动循环语言模型及编码器-解码器架构的发展边界。
循环模型通常沿输入与输出序列的符号位置进行逐步计算。通过将位置与计算时间步骤对齐,它们以前一隐藏状态
注意力机制已成为各类任务中构建强大序列建模与转换模型的关键组成部分,它允许对依赖关系进行建模而无需考虑其在输入或输出序列中的距离。然而,除极少数情况外,此类注意力机制均与循环网络结合使用。
本工作中,我们提出了 Transformer——一种摒弃循环结构、完全依赖注意力机制来捕捉输入与输出之间全局依赖关系的模型架构。Transformer 能够实现远高于以往的并行化程度,在仅使用 8 个 P100 GPU 训练十二小时后,便能在翻译质量上达到新的先进水平。
Background
The goal of reducing sequential computation also forms the foundation of the Extended Neural GPU, ByteNet and ConvS2S, all of which use convolutional neural networks as basic building block, computing hidden representations in parallel for all input and output positions. In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions, linearly for ConvS2S and logarithmically for ByteNet. This makes it more difficult to learn dependencies between distant positions. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention as described in section 3.2.
Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence. Self-attention has been used successfully in a variety of tasks including reading comprehension, abstractive summarization, textual entailment and learning task-independent sentence representations.
End-to-end memory networks are based on a recurrent attention mechanism instead of sequence-aligned recurrence and have been shown to perform well on simple-language question answering and language modeling tasks.
To the best of our knowledge, however, the Transformer is the first transduction model relying entirely on self-attention to compute representations of its input and output without using sequence-aligned RNNs or convolution. In the following sections, we will describe the Transformer, motivate self-attention and discuss its advantages over models.
降低顺序计算的需求同样构成了扩展神经GPUByteNet 与 ConvS2S 等模型的基础,这些模型均以卷积神经网络为基本构建模块,能够并行计算所有输入及输出位置的隐藏表示。在这些模型中,关联两个任意输入或输出位置的信号所需操作数,会随位置间距离的增加而增长:ConvS2S为线性增长,ByteNet为对数增长。这使得学习远距离位置间的依赖关系变得更加困难。而在Transformer中,这一操作数被降低至常数级别,尽管这是以对注意力加权位置进行平均而降低有效分辨率为代价的——我们通过第3.2节中描述的多头注意力机制来抵消这一影响。
自注意力(有时称为内部注意力)是一种注意力机制,它通过关联单个序列中不同位置的信息来计算该序列的表示。自注意力已成功应用于多种任务,包括阅读理解、抽象摘要、文本蕴含以及学习与任务无关的句子表示。
端到端记忆网络基于循环注意力机制而非按序列顺序的循环,并已被证明在简单语言问答和语言建模任务中表现良好。
然而,据我们所知,Transformer是首个完全依赖自注意力来计算其输入与输出表示,而不使用序列对齐的RNN或卷积的转换模型。在接下来的章节中,我们将描述Transformer的架构,阐述自注意力的原理,并讨论其相较于其他模型的优势。
Model Architecture
Most competitive neural sequence transduction models have an encoder-decoder structure. Here, the encoder maps an input sequence of symbol representations
The Transformer follows this overall architecture using stacked self-attention and point-wise, fully connected layers for both the encoder and decoder, shown in the left and right halves of Figure 1, respectively.
大多数具有竞争力的神经序列转换模型都采用编码器-解码器结构。在此结构中,编码器将输入符号表示序列
Transformer 遵循这一整体架构,在编码器和解码器中均使用堆叠的自注意力层和逐点全连接层。
3.1 Encoder and Decoder Stacks
Encoder: The encoder is composed of a stack of
Decoder: The decoder is also composed of a stack of
3.1 编码器与解码器堆栈
编码器: 编码器由
解码器: 解码器同样由
3.2 Attention
An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key.
3.2 注意力机制
注意力函数可描述为将一个查询(query)和一组键值对(key-value pairs)映射为一个输出的过程,其中查询、键、值及输出均为向量。输出通过对值进行加权求和计算得到,而分配给每个值的权重则由查询与对应键的兼容性函数计算得出。
3.2.1 Scaled Dot-Product Attention
We call our particular attention "Scaled Dot-Product Attention" (Figure 2). The input consists of queries and keys of dimension
In practice, we compute the attention function on a set of queries simultaneously, packed together into a matrix
3.2.1 缩放点积注意力
我们将我们特定的注意力机制称为“缩放点积注意力”。输入由维度为
在实践中,我们同时计算一组查询的注意力函数,将它们打包成一个矩阵
The two most commonly used attention functions are additive attention , and dot-product (multiplicative) attention. Dot-product attention is identical to our algorithm, except for the scaling factor of
While for small values of
最常用的两种注意力函数是加性注意力和点积(乘法)注意力。点积注意力与我们的算法相同,除了没有
当
3.2.2 Multi-Head Attention
Instead of performing a single attention function with
Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions. With a single attention head, averaging inhibits this.
3.2.2 多头注意力
与使用
多头注意力使得模型能够同时关注来自不同位置的不同表示子空间的信息。如果仅使用单一的注意力头,其平均操作会抑制这种能力。
Where the projections are parameter matrices
In this work we employ
其中,投影为参数矩阵
在本工作中,我们采用
3.2.3 Applications of Attention in our Model
The Transformer uses multi-head attention in three different ways:
- In "encoder-decoder attention" layers, the queries come from the previous decoder layer, and the memory keys and values come from the output of the encoder. This allows every position in the decoder to attend over all positions in the input sequence. This mimics the typical encoder-decoder attention mechanisms in sequence-to-sequence models.
- The encoder contains self-attention layers. In a self-attention layer all of the keys, values and queries come from the same place, in this case, the output of the previous layer in the encoder. Each position in the encoder can attend to all positions in the previous layer of the encoder.
- Similarly, self-attention layers in the decoder allow each position in the decoder to attend to all positions in the decoder up to and including that position. We need to prevent leftward information flow in the decoder to preserve the auto-regressive property. We implement this inside of scaled dot-product attention by masking out (setting to
) all values in the input of the softmax which correspond to illegal connections.
3.2.3 注意力机制在我们模型中的应用
Transformer 在三个不同方面使用了多头注意力机制:
- 在 “编码器-解码器注意力” 层 中,查询来自前一个解码器层,而记忆键和值则来自编码器的输出。这使得解码器中的每个位置都能够关注输入序列中的所有位置。这模仿了序列到序列模型中典型的编码器-解码器注意力机制。
- 编码器包含自注意力层。在自注意力层中,所有的键、值和查询都来自同一个地方,即编码器中前一层的输出。编码器中的每个位置都可以关注编码器前一层中的所有位置。
- 类似地,解码器中的自注意力层 允许解码器中的每个位置关注解码器内包括该位置在内的所有先前位置。为了保持自回归特性,我们需要阻止解码器中的信息向左流动(即关注未来的位置)。我们在缩放点积注意力内部通过掩码来实现这一点:将所有在 softmax 输入中对应非法连接的值掩盖掉(设置为
)。
3.3 Position-wise Feed-Forward Networks
In addition to attention sub-layers, each of the layers in our encoder and decoder contains a fully connected feed-forward network, which is applied to each position separately and identically. This consists of two linear transformations with a ReLU activation in between.
3.3 逐位置前馈网络
除了注意力子层之外,我们的编码器和解码器中的每一层还包含一个全连接前馈网络。该网络分别并相同地应用于每个位置。它由两个线性变换组成,中间通过一个 ReLU 激活函数连接。
While the linear transformations are the same across different positions, they use different parameters from layer to layer. Another way of describing this is as two convolutions with kernel size 1. The dimensionality of input and output is
尽管这些线性变换在不同位置上是相同的,但它们在层与层之间使用不同的参数。描述此结构的另一种方式是将其视为两个核大小为 1 的卷积。输入和输出的维度为
3.4 Embeddings and Softmax
Similarly to other sequence transduction models, we use learned embeddings to convert the input tokens and output tokens to vectors of dimension
3.4 嵌入和Softmax
与其他序列转换模型类似,我们使用学习到的嵌入将输入词元和输出词元转换为维度为
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
In this work, we use sine and cosine functions of different frequencies:
3.5 位置编码
由于我们的模型不含循环结构,也没有卷积操作,为了让模型能够利用序列的顺序信息,我们必须注入关于序列中词元相对位置或绝对位置的一些信息。为此,我们在编码器栈和解码器栈底部的输入嵌入中添加“位置编码”。位置编码与嵌入具有相同的维度
在本工作中,我们使用不同频率的正弦和余弦函数:
where
We also experimented with using learned positional embeddings instead, and found that the two versions produced nearly identical results. We chose the sinusoidal version because it may allow the model to extrapolate to sequence lengths longer than the ones encountered during training.
其中
我们还尝试使用可学习的位置嵌入,并发现两种版本产生的结果几乎相同。我们选择正弦波版本,是因为它可能允许模型外推到比训练时遇到的序列长度更长的序列。
Why Self-Attention
In this section we compare various aspects of self-attention layers to the recurrent and convolutional layers commonly used for mapping one variable-length sequence of symbol representations
One is the total computational complexity per layer. Another is the amount of computation that can be parallelized, as measured by the minimum number of sequential operations required.
The third is the path length between long-range dependencies in the network. Learning long-range dependencies is a key challenge in many sequence transduction tasks. One key factor affecting the ability to learn such dependencies is the length of the paths forward and backward signals have to traverse in the network. The shorter these paths between any combination of positions in the input and output sequences, the easier it is to learn long-range dependencies. Hence we also compare the maximum path length between any two input and output positions in networks composed of the different layer types.
在本节中,我们将自注意力层与常用于将变长符号表示序列
首先是每层的总计算复杂度。其次是可以并行化的计算量,以所需的最小顺序操作数来衡量。
第三是网络中长距离依赖之间的路径长度。学习长距离依赖是许多序列转换任务中的关键挑战。影响此类依赖学习能力的一个关键因素是前向和后向信号在网络中必须 travers 的路径长度。输入和输出序列中任意位置组合之间的路径越短,学习长距离依赖就越容易。因此,我们还比较了由不同层类型组成的网络中任意两个输入和输出位置之间的最大路径长度。
表1:不同层类型的最大路径长度、每层复杂度及最小顺序操作数。其中
| 层类型 | 每层复杂度 | 顺序操作数 | 最大路径长度 |
|---|---|---|---|
| 自注意力 | |||
| 递归 | |||
| 卷积 | |||
| 自注意力(受限) |
As noted in Table 1, a self-attention layer connects all positions with a constant number of sequentially executed operations, whereas a recurrent layer requires
A single convolutional layer with kernel width
As side benefit, self-attention could yield more interpretable models. We inspect attention distributions from our models and present and discuss examples in the appendix. Not only do individual attention heads clearly learn to perform different tasks, many appear to exhibit behavior related to the syntactic and semantic structure of the sentences.
如表 1 所示,自注意力层通过常数次顺序执行的操作即可连接所有位置,而循环层则需要
卷积层若卷积核宽度
作为附加好处,自注意力还能产生更具可解释性的模型。我们检查了模型中的注意力分布,并在附录中展示并讨论了一些示例。不仅各个注意力头明显学会了执行不同的任务,其中许多还表现出与句子句法结构和语义结构相关的行为。
Training
5.1 Training Data and Batching
We trained on the standard WMT 2014 English-German dataset consisting of about 4.5 million sentence pairs. Sentences were encoded using byte-pair encoding, which has a shared source-target vocabulary of about 37000 tokens. For English-French, we used the significantly larger WMT 2014 English-French dataset consisting of 36M sentences and split tokens into a 32000 word-piece vocabulary. Sentence pairs were batched together by approximate sequence length. Each training batch contained a set of sentence pairs containing approximately 25000 source tokens and 25000 target tokens.
5.1 训练数据与批处理
我们在标准的 WMT 2014 英德数据集上进行训练,该数据集包含约 450 万个句子对。句子采用字节对编码,共享的源-目标词汇表包含约 37000 个词元。对于英法任务,我们使用了规模更大的 WMT 2014 英法数据集,包含 3600 万个句子,并将词元切分为 32000 个词片段(word-piece)词汇表。句子对按近似序列长度进行批处理。每个训练批次包含一组句子对,其中源语言和目标语言各约有 25000 个词元。
5.2 Hardware and Schedule
We trained our models on one machine with 8 NVIDIA P100 GPUs. For our base models using the hyperparameters described throughout the paper, each training step took about 0.4 seconds. We trained the base models for a total of 100,000 steps or 12 hours. For our big models,(described on the bottom line of table 3), step time was 1.0 seconds. The big models were trained for 300,000 steps (3.5 days).
5.2 硬件与训练计划
我们在一台配备 8 块 NVIDIA P100 GPU 的机器上训练模型。对于采用本文所述超参数的基础模型,每步训练时间约为 0.4 秒。我们对基础模型总共进行了 100000 步(即 12 小时)的训练。对于大模型(见表 3 底行),每步训练时间为 1.0 秒。大模型训练了 300000 步(3.5 天)。
5.3 Optimizer
We used the Adam optimizer with
5.3 优化器
我们使用 Adam 优化器,其中
This corresponds to increasing the learning rate linearly for the first
这对应于在前
5.4 Regularization
We employ three types of regularization during training:
Residual Dropout We apply dropout to the output of each sub-layer, before it is added to the sub-layer input and normalized. In addition, we apply dropout to the sums of the embeddings and the positional encodings in both the encoder and decoder stacks. For the base model, we use a rate of
Label Smoothing During training, we employed label smoothing of value
5.4 正则化
我们在训练过程中采用了三种正则化方法:
残差 Dropout 我们在每个子层的输出上应用 Dropout,然后再将其添加到子层输入并进行层归一化。此外,我们还对编码器和解码器堆栈中的嵌入与位置编码的求和结果应用 Dropout。对于基础模型,我们使用的丢弃率为
标签平滑 在训练过程中,我们采用了值为
Results
6.1 Machine Translation
On the WMT 2014 English-to-German translation task, the big transformer model (Transformer (big) in Table 2) outperforms the best previously reported models (including ensembles) by more than 2.0 BLEU, establishing a new state-of-the-art BLEU score of 28.4. The configuration of this model is listed in the bottom line of Table 3. Training took 3.5 days on 8 P100 GPUs. Even our base model surpasses all previously published models and ensembles, at a fraction of the training cost of any of the competitive models.
On the WMT 2014 English-to-French translation task, our big model achieves a BLEU score of 41.0, outperforming all of the previously published single models, at less than 1/4 the training cost of the previous state-of-the-art model. The Transformer (big) model trained for English-to-French used dropout rate
For the base models, we used a single model obtained by averaging the last 5 checkpoints, which were written at 10-minute intervals. For the big models, we averaged the last 20 checkpoints. We used beam search with a beam size of 4 and length penalty
Table 2 summarizes our results and compares our translation quality and training costs to other model architectures from the literature. We estimate the number of floating point operations used to train a model by multiplying the training time, the number of GPUs used, and an estimate of the sustained single-precision floating-point capacity of each GPU 2.
6.1 机器翻译
在 WMT 2014 英德翻译任务上,大号 Transformer 模型(表 2 中的 Transformer (big))以超过 2.0 BLEU 的优势击败了此前报告的最佳模型(包括集成模型),取得了 28.4 BLEU 分数,树立了新的 SOTA。该模型的配置列于表 3 底行。训练在 8 块 P100 GPU 上耗时 3.5 天。即便是我们的基础模型,也以远低于任何竞争模型的训练成本,超越了所有先前发表的模型与集成模型。
在 WMT 2014 英法翻译任务上,我们的大号模型取得了 41.0 BLEU 分数,优于此前发表的所有单模型,而训练成本不到此前最先进模型的 1/4。用于英法任务的 Transformer (big) 模型使用的丢弃率为
对于基础模型,我们采用对最后 5 个检查点(每 10 分钟写入一次)进行平均所得到的单一模型。对于大号模型,我们对最后 20 个检查点进行平均。我们使用集束搜索,束大小为 4,长度惩罚
表 2 总结了我们的结果,并将我们的翻译质量与训练成本同文献中的其他模型架构进行了比较。我们通过将训练时间、使用的 GPU 数量以及对每个 GPU 持续单精度浮点运算能力的估算相乘,来估计训练一个模型所需的浮点运算次数。
表2:在 newstest2014 英德和英法翻译测试中,Transformer 以远低于先前最优模型的训练成本,取得了更高的 BLEU 分数。
| Model | BLEU | Training Cost (FLOPs) | |||
|---|---|---|---|---|---|
| EN-DE | EN-FR | EN-DE | EN-FR | ||
| Deep-Att + PosUnk | 39.2 | 1.0·1020 | |||
| GNMT + RL | 24.6 | 39.92 | 2.3·1019 | 1.4·1020 | |
| ConvS2S | 25.16 | 40.46 | 9.6·1018 | 1.5·1020 | |
| MoE | 26.03 | 40.56 | 2.0·1019 | 1.2·1020 | |
| Deep-Att + PosUnk Ensemble | 40.4 | 8.0·1020 | |||
| GNMT + RL Ensemble | 26.30 | 41.16 | 1.8·1020 | 1.1·1021 | |
| ConvS2S Ensemble | 26.36 | 41.29 | 7.7·1019 | 1.2·1021 | |
| Transformer (base model) | 27.3 | 38.1 | 3.3·1018 | ||
| Transformer (big) | 28.4 | 41.8 | 2.3·1019 | ||
6.2 Model Variations
To evaluate the importance of different components of the Transformer, we varied our base model in different ways, measuring the change in performance on English-to-German translation on the development set, newstest2013. We used beam search as described in the previous section, but no checkpoint averaging. We present these results in Table 3.
In Table 3 rows (A), we vary the number of attention heads and the attention key and value dimensions, keeping the amount of computation constant, as described in Section 3.2.2. While single-head attention is 0.9 BLEU worse than the best setting, quality also drops off with too many heads.
In Table 3 rows (B), we observe that reducing the attention key size
6.2 模型变体
为了评估 Transformer 不同组件的重要性,我们以多种方式改变基础模型,并在开发集 newstest2013 上测量英德翻译性能的变化。我们使用前一节所述的集束搜索,但未采用检查点平均。我们在表 3 中展示了这些结果。
在表 3 的 (A) 行中,我们改变注意力头数量以及注意力键和值的维度,同时保持计算量不变,如第 3.2.2 节所述。虽然单头注意力的 BLEU 值比最佳设置低 0.9,但头数过多时质量同样会下降。
在表 3 的 (B) 行中,我们观察到减小注意力键的尺寸
表3:Transformer 架构的变体。 未列出的数值与基础模型相同。所有指标均基于英德翻译开发集 newstest2013。所列困惑度为根据我们的字节对编码计算的每词片段困惑度,不应与每词困惑度进行比较。
| train steps | PPL (dev) | BLEU (dev) | params ×106 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| base | 6 | 512 | 2048 | 8 | 64 | 64 | 0.1 | 0.1 | 100K | 4.92 | 25.8 | 65 |
| (A) | 1 | 512 | 512 | 5.29 | 24.9 | |||||||
| 4 | 128 | 128 | 5.00 | 25.5 | ||||||||
| 16 | 32 | 32 | 4.91 | 25.8 | ||||||||
| 32 | 16 | 16 | 5.01 | 25.4 | ||||||||
| (B) | 16 | 5.16 | 25.1 | 58 | ||||||||
| 32 | 5.01 | 25.4 | 60 | |||||||||
| (C) | 2 | 6.11 | 23.7 | 36 | ||||||||
| 4 | 5.19 | 25.3 | 50 | |||||||||
| 8 | 4.88 | 25.5 | 80 | |||||||||
| 256 | 32 | 32 | 5.75 | 24.5 | 28 | |||||||
| 1024 | 128 | 128 | 4.66 | 26.0 | 168 | |||||||
| 1024 | 5.12 | 25.4 | 53 | |||||||||
| 4096 | 4.75 | 26.2 | 90 | |||||||||
| (D) | 0.0 | 5.77 | 24.6 | |||||||||
| 0.2 | 4.95 | 25.5 | ||||||||||
| 0.0 | 4.67 | 25.3 | ||||||||||
| 0.2 | 5.47 | 25.7 | ||||||||||
| (E) | positional embedding instead of sinusoids | 4.92 | 25.7 | |||||||||
| big | 6 | 1024 | 4096 | 16 | 0.3 | 300K | 4.33 | 26.4 | 213 | |||
6.3 English Constituency Parsing
To evaluate if the Transformer can generalize to other tasks we performed experiments on English constituency parsing. This task presents specific challenges: the output is subject to strong structural constraints and is significantly longer than the input. Furthermore, RNN sequence-to-sequence models have not been able to attain state-of-the-art results in small-data regimes.
We trained a 4-layer transformer with
We performed only a small number of experiments to select the dropout, both attention and residual (section 5.4), learning rates and beam size on the Section 22 development set, all other parameters remained unchanged from the English-to-German base translation model. During inference, we increased the maximum output length to input length + 300. We used a beam size of 21 and
Our results in Table 4 show that despite the lack of task-specific tuning our model performs surprisingly well, yielding better results than all previously reported models with the exception of the Recurrent Neural Network Grammar.
In contrast to RNN sequence-to-sequence models, the Transformer outperforms the BerkeleyParser even when training only on the WSJ training set of 40K sentences.
6.3 英语成分句法分析
为了评估 Transformer 能否泛化到其他任务,我们在英语成分句法分析上进行了实验。该任务具有特殊的挑战:输出受限于严格的结构约束,且长度明显长于输入。此外,RNN 序列到序列模型在小数据场景下一直未能取得最先进的结果。
我们在宾州树库的华尔街日报(WSJ)部分(约 40K 训练句子)上训练了一个 4 层 Transformer,其中
我们仅在 Section 22 开发集上进行了少量实验,以选择丢弃率(包括注意力和残差,见第 5.4 节)、学习率和束大小,所有其他参数均与英德翻译基础模型保持一致。推理时,我们将最大输出长度增加到输入长度 + 300。对于仅 WSJ 和半监督两种设置,我们都使用束大小为 21,长度惩罚
表 4 中的结果表明,尽管缺乏针对任务的调优,我们的模型仍表现出色,除了循环神经网络文法之外,其结果优于此前所有已报告的模型。
与 RNN 序列到序列模型相比,Transformer 即便仅在 40K 句子的 WSJ 训练集上训练,也优于 BerkeleyParser。
表4:Transformer 能够很好地泛化到英语成分句法分析任务(结果基于 WSJ 第23节)
| Parser | Training | WSJ 23 F1 |
|---|---|---|
| Vinyals & Kaiser el al. (2014) | WSJ only, discriminative | 88.3 |
| Petrov et al. (2006) | WSJ only, discriminative | 90.4 |
| Zhu et al. (2013) | WSJ only, discriminative | 90.4 |
| Dyer et al. (2016) | WSJ only, discriminative | 91.7 |
| Transformer (4 layers) | WSJ only, discriminative | 91.3 |
| Zhu et al. (2013) | semi-supervised | 91.3 |
| Huang & Harper (2009) | semi-supervised | 91.3 |
| McClosky et al. (2006) | semi-supervised | 92.1 |
| Vinyals & Kaiser el al. (2014) | semi-supervised | 92.1 |
| Transformer (4 layers) | semi-supervised | 92.7 |
| Luong et al. (2015) | multi-task | 93.0 |
| Dyer et al. (2016) | generative | 93.3 |
Conclusion
In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention.
For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. In the former task our best model outperforms even all previously reported ensembles.
We are excited about the future of attention-based models and plan to apply them to other tasks. We plan to extend the Transformer to problems involving input and output modalities other than text and to investigate local, restricted attention mechanisms to efficiently handle large inputs and outputs such as images, audio and video. Making generation less sequential is another research goals of ours.
在本工作中,我们提出了 Transformer——首个完全基于注意力机制的序列转换模型,它用多头自注意力层取代了编码器-解码器架构中最常使用的循环层。
在翻译任务上,Transformer 的训练速度显著快于基于循环层或卷积层的架构。在 WMT 2014 英德翻译和英法翻译两项任务上,我们都取得了新的最先进成果。在前一项任务中,我们最好的模型甚至超越了此前报告的所有集成模型。
我们对基于注意力的模型的未来充满期待,并计划将其应用于其他任务。我们计划将 Transformer 扩展到涉及文本以外的输入输出模态的问题,并研究局部的、受限的注意力机制,以高效处理图像、音频和视频等大规模输入与输出。减少生成的顺序性也是我们的后续研究目标之一。