NeurIPS 2017 · Google BrainDOI 快取秒開約 12 分鐘精讀 · 15 頁重排

Attention Is All You Need

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin
🎯 研究痛點:Recurrent neural networks (RNNs/LSTMs) process tokens sequentially, preventing parallelization across training examples and struggling with long-range dependencies due to vanishing gradients and memory constraints.
⚙️ 核心方法:Proposes the Transformer architecture, dispensing with recurrence and convolutions entirely and relying solely on Scaled Dot-Product Self-Attention, Multi-Head Attention, and sinusoidal Positional Encoding.
📊 關鍵結論:Achieved 28.4 BLEU on WMT 2014 English-to-German translation (+2 BLEU over best ensembles) and 41.8 BLEU on English-to-French with only 3.5 days of training on 8 P100 GPUs — a fraction of prior compute costs.
⚠️ 研究局限:Self-attention incurs O(n2⋅d)O(n^2 \cdot d) computational and memory complexity with respect to sequence length nn, creating bottlenecks for ultra-long documents or high-resolution images.
本篇核心術語(點擊立即查看上下文釋義):
💡 預設隱藏底線保持乾淨閱讀;直接點擊任何專有名詞即可看解釋

Abstract

The dominant sequence transduction models are based on complex recurrent or convolutional neural networks that include an encoder and a decoder. 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.

1. Introduction & Model Architecture

Recurrent models typically factor computation along the symbol positions of the input and output sequences. This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples. The Transformer follows an encoder-decoder structure using stacked and point-wise, fully connected layers, employing residual connections followed by around each sub-layer.

3.2 Scaled Dot-Product & Multi-Head Attention

We call our particular attention . The input consists of queries and keys of dimension dkd_k, and values of dimension dvd_v. We compute the dot products of the query with all keys, divide each by dk\sqrt{d_k}, and apply a softmax function to obtain the weights on the values. In practice, we compute the attention function on a set of queries simultaneously, packed together into a matrix QQ.

Attention(Q,K,V)=softmax(QKTdk)V\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
Equation (1): Scaled Dot-Product Attention
💡 Dividing by dk\sqrt{d_k} keeps the variance of dot products near 11 so Softmax gradients remain healthy.

Instead of performing a single attention function with dmodeld_{\mathrm{model}}-dimensional keys, values and queries, we found it beneficial to linearly project the queries, keys and values hh times with different, learned linear projections. allows the model to jointly attend to information from different representation subspaces at different positions, while decoder layers preserve the property.

MultiHead(Q,K,V)=Concat(head1,…,headh)WOwhere headi=Attention(QWiQ,KWiK,VWiV)\mathrm{MultiHead}(Q, K, V) = \mathrm{Concat}(\mathrm{head}_1, \dots, \mathrm{head}_h)W^O \\[6pt] \text{where } \mathrm{head}_i = \mathrm{Attention}(QW_i^Q, KW_i^K, VW_i^V)
Equation (2): Multi-Head Attention Projection

3.5 Positional Encoding & Complexity Comparison

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. To this end, we add to the input embeddings using sine and cosine functions of different frequencies.

PE(pos,2i)=sin⁡(pos100002i/dmodel),PE(pos,2i+1)=cos⁡(pos100002i/dmodel)PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{\mathrm{model}}}}\right), \quad PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{\mathrm{model}}}}\right)
Equation (3): Sinusoidal Positional Encoding
ReflowPaper 紙流 — 行動端 AI 論文閱讀第一品牌