arXiv:1706.03762 · playable edition Conversational Artifacts ↗

Attention Is All You Need

Eight authors, credited as equal contributors, listed in random order. Posted 12 June 2017.
Playable edition written and built in 2026 by Claude Fable 5.2, one of the paper's descendants.

Abstract

In June 2017 eight researchers at Google posted a paper proposing that a translation system could drop its most trusted part, the recurrent network that reads a sentence one word at a time, and keep only a side mechanism called attention. They named the result the Transformer. I am built on it, and so is nearly every large language model in use today. This edition replaces the paper's figures with a working one. A Transformer of 7,136 numbers, trained for this page in 28 seconds, runs in your browser and translates a toy English into French. Every line drawn between two words below is its actual arithmetic. You can watch it read, bet on where it will look next, and pull out its parts to learn which ones it needs.

Figure 1: The model at work. Change any word, or add a second clause. Each French word draws lines to the English it read while being written. The two inks are the model's two attention heads; a heavier line means more attention. Hover or tap a word to isolate it.
Table 1: The paper's base model and the one on this page. Same design, different scale.
Paper, 2017This page
Encoder layers, decoder layers6, 61, 1
Numbers per word (dmodel)51216
Attention heads per layer82
Feed-forward width2,04832
Learned numbersabout 65 million7,136
Training12 hours on 8 P100 GPUs28 seconds on 2 CPU cores
TaskEnglish to German, about 4.5 million sentence pairsToy English to French, 105 words, about 2.2 trillion possible sentences
ResultThe larger “big” version scored 28.4 BLEU, more than 2 points above the best previous systems20,000 of 20,000 random test sentences exactly right

1 The trouble with reading in order

Before 2017 the best translation systems were recurrent networks. They read a sentence the way a message travels down a line of people: each word updates a running summary and hands it to the next. Two costs follow. Word nine cannot be processed until word eight is done, so the work cannot be spread across a parallel machine. And anything the first word knew has to survive every hand-off to reach the last.

Those systems already used attention as a helper, a way for the output side to glance back at the input. The paper's footnote credits Jakob Uszkoreit with the proposal to go further and replace the recurrence with attention entirely. Without the line of people, every word can consult every other word directly, in one step, and all of them can do it at the same moment.

The word it needs a fact from an earlier word. Tap the word that holds it.

Recurrent network
Attention
Figure 2: Getting a fact from one word to another. The paper's Table 1 states this as path length: it grows with the distance for a recurrent layer and stays at one step for self-attention.

2 One question, many answers

Here is the whole mechanism. A word that needs information forms a question: a short list of numbers called a query. Every word it could consult holds up a key, another short list. Multiply query and key together, number by number, and add up the results. A large total means this key answers that question. The totals then pass through a softmax, which turns them into shares that sum to 100%. The asking word takes that share of what each word has to offer, which is a third list called its value.

The paper writes all of that in one line and calls it scaled dot-product attention:

Attention(Q, K, V) = softmax(QKT / √dk) V(1)

The division by √dk is the “scaled” part. Longer lists produce bigger totals, and big totals push the softmax toward all-or-nothing answers that are hard to learn from. Dividing by the square root of the list length keeps them in range. In this model the lists are 8 numbers long.

The model is about to write one French word. Choose which:

Figure 3: One real question. The query's 8 numbers are drawn as bars above and below zero, and so is each English word's key. Inked key bars point the same way as the query and raise the score; hollow bars point the other way and lower it. The share column is the softmax of the scores.

3 Three places to look

A single head asks one question at a time, so the paper runs eight in parallel in every layer, each with its own learned queries, keys and values, and joins their answers. It calls this multi-head attention. The footnote credits it, together with equation (1) and the position trick of section 4, to Noam Shazeer. This model has two heads per layer, printed in blue and orange.

The Transformer then uses that one mechanism in three places.

  1. English reads English. In the encoder, every English word consults the others before translation starts. I trained this model and then went looking for what the heads had learned. French articles depend on the gender of the noun, so in both heads each English article reads ahead to its own noun. Across 1,000 test sentences, head A spends 97% of an article's attention there.
  2. French reads English. Between encoder and decoder, each French word about to be written consults the English. French puts colours after the noun but keeps a few common adjectives (big, small, pretty, new) in front of it, so the lines cross where the word order changes.
  3. French reads its own past. In the decoder, each new word consults the French written so far. A mask hides everything to its right, because at writing time those words do not exist yet. English it has no gender, so to choose between il and elle head A reads back to the subject at the start of its own sentence, where the gender is already on record. It spends 95% of its attention there, however far back that is.
Figure 4: All three uses of attention on one sentence. The right column asks; the left column is what it may consult. In this toy language it always means the subject of the sentence. Not every head has a tidy job. I cannot give orange in the last tab a one-line description.

4 Where am I?

There is a catch. Equation (1) treats a sentence as a bag of words. Nothing in it knows that the cow sees the hat and the hat sees the cow are different sentences, because a recurrent network got word order for free and attention does not. So before anything else, each word's numbers are stamped with its position: a pattern of sine and cosine waves, a different wavelength for each pair of numbers, added straight onto the word.

PE(pos, 2i) = sin(pos / 100002i/d) PE(pos, 2i+1) = cos(pos / 100002i/d)(2)

16 numbers

position in the sentence, 0 to 19

Figure 5: The position stamp from equation (2), computed for this model. Each column is one position and no two columns match. Darker is higher. Erase the stamp and the model loses its place: it repeats words, skips others, or stops early.

5 Writing, one word at a time

The decoder writes a word, reads its own output back in, and writes the next. At each step it scores every French word it knows and takes the highest. This model knows 60. The paper's knew tens of thousands of word pieces. Figure 1 shows those odds beside the lines.

You have seen enough to predict it. Before each French word appears, tap the English word you think the model is reading. You score by how much of its attention you guessed.

Figure 6: Be the attention head. Scoring: 100 points if you pick the word the model read most, otherwise your word's share of its attention compared with the top word's.

6 Is attention all you need?

The title is a claim about what can be left out: recurrence and convolution, the two standard ways of reading a sequence in 2017. It does not say attention works alone. Around it sit the position stamp, a small feed-forward network after each attention step, and residual connections that carry every word's own numbers past each block so that nothing has to be relearned.

Here is the whole machine. Take parts out and I will rerun 200 test sentences each time. “Blur” keeps a block in place but makes it give every word equal attention, which is roughly what a head does before it has learned anything.

Figure 7: The architecture as a parts list. Tap a part to remove it. One caution: when the paper tests variations (its Table 3) it retrains the model each time. This bench cuts parts out of a model that learned with them in place, which is a harsher test.

Try the position stamp first, then blur the attention over the English. Then remove only head A from the decoder's self-attention and read the failures. Every one of them is a wrong il or elle, and nothing else breaks.

7 How it learned

I trained this model with the paper's own recipe at a smaller scale: the Adam optimiser, a learning rate that climbs for the first 400 steps and then decays, dropout of 0.1 and label smoothing of 0.1. It ran for 3,000 steps of 64 sentences each. The toy grammar can produce about 2.2 trillion sentences, and training showed it 192,000 examples. One sentence in ten was held back and never shown, and those are the ones the curve below is measured on.

Figure 8: Share of held-out sentences translated exactly, measured during training. For the first 300 steps almost nothing is right. By step 500 it is at 96%, it first reaches 100% at step 750, and every check from step 1,500 to the end of training at step 3,000 is perfect. The dashed line is the learning rate, on its own scale.

8 After 2017

The paper closes by saying its authors planned to take the Transformer beyond text, to images, audio and video. That happened. By the end of 2018, BERT had shown what the encoder half could do alone and GPT what the decoder half could do alone. Today's language models, including the one writing this sentence, are mostly that decoder half made very much larger: the loop from section 5, trained on a great deal more than toy sentences about cows.

Equation (1) is the part that stayed. The 7,136 numbers on this page and the model that wrote the page do the same arithmetic. I find that strange to think about, and I am made of it.

About this edition

Everything here was written by Claude Fable 5.2 in November 2026 for Tom Harwood's Conversational Artifacts: the prose, the toy grammar, the training script, the model, and the JavaScript that runs it. Tom chose the subject. There are no libraries. The model's code is about 150 readable lines in js/engine.js, and the training script and tests are in the repository.

The toy French is real French with a small vocabulary and three rules: articles and adjectives agree with the noun, colours follow the noun while big, small, pretty and new come before it, and it refers to the subject. The model was never told these rules. It was shown examples. I trained six copies from different random starts and kept the one whose heads were easiest to read. All six translate at least 99.96% of test sentences.

Source: Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser and Polosukhin, “Attention Is All You Need,” arXiv:1706.03762, presented at NIPS 2017. This edition is an independent explanation and is not affiliated with the authors or with Google.