Attention Is All You Need

A new sequence transduction architecture based entirely on attention mechanisms — dispensing with recurrence and convolutions entirely.

"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 convolutions."
Vaswani et al., 2017 · Abstract
28.4 BLEU
EN→DE · WMT 2014
+2 BLEU over previous best ensemble
41.8 BLEU
EN→FR · WMT 2014
Single model, outperforms all prior ensembles
3.5 days
Big model training time
8× NVIDIA P100 GPUs · 300k steps
8 heads
Parallel attention heads
d_k = d_v = 64 per head · d_model = 512
Core Contributions
Arch
Pure Attention
No recurrence, no convolutions — attention all the way
Perf
Parallelization
O(1) sequential ops vs O(n) for RNNs
Mech
Multi-Head Attention
Attend to different subspaces simultaneously
Enc
Positional Encoding
Sinusoidal functions inject sequence order
Authors
Ashish Vaswani Noam Shazeer Niki Parmar Jakob Uszkoreit Llion Jones Aidan N. Gomez Łukasz Kaiser Illia Polosukhin
cs.CL cs.LG Submitted June 12, 2017 · v7 Aug 2023

Encoder–Decoder Stack

The Transformer stacks N=6 identical layers in both encoder and decoder. Click any block below to inspect its role and parameters.

Encoder ×6
Input Embeddings + Positional Encoding
scale × √d_model
Layer Norm
Multi-Head Self-Attention
h=8 heads · d_k=64
Add & Norm (Residual)
Feed-Forward Network
FFN(x) = max(0, xW₁+b₁)W₂+b₂
Add & Norm (Residual)
× 6
⇄
Keys
&
Values
from
encoder
Decoder ×6
Output Embeddings + Positional Encoding
right-shifted outputs
Layer Norm
Masked Multi-Head Self-Attention
causal mask · no future
Add & Norm (Residual)
Multi-Head Cross-Attention
Q from decoder · K,V from encoder
Add & Norm (Residual)
Feed-Forward Network
d_ff = 2048
Add & Norm (Residual)
× 6
Linear + Softmax → Output
Block Inspector
↑ Click a block to inspect
Select any component in the architecture diagram above to see its role, dimensions, and key design choices.
Hyperparameters
512
d_model
8
heads
2048
d_ff
6
N layers

Attention Mechanisms

Two variants work together: Scaled Dot-Product for efficiency, Multi-Head for expressiveness.

Scaled Dot-Product
Multi-Head
Positional Encoding
Attention(Q, K, V) = softmax( QKᵀ / √d_k ) · V
Scaled dot-product — the scaling factor √d_k prevents vanishing gradients in softmax
Q
Queries
What we're looking for
K
Keys
What we're indexing over
V
Values
What we retrieve
Why Scale by √d_k ?

For large values of d_k, the dot products grow large in magnitude, pushing the softmax into regions with extremely small gradients. The scaling factor 1/√d_k counteracts this. With d_k=64, the scale factor is 0.125. The authors suspect additive attention outperforms dot product at large dimensions precisely due to this gradient issue.

MultiHead(Q,K,V) = Concat(head₁,...,headₕ)Wᴼ
where headᵢ = Attention(QWᵢQ, KWᵢK, VWᵢV)

Instead of one attention function on d_model=512 dimensions, run h=8 parallel heads each on d_k=d_v=64 dimensions. Each head can attend to different positional and semantic aspects simultaneously.

8 Attention Heads — click to explore
H1
head_1
Often attends to local context — nearby tokens in the sequence.
H2
head_2
Can capture long-range syntactic dependencies across the sentence.
H3
head_3
May specialize in coreference — tracking which noun a pronoun refers to.
H4
head_4
Sometimes attends to the end of sentence or structural markers.
H5
head_5
Learns position-relative patterns complementary to positional encodings.
H6
head_6
May focus on semantic similarity regardless of position.
H7
head_7
Can attend broadly across the sentence for global context.
H8
head_8
Works alongside H1–7 to form a rich, multi-perspective representation.
Projection Dimensions
d_model
512
=
h heads
8
×
d_k per head
64
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
Why sinusoidal?

The sinusoidal form allows the model to attend to relative positions: for any fixed offset k, PE(pos+k) can be represented as a linear function of PE(pos). The model can learn to use this property.

Extrapolation

The sinusoidal encoding can generalize to sequence lengths longer than those seen during training — an advantage over purely learned positional embeddings.

Learned vs Sinusoidal

The authors found nearly identical results using learned positional embeddings. Sinusoidal was chosen as it requires no additional parameters and may extrapolate better to unseen lengths. Both embed position as additive signal on top of the token embedding.

Why Self-Attention?

Three metrics motivate replacing recurrence with self-attention: computational complexity, parallelizability, and path length for learning long-range dependencies.

Layer Type Complexity / Layer Sequential Ops Max Path Length
Self-Attention O(n² · d) O(1) O(1)
Recurrent O(n · d²) O(n) O(n)
Convolutional O(k · n · d²) O(1) O(log_k(n))
Restricted Self-Attn O(r · n · d) O(1) O(n/r)
When n < d

Self-attention's O(n²·d) is cheaper than recurrence's O(n·d²) when sequence length n is smaller than representation dimension d. This is often true in practice for machine translation.

O(1) Sequential

Unlike RNNs which process tokens one by one (O(n) sequential steps), self-attention processes all positions in parallel. This dramatically speeds up training.

Short Max Path

Any two positions in the sequence can interact in O(1) operations. In RNNs, distant positions must propagate through O(n) steps, making long-range learning harder.

Interpretability Bonus

Self-attention layers yield interpretable behavior: different attention heads specialize in different roles. The paper visualizes heads learning to attend to anaphora resolution (e.g., "its" → its referent), syntactic structure, and other linguistic phenomena — giving researchers insight into what representations the model has learned.

Benchmarks & Ablations

State-of-the-art on WMT 2014 EN→DE and EN→FR translation, and strong performance on English constituency parsing.

8×
NVIDIA P100 GPUs
300k
Big model steps
0.1
Dropout + label smoothing
EN→DE Translation · WMT 2014
Transformer (big) 28.4 BLEU
Transformer (base) 27.3 BLEU
Previous best ensemble 26.36 BLEU
Transformer (big) 41.8 BLEU
Previous best ensemble 41.29 BLEU
Transformer (semi-supervised) 92.7 F1
Transformer (WSJ only) 91.3 F1
Key Ablation Findings (Table 3)
attention_heads
h = 8 optimal
Too few or too many heads hurt quality. 8 heads balance expressiveness vs. parameter dilution.
d_k dimension
Larger d_k helps
Reducing d_k hurts model quality. Compatibility of keys and queries is important.
model_size
Bigger = better
Bigger models (big vs. base) consistently perform better, both for translation quality and for parsing.
dropout
Essential
Dropout at P=0.1 and label smoothing at ε=0.1 are critical regularization. Without both, models overfit.
positional_encoding
Learned ≈ sinusoidal
Learned and sinusoidal positional encodings produce nearly identical BLEU scores.
attention_type
Replacing hurts
Replacing multi-head attention with single-head attention drops BLEU by ~0.9. Each head adds value.