NIPS 2017 · The Paper That Changed AI

Attention
Is All You Need

Eight researchers at Google asked a radical question: what if we threw out the entire playbook for how machines understand language — and replaced it with a single, beautifully simple idea?

Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser & Polosukhin · 2017

🔥 Why should I care?

The AI You Use Every Day? It Started Here.

ChatGPT, Google Translate, Siri, email autocomplete — all of these are built on a foundation called the Transformer. This is the paper that invented it. It is, by citation count, arguably the most influential AI paper ever written.

But to understand why the Transformer was revolutionary, you first need to understand what it replaced — and why the old approach was cracking under pressure.

The Old Way: Reading One Word at a Time

Imagine you're translating a book from English to French, but you're only allowed to read one word at a time, and you must remember everything you've read so far by passing a single sticky note forward to your future self. That's roughly how Recurrent Neural Networks (RNNs) worked.

Before the Transformer, the best language models were built on recurrent neural networks — systems like LSTMs (Long Short-Term Memory) and GRUs (Gated Recurrent Units). These process text sequentially: word 1, then word 2, then word 3, and so on. Each word updates a hidden state — that "sticky note" — which is the model's running summary of everything so far.

This approach had two devastating problems:

🐌 Painfully Slow

Because you process word by word, you can't parallelize. Word 5 has to wait for Word 4. Word 100 has to wait for all 99 before it. On modern GPUs that thrive on parallel work, this was like using a 16-lane highway with only one car on it.

🧠 Forgetful

By the time the model reaches word 50, the information from word 1 has been squeezed through 49 processing steps. Long-range relationships — like a pronoun referring to a noun from a paragraph ago — get lost. The "sticky note" can only hold so much.

Other researchers tried using Convolutional Neural Networks (CNNs) — the technology behind image recognition — for language. Models like ByteNet and ConvS2S could process words in parallel, which was faster. But they had their own limitation: a convolutional filter only looks at a small window of nearby words. To connect distant words, you need to stack many layers — the relationship between word 1 and word 100 might need to pass through O(log n) layers. The Transformer connects any two words in a single step.

So the field was stuck: fast but forgetful, or thorough but slow. The Transformer's insight was that there's a third option.

The Big Idea: Just Look at Everything at Once

Imagine you're at a cocktail party. Instead of talking to people one by one in a line (the RNN way), you walk into the room and instantly scan everyone's nametag. You decide who's most relevant to your current conversation, and you give them more of your attention. That's self-attention.

The Transformer's radical move was simple: throw out the conveyor belt entirely. Instead of processing words one at a time, let every word look at every other word, all at once, and figure out which words are most important for understanding each other.

This mechanism is called self-attention (sometimes called "intra-attention"). It's the only core mechanism in the Transformer. No recurrence. No convolutions. Just attention.

"We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely."

The result? Three transformative advantages:

Massively Parallel

Every word is processed simultaneously. Training time drops from weeks to days.

🔭

Perfect Memory

Word 1 and word 100 are connected in a single step. No more information decay.

🏆

Better Results

Set new records on translation benchmarks, beating even ensembles of older models.

🎯 The Key Mechanism

How Self-Attention Actually Works

Think of Google Search. You type a query ("best coffee shops"). Google compares your query against the keys (titles/descriptions of every webpage). The better the match, the higher the result ranks. Then Google shows you the values (the actual content of those pages). Self-attention works the same way — but every word in a sentence is simultaneously the searcher and the result.

For each word in a sentence, the Transformer creates three things:

Q
Query — "What am I looking for?" This is the word's question to the rest of the sentence.
K
Key — "What do I contain?" This is what the word advertises about itself.
V
Value — "Here's my actual information." The content that gets passed along if there's a match.

The attention formula computes how much each word should attend to every other word:

\[\text{Attention}(Q, K, V) = \text{softmax}\!\left(\frac{QK^T}{\sqrt{d_k}}\right) V\]

In plain English:

1. Compare every query with every key (the \(QK^T\) part) — this produces a score showing how relevant each word is to every other word.

2. Scale down by dividing by \(\sqrt{d_k}\) — without this, the scores would get too extreme for large dimensions, making the model focus on just one word and ignore everything else.

3. Convert to percentages (softmax) — turn the raw scores into weights that add up to 1, like a probability distribution.

4. Weighted blend of values — each word's output is a custom cocktail of information from all the words it decided to pay attention to.

When the dimension \(d_k\) is large (say 64), the dot products \(q \cdot k\) tend to become very large in magnitude. This is because if each component of q and k has mean 0 and variance 1, the dot product has variance \(d_k\). Large values push the softmax into regions where the gradient is extremely small — meaning the model can't learn. Dividing by \(\sqrt{d_k}\) keeps the variance at 1, keeping gradients healthy. The paper uses \(d_k = 64\).

🖱️ Interactive · Click a word

See Attention in Action

Click any word below to see which other words it might attend to. The brighter the highlight, the stronger the attention. This simulates how self-attention connects distant, related words.

EXAMPLE SENTENCE

🔑 Critical Innovation

Multi-Head Attention: Looking Through Many Lenses

Imagine you're analyzing a photograph. One lens might focus on colors, another on shapes, another on textures. You'd get a much richer understanding than using a single lens. Multi-head attention does exactly this — it runs the attention mechanism multiple times in parallel, each "head" learning to focus on different types of relationships.

Instead of performing one big attention operation, the Transformer splits queries, keys, and values into h = 8 parallel "heads". Each head uses smaller dimensions (\(d_k = d_v = 64\) instead of the full \(d_{model} = 512\)), so the total computational cost stays roughly the same.

After all 8 heads compute their attention independently, their outputs are concatenated and projected back to the full dimension.

What each head learns to do:

Head A might track subject-verb agreement
"The cat that sat on the mat is fluffy"
Head B might connect pronouns to nouns
"The Law will never be perfect, but its application..."
Head C might link modifiers across distance
"making the process more difficult"
Head D might capture adjacent word patterns
Local grammar and word-order patterns

The paper's attention visualizations (Figures 3–5 in the appendix) beautifully demonstrate this: different heads clearly specialize in different linguistic tasks — some tracking long-distance dependencies, others resolving pronouns, others following sentence structure.

Formally:

\[\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \ldots, \text{head}_h) W^O\]

where \(\text{head}_i = \text{Attention}(Q W_i^Q,\; K W_i^K,\; V W_i^V)\)

The projection matrices \(W_i^Q, W_i^K \in \mathbb{R}^{d_{model} \times d_k}\), \(W_i^V \in \mathbb{R}^{d_{model} \times d_v}\), and \(W^O \in \mathbb{R}^{h d_v \times d_{model}}\) are all learned. With h = 8 and dk = dv = 64, each head operates on a 64-dimensional slice. The results are concatenated (8 × 64 = 512) and projected back with WO.

The Full Architecture: Building Block by Block

Think of the Transformer like a two-stage translation factory. The encoder reads the original sentence and builds a rich understanding of it. The decoder then uses that understanding to produce the translation, one word at a time. What's special is that within each stage, the machinery is made of identical, stacked layers — like floors of a building, each refining the understanding further.
🔨 Interactive · Build the architecture step by step

Step 1: Turn Words into Numbers

Each word (technically, each token) is converted into a vector of 512 numbers — its embedding. These aren't random; they're learned during training so that similar words get similar vectors. The word "king" and "queen" would end up close to each other in this 512-dimensional space.

Key detail: The embedding weights are multiplied by \(\sqrt{d_{model}} = \sqrt{512} \approx 22.6\) to scale them up, and the same weight matrix is shared between the input embedding, output embedding, and the final prediction layer.

Step 2: Stamp Each Word with Its Position

Since the Transformer processes all words simultaneously (no sequential order), it has no built-in sense of word order. "Dog bites man" and "Man bites dog" would look the same!

The fix: add a unique positional encoding to each word's embedding. The authors use sine and cosine waves of different frequencies:

\[PE_{(pos, 2i)} = \sin\!\left(\frac{pos}{10000^{2i/d_{model}}}\right)\] \[PE_{(pos, 2i+1)} = \cos\!\left(\frac{pos}{10000^{2i/d_{model}}}\right)\]

Each position gets a unique pattern of values — like a fingerprint. The clever use of sinusoids means the model can learn to figure out relative distances between words (e.g., "3 positions apart"), and potentially generalize to sequences longer than those seen during training.

Fun fact: The authors also tried learned positional embeddings and found nearly identical results (Table 3, row E). They chose sinusoids for the extrapolation benefit.

Step 3: The Encoder (×6 layers)

The encoder is a stack of N = 6 identical layers. Each layer has two sub-layers:

1
Multi-head self-attention: Every word attends to every other word. This is where the magic of understanding context happens.
2
Feed-forward network: A simple two-layer neural network applied to each position independently. Think of it as "thinking about" what the attention layer found. Inner dimension is 2048, with a ReLU activation: \(\text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2\)

Each sub-layer has a residual connection (a shortcut that adds the input to the output) followed by layer normalization. In formula: \(\text{LayerNorm}(x + \text{Sublayer}(x))\). Residual connections are like emergency exits — they let information flow directly through without being corrupted, making deep networks trainable.

Step 4: The Decoder (×6 layers)

The decoder is similar but has three sub-layers per layer:

1
Masked self-attention: Like the encoder's self-attention, but each word can only attend to previous words (not future ones). When generating "The cat sat", the model predicting "sat" can see "The" and "cat" but not what comes after. This preserves the auto-regressive property (generating one word at a time, left to right).
2
Encoder-decoder attention: The decoder's queries attend to the encoder's output. This is where the translation "looks back" at the original sentence. Queries come from the decoder; keys and values come from the encoder.
3
Feed-forward network: Same as in the encoder — independent processing at each position.

Every sub-layer again uses residual connections + layer normalization.

Step 5: Generating the Output

The decoder's final output goes through a linear layer (projecting to vocabulary size) and then a softmax to produce probabilities over all possible next words. The word with the highest probability is chosen (or, during search, the top candidates are explored).

At inference time: the model uses beam search with a beam size of 4 and a length penalty α = 0.6. It generates words one at a time (auto-regressively), feeding each generated word back as input for the next step.

⚖️ Why this design wins

The Three-Way Showdown: Self-Attention vs. Recurrence vs. Convolution

The paper makes a careful comparison across three criteria that matter for practical language processing. Here's how the layer types stack up:

📊 Interactive · Hover for details

Maximum path length = the farthest two words in a sentence have to "travel" through the network to communicate. Shorter is better — shorter paths make long-range relationships easier to learn.

Self-AttentionO(1) — instant! ✨
1
Recurrent (RNN)O(n) — linear with length
n
ConvolutionalO(logk(n))
log(n)

Minimum sequential operations = how many steps must happen one after another (can't be parallelized). Lower means more parallelization on modern GPUs.

Self-AttentionO(1) — fully parallel ✨
1
Recurrent (RNN)O(n) — strictly sequential
n
ConvolutionalO(1) — parallel too
1

Computational complexity per layer — how much total work is done. Self-attention's cost is O(n²·d), which is fast when n (sequence length) is smaller than d (dimension = 512), as is typical for sentences.

Self-AttentionO(n² · d)
n²d
Recurrent (RNN)O(n · d²)
nd²
ConvolutionalO(k · n · d²)
knd²

Note: For very long sequences where n > d, the paper suggests restricted self-attention — only attending to a local neighborhood of size r — which reduces complexity to O(r·n·d) but increases path length to O(n/r).

How They Trained It

The details of training matter because they show just how efficient the Transformer was compared to what came before.

📚 Training Data

English→German: WMT 2014 dataset, ~4.5 million sentence pairs. Shared vocabulary of ~37,000 tokens (byte-pair encoding).

English→French: WMT 2014, ~36 million sentence pairs. 32,000 word-piece vocabulary.

Sentences batched by approximate length, ~25,000 source + 25,000 target tokens per batch.

🖥️ Hardware

Machine: 1 machine, 8 NVIDIA P100 GPUs.

Base model: 100K steps, ~0.4 seconds/step = 12 hours total.

Big model: 300K steps, ~1.0 seconds/step = 3.5 days total.

Compare this to competitors that trained for weeks on more GPUs!

The paper uses a "warm-up then decay" learning rate schedule:

\[lrate = d_{model}^{-0.5} \cdot \min(step\_num^{-0.5},\; step\_num \cdot warmup\_steps^{-1.5})\]

For the first 4,000 steps, the learning rate increases linearly (warm-up). Then it decreases proportionally to the inverse square root of the step number. This prevents the model from making wild jumps early in training when it knows nothing.

They used the Adam optimizer with β₁ = 0.9, β₂ = 0.98, ε = 10⁻⁹.

Residual Dropout: Applied to the output of each sub-layer (before adding the residual) and to the sum of embeddings + positional encodings. Rate Pdrop = 0.1 for base, 0.3 for big model on EN-DE (0.1 on EN-FR).

Label Smoothing: εls = 0.1. Instead of telling the model "the answer is definitely word X," they say "it's 90% likely word X, with 10% spread across all other words." This hurts perplexity (the model appears less confident) but actually improves translation quality (BLEU score). It prevents overconfidence.

Checkpoint Averaging: For base models, averaged the last 5 checkpoints (written at 10-minute intervals). For big models, averaged the last 20 checkpoints.

🏆 The proof

The Results: Shattering Records

The Transformer didn't just match existing models — it dominated them, while being dramatically cheaper to train. Here are the results on two major machine translation benchmarks.

📊 Interactive · Compare models

WMT 2014 English-to-German (newstest2014)

BLEU score measures translation quality (higher = better). A 1-point improvement is significant; 2+ points is huge.

The Transformer (big) scores 28.4 BLEU — over 2 points above the previous best (including ensembles of multiple models). The base model alone (27.3) already beats every prior single model and every ensemble.

WMT 2014 English-to-French (newstest2014)

The Transformer (big) achieves 41.8 BLEU, a new single-model state-of-the-art, surpassing even the ConvS2S Ensemble (41.29). And it trained in just 3.5 days — a quarter of the cost of previous state-of-the-art.

Training Cost Comparison (FLOPs = floating point operations)

Lower is cheaper. Note the logarithmic scale — each step is 10× more expensive.

Model BLEU (EN-DE) Training FLOPs (EN-DE)
GNMT + RL24.62.3 × 10¹⁹
ConvS2S25.169.6 × 10¹⁸
MoE26.032.0 × 10¹⁹
GNMT + RL Ensemble26.301.8 × 10²⁰
ConvS2S Ensemble26.367.7 × 10¹⁹
Transformer (base)27.33.3 × 10¹⁸ ⚡
Transformer (big)28.42.3 × 10¹⁹

The base Transformer costs 3.3 × 10¹⁸ FLOPs — roughly 3× cheaper than ConvS2S and 7× cheaper than GNMT+RL — while producing better translations than either. The big model matches the cost of GNMT+RL but scores 3.8 BLEU points higher.

28.4 BLEU EN→DE
(+2.0 vs prior best)
41.8 BLEU EN→FR
(new SOTA)
3.5 Days to train
(on 8 GPUs)
🔬 Scientific rigor

What If We Changed Things? (Ablation Studies)

Great research doesn't just show that something works — it figures out which parts make it work. The authors systematically changed one component at a time and measured the effect. All results below are on the English-to-German dev set (newstest2013).

📊 Interactive · Explore variations

How Many Attention Heads?

Keeping total computation constant (by adjusting dk and dv proportionally), they varied the number of heads:

Sweet spot: 8 heads (the default). Single-head attention (h=1) loses 0.9 BLEU — a meaningful drop. But 32 heads (dk=16 each) also degrades slightly. Too few heads means not enough perspectives; too many heads means each perspective is too narrow.

Does Bigger = Better?

Varying the model dimensions and number of layers:

ConfigurationNdmodeldffBLEUParams (M)
Small2512204823.736
Medium-small4512204825.350
Base6512204825.865
More layers8512204825.580
Wider (dmodel=1024)61024409626.0168
Big61024409626.4213

Yes, bigger is generally better. Halving the layers (N=2) costs 2.1 BLEU. Doubling the model width to dmodel=1024 helps, especially in the "big" configuration with higher dropout (0.3) and longer training (300K steps).

Interestingly, shrinking dmodel to 256 (with proportionally smaller dk=32) drops to 24.5 BLEU with only 28M parameters. The dff dimension also matters — changing it from 2048 to 1024 gives 25.4, while 4096 gives 26.2.

How Important Is Dropout?

Residual DropoutLabel Smoothing (εls)PPL (dev)BLEU (dev)
0.00.15.7724.6
0.20.14.9525.5
0.1 (base)0.14.9225.8
0.10.04.6725.3
0.10.25.4725.7

Removing dropout entirely (Pdrop=0.0) costs 1.2 BLEU points — the model overfits significantly. Label smoothing also helps BLEU despite hurting perplexity (the model becomes less confident on individual predictions but more accurate overall).

Key Dimension & Positional Encoding

Reducing dk (attention key dimension): When dk is reduced from 64 to 16 (row B), BLEU drops from 25.8 to 25.1. This suggests that computing word-to-word compatibility is a hard problem that benefits from higher-dimensional comparisons.

Learned vs. sinusoidal positional encoding (row E): Replacing the sinusoidal positional encoding with learned positional embeddings produces BLEU of 25.7 vs. 25.8 — virtually identical. The authors chose sinusoids because they might generalize to longer sequences.

Beyond Translation: English Constituency Parsing

Constituency parsing is like diagramming a sentence in school: breaking "The cat sat on the mat" into nested groups like [The cat] [sat [on [the mat]]]. It's a very different task from translation — the output is a tree structure, not another language. Can the Transformer handle it?

To test whether the Transformer was a one-trick pony or a genuinely versatile architecture, the authors applied it to English constituency parsing — with minimal task-specific tuning. They used a 4-layer Transformer with dmodel=1024, trained on:

ParserSettingF1 Score (WSJ §23)
Vinyals & Kaiser (2014)WSJ only88.3
Petrov et al. (2006)WSJ only90.4
Zhu et al. (2013)WSJ only90.4
Dyer et al. (2016)WSJ only91.7
Transformer (4 layers)WSJ only91.3
Zhu et al. (2013)Semi-supervised91.3
McClosky et al. (2006)Semi-supervised92.1
Vinyals & Kaiser (2014)Semi-supervised92.1
Transformer (4 layers)Semi-supervised92.7
Luong et al. (2015)Multi-task93.0
Dyer et al. (2016)Generative93.3

With just 40K training sentences and no task-specific tuning, the Transformer (91.3 F1) nearly matches the best discriminative parser (91.7). In the semi-supervised setting, it achieves 92.7 — the best result among comparable methods, surpassing all previously reported semi-supervised parsers.

This was a powerful early signal that the Transformer wasn't just good at translation — it was a general-purpose architecture for sequence problems.

🌊 Interactive · Adjust parameters

Visualizing Positional Encodings

The positional encodings are sine and cosine waves at different frequencies. Move the slider to see how the encoding pattern changes across positions and dimensions. Each row is a position; each column is a dimension.

Warm colors (gold/red) = positive values, cool colors (blue/purple) = negative values. Notice how the low-frequency waves on the right change slowly across positions, while the high-frequency waves on the left oscillate rapidly. This gives each position a unique "fingerprint."

📈 Interactive · Adjust warmup steps

The Warmup Learning Rate Schedule

The Transformer uses an unusual learning rate strategy: ramp up quickly, then slowly decay. Adjust the warmup period to see how it changes the schedule.

The peak learning rate occurs exactly at the warmup step. After that, the rate decays as 1/√step. With dmodel = 512, the peak rate is about 0.00098.

The Transformer Models at a Glance

Base Model

Layers (N)6
dmodel512
dff2048
Heads (h)8
dk = dv64
Dropout0.1
Parameters65M
Training100K steps / 12h

Big Model

Layers (N)6
dmodel1024
dff4096
Heads (h)16
dk = dv64
Dropout0.3
Parameters213M
Training300K steps / 3.5d

What Changed After This Paper

In their conclusion, the authors wrote: "We are excited about the future of attention-based models and plan to apply them to other tasks." That turned out to be perhaps the greatest understatement in AI history.

2018
BERT (Google) and GPT (OpenAI) both built directly on the Transformer. BERT used the encoder; GPT used the decoder.
2019
GPT-2 showed that scaling up Transformers produces remarkably coherent text generation.
2020
GPT-3 (175 billion parameters) demonstrated that Transformers could perform tasks they weren't trained for, via "in-context learning."
2020+
Vision Transformers (ViT) applied the architecture to images. Transformers spread to audio, protein folding (AlphaFold), code generation, robotics, and more — fulfilling the authors' hope to extend beyond text.
2022–
ChatGPT, Claude, Gemini, and every major large language model is a Transformer descendant. The architecture this paper introduced became the foundation of the entire generative AI revolution.

Key Takeaways

Here's what you could confidently explain to someone after reading this:

1. The Core Innovation

The Transformer replaced sequential processing (RNNs) with self-attention — a mechanism that lets every word in a sentence directly interact with every other word, all at once. This is faster to train, better at capturing long-range relationships, and produces superior results.

2. Multi-Head Attention Is Key

Running 8 parallel attention mechanisms (heads) lets the model simultaneously track different types of linguistic relationships — syntax, semantics, pronoun references, and more.

3. Record-Breaking Performance

28.4 BLEU on English→German (+2 over prior best), 41.8 on English→French (new single-model SOTA). The base model trained in just 12 hours on 8 GPUs — a fraction of the cost of competitors.

4. Generalization

The Transformer isn't just good at translation. With minimal modification, it matched or beat specialized parsers on English constituency parsing — hinting at its potential as a universal sequence-processing architecture.

5. Historical Impact

This paper is the foundation of GPT, BERT, ChatGPT, Gemini, and essentially all modern large language models. The Transformer architecture became the backbone of the AI revolution that followed.