Chapter 8
Attention
A word’s meaning depends on the words around it. Attention is the part of the machine that lets every word look at its neighbours, decide which of them matter, and take in a little of what they say. It is built from parts you already own: a mix, a dot product and softmax.
- Say why one fixed place for each word is not enough.
- Change what a word means by mixing in some of its neighbours.
- Explain why the amounts in that mix cannot be fixed knobs, and must be worked out fresh for every sentence.
- Name the three things each word makes, a question, a label and a message, and say what each is for.
- Work through attention for one word in five stages: ask, score, share, mix, add.
One word, two meanings
Read these two sentences.
the bat flew away
the bat was swung
You did not stop to wonder what “bat” meant. In the first it is a small animal. In the second it is a piece of wood. You knew at once, because of the words around it.
The machine of chapter 6 cannot do that. It gives “bat” one place on the map, the same in every sentence. The best it can do is put “bat” halfway between the animals and the sports gear, which is right for neither.
So the place of a word has to be able to move, depending on its sentence. And you already have a tool for moving a place towards other places: the mix from chapter 4. Take some of this and some of that, in set amounts, and add them up.
Try it by hand first. You decide how much “bat” should listen to each word.
“Bat” sits at (0.0, 2.0). It could still be the animal or the thing you hit a ball with. Which word in the sentence gives it away?
Mixing worked. A share of “flew” pulled “bat” to the animal side. A share of “swung” pulled it to the sport side. A word can pick up meaning from its neighbours by mixing a little of them into itself.
But the second sentence showed the catch. You had set the amounts by position: so much for word 3, so much for word 4. In a new sentence the helpful word was somewhere else, and the same amounts pointed at the wrong word.
In chapter 4 the amounts in a mix were knobs: learned once, then fixed. That cannot work here. There are endless sentences, and the helpful word can be anywhere.
The amounts have to be worked out fresh for each sentence, from the words that are in it. The machine has to decide for itself what to pay attention to.
The first idea: listen to words like yourself
Chapter 7 gave us a way to get shares out of lists of numbers. Score with a dot product. Turn the scores into shares with softmax. So here is a first try. Let “bat” score its own place against the place of every word in the sentence, and use the shares as the amounts.
“bat” gives 96% to itself. Of course: nothing is more like a word than the same word. It learns nothing new from the sentence.
It fails, and the reason is worth a moment. The dot product is big when two arrows point the same way. No arrow points more the same way as “bat” than “bat” itself. So a word that looks for words like itself finds itself.
Think about what “bat” really needs. Not a word that is like it. It needs a word that tells it something: an action word, saying what is being done. “Flew” is nothing like “bat”. That is exactly why it is useful.
What a word is, and what a word is looking for, are two different things. One list of numbers cannot do both jobs.
A question and a label
Picture a room full of people, each wearing a name badge that says what they do: plumber, doctor, baker. You have a leaking tap. You do not look for people like you. You call out what you need, and look for the badge that matches.
Give every word the same two things.
A question: a list of numbers that says what this word is looking for.
A label: a list of numbers that says what this word has to offer, like a badge.
The score is now the dot product of one word’s question with another word’s label. A big score means “you are what I was looking for”.
Below is a map of labels. Notice that it is laid out differently from the map of meaning. On the map of meaning, “flew” and “swung” were far apart, one with the animals and one with the sports. Their labels say something else: what kind of word each one is.
“bat” reads as: the animal
“bat” reads as: could be either
The question has no length yet, so every word gets the same share. Drag the orange tip.
You found one arrow that works in both sentences. It does not point at “flew”. It does not point at “swung”. It points at what their labels share: both are action words. The question means “which word here says what is being done?”, and each sentence answers it with a different word.
That is the trick that position could not do. The question is fixed, but the answer depends on the sentence.
The message
One piece is left. Once “bat” has chosen to listen to “flew”, what does “flew” hand over?
Not its label. The label only says “I am an action word”. That was for matching, and its job is done.
The next guess is that “flew” hands over its whole place, as in the very first figure of this chapter. Look back at what happened there. “Bat” moved to the animal side, which is what we wanted. But it also sank halfway down the map, from 2.0 to 0.3 in its second number. That second number says whether a word is a thing or an action. By swallowing all of “flew”, “bat” was turning into an action word. It should stay a thing.
So a word should not pass on everything about itself. It should pass on the part that is useful to a listener. For “flew”, the useful part is “this is about animals”. The fact that “flew” is an action is its own business.
That useful part is the word’s message. And it is not a new thing to store. Like the question and the label, it is worked out from the word’s place.
second number −1.5: an action(−2.0, −1.5)
The message of “flew” is (−2.0, 0.0). It keeps the first number of the place, which says “animal side”. It drops the second, which says “I am an action”. A listener learns which side it is on, and is not turned into an action itself.
Read the four rows as one story. A word has a place. From the place, three small weighted sums make three lists. The orange numbers are their knobs.
The question and the label are both built from the second number of the place, the one that says thing or action. That is why they are good for finding the right kind of word.
The message is built from the first number, the one that says animal or sport. Its knobs are 1 and 0: keep the first number, drop the second. The message is the word’s place with everything taken out except what a listener can use.
I set these knobs by hand so that you can read them. In a real machine they are learned, and we come back to that below.
Now every part is in place. Here is the whole thing for the word “bat”, one stage at a time.
“Bat” makes its question. Every word in the sentence, “bat” included, holds up its label.
Say the five stages once more, because this is the heart of the whole article.
Ask. The word makes a question. Every word holds up a label.
Score. Question times label, pair by pair, added up. Chapter 7’s dot product.
Share. The scores become shares of 100%. Chapter 7’s softmax.
Mix. The messages are mixed in those shares. Chapter 4’s weighted sum.
Add. The mix is added to the word’s own place. The word keeps who it was, and gains what it heard.
Attention is a mix where the words themselves work out the amounts.
Who sets those knobs?
You saw that the question, the label and the message are each made from a word’s place by a few weighted sums.
So there are three small sets of knobs: one that turns a place into a question, one that turns it into a label, one that turns it into a message. The same three sets are used for every word in every sentence. Nothing is stored for any one sentence. That is how a machine with a fixed number of knobs can handle sentences it has never seen.
In the figures, I set those knobs by hand so that things ask about actions, and actions ask about things. In a real machine nobody sets them. They are learned with the loop from chapter 3 and the blame from chapter 5, exactly as before. Each stage is made of multiplying and adding, and softmax has a slope, so blame can flow backwards through all five stages, into the three sets of knobs, and on into the places of the words.
Nobody tells the machine that “bat” should ask about actions. It finds questions that help it do its job, which is the subject of the last chapter.
Every word asks at once
We followed “bat”. But nothing about the five stages is special to “bat”. Every word in the sentence makes its own question, at the same moment, and gets its own mix.
“flew” gives 98% of its attention to “bat”. An action asks: who or what is doing this?
Each word came in as a fixed pin on the map. Each word goes out moved a little, towards whatever it found useful in the others. “Bat” now knows it is an animal. “Flew” now knows what was doing the flying.
Words go in alone and come out knowing about each other. That is what attention is for.
What is still missing
We have the main part. Three things are still missing before it becomes a machine that can write.
Order. Look back at the five stages. Nowhere did we use where a word is in the sentence. To this machine, “dog bites man” and “man bites dog” are the same bag of words.
Depth. One round of attention lets each word hear from the others once. Real meaning needs more than one round, and it needs the bends of chapter 5 as well.
A job. All these knobs need something to be wrong at, or they cannot learn. Chapter 6 gave a hint: guess the missing word.
The last chapter puts these in, and then lets the whole thing run.
- Say why one fixed place for each word is not enough.
- Change what a word means by mixing in some of its neighbours.
- Explain why the amounts in that mix cannot be fixed knobs, and must be worked out fresh for every sentence.
- Name the three things each word makes, a question, a label and a message, and say what each is for.
- Work through attention for one word in five stages: ask, score, share, mix, add.