0% completed
Attention: How Words Use Context
On This Page
Part 1: One Word, Several Meanings
Part 2: A Weight for Every Earlier Word
Part 3: From Scores to Weights
Part 4: What a Real Model Does
Key Takeaways
Many people think of an LLM as a program that handles a sentence one word at a time, with each word treated separately. This description fits the steps covered so far. The last two lessons did treat every token separately: each token received one ID, and each ID selected one embedding.
The description stops being correct at the next step. Read the sentence about the chef again and decide what the word "it" refers to.
The chef tasted the soup and said it needed more
The soup needed more, not the chef. The word "it" does not contain that information. The answer comes from the other words in the sentence, and this lesson explains how a model uses them. This work is step 3 of the five steps in The Life of a Prompt.
Part 1: One Word, Several Meanings
The Embeddings lesson ended with a limit. The embedding table gives "it" the same list of numbers in every sentence. In the sentence about the chef, "it" refers to the soup, and in "The car stopped because it had no fuel" the same word refers to a car.
Many ordinary words have the same problem. The word "bank" means one thing in "She put her money in the bank" and another thing in "She sat on the bank of the river". A reader understands both sentences because of the words "money" and "river".
The words around a word are called its context. A person uses context without effort. A model needs a step that moves information from the context into the list of numbers for each word.
Part 2: A Weight for Every Earlier Word
That step is called attention. For each word, the model gives every earlier word in the text a number between 0 and 1. A higher number means that the earlier word is more useful for this word. These numbers are called attention weights, and the weights for one word always add up to 1.
The model then builds a new list of numbers for the word. It takes a share of the list of each earlier word, and the size of each share is the weight. A word with a high weight supplies most of the new list, and a word with a low weight supplies almost nothing.
The diagram below shows the weights for "it" in the sentence about the chef.
Look at the thickness of the lines. The thickest line goes to "soup" with a weight of 0.63, and the second thickest goes to "chef" with 0.14. Words such as "the" and "and" receive thin lines.
These weights are illustrative, and Part 3 shows how such weights are computed. After this step, the list of numbers for "it" holds information about the soup, and that information later helps make "salt" a likely next word.
No line goes to "needed" or "more". In the LLMs that this course describes, a word takes information only from the words before it.
Part 3: From Scores to Weights
The model does not produce the weights directly. It first computes a score for each earlier word, and a score can be any number, high or low. The scores are then converted into weights.
The formula that does this conversion is called softmax, and it works in two steps. The first step turns every score into a positive number, in a way that makes a high score count for much more than a low one. The second step divides each result by the total, so that the results add up to 1.
The playground below applies softmax to seven scores for the word "it". The scores are a stand-in written by hand, because a real model cannot run here. Run it and compare the score column with the weight column.
The program prints this result:
Attention weights for the word "it" word score weight The 0.0 0.03 chef 1.5 0.14 tasted 1.0 0.09 the 0.0 0.03 soup 3.0 0.63 and 0.0 0.03 said 0.5 0.05 total 1.00 "it" takes the most information from: soup
"Soup" has the highest score and receives a weight of 0.63. "Chef" has half the score of "soup", and it receives a weight of only 0.14. Now change the score of "chef" from 1.5 to 4.0 and run the program again. "Chef" rises to 0.67, every other weight falls, and the total is still 1.00.
Part 4: What a Real Model Does
The formula in the playground is the real one. A real model differs from the playground in three other ways.
The scores are learned. Nobody writes the scores by hand or tells the model that "it" refers to the soup. The model computes each score from the lists of numbers of the two words, using parameters that it learned during training. The weights inside a real model are also often harder to interpret than the weights in this lesson.
Every word gets its own weights. The diagram shows one word, but a real model computes weights for each word in the text. It also gives each word a weight for itself, which the diagram and the playground leave out. All of this is done for tokens, and in the sentence about the chef each word is one token.
Attention runs many times. A real model runs many copies of the attention step at the same moment, and each copy has its own learned parameters. The model then repeats the whole step many times, one after another, so the list for each word is updated again and again.
Key Takeaways
- The meaning of a word depends on its context. The embedding table gives "it" the same list in every sentence, so the model must use the other words.
- Attention moves information between words. Each word receives a new list of numbers, built from the lists of the words before it.
- Attention weights set the size of each share. They add up to 1, and for "it" the highest weight goes to "soup".
- Real weights come from training. A model computes them with learned parameters, and it runs attention many times.
The course has now covered tokens, embeddings, and attention. The next lesson, The Transformer in One Picture, puts the three together and shows the whole model in a single diagram.
Reading Progress
0%
On This Page
Part 1: One Word, Several Meanings
Part 2: A Weight for Every Earlier Word
Part 3: From Scores to Weights
Part 4: What a Real Model Does
Key Takeaways