Advanced Mathematics

Embeddings and Similarity

Representing words, images or items as vectors learned so that similar things point in similar directions, compared by cosine similarity.

Needs a step first: 34

A network can turn each token or item into a vector of a few hundred numbers, learned during training rather than designed by hand. Meaning becomes geometry: related things end up close, and directions can carry meaning, such as a consistent offset between singular and plural. Closeness is usually measured by cosine similarity, the dot product of unit vectors, which ignores length. Embeddings are the input to transformers, where attention scores are exactly such dot products, and they power search and recommendation by finding nearest neighbours.

Where it shows up: Token embeddings are what attention operates on in a transformer, and what a language model's softmax output layer compares against.

How to hold it: In your head

See meaning as direction in a space of vectors, compared by cosine similarity, which ignores length and keeps only the angle.

What knowing it looks like

  • Compute the cosine similarity of two embedding vectors and rank neighbours by it.
  • Explain why cosine similarity, not the plain dot product, is used to compare meanings.
  • Describe how an embedding table is a weight matrix looked up by row and learned by backpropagation.

Test yourself

Three embeddings are a⃗=(1,2,2)\vec a = (1, 2, 2), b⃗=(2,1,2)\vec b = (2, 1, 2) and c⃗=(−2,1,0)\vec c = (-2, 1, 0), and d⃗=(2,4,4)\vec d = (2, 4, 4). Compute the cosine similarity of a⃗\vec a with each of b⃗\vec b, c⃗\vec c and d⃗\vec d. Which is nearest to a⃗\vec a by cosine, and which by plain dot product?
Show the worked answer
∣a⃗∣=∣b⃗∣=3|\vec a| = |\vec b| = 3, ∣c⃗∣=5|\vec c| = \sqrt5, ∣d⃗∣=6|\vec d| = 6. cos⁡(a⃗,b⃗)=(2+2+4)/9=8/9≈0.889\cos(\vec a, \vec b) = (2 + 2 + 4)/9 = 8/9 \approx 0.889. cos⁡(a⃗,c⃗)=(−2+2+0)/(35)=0\cos(\vec a, \vec c) = (-2 + 2 + 0)/(3\sqrt5) = 0: unrelated. cos⁡(a⃗,d⃗)=18/18=1\cos(\vec a, \vec d) = 18/18 = 1, since d⃗=2a⃗\vec d = 2\vec a points the same way. So d⃗\vec d is nearest by cosine, and also by dot product (1818 against 88 and 00); but its dot product is large partly because it is long, which cosine ignores.

Needs a step first: 34

Learn it with an AI

Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.

Be my maths tutor. I'm learning "Embeddings and Similarity" (Frontier tier): Representing words, images or items as vectors learned so that similar things point in similar directions, compared by cosine similarity.
I already know: Neural Networks, Orthogonality and Projections.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: Three embeddings are $\vec a = (1, 2, 2)$, $\vec b = (2, 1, 2)$ and $\vec c = (-2, 1, 0)$, and $\vec d = (2, 4, 4)$. Compute the cosine similarity of $\vec a$ with each of $\vec b$, $\vec c$ and $\vec d$. Which is nearest to $\vec a$ by cosine, and which by plain dot product?
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.

On the way to

Recommended