Curriculum · Unit 6 · Attention and the transformer

How does each word decide which other words matter?

To understand "it" in a sentence, you have to know what "it" refers to. Attention is how a model works that out: every word asks a question, every other word offers an answer, and the best matches get listened to most. Stack that idea into layers and you have the transformer, the design inside nearly every chatbot.

Nib says: Tap any word in my sentence to see which earlier words it pays attention to. Thicker lines and warmer colors mean more attention. Then flip tired to wide and watch the meaning of "it" move. After that, press Next to walk one word through a whole transformer block. I set all the weights by hand, so they're illustrative.

Try itWho is "it"?

Last word
Can see

Tap a word. Faded, dashed words are hidden from it.

Attention weights are hand-made to show the idea; a real model has many attention heads per layer, and they're messier. Illustrative.

Step through itInside one transformer block

    Tiny 4-number vectors with made-up values, so you can check the arithmetic. Real models use hundreds or thousands of numbers per token. The math is computed on this page and rounded for display. Illustrative.

    The idea in plain words

    Attention started as a fix for translation. In 2014, translation networks read a whole sentence and squeezed it into one fixed list of numbers before writing the translation, and long sentences got garbled. Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio let the network look back at every word of the original as it wrote each new word, and decide for itself which ones mattered right then. That learned looking-back is attention.

    In 2017 a team at Google, with Ashish Vaswani as first author, asked, in effect: what if attention is the whole model? Their transformer gives every token three vectors. A query says what it's looking for, a key says what it offers, and a value is what it hands over. Each token compares its query with every key, turns the match scores into percentages with a softmax, and takes that blend of values. Several of these "heads" run side by side, each free to track something different. A small feed-forward network then works on each token, and residual connections add each layer's result back onto its input, so information can flow straight up through a tall stack.

    Attention on its own ignores word order, so the transformer adds position information to each token at the start. And because every token can be processed at the same moment, rather than one after another as in older networks like the LSTM, the work splits neatly across the thousands of cores in a GPU. That's a big reason transformers could be scaled up so far.

    GPT-style models keep only the decoder half and train on one job: predict the next token. To make that honest, each token may only look at tokens before it. That's the causal mask, and it's why "it" in the map above can't see "tired" yet. In a GPT, the meaning gets settled a bit later, when "tired" or "wide" arrives and looks back at both "it" and the noun. OpenAI's GPT-2 (2019) showed how far this goes: trained only to predict the next word of web text, it wrote fluent paragraphs and picked up tasks nobody had trained it for. How many tokens a model can look back across is its context window.

    Three steps to GPT

    1. 2014

      Attention for translation

      Bahdanau, Cho and Bengio: while writing each word, look back over the whole source sentence.

    2. 2017

      The transformer

      Vaswani et al.: drop the step-by-step network and build everything from attention, run in parallel.

    3. 2019

      GPT-2

      OpenAI: a decoder-only transformer, causal mask, next-token prediction on lots of web text.

    Three things to keep

    Check yourself

    Why does a GPT-style model need a causal mask during training?

    It's being trained to predict the next token. If each position could see the tokens after it, it could simply copy the answer, and it would learn nothing useful. The mask hides the future so every prediction is a real guess.

    In the GPT-style map, "it" can't see "tired". So where does the model work out what "it" refers to?

    At later tokens. When "tired" (or "wide") arrives, it can look back at both "it" and the candidate nouns, and in a deep model the layers above build on that. Try clicking the last word in the map.

    What do the residual connections in a transformer block do?

    They add each layer's output back onto its input instead of replacing it. Each layer only has to learn a change to make, and the original signal can travel up through dozens of layers without fading.

    Go deeper