Paper Guides Paper guide

Attention Is All You Need

The Transformer adds positional encodings to token embeddings, injecting the order information that self-attention cannot infer on its own. Sinusoidal encodings at different frequencies give every position a distinct pattern without recurrence or convolution.

Reading guide

The Transformer has no recurrence, so nothing in its basic operation tells it that one token came before another. The reading question asks how order is represented anyway. The answer is in Section 3.5: before tokens enter the stack, the model adds a positional encoding to each token embedding. Self-attention then mixes content and position together.

Self-attention on its own ignores token order. For a sequence of embeddings, it computes queries, keys, and values by linear projection, takes scaled dot-product scores, applies softmax, and returns weighted sums (Section 3.2.1). Shuffle the input tokens without positional vectors, and each token receives the same set of values in a different order; the layer's output set is unchanged. Position must enter as data.

Section 3.5 supplies it with fixed sinusoids. For position pos, each dimension pair i uses sin(pos / 10000^(2i/dmodel)) and cos(pos / 10000^(2i/dmodel)). Different dimensions oscillate at different frequencies, so each position gets a distinct pattern. The paper notes that for any fixed offset k, the encoding at pos + k can be written as a linear function of the encoding at pos, which gives the model a way to express relative position. Learned positional embeddings performed about as well in the ablation (Table 3), so the sinusoidal choice is mainly about extending to sequence lengths not seen in training.

import math

def positional_encoding(pos, d_model):
    values = []
    for pair in range(d_model // 2):
        angle = pos / (10000 ** (2 * pair / d_model))
        values.extend([math.sin(angle), math.cos(angle)])
    return values

Section 3.1 stacks six encoder and six decoder layers. Section 3.2.2 replaces a single attention operation with eight heads of dimension 64 in the base model, letting each head learn a different projection. Section 3.3 adds a position-wise feed-forward network, and Section 3.4 ties embeddings and softmax weights. Section 4 compares self-attention with recurrence and convolution: self-attention connects any two positions in one step, while a recurrent layer needs a step per position.

Layer type Complexity per layer Sequential operations Longest path between positions
Self-attention O(n^2 * d) O(1) O(1)
Recurrent O(n * d^2) O(n) O(n)
Convolutional O(k * n * d^2) O(1) O(log_k n)

Three tokens illustrate the mechanism. Each token embedding is summed with its position vector: position 0 gets sin(0), cos(0), and higher-frequency terms, while position 1 gets sin(1), cos(1), and so on. The attention scores still depend on content, but the queries and keys now carry position, so the model can learn to prefer nearby tokens, previous tokens, or particular offsets. The relational model reading guide offers another example of an interface that hides implementation detail, though in storage rather than sequence modeling.

Evidence and limits

The authors evaluate the Transformer on machine translation and constituency parsing. Section 5 trains on WMT 2014 English-German (about 4.5 million sentence pairs) and English-French (about 36 million), using byte-pair encoding. Section 6 reports 28.4 BLEU on English-German and 41.8 on English-French for the big model, and Section 6.2 varies heads, dimensions, dropout, and positional encoding. Section 6.3 reports parsing results on the Wall Street Journal portion of the Penn Treebank. Training used eight NVIDIA P100 GPUs: about 12 hours for the base model and 3.5 days for the big model.

The evidence is strong for supervised sequence transduction with large training sets and substantial compute. It does not establish that the same architecture is best for every sequence task, nor that attention alone learns linguistic structure without the training signal. The BLEU comparisons are against specific prior systems on two language pairs.

The main boundary is cost. Table 1, reproduced above, shows that self-attention is quadratic in sequence length per layer. Doubling the context quadruples the attention matrix, which is why later work changes the attention pattern or approximates it. The paper's path-length argument favors self-attention for long-range dependencies, but it does not remove the quadratic cost. Positional encodings also solve order only within the range and frequencies the model sees; neither form is a general solution for unbounded sequences.

Cite the paper

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin. "Attention Is All You Need." NIPS 2017. https://arxiv.org/abs/1706.03762

Open the original paper

BibTeX

@inproceedings{vaswani2017attention,
  author    = {Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and {\L}ukasz Kaiser and Illia Polosukhin},
  title     = {{Attention Is All You Need}},
  booktitle = {Advances in Neural Information Processing Systems 30 (NIPS 2017)},
  year      = {2017},
  pages     = {5998--6008},
  publisher = {Curran Associates, Inc.},
  url       = {https://arxiv.org/abs/1706.03762}
}

Download all paper references (BibTeX)

https://www.fieldnotely.xyz/papers/machine-learning/attention-is-all-you-need/

Sources

Revision history

Published
Updated
— Initial publication.