Linear algebra for LLMsPart 1: Vectors and embedding space

Norms and cosine similarity

7 min

The length of a vector

a = [3, 4]
∣a∣=32+42=5\displaystyle |a| = \sqrt{3^2 + 4^2} = 5

Pythagoras: square the entries, add, take the square root. This is the L2L_2 norm, written ∣a∣|a| or ∥a∥2\|a\|_2.

Write

∣a∣=∑iai2|a| = \sqrt{\textstyle\sum_i a_i^2}. ∣[3,4]∣=5|[3, 4]| = 5

Same rule in any dimension

∣[1,2,2]∣=1+4+4=9=3\displaystyle |[1, 2, 2]| = \sqrt{1 + 4 + 4} = \sqrt{9} = 3

A neat way to remember it: ∣a∣2=a⋅a|a|^2 = a \cdot a. The length squared is the vector dotted with itself.

Write

∣a∣2=a⋅a|a|^2 = a \cdot a

The L1L_1 norm

∥a∥1=∑i∣ai∣\displaystyle \|a\|_1 = \sum_i |a_i|
∥[3,−4]∥1=3+4=7\displaystyle \|[3, -4]\|_1 = 3 + 4 = 7
∥[3,−4]∥2=5\displaystyle \|[3, -4]\|_2 = 5

L1L_1 adds the sizes of the entries: the distance walking along the grid instead of in a straight line. It shows up in regularization and sparsity; “norm” on its own almost always means L2L_2.

Write

∥a∥1=∑i∣ai∣\|a\|_1 = \sum_i |a_i|. “Norm” alone means L2L_2.

Unit vectors

→a^=a∣a∣=\hat a = \dfrac{a}{|a|} =

Divide a vector by its length and you get a unit vector: the same direction, length 1. Check: 0.62+0.82=0.36+0.64=10.6^2 + 0.8^2 = 0.36 + 0.64 = 1. This is called normalizing.

Write

a^=a/∣a∣\hat a = a / |a| has length 1. [3,4]→[0.6,0.8][3, 4] \to [0.6, 0.8]

✏️ Your turn

Normalize this

Write ∣a∣|a|, then a^\hat a.

Work it out on paper, then go to the next card to check.

Answer

∣a∣=4+1+4=3\displaystyle |a| = \sqrt{4 + 1 + 4} = 3
a^=13[2,1,2]≈\hat a = \tfrac{1}{3}[2, 1, 2] \approx

Every entry divided by 3. The direction didn’t change; only the length did.

Cosine similarity

cos⁡θ=a⋅b∣a∣ ∣b∣\displaystyle \cos\theta = \frac{a \cdot b}{|a|\,|b|}
a=[3,4],  b=[4,3]\displaystyle a = [3, 4],\; b = [4, 3]
cos⁡θ=12+125⋅5=0.96\displaystyle \cos\theta = \frac{12 + 12}{5 \cdot 5} = 0.96

Rearrange a⋅b=∣a∣∣b∣cos⁡θa \cdot b = |a||b|\cos\theta and you get the cosine: how aligned two vectors are, from −1-1 (opposite) through 00 (orthogonal) to 11 (same direction), whatever their lengths.

Write

cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \dfrac{a \cdot b}{|a|\,|b|}, between −1-1 and 11.

A dot product is not a cosine

Key q⋅kq \cdot k cos⁡θ\cos\theta
k1=[5,5]k_1 = [5, 5] 5 0.707
k2=[1,0.1]k_2 = [1, 0.1] 1 0.995

With q=[1,0]q = [1, 0], the dot product prefers k1k_1 because it’s long. Cosine similarity prefers k2k_2 because it points almost exactly along qq. Which one you want depends on whether length should count.

Write

Dot product rewards length and alignment; cosine only alignment.

Unit vectors make them the same

∣a∣=∣b∣=1  ⇒  a⋅b=cos⁡θ\displaystyle |a| = |b| = 1 \;\Rightarrow\; a \cdot b = \cos\theta

In a transformer

Search with embeddings ranks documents by cosine similarity. Normalize every vector once and a plain dot product gives the cosine, which is what vector databases compute fast. Many embedding APIs return unit-length vectors already (OpenAI’s do).

Write

For unit vectors, a⋅b=cos⁡θa \cdot b = \cos\theta: dot product = cosine similarity.

In PyTorch

torch.linalg.vector_norm(a)          # |a|, the L2 norm
torch.linalg.vector_norm(a, ord=1)   # the L1 norm
F.normalize(a, dim=-1)               # a / |a|, along the last axis
F.cosine_similarity(a, b, dim=-1)    # a·b / (|a| |b|)

F is torch.nn.functional.

📌 Recap

Norms and cosine similarity in one card

  • ∣a∣=∑iai2|a| = \sqrt{\textstyle\sum_i a_i^2}, and ∣a∣2=a⋅a|a|^2 = a \cdot a. L1L_1: ∑i∣ai∣\sum_i |a_i|.
  • a^=a/∣a∣\hat a = a / |a| is a unit vector: same direction, length 1.
  • cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \frac{a \cdot b}{|a|\,|b|}, from −1-1 to 11, ignoring length.
  • For unit vectors, a⋅b=cos⁡θa \cdot b = \cos\theta.

Write

The four lines above.