Attention Is All You Need

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin

TL;DR

This paper introduces the Transformer, a novel neural network architecture that completely abandms recurrence and convolutions, relying solely on attention mechanisms. This innovation leads to significantly more parallelizable models that train faster and achieve state-of-the-art results in machine translation.

What Problem Does This Paper Address?

Traditional sequence transduction models, such as those used for machine translation, heavily relied on recurrent neural networks (RNNs) like LSTMs and GRUs. While effective, RNNs process sequences sequentially, meaning each step depends on the previous one. This inherent sequentiality severely limits parallelization during training, making it computationally expensive and time-consuming, especially for long sequences where memory constraints further hinder efficiency. Although attention mechanisms had been integrated to improve performance by allowing models to focus on relevant parts of the input, they were typically used in conjunction with recurrent networks. The core challenge this paper addresses is how to build powerful sequence transduction models that are both highly parallelizable and capable of capturing long-range dependencies without relying on sequential computation.

Methods

The paper's central contribution is the Transformer architecture. Instead of recurrence or convolutions, the Transformer exclusively uses attention mechanisms. The architecture consists of an encoder and a decoder, each composed of stacks of identical layers.

Key components include:

  • Self-Attention: This mechanism allows each position in a sequence to attend to all other positions within the same sequence. This is crucial for capturing dependencies regardless of their distance.
  • Multi-Head Attention: This extends self-attention by linearly projecting queries, keys, and values multiple times in parallel. This allows the model to jointly attend to information from different representation subspaces at different positions, enriching its understanding.
  • Positional Encoding: Since the Transformer lacks recurrence, it needs a way to incorporate information about the order of tokens. Positional encodings (using sine and cosine functions) are added to the input embeddings to provide this positional information.
  • Encoder-Decoder Attention: In the decoder, attention layers attend to the output of the encoder stack, allowing the decoder to focus on relevant parts of the input sequence.
  • Position-wise Feed-Forward Networks: Each layer also includes a simple, position-wise fully connected feed-forward network, applied identically to each position.
  • Residual Connections and Layer Normalization: These standard techniques are used to facilitate training of deep networks.

Datasets

The paper uses standard datasets for evaluation:

  • WMT 2014 English-German: For machine translation.
  • WMT 2014 English-French: For machine translation.
  • Wall Street Journal (WSJ) portion of the Penn Treebank: For English constituency parsing.
  • High-confidence and BerkleyParser corpora: Used in a semi-supervised setting for English constituency parsing.

Key Insights and Ideas

  • Attention is Sufficient: The most significant insight is that attention mechanisms alone, without recurrence or convolutions, are sufficient to achieve state-of-the-art performance in sequence transduction tasks. This challenges the long-held reliance on sequential processing.
  • Parallelization is Key to Speed: By eliminating recurrence, the Transformer allows for massive parallelization during training. This dramatically reduces training time, making it feasible to train much larger models or train on more data within practical timeframes.
  • Long-Range Dependencies are Handled Naturally: Self-attention directly connects all positions, creating shorter paths for information to travel between distant tokens compared to RNNs. This makes learning long-range dependencies much easier.
  • Multi-Head Attention Enhances Representation: Using multiple attention heads allows the model to capture diverse relationships and dependencies simultaneously, leading to richer representations.
  • Positional Encoding is a Clever Workaround: The sine/cosine positional encoding is an elegant way to inject order information into an otherwise order-agnostic attention mechanism, enabling it to learn sequential patterns.

Key Results

  • Machine Translation:
    • Achieved a new state-of-the-art BLEU score of 28.4 on the WMT 2014 English-to-German task, surpassing previous best models (including ensembles) by over 2 BLEU.
  • Achieved a new single-model state-of-the-art BLEU score of 41.8 on the WMT 2014 English-to-French task.
  • Trained significantly faster: the "big" model trained for 3.5 days on eight GPUs, a fraction of the cost of previous best models.
  • English Constituency Parsing:
    • Generalized well to this task, achieving competitive results with a 4-layer Transformer, even without task-specific tuning.

Limitations and Future Work

  • Computational Complexity: While parallelizable, the self-attention mechanism has a quadratic complexity ($O(n^2 \cdot d)$) with respect to sequence length $n$. The authors acknowledge this and suggest restricting attention to a neighborhood ($r$) for very long sequences, which would reduce complexity to $O(n/r)$.
  • Extrapolation to Longer Sequences: While the sinusoidal positional encoding was chosen for its potential to extrapolate to longer sequences, this was a hypothesis and not fully proven.
  • Interpretability: While attention distributions offer some interpretability, the authors note that further investigation into how individual attention heads learn specific tasks is a future direction.
  • Other Modalities: The authors express excitement about extending the Transformer to non-textual modalities (images, audio, video) and exploring local attention mechanisms for handling large inputs.

Summary updated

BibTeX
@inproceedings{vaswani2017attention,
  author = {Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and Lukasz Kaiser and Illia Polosukhin},
  title = {{Attention Is All You Need}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2017},
  url = {https://openreview.net/forum?id=H1ZGYPb_ZS},
}
Figure