Train your own embedding model
Setup & orientation·4 min

Vectors & cosine, refreshed

0 / 1

One idea underlies the whole course: cosine similarity measures angle, not magnitude. Two vectors that point the same way are similar even if one is ten times longer. That's exactly what we want for text — we care that two sentences mean the same thing, not how long they are.

A worked example

cosine = (u·v) / (‖u‖ ‖v‖)
u = [3, 4]      ‖u‖ = √(9+16) = 5      û = [0.6, 0.8]
v = [1, 0]      ‖v‖ = 1                 v̂ = [1, 0]

cosine(u, v) = (3·1 + 4·0) / (5 · 1) = 3/5 = 0.6
             = û · v̂  = 0.6·1 + 0.8·0 = 0.6   ← same thing

Notice the second line: once both vectors are L2-normalized (scaled to unit length), cosine is just their dot product. That's why embedding models normalize their output once, up front — every downstream comparison becomes a cheap dot product, and magnitude can never sneak back in.

What you reuse

If you came from semantic-search you already wrote cosine and recall@k; from train-gpt you have Adam and finite-difference grad-checking. This course rebuilds the pieces it needs from scratch in TypeScript, so no prior course is required — but the ideas connect.