Attention in Pictures
After this lesson, you will be able to:
- Say what attention does in one sentence, using the idea of a spotlight with a fixed budget
- Work out by hand how one word turns scores into shares that add up to 100%
- Explain how the new summary of a word is a blend of the words it paid attention to
- Explain why a chatbot lets each word look only backwards
The step in which each word decides how much to look at every word, and then blends what it sees into a new summary of itself.
A number that says how well one word's question fits another word's label. A bigger score means a better fit.
The part of a word's attention that goes to one other word, written as a percentage. All the shares of one word add up to 100%. The expert name is attention weight.
The little recipe that turns a list of scores into shares that add up to 100%.
The question a word asks is its query. The label a word wears so others can find it is its key. A score compares a query with a key.
In a chatbot, each word may look at itself and earlier words only, never at later words. The expert name is the causal mask.
#Why a Word Needs Its Neighbours
In 'She sat on the bank and watched the river', which other word helps you most to decide what 'bank' means?
In one sentence: attention lets each word decide which other words to look at, so its meaning can depend on its neighbours.
#A Spotlight With a Budget
Picture a stage with a spotlight. The spotlight has only 100 points of light to give out. A word that is asking stands at the front and decides where to point the light. It can put all 100 points on one word, or spread them thin over all of them. Either way, the total is always 100.
After the light is pointed, the asking word collects a little from each word it looked at, in proportion to its share, and blends them into a new summary of itself. A word that looked mostly at "river" ends up with a summary that is mostly about rivers.
In one sentence: each word has a fixed budget of attention, shares it over the words it can see, and blends what it sees into a new summary.
#Words as Short Lists of Numbers
A program cannot read a word. It can only work with numbers. So each word is described by a short list of numbers. A real model uses hundreds or thousands of numbers per word. We will use two, which we can write on paper.
| Word | Thing-ness | Action-ness |
|---|---|---|
| the | 0.2 | 0.1 |
| cat | 0.9 | 0.1 |
| ate | 0.1 | 0.9 |
| fish | 0.8 | 0.2 |
"cat" is mostly a thing, "ate" is mostly an action, and "fish" is mostly a thing. Next, each word also asks a question, which is what it is looking for (its query), again written as two numbers.
| Word | Looks for a thing | Looks for an action |
|---|---|---|
| the | 0.6 | 0.2 |
| cat | 0.0 | 1.0 |
| ate | 1.0 | 0.0 |
| fish | 0.0 | 1.0 |
The word "ate" asks "who or what is involved?", so it looks for things. The word "cat" asks "what did I do?", so it looks for an action. In a real model nobody writes these numbers. The model learns them. We write them by hand so that you can follow every step.
In one sentence: every word gets a label and a question, both written as short lists of numbers.
#Hand Trace: One Word Asks
| Word | Calculation | Score |
|---|---|---|
| the | 1.0 × 0.2 + 0.0 × 0.1 | 0.2 |
| cat | 1.0 × 0.9 + 0.0 × 0.1 | 0.9 |
| ate | 1.0 × 0.1 + 0.0 × 0.9 | 0.1 |
| fish | 1.0 × 0.8 + 0.0 × 0.2 | 0.8 |
"cat" and "fish" score high because they are mostly things, and "ate" is looking for things.
| Word | Sharpened score | exp of it | Share |
|---|---|---|---|
| the | 0.8 | 2.2 | 3% |
| cat | 3.6 | 36.6 | 56% |
| ate | 0.4 | 1.5 | 2% |
| fish | 3.2 | 24.5 | 38% |
The total of the third column is about 64.8, and each share is its number divided by 64.8. The shares are 3%, 56%, 2% and 38%. They add up to 99% because of rounding; the exact values add up to 100%. The word "ate" puts almost all its attention on "cat" and "fish".
Before you try the demo, make a guess.
Suppose you double the sharpness from 4 to 8 for the word 'ate'. What happens to the share it gives to 'cat'?
Do: Leave "ate is asking" selected and Sharpness at 4.0. Then slide Sharpness down to 1.0 and up to 8.0. Next choose "cat is asking" and "the is asking" in turn.Notice: At 4.0 the bar for "cat" is 56% and the bar for "fish" is 38%, as in the trace. At 1.0 the bars are nearly even. Each asking word has its own pattern of bars, and the shares always add up to 100%.Why: The word asks a question, each other word wears a label, and the best fits get the biggest shares. Sharpness decides how strongly the best fit wins. Drawn stand-in, not a trained model: all the numbers are hand-made.
In one sentence: attention works out a score for each word, turns the scores into shares that add up to 100%, and blends the words by those shares.
#The Look-Back Rule
In the demo, tick the box that says words may only look back. Choose "the is asking". Its only visible word is itself, so it must give itself 100%. Choose "ate is asking": "fish" is hidden, and the shares move to the remaining words, with 91% on "cat". In our hand trace the share for "fish" was 38%. Hiding it changed the answer, which shows that a word's new summary depends on what it is allowed to see.
Models that read a whole text at once to understand it, rather than to continue it, do not use this rule. Both kinds exist, and a later lesson in this track compares them.
In one sentence: a chatbot lets each word look only at itself and earlier words, because later words have not been written yet.
#Run It: Attention in a Few Lines
The cell below does the four steps for every word at once, with numpy. It prints a table of shares and the new summary of "ate". Press Run and compare with your hand trace.
Tests · Run the script. The first table should show the row for 'ate' as 0.03 0.56 0.02 0.38, matching the hand trace. The new summary of 'ate' should print as [0.82 0.16]. The look-back table should show 1.00 for 'the' on itself and 0.00 for every later word. The sharpness 1 line should print 0.17 0.35 0.16 0.32. The checks at the end should pass without an error.
Do: Run the cell. Find the row for "ate" and compare it with your hand-worked table. Then changesharpness=4.0in the first line ofattentiontosharpness=1.0and run again.Notice: The row for "ate" reads 0.03, 0.56, 0.02, 0.38, the same as your hand trace, and each row adds up to 1. In the look-back table, every number above the diagonal is 0.00. At sharpness 1 the shares are close to even.Why: The cell does exactly the four steps you did by hand, for all four words at once: scores, sharpen, shares, blend. Drawn stand-in, not a trained model: the numbers are hand-made.
In one sentence: the whole attention step is a few lines of multiplying, adding and dividing.
#What a Real Model Adds
Our toy leaves out four things. Each is a later lesson, and each is only a refinement of what you just did.
- The numbers are learned. In a real model nobody writes the labels and questions. Training finds them. The model also learns a third list for each word, called the value, which is what gets blended. We blended the labels to keep the example small.
- The lists are longer. A real word has hundreds or thousands of numbers, not two, so a word can be a thing, an action, a place, a mood and much more all at once.
- Many spotlights run at once. One spotlight can follow one kind of link. A real model runs many in parallel, so one can follow grammar and another meaning. This is the subject of Multi-Head Attention.
- It is stacked. The new summaries go through attention again, many times in a row. Each pass lets the words build on the summaries from the pass before. The full design is in The Full Transformer.
#Key Takeaways
- Attention lets each word decide which other words to look at, so its meaning can depend on its neighbours
- Each word has a fixed budget of attention. The shares it gives to the words it can see add up to 100%
- The steps are: score each word, sharpen, turn the scores into shares with softmax, and blend by those shares
- The new summary of a word is mostly made of the words that got the biggest shares
- A chatbot uses the look-back rule: each word sees only itself and earlier words
#Quick Check
The word 'ate' gives shares of 3%, 56%, 2% and 38% to four words. What must those four shares add up to, apart from rounding?