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

a=[1,2,3]a = [1, 2, 3], b=[4,−1,2]b = [4, -1, 2]

Multiply matching entries

First with first, second with second, third with third: 1×4=41 \times 4 = 4, 2×(−1)=−22 \times (-1) = -2, 3×2=63 \times 2 = 6.

Then add them up

a⋅b=∑iaibi\displaystyle a \cdot b = \sum_i a_i b_i
=4+(−2)+6=8\displaystyle = 4 + (-2) + 6 = 8

That’s the whole definition: multiply matching entries, add the products.

Write

a⋅b=∑iaibi=4−2+6=8a \cdot b = \sum_i a_i b_i = 4 - 2 + 6 = 8

✏️ 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

2⋅3+0⋅5+(−1)⋅4\displaystyle 2 \cdot 3 + 0 \cdot 5 + (-1) \cdot 4
=6+0−4=2\displaystyle = 6 + 0 - 4 = 2

A zero entry contributes nothing, and a negative product pulls the total down.

The same number, from lengths and an angle

θuw
u⋅w=∣u∣ ∣w∣cos⁡θ\displaystyle u \cdot w = |u|\,|w| \cos\theta

Here u⋅w=3+2=5u \cdot w = 3 + 2 = 5, ∣u∣=10|u| = \sqrt{10} and ∣w∣=5|w| = \sqrt{5}, so cos⁡θ=5/50≈0.707\cos\theta = 5 / \sqrt{50} \approx 0.707: θ=45∘\theta = 45^\circ. The algebra (sum of products) and the geometry (lengths and angle) always agree.

Write

a⋅b=∣a∣ ∣b∣cos⁡θa \cdot b = |a|\,|b| \cos\theta

The sign tells you the angle

a⋅ba \cdot b 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
e+20−2

Against e=[2,0]e = [2, 0]: [1,2][1, 2] gives +2+2, [0,2][0, 2] gives 00, [−1,2][-1, 2] gives −2-2.

Write

a⋅b>0a \cdot b > 0: same way. a⋅b=0a \cdot b = 0: orthogonal. a⋅b<0a \cdot b < 0: opposite.

Projection: the shadow of one vector on another

aû

When u^\hat u has length 1, a⋅u^a \cdot \hat u is the length of aa’s shadow on u^\hat u’s direction: here [2,3]⋅[1,0]=2[2, 3] \cdot [1, 0] = 2. For any bb, the shadow length is a⋅b/∣b∣a \cdot b / |b|.

Write

a⋅u^a \cdot \hat u = length of aa’s shadow on u^\hat u (when ∣u^∣=1|\hat u| = 1).

Dot products as scores

→

In a transformer

Attention scores a query against every key with dot products: here 44, −1-1 and 77. k3k_3 wins: it points nearly the same way as qq, and it’s long. Part 5 turns these scores into weights.

Write

score(q,k)=q⋅k\text{score}(q, k) = q \cdot k. Highest score = most aligned (and longest).

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

  • a⋅b=∑iaibia \cdot b = \sum_i a_i b_i: multiply matching entries, add. One number out.
  • a⋅b=∣a∣ ∣b∣cos⁡θa \cdot b = |a|\,|b| \cos\theta. Positive: same way; zero: orthogonal; negative: opposite.
  • With ∣u^∣=1|\hat u| = 1, a⋅u^a \cdot \hat u is the length of aa’s shadow on u^\hat u.
  • Attention scores are dot products between a query and each key.

Write

The four lines above.