Skip to content
Hitesh Dua

ELI5 · explained in picturesarXiv 1706.03762 · NIPS 2017

How AI learned to read a whole sentence at once

The 2017 Google paper behind the “T” in ChatGPT, explained with pictures and as few words as possible.

31st Conference on Neural Information Processing Systems (NIPS 2017)

Attention Is All You Need

Ashish Vaswani · Noam Shazeer · Niki Parmar · Jakob Uszkoreit · Llion Jones · Aidan N. Gomez · Łukasz Kaiser · Illia Polosukhin

Google Brain · Google Research · University of Toronto

Therobotdroppedtheboxbecauseitwasclumsy
Every word is linked to every other word. The orange lines show where “it” looks, and the thickest one goes to “robot”.

Old AI read one word at a time.

It passed a small note from word to word, so long sentences got blurry and every step waited for the one before. The Transformer lets every word look at every other word, all in one step.

In the paperrecurrent networks, replaced by self-attention (Table 1)

Before: a chain

the note, when itreaches “it”:becauseboxthedroppedrobot?TheTherobotdroppedtheboxbecauseitwasclumsy

9 turns, one after another. News of “robot” reaches “it” after 5 hand-offs.

Transformer: a web

1 stepTherobotdroppedtheboxbecauseitwasclumsy

Every pair at the same time. “it” sees “robot” directly.

The same nine words, read two ways.

Who is “it”?

To understand “it”, the model checks every word and leans on the ones that fit. Change one word and the lean moves. A translator needs this: in German, “it” is sie for the box but er for the robot.

In the paperattention weights. Figure 4 shows real attention heads working out what “its” refers to.

…because it was heavy.

itThe3%robot12%dropped5%the2%box54%because3%it5%was3%heavy13%

“it” is the box, 54%. German: sie (die Kiste).

…because it was clumsy.

itThe3%robot57%dropped4%the2%box10%because3%it5%was3%clumsy13%

“it” is the robot, 57%. German: er (der Roboter).

Line thickness shows how much “it” leans on each word. The percentages are made up for teaching.

Ask, match, mix.

Every word carries three cards: a question, a name tag, and its meaning. Each question is matched against every name tag, and each word mixes in the meanings of its best matches.

In the paperquery (the question), key (the name tag), value (the meaning)

1 · Ask

itwho could be clumsy?Qevery word wears a name tagrobotcan do thingsKboxgets moveddroppedan actionclumsya description+ 5 more words, each with a tag

“it” asks a question. Every word shows a name tag.

2 · Match

it“who could be clumsy?”The3%robot57%dropped4%the2%box10%because3%it5%was3%clumsy13%the shares add up to 100%

Good matches get big shares.

3 · Mix

robot57%clumsy13%box10%the rest20%itthe new “it”57% robot20% the rest

The new “it” is a mix: 57% robot.

The paper’s version: one line

Attention(Q, K, V) =softmax(QKT/dk)Vmatchmixmake shares (100%)÷ 8 keeps scores calm
This happens for every word at the same time. Real questions and name tags are lists of numbers, not sentences. The paper writes all three steps as one line (its Equation 1); dividing by √64 = 8 keeps the scores from getting too big.

Eight looks at once.

One look can’t notice everything. So the model takes 8 looks side by side, like 8 readers with 8 highlighters, and glues their notes together.

In the papermulti-head attention: 8 heads of 64 numbers each (Figure 2)

who is “it”?itrobotwho dropped?droppedrobotdropped what?droppedboxwhy?becauseclumsywhich one?theboxword beforeeachpreviousword aftereachnextbig pictureiteverything
glue the 8 notes togetherone note per word: 8 × 64 = 512 numbers
Each head learns its own job during training. These jobs are examples; the paper’s Figures 3 to 5 show real heads doing jobs like these.

Who sits where?

Looking at everything at once loses the order: “dog bites man” and “man bites dog” look the same. So every word gets a stamp of its seat number, drawn with clock hands.

In the paperpositional encoding, made of sine and cosine waves

Without seats

dogbitesmanmandogbitesmanbitesdogmandogbitessame!

Same words, same jar. The order is gone.

With seat stamps

1The2robot3dropped4the5box6because7it8was9clumsyfast handmedium handslow handno two seatslook alike

A fast, a medium and a slow hand give every seat its own pattern.

The hands are drawn at their real angles from the paper’s formula. The real stamp has 256 hands; the slowest turns about 10,000 times slower than the fastest.

A Reader and a Writer.

The Reader turns the English into notes. The Writer writes German while checking those notes. Each is a tower of 6 floors, and every floor does the same thing: look, then think.

In the paperencoder (the Reader) and decoder (the Writer), N = 6 layers each (Figure 1)

  • look at the other words
  • look at the Reader’s notes
  • think it over
123456the Reader’s notesdienext wordgo againThe robot …Der Roboter ließReaderreads the EnglishWriterwrites the German
The paper’s Figure 1, redrawn as two towers. Inside, each word is a list of 512 numbers; the “think” step briefly widens it to 2,048.

It writes one word at a time.

The Writer picks the most likely next word, adds it, and goes again. It can never peek at words it hasn’t written yet.

In the paperauto-regressive decoding, with a mask that hides future words

not written yet, no peekingnew wordDerRoboterließdieKistefallen,weilerungeschicktwar.(end)already writtenat “er”, the options were:er91%sie6%es2%
Each row is one moment. Orange is the word being written now; dashed chips are words that don’t exist yet. Chatbots write their answers the same way. The options at “er” are made up for teaching.

Better, and much cheaper.

It beat the best translation systems of its day and needed a fraction of the computer time to train.

In the paperTable 2: English-to-German news translation (WMT 2014), scored in BLEU

Trained in 12 hours

8 GPUsgraphics chips12 hoursone full lap

On one machine with 8 graphics chips. The big version trained for 3.5 days.

23 times less work

best beforescore 26.4Transformerscore 27.323×less work7.7 × 10193.3 × 1018calculationscalculations

Score 27.3 against 26.4 for the best system before it. Higher is better. The big version scored 28.4.

Base model compared with the best earlier system, a group (ensemble) of ConvS2S models. Training work is counted in calculations (FLOPs), as in the paper’s Table 2.

Where it went.

The two towers became the blueprints for modern AI. GPT stands for Generative Pre-trained Transformer, and today’s chatbots are giant Writers.

Transformer2017BERT2018 · Reader halfGPT2018 · Writer halfChatGPT2022 · a giant Writer
From one paper to today’s chatbots.

The whole idea, in four steps

  1. Stamp every word with its seat.
  2. Every word asks, matches and mixes, 8 ways at once.
  3. Repeat on 6 floors.
  4. Write the answer one word at a time.

Read the paper on arXiv

Numbers marked as examples are teaching props, not measurements from the model. Facts about the model and its results come from the paper, arXiv:1706.03762.