3. Vectors and the dot product
A vector is a list of numbers you can draw as an arrow. The dot product of two vectors says how much they point the same way, and it is the score attention uses to decide what matters.
a · b = 3 × 1 + 1 × 2 = 5
Lengths: |a| = 3.16, |b| = 2.24
Angle between them: 45°, cos θ = 0.71
They point partly the same way.
Two of the model's words
dog and cat: cosine similarity 0.41 in all 64 dimensions; on the map's two axes the angle between them is 160°.
Two arrows, a and b, start at the centre. Drag their tips, or move the sliders: the dot product a · b, the two lengths, the angle between the arrows and a verdict (the same way, partly, at right angles, against, opposite) update as you move. Below it, two of our model's words are compared in the same way, by the cosine of the angle between their 64-number vectors.
The idea
A vector is a list of numbers. Two numbers can be drawn as an arrow: go right by the first, up by the second. Our models use lists of 64 numbers, which no page can draw, but the arithmetic is the same as for two.
The dot product of two vectors multiplies them entry by entry and adds up the results. Drag the arrows and watch what it does: large and positive when they point the same way, zero at right angles, negative when they point apart. Longer arrows give bigger numbers, in both directions.
That is all attention needs to begin with: a way to score how well two lists of numbers match. In chapter 9, each word's query is dotted with every word's key, and the biggest scores decide where the word looks.
A worked example
Charles's example sentence, “She eats apples”, gives each word two numbers: She is [1, 0] and apples [1, 1]. Their dot product is one multiplication per entry, added up:
Now real vectors. In our model every word is 64 numbers, learned in training (chapter 5). The cosine similarity, the dot product of the two arrows scaled to length 1, is 0.70 for “dog” and “bird”, 0.70 for “sees” and “reads”, and 0.05 for “dog” and “Hund”, its German translation. The model has put words that do the same job in a sentence close together; the translation is almost unrelated, which chapter 5 explains.
The formula
For two vectors of the same length, the dot product is:
- \(a_i,\ b_i\)
- entry i of each vector
- \(d\)
- how many numbers each vector has: 2 on the page, 64 in our model, 512 in the paper
- \(\sum\)
- add up the products for every i
- \(|a|\)
- the length of a: the square root of a · a
- \(\theta\)
- the angle between the two arrows; cos θ is 1 for the same way, 0 at right angles, −1 for opposite
Divide the dot product by both lengths and only the angle is left: that is the cosine similarity, between −1 and 1, which the figure shows for the word vectors. Attention, as the paper writes it, uses the dot product itself, \(q \cdot k\), and chapter 9 shows why it then divides by \(\sqrt{d_k}\).
Why it matters
If you have used an embeddings API or a vector database, you have used this. A search for documents “similar” to a question is a search for the vectors with the largest cosine similarity to the question's vector. And inside every transformer, each layer computes one dot product between every pair of tokens in every head: that is where much of the work of a long prompt goes (chapter 9).
Where it fails
- Length counts. A long vector gets a big dot product with almost anything. That is why similarity search usually uses the cosine, and why attention scales its scores (chapter 9).
- Close is not the same meaning. In our model “dog” is far closer to “bird” than to its translation “Hund”. Vectors that point the same way were used the same way in training; that is not the same as meaning the same thing.
- A flat picture hides most of the space. The arrows on the page have 2 numbers; our model's have 64. Two words can look close on a 2D drawing and be far apart in the dimensions it leaves out, so the cosine in the figure is computed from all 64.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
A dot product by hand
What is the dot product of [2, 1] and [1, 3]?
The angle between She and apples
In Charles's example, She is [1, 0] and apples is [1, 1]. What is the cosine of the angle between them? Two decimals.
At right angles
Which of these pairs of vectors are at right angles to each other?
Which words point the same way?
In our model, which pair of words has the largest cosine similarity?