05 · Attention
Scaled Dot-Product Attention
The full formula read as a pipeline of three matrix multiplications.
Every query against every key
Lay the queries out as the rows of a matrix
One matrix product computes all of them:
Up to now there has been exactly one query, written by hand: what did the cat chase? From here every word carries its own.
So what is a word asking? Nothing that could be written down as a sentence. A query is a vector, not English, and nobody hands the model a list of questions; it learns whatever directions happen to help. What can be described is how a query behaves. Take word (3) chased. It is a verb missing its object, and a query that scores highly against mouse behaves like the question this tutorial has been using all along. The hand-written query was standing in for word 3 the whole time. The difference now is that the other four words get one too.
In the figure above:
- Query — a row of
. Row is the question word is asking. - Key — a column of
, the same thing as a row of . Column is the label word wears so it can be found. - Score — an entry of
. Row , column is how well word 's query matches word 's key.
That explains the shading. The single dark cell is one score,
Now apply the scaling. Divide the whole matrix by
The final matrix is a complete set of weights: five rows, each a distribution over the five words. But weights are not an answer. Nothing in that table carries information about the words themselves. It says only how much of each to use, and supplying the content is the job of the values.
The values matrix
Every word hands over a value vector just as it hands over a key, and stacking those as rows gives
Every row of the weights answers one question: when this word builds its new representation, how much of each other word should it pull in?
So the 0.60 where chased meets mouse says that most of chased's answer is made out of mouse's value. Influence has an exact meaning inside this mechanism: the fraction of one word's output vector that came from another word's value. A verb is incomplete without its object, so after this step the representation of chased is no longer chased in the abstract. It is closer to chased a mouse.
The whole mechanism on one line
Everything from the last three chapters fits on one line:
Read it from the inside out and nothing in it is new.
One thing the formula does not say, and it is the question that has been waiting since the dictionary chapter: where do