Skip to content

AI, explained with six sketches

How does an AI make sense of words?

Let’s look at how a language AI connects words, keeps track of their order, and builds a reply. The design behind this is called a Transformer.

We’ll start with a game of telephone. No maths needed.


Self-attention

Let words connect directly

Top: five words pass a message one to the next like Chinese whispers and it comes out garbled. Bottom: every word holds a direct line to every other word, so all stay true.

Imagine a game of telephone: a message travels along a line of people, changing a little at each stop. Some earlier language models passed information along a sentence in a similar way. Self-attention gives a word a more direct route to other words it needs. In “the cat sat because it was tired,” connecting “it” with “cat” helps make sense of the sentence. Those connections are learned from examples. The direct route helps information travel; it doesn’t guarantee the AI will get the meaning right.

A shorter route to the words that matter.

Parallel reading

Open the other checkouts

Left: words queue at a single checkout, served one at a time — slow. Right: every word served at its own open till at the same instant — fast.

You’re waiting at the only open checkout while the other tills sit empty. Now imagine them all opening. That’s the shift in this sketch: the model can work on many word positions together, instead of passing through them in a fixed queue. Computers are good at doing many calculations at once, so this makes training more efficient. There’s a distinction worth keeping: learning from a sentence can happen in parallel, while writing a new reply still happens a piece at a time.

Many positions together when learning; one new piece at a time when replying.

Query · Key · Value

Mix the useful bits together

A word arrives with a question. Three labelled bottles pour into one glass — a thick stream from the best-matching bottle, thin trickles from the rest — so the takeaway is a weighted blend.

Think of mixing a drink from several bottles. A generous pour from one, a splash from another: the proportions change the result. Attention combines information in a similar way. A query is what a word is looking for; a key helps decide how relevant another word is; its value is the information it contributes. Better matches get a bigger share of the mixture. These are learned number patterns, rather than literal questions or labels. The bottles give us a way to picture what those numbers do.

The proportions make the mixture.

Multi-head attention

A few different ways to read

Three readers each study the same sentence and mark a different kind of link — subject to verb, which pronoun, nearby words — then pool into one richer combined reading.

Ask a few friends to read the same sentence and they may notice different things. One spots who did what; another notices what “she” refers to. A Transformer makes several sets of connections at once and combines the results. Each set is called an attention head. The sketch imagines them as a panel of readers. The heads learn their own patterns; nobody assigns them jobs such as “find the subject.” Having several gives the model more ways to connect the same words.

Different connections, brought together.

Positional encoding

Keep the words in order

The same loose word-cards can spell two opposite sentences until each card is stamped with a coral position number, which fixes the order and the meaning.

“The cat sat on the mat” and “the mat sat on the cat” use exactly the same words. The order changes the whole picture. Attention needs help keeping track of that order. The original Transformer adds a position signal to each word’s numerical representation, a little like numbering cards before shuffling them. The signal lets the model use where a word sits as well as what it is. The numbers on the sketch are our shorthand for that extra information.

Same words, different order, different meaning.

Masked generation

Build the reply a piece at a time

A sentence is written left to right, one word at a time. The word being written sends attention arrows only backward, to the words already written; the words not yet written sit hidden behind a coral mask, and a forward arrow that reaches toward them is blocked.

Try finishing this sentence: “I put the coffee on the…” You choose what comes next from the words already there. A language model builds a reply in a similar sequence. It predicts a small piece of text, adds it, then predicts the next. These pieces are called tokens; a token can be a word or part of one. During training, a rule called a mask blocks later answer pieces so the model can’t peek ahead. When generating, it has its prompt and the reply so far to work with.

Each new piece joins the context for the next.

Why it mattered

Putting it together

We’ve followed six parts of the picture: words connect, many positions are processed together, information gets mixed, several views come together, order gets marked, and a reply grows piece by piece.

The 2017 paper used this design for translation. Its approach made training more efficient and helped connect words across a sentence. These sketches give you a way into that design. The full model has other parts too, and making connections is no promise that an answer will be correct.

Connect the words. Keep their places. Build the reply.