Attention Is All You Need: The Paper, Explained

A beginner-friendly guide to the Transformer — the architecture behind ChatGPT, Gemini, Claude, and almost every modern AI model. Every technical term is defined. Every concept is grounded in analogy.

Paper by Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser & Polosukhin (Google Brain & Google Research, 2017) • Explainer published June 2026

The Big Picture

In 2017, the best way to make a computer translate a sentence — or do almost anything with language — was to feed it the words one at a time, in order, using a kind of network that kept a running memory of what it had seen so far. These were called recurrent neural networksA recurrent neural network (RNN) processes a sequence one element at a time, updating an internal "memory" (hidden state) at each step and passing it forward. Powerful for sequences, but inherently sequential.. They worked, but they had a stubborn flaw: because each word had to wait for the previous word to be processed, you couldn't do the work in parallel — and modern hardware (GPUsGraphics Processing Units — chips with thousands of small cores that do many calculations at once. They make deep learning fast, but only if the work can be split into parallel pieces. RNNs couldn't.) is built to do thousands of things at once.

The Transformer's radical proposal was in its title: attention is all you need. Throw out the step-by-step recurrence entirely. Instead, let every word look directly at every other word in the sentence, all at the same time, and decide for itself which other words matter. That single idea — called self-attentionA mechanism that lets each word in a sentence directly "look at" and pull information from every other word, computing for itself how relevant each one is. The core building block of the Transformer. — is what the whole paper is built on. The problems it solved:

  1. Recurrence forbids parallelism. Processing word 10 required finishing words 1 through 9 first. On a chip designed to run thousands of operations simultaneously, that's like owning a highway and being forced to drive single file. The Transformer processes all words at once.
  2. Long-range connections were fragile. To link the first word of a paragraph to the last, an RNN had to pass information through every word in between — a long, lossy game of telephone. The Transformer connects any two words directly, in a single step, no matter how far apart.
  3. Training was slow and expensive. Because of the above, top translation models took a long time and a lot of compute. The Transformer beat them while training in a small fraction of the time.
Replace the sequential "read word by word and remember" loop with a parallel "every word attends to every other word at once" operation. This made training dramatically faster, made long-range relationships easy to learn, and — almost as a side effect — produced an architecture so general it now powers nearly every large AI model in existence.

See It In Action

Before any math, here's the intuition you should hold onto. When a model reads a sentence, the word “it” is ambiguous — what does it refer to? Self-attention lets the word “it” look at every other word and decide. Click any word below to see (illustratively) which other words it would pay the most attention to. Notice that “it” leans heavily on “animal”.

👈 Click a word to make it the “query.” Brighter teal = more attention paid to that word.
Click any word to begin.
less attentionmore attention

That's the entire promise of attention, in miniature: relevance is computed, not assumed. The model isn't told that “it” refers to “animal” — it learns to compute a high relevance score between them. Now let's build up how that actually works.

Background Concepts

This paper assumes you're comfortable with the basics of neural networks for language. Let's build that vocabulary from scratch — each idea here is a prerequisite for understanding the Transformer.

From words to numbers: tokens and embeddings

A neural network only understands numbers, not letters. So the first step in any language model is to chop text into pieces called tokensThe small units a model reads — often words or word-fragments. "unbelievable" might become "un", "believ", "able". The paper uses ~32,000–37,000 sub-word tokens via byte-pair encoding. (roughly, words or word-fragments), then map each token to a list of numbers called an embeddingA vector (a list of numbers, e.g. 512 of them) that represents a token's meaning. Words with similar meanings get similar vectors. The model learns these during training.. An embedding is just a vectorAn ordered list of numbers, like [0.2, -1.1, 0.7, ...]. You can think of it as coordinates pointing to a location in a high-dimensional "meaning space." — a point in a high-dimensional “meaning space” where similar words sit near each other. In this paper every token becomes a vector of 512 numbers.

An embedding is like giving every word GPS coordinates in a vast map of meaning. “King” and “queen” live in the same neighborhood; “banana” is on the other side of town. The model isn't handed this map — it draws it during training, nudging related words closer together.

The old workhorse: recurrent networks and their bottleneck

Before the Transformer, sequence problems were ruled by RNNsRecurrent Neural Networks — networks that read a sequence step by step, carrying a memory (hidden state) forward from one step to the next. and their smarter cousins, LSTMsLong Short-Term Memory networks — a type of RNN with gates that help it remember information over longer stretches without it fading away. and GRUsGated Recurrent Units — a streamlined LSTM variant. Like LSTMs, designed to carry memory across many steps.. They read a sentence the way you read aloud: one word at a time, updating a mental summary (the hidden stateThe RNN's running memory — a vector updated at each step that's supposed to summarize everything seen so far.) as you go. This is intuitive, but it forces two costs: the work is strictly sequential (word 10 can't start until word 9 is done), and information from far-back words has to survive a long relay to reach the present, often fading along the way.

Encoder and decoder: read, then write

Translation models share a common shape: an encoderThe half of the model that reads and "understands" the input sentence, turning it into a set of rich numeric representations. reads the whole input sentence and compresses its meaning into a set of vectors; a decoderThe half that writes the output sentence, one token at a time, using both what it has written so far and the encoder's representation of the input. then writes the output sentence using that understanding. The decoder works autoregressively"Auto-regressive" means it generates one token at a time, and each new token is fed back in as input to help predict the next one. This is how GPT-style models still generate text today. — it produces one word, feeds that word back in, produces the next, and so on. The Transformer keeps this encoder–decoder shape but rebuilds both halves out of attention.

Softmax: turning scores into a spotlight

One small function shows up everywhere in this paper: softmaxA function that turns a list of raw scores into positive numbers that add up to 1 (a probability distribution). Big scores get exaggerated; small ones get squashed toward zero.. It takes a list of raw numbers (“scores”) and turns them into percentages that add up to 100%, exaggerating the big ones and shrinking the small ones. Attention uses softmax to turn raw “how relevant is this word?” scores into clean weights — a spotlight that mostly lands on the few words that matter and dims the rest.

The Heart of It: Attention

Everything in the Transformer rests on one operation. The paper describes attention with three roles that every word plays at once: a Query, a Key, and a Value. The names come from databases, and the analogy is the clearest way in.

Q — Query

What I'm looking for. Each word forms a question: “which other words are relevant to me?”

K — Key

What I advertise about myself. Every word offers a label that queries can match against.

V — Value

What I actually contribute. The information a word hands over if it gets attended to.

Attention is like searching a library. Your query is what you want ("books about volcanoes"). Each book has a spine label — its key ("Geology", "Cooking", "Volcanoes"). You compare your query to every key to score the match, then you actually read the contents — the value — of the books that matched, weighted by how good the match was. You don't read just one book; you blend the contents of all of them, paying most attention to the best matches.

Scaled dot-product attention

Here is the actual recipe, in plain steps. For one query word:

  1. Score every word. Compare the query against each word's key using a dot productA way of multiplying two vectors into a single number that measures how aligned they are. Big when the vectors point the same way — a natural "similarity" score. — a single number measuring how well they match.
  2. Scale the scores. Divide every score by √dkThe square root of the key dimension (d_k = 64 in the base model). Dividing by it keeps the scores from growing too large, which would push softmax into a region where it barely learns. A small but crucial stabilizer. to keep them from getting too large.
  3. Softmax into weights. Run the scores through softmax so they become positive weights that sum to 1 — the attention spotlight.
  4. Blend the values. Take a weighted sum of all the words' values, using those weights. The result is the query word's new, context-aware representation.

The paper writes this as one compact formula, Attention(Q, K, V) = softmax(QKT / √dk) V, and computes it for all words simultaneously as a single matrix multiplication — which is exactly why it's so fast on a GPU. The video below walks through it for the word “it.”

made withHyperFrames A query word scores every other word, softmax turns those scores into attention weights, and the output is a weighted blend of the matched words' values.
Why divide by √dk? (the “scaled” part)

When you take a dot product of two long vectors of random-ish numbers, the result tends to grow larger as the vectors get longer (specifically, its variance grows with the dimension dkThe dimension of the key and query vectors — 64 in the base model (512 total, split across 8 heads).). Very large scores push softmax into a regime where almost all the weight piles onto one option and the gradientsThe signals used to train a network — they tell each weight which direction to adjust. If softmax saturates, these signals shrink toward zero and learning stalls. become tiny, so the model learns slowly. Dividing by √dk rescales the scores back to a sane range, keeping training stable. It's a one-character fix with an outsized effect.

Self-attention vs. cross-attention

The same operation gets used in two modes. In self-attention, the queries, keys, and values all come from the same sentence — words looking at their own neighbors to build context (this is what the “it” → “animal” demo showed). In cross-attention, the queries come from the sentence being written (the decoder) while the keys and values come from the input sentence (the encoder) — this is how the translation looks back at the original. Same math, different sources.

Many heads are better than one

A single attention operation can only focus one way at a time. But a word relates to others in many ways at once — grammatically, semantically, by position. So the Transformer runs attention multiple times in parallel, each with its own learned projections, and calls each one a headOne independent attention computation. The base Transformer uses 8 heads, each operating on a 64-dimensional slice, so the total cost matches a single full-size attention.. The paper uses 8 heads. Each head can specialize — one might track the subject of a verb, another the next word, another a long-range reference — and their outputs are concatenated and mixed back together.

made withHyperFrames The input is split into 8 heads that each attend differently — tracking adjacent words, the subject, the object, long-range links — all in parallel, then concatenated back into one richer vector.
Multi-head attention is the difference between reading a sentence with one lens and reading it with eight at once. Each head learns to capture a different kind of relationship, and because they run in parallel on smaller slices, you get this richness for roughly the same compute as a single attention.

The Transformer, Assembled

Now we can stack the pieces into the full model. The Transformer keeps the classic two-tower shape — an encoder that reads the input and a decoder that writes the output — but each tower is a stack of 6 identical layers built from attention and simple feed-forward networks.

Encoder ×6
input tokens+ positional encoding
Multi-Head Self-Attention
every word attends to every other word
Add & Norm
residual connection + layer normalization
Feed-Forward Network
a small per-word transformation
Add & Norm
residual + normalize again
output: a context-rich vector per input word →
Decoder ×6
output so far+ positional encoding
Masked Multi-Head Self-Attention
attends only to earlier output words
Cross-Attention
queries the encoder's output (the input sentence)
Feed-Forward + Add & Norm
same per-word transform & normalization
Linear + Softmax
probabilities for the next token
output: the next word of the translation

A few components in that diagram deserve a closer look.

The feed-forward network: thinking about each word on its own

After attention has mixed information between words, each word passes through a small feed-forward networkA simple two-layer transformation applied to each position independently: expand to 2048 dimensions, apply a ReLU non-linearity, shrink back to 512. It lets the model process the information attention just gathered. — the same little two-step transformation applied to every position separately. If attention is the step where words talk to each other, the feed-forward network is the step where each word thinks privately about what it just heard. It expands each 512-number vector to 2048, applies a ReLURectified Linear Unit — a simple non-linear function that keeps positive numbers and zeroes out negatives. It lets the network learn non-linear, more expressive patterns. non-linearity, and shrinks it back.

Residual connections and layer normalization: keeping deep stacks trainable

Around every sub-layer, the Transformer adds two pieces of plumbing. A residual connectionA shortcut that adds a sub-layer's input to its output (output = x + Sublayer(x)). It gives gradients a clear path through deep networks, making them much easier to train. adds each sub-layer's input back to its output, giving information (and training signal) a clean shortcut through all 6 layers. Then layer normalizationA step that re-centers and re-scales a vector to have a consistent statistical range. It stabilizes and speeds up training of deep networks. rescales the result to a stable range. These aren't glamorous, but without them, stacking 6+ layers of attention would be far harder to train.

Positional encoding: putting the words back in order

Here's a subtle problem. Because the Transformer looks at all words at once instead of in sequence, it has no inherent idea of word order — to it, “dog bites man” and “man bites dog” would look identical. The fix is positional encodingA pattern of numbers added to each word's embedding that encodes its position in the sentence. The paper uses sine and cosine waves of different frequencies so every position gets a unique, learnable-to-interpret signature.: before the first layer, each word's embedding gets a unique “position fingerprint” added to it, built from sine and cosine waves of different frequencies.

made withHyperFrames Each column is one position's unique code, built from sine/cosine waves — fast-changing in some dimensions, slow in others. Added to the word embedding, it tells the model where each word sits.
Why sine waves instead of just numbering the positions 1, 2, 3…?

Plain position numbers don't generalize well and grow without bound. The sinusoidal scheme has two nice properties. First, every position gets a distinct pattern across the 512 dimensions (slow waves capture coarse position, fast waves capture fine position). Second — and this is the clever part — for any fixed offset k, the encoding of position pos+k is a simple linear function of the encoding at pos, which the authors hypothesized would make it easy for the model to learn to attend by relative position ("the word three back"). They also tried learned position vectors and got nearly identical results — but chose sinusoids because they might extrapolate to sentences longer than any seen in training.

Masking: why the decoder can't peek ahead

When the decoder writes word number 5, it must not be allowed to see words 6, 7, 8… — those don't exist yet at generation time, and letting the model see them during training would be cheating. So the decoder's self-attention is maskedA trick that blocks attention to future positions by setting their scores to negative infinity before softmax (which makes their weights zero). It preserves the auto-regressive property: position i can only use positions up to i.: any attention weight pointing to a future word is forced to zero. We'll see this in motion in the inference section.

Why Self-Attention Wins

Section 4 of the paper makes the efficiency argument explicit by comparing self-attention to recurrent and convolutional layers on three measures. The headline is about path lengthThe number of steps information must travel to get from one position to another through the network. Shorter paths make long-range dependencies easier to learn.: how many steps must information travel to connect two words?

made withHyperFrames An RNN connects the first and last words only after a chain of n sequential hops. Self-attention connects every pair of words directly, in a single step — and does it for all pairs in parallel.
Layer typeCompute per layerSequential stepsMax path length
Self-AttentionO(n² · d)O(1)O(1)
Recurrent (RNN/LSTM)O(n · d²)O(n)O(n)
ConvolutionalO(k · n · d²)O(1)O(logk n)

Here n is the number of words and d is the vector size. The two columns that matter: self-attention needs only O(1) sequential steps (everything happens in one parallel pass) and has an O(1) maximum path length (any word reaches any other directly). Recurrent layers need O(n) on both — the bottleneck the Transformer was designed to escape.

The catch: that O(n²) compute term

Self-attention isn't free. Because every word attends to every other word, the compute grows with the square of the sentence length (n²). For the sentence lengths in this paper that's a great trade — it's cheaper than recurrence whenever n is smaller than the vector size d, which is usually true. But that same n² term is exactly why, years later, a whole research field sprang up to make attention cheaper for very long inputs (think book-length documents). The authors even floated restricting attention to a local neighborhood as future work. The Transformer won by spending compute to buy parallelism and short paths — a bet that paid off as hardware kept getting faster.

A bonus the authors noticed: self-attention is more interpretable. You can literally read off which words each head attended to, and many heads turn out to learn recognizable grammatical roles — following a verb to its object, or resolving what a pronoun refers to.

How It's Trained

The Transformer was trained on standard machine-translation datasets — about 4.5 million English–German sentence pairs, and a larger English–French set — with a few important tricks that made training fast and stable.

1 Prepare the data Split text into ~37K sub-word tokens; batch sentences of similar length together (~25K tokens per batch).
2 Optimize with warmup Use the Adam optimizer with a learning rate that ramps up for 4,000 steps, then decays — a schedule that proved key to stable training.
3 Regularize Apply dropout and label smoothing so the model generalizes instead of memorizing the training set.
The learning-rate warmup schedule

The learning rateHow big a step the optimizer takes when adjusting the model's weights. Too high and training diverges; too low and it crawls. isn't constant. The paper increases it linearly for the first 4,000 warmup stepsAn initial phase where the learning rate ramps up from near zero. It prevents the unstable early updates that a large fixed rate would cause in a fresh, randomly-initialized model., then decreases it in proportion to the inverse square root of the step number. The intuition: a brand-new model is fragile, so take small steps at first; once it finds its footing, take bigger steps, then gradually settle down. This warmup trick became standard practice for training Transformers. They used the Adam optimizerA popular optimization algorithm that adapts the step size for each weight automatically. The workhorse optimizer for deep learning. to do the actual weight updates.

Dropout and label smoothing

DropoutDuring training, randomly switch off a fraction of the network's connections each step. This stops the model from over-relying on any one path and reduces overfitting. randomly disables 10% of connections during training so the model can't lean too hard on any single pathway — a classic defense against overfittingWhen a model memorizes its training data instead of learning general patterns, so it performs well in practice runs but poorly on new examples.. Label smoothingInstead of training the model to be 100% certain of the correct word, train it toward, say, 90% — leaving a little probability spread across others. It hurts raw confidence but improves accuracy and translation quality. stops the model from becoming overconfident: rather than demanding it predict the correct word with 100% certainty, it's trained toward a slightly softer target. Counter-intuitively this worsens the model's confidence metric but improves its actual accuracy and translation scores.

The whole base model trained in about 12 hours on 8 GPUs; the larger model took 3.5 days — a small fraction of what competing models of the day required.

How It Generates Text

Once trained, the Transformer generates a translation the same way GPT-style models still generate text today: one token at a time, feeding each new token back in to predict the next. At every step, the decoder attends to the full input sentence (cross-attention) and to the words it has already written — but, thanks to masking, never to words it hasn't written yet.

made withHyperFrames The decoder writes the translation word by word. The causal mask (right) guarantees each position can only look at positions up to itself — the future stays hidden until it's written.
Beam search: not just grabbing the single best word each step

Greedily picking the highest-probability word at every step can paint you into a corner — an early choice that looks good locally might doom the rest of the sentence. So at inference the paper uses beam searchA search strategy that keeps the few most promising partial sentences ("beams") alive at each step instead of committing to one, then picks the best complete sentence at the end. The paper uses a beam size of 4., which keeps the top 4 candidate sentences alive simultaneously and only commits at the end. A small length penaltyAn adjustment that keeps beam search from unfairly preferring very short outputs (which tend to have higher total probability just by being shorter). keeps it from preferring suspiciously short translations.

Results

The Transformer didn't just match the previous best translation systems — it beat them, while training far more cheaply. Translation quality is measured in BLEUBilingual Evaluation Understudy — a score from 0 to 100 measuring how closely a machine translation matches human reference translations. Higher is better; a couple of points is a meaningful gap., where higher is better.

ModelEN→DE BLEUEN→FR BLEUTraining cost (FLOPs)
GNMT + RL (Google, 2016)24.639.9~1.4 × 1020
ConvS2S (Facebook)25.240.5~1.5 × 1020
Previous best ensemble26.441.3~1.2 × 1021
Transformer (base)27.338.13.3 × 1018
Transformer (big)28.441.82.3 × 1019

Two things stand out. The big Transformer set a new state of the art on English→German (28.4 BLEU, more than 2 points above the previous best, including ensembles) and English→French (41.8). And the cost column is the real shock: even the base model trained for 10–100× less compute — measured in FLOPsFloating-point operations — the total count of arithmetic operations a computer performs. A standard way to measure how much raw computation training a model takes., the total arithmetic operations — than the competition. More quality, far less cost.

What the ablations taught us

The authors also varied the model piece by piece to see what mattered. The lessons: too few attention heads hurt quality, but so did too many (8 was a sweet spot); shrinking the key size hurt, suggesting matching words is genuinely hard; bigger models were reliably better; and dropout was essential. They also confirmed the Transformer generalizes beyond translation by applying it successfully to English sentence parsingConstituency parsing — analyzing the grammatical structure of a sentence into a tree. A very different task from translation, used here to show the architecture is general., a structurally very different task.

Final Quiz

What was the Transformer's core proposal?
In attention, what do the Query, Key, and Value represent?
Why does the Transformer add positional encodings?
Why use multiple attention heads instead of one?
What's the key advantage of self-attention's "path length"?
Why is the decoder's self-attention "masked"?

Why This Paper Matters

For builders and practitioners

If you've used ChatGPT, Claude, Gemini, Copilot, or virtually any modern AI product, you've used a descendant of this exact architecture. The Transformer turned out to be far more than a translation model: because it's parallelizable and scales gracefully, it became the substrate for large language modelsModels like GPT, Claude, and Gemini, trained on vast amounts of text. They are, at their core, very large Transformers., image models, speech models, and protein-folding models. The practical lessons are still copied verbatim today — multi-head attention, residual-plus-normalization blocks, warmup schedules, sub-word tokenization. Learning this paper isn't studying history; it's learning the blueprint your tools are built from.

For the research community

The paper's deepest contribution was a removal, not an addition: it showed that the recurrence everyone assumed was essential for sequence modeling could be dropped entirely. That cleared the way for scaling. Within a year, BERT and GPT (the first big Transformer-based language models) showed the same architecture, trained on raw text at scale, could be repurposed for almost any language task — kicking off the pretraining era. The Transformer also made models more interpretable (you can inspect attention) and more general (the same block works across modalities). Few papers have ever changed a field's default architecture this completely.

The bigger picture

“Attention Is All You Need” is arguably the most consequential machine-learning paper of its decade. It didn't just win a translation benchmark; it handed the field an architecture that kept getting better as you made it bigger and fed it more data — the property that, scaled up a thousandfold, produced the generative-AI moment we're living through now. The authors closed by saying they were “excited about the future of attention-based models” and planned to extend them beyond text to images, audio, and video. That turned out to be a dramatic understatement. The entire modern AI landscape is, in a real sense, a footnote to this 2017 paper.

Glossary

Transformer

The neural-network architecture introduced by this paper, built entirely from attention instead of recurrence. The foundation of modern large AI models.

Attention

An operation where each element scores how relevant every other element is, then blends their information by those scores.

Self-attention

Attention where queries, keys, and values all come from the same sequence — words building context from their own neighbors.

Cross-attention

Attention where queries come from the decoder and keys/values from the encoder — how the output looks back at the input.

Query / Key / Value (Q/K/V)

The three roles each word plays: what it's looking for, what it advertises, and what it contributes when matched.

Multi-head attention

Running several attention computations in parallel, each on a slice of the vector, so different heads capture different relationships. The paper uses 8.

Scaled dot-product attention

The specific attention recipe: dot-product scores, divided by √d_k, softmaxed into weights, used to blend values.

Encoder

The stack that reads the input sentence and turns it into context-rich vectors.

Decoder

The stack that writes the output one token at a time, using its own past output and the encoder's vectors.

RNN / LSTM / GRU

Recurrent networks that process sequences step by step, carrying a memory forward. What the Transformer replaced.

Token

The small unit of text a model reads — a word or word-fragment.

Embedding

A vector of numbers representing a token's meaning; similar tokens get similar vectors.

Softmax

Turns raw scores into positive weights that sum to 1, exaggerating big scores and shrinking small ones.

Dot product

Multiplying two vectors into one number that measures how aligned they are — a similarity score.

Feed-forward network

A small per-word transformation (expand to 2048, ReLU, shrink to 512) applied after attention.

Residual connection

A shortcut adding a sub-layer's input to its output, easing gradient flow through deep stacks.

Layer normalization

Re-scales a vector to a stable statistical range, stabilizing training.

Positional encoding

Position "fingerprints" (sine/cosine waves) added to embeddings so the model knows word order.

Masking

Blocking attention to future positions (scores set to −∞) so the decoder can't peek ahead.

Autoregressive

Generating one token at a time, feeding each output back in to predict the next.

Beam search

Keeping the few most promising partial outputs alive during generation, then choosing the best complete one.

Path length

How many steps information travels to connect two positions. O(1) for self-attention, O(n) for RNNs.

BLEU

A 0–100 score for how closely a machine translation matches human references. Higher is better.

Warmup schedule

Ramping the learning rate up for the first 4,000 steps, then decaying it — key to stable Transformer training.

Label smoothing

Training toward slightly-less-than-100% confidence in the correct token; hurts confidence but improves accuracy.

Dropout

Randomly switching off connections during training to prevent overfitting.