Deep Learning

Embeddings & Cosine Similarity

An embedding turns a word into an arrow. Two words mean similar things when their arrows point the same way, so similarity is an angle and nothing else. Or is it? Rank the same words by straight-line distance instead and the level sets change from rays out of the origin into rings around the query, which is a different question with different answers, unless every arrow has length one.

Angle θ
cos θ
Dot product
Distance ‖q−w‖
Query length

Drag the arrow anywhere on the stage to move the query, or click a word to compare with it. Real embeddings have hundreds of dimensions; this one has two so the angle is something you can see.

What to observe

  1. Swing the query around the circle. The match changes the moment theangle changes, and it never changes while you only make the arrow longer or shorter. Cosine similarity divides the length out, so it measuresdirection alone.
  2. cos θ runs from 1 (same direction, same meaning) through 0 (at right angles, unrelated) to −1 (opposite). Lay the query straight onpuppy and the panel reads 1.00, with dog just behind it, car near 0 and king below zero. Those numbers are the whole of semantic search.
  3. Turn on the shadow. The dashed line drops the query tip onto the word's direction, and the bright segment left behind has length |q| cos θ. That shadow is what the dot product measures; the cosine is the same shadow after dividing by both lengths.
  4. Now switch the ranking to dot product. The generic word thing, drawn long because it appears everywhere, takes over the top spot from whatever you are actually pointing at. Raw dot products reward long vectors, which is exactly why retrieval systems normalise and use the cosine.
  5. Tick force the query to length 1. Every arrow now sits on the circle, the dot product and the cosine become the same number, and the shadow is literally cos θ. Normalised embeddings are used precisely so these two agree.
  6. Switch the ranking to distance. The picture changes shape: the cosine's level sets are rays out of the origin, so length is invisible to it, while distance measures rings around the query tip, so a word can lose simply by being long. Watch kitten, a short arrow, beat cat whenever your query is short, even though cat points more the way you are pointing.
  7. Then put both on the circle: tick length 1 and click a word whose arrow is already about that long. Now the two rankings agree, always, and they must, because ‖q−w‖² = 2 − 2 cos θ when both have length one. That one identity is why a vector database can index cosine similarity with a nearest-neighbour structure that only knows how to measure distance.

Shortcuts: space run/pause · s step · r reset · f fullscreen