Linear algebra for LLMsPart 1: Vectors and embedding space
The dot product
7 min
Two vectors of the same length
The dot product takes two vectors with the same number of entries and gives back one number.
Write
Multiply matching entries
First with first, second with second, third with third: , , .
Then add them up
That’s the whole definition: multiply matching entries, add the products.
Write
✏️ Your turn
Your dot product
Write the products, then the sum.
Work it out on paper, then go to the next card to check.
Answer
A zero entry contributes nothing, and a negative product pulls the total down.
The same number, from lengths and an angle
Here , and , so : . The algebra (sum of products) and the geometry (lengths and angle) always agree.
Write
The sign tells you the angle
| Angle | Meaning | |
|---|---|---|
| positive | less than 90° | they point roughly the same way |
| zero | exactly 90° | orthogonal: no overlap at all |
| negative | more than 90° | they point roughly opposite ways |
Against : gives , gives , gives .
Write
Projection: the shadow of one vector on another
When has length 1, is the length of ’s shadow on ’s direction: here . For any , the shadow length is .
Write
Dot products as scores
In a transformer
Attention scores a query against every key with dot products: here , and . wins: it points nearly the same way as , and it’s long. Part 5 turns these scores into weights.
Write
In PyTorch
a @ b # dot product of two 1-D tensors
torch.dot(a, b) # the same
(a * b).sum() # the same, written out: multiply, then add
K @ q # one dot product per row of K: all the scores📌 Recap
The dot product in one card
- : multiply matching entries, add. One number out.
- . Positive: same way; zero: orthogonal; negative: opposite.
- With , is the length of ’s shadow on .
- Attention scores are dot products between a query and each key.
Write