Attention and transformers
Self-attention lets every token weigh every other token, so “it” can borrow meaning from “animal” or “street”. Change one word at the end of a sentence and watch the attention move.
10 minFree
- Tokens
- Query
- Scores
- Softmax
- Blend
- Every token
A sentence from Google's ML Crash Course. As a person you know what “it” means. Attention is how a transformer works that out. Press Play.
The scores are illustrative: hand-set to show what trained heads tend to learn. Masking, softmax and the weighted blend are the real maths. Real models use many heads in each of many layers.
Background: the idea in plain words
Read this sentence: "The animal didn't cross the street because it was too tired." You know "it" is the animal. Change the last word to "wide" and "it" becomes the street. One word at the end changed the meaning of a word in the middle.
A model needs a way to do the same thing. Here is the problem. When a token (a small piece of text) first becomes an embedding (a list of numbers that stands for it, see Embeddings), "it" has the same numbers in both sentences. Those numbers know nothing about the rest of the sentence yet.
Self-attention fixes that. For every token, it asks: how much does each other token affect how I should read this one? Then it mixes some of those tokens' information into this token.
Here it is step by step for the token "it". Every other token gets a score for how useful it is to "it". The scores are turned into weights (shares that add up to 100%). Then "it" gets new numbers: a mix of the other tokens, where a bigger weight means a bigger share. With "tired", most of the weight goes to "animal". With "wide", it moves to "street".
The transformer is the architecture (the overall design of a model) built around this idea. It stacks many attention layers, and each layer has several attention heads that each learn their own pattern. Today's LLMs, Gemini included, are built on transformers.
Press Play to see attention computed for "it". Then switch Try it to wide, try the other head, and try Look back only.
What to notice
- Attention is a weighted average. Each token gets a score against every other token (query times key), softmax turns the scores into weights, and the token's new value is a weighted mix. Nothing more mysterious than that, repeated many times.
- The weights depend on the whole sentence. Swap "tired" for "wide" and the weight moves from "animal" to "street". The same word gets a different meaning from its context.
- Different heads learn different jobs. Google's crash course gives examples: one head might resolve pronouns, another might track grammar. Head 2 here simply looks at the previous word. A model combines the output of all its heads.
- Decoders only look back. Models that generate text predict one token at a time, so each token may only attend to tokens before it. That's the triangle you see with Look back only. Encoders, used for tasks like creating embeddings, can look both ways.
- Every token looks at every other token, so cost grows fast. Self-attention is roughly O(N²) in the number of tokens: double the prompt and that part of the work roughly quadruples. This is one reason long context costs more. See Context windows.
Layers build on layers The output of one attention layer is the input to the next. According to Google's crash course, earlier layers tend to pick up basic syntax, and deeper layers more abstract ideas such as sentiment. The single layer here is a small slice of that.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Self-attention | The step where each token looks at the other tokens and takes in information from the useful ones |
| Attention weight | The share of attention one token gives to another. The weights add up to 100% |
| Attention head | One set of attention weights inside a layer. Each head learns its own pattern |
| Transformer | The model design built around attention. Today's LLMs use it |
| Decoder | A model that writes text one token at a time, so each token can only look back |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.