ElemedexA pocket encyclopedia of AI architectures

Glossary (TM) › Minimal mathematics

TM036

Cosine similarity

Also: cosine

In Spanish: Similitud coseno

A measure of how alike two vectors are, based on the angle between them: 1 if they point the same way, 0 if perpendicular, −1 if opposite.

Cosine similarity is the dot product of two vectors divided by the product of their lengths: cos(a, b) = a·b / (|a|·|b|), where |a| = √(a·a). It is the cosine of the angle between them, so it ignores how long the vectors are and attends only to where they point (Alammar & Grootendorst, 2024, p. 125).

Tiny example. Let a = (1, 1, 0) and b = (1, 0, 1). Then a·b = 1, |a| = |b| = √2, and cos(a, b) = 1/2 = 0.5, an angle of 60°. By contrast, (2, 2, 0) and (1, 1, 0) have cosine 1: one is twice the other, but they point exactly the same way. And (1, 0, 0) and (0, 1, 0), which share nothing, have cosine 0.

This indifference to scale explains its popularity with text embeddings: a long and a short document on the same topic may have vectors of very different sizes but similar directions. It is the central operation of semantic search and of a zero-example classification trick: embed the document and descriptions of the labels (“a negative review”, “a positive review”) and pick the closest label (Alammar & Grootendorst, 2024, p. 125). In cognitive science the idea has precedents in similarity spaces, where the cosine between feature vectors measures the perceived resemblance between exemplars (Buckner, 2024, p. 114).

Two caveats. Similarity is neither synonymy nor entailment: since embeddings are learned from the distribution of words, terms that share contexts but are opposites, such as hot and cold, can end up very close. And the measure is relative: there is always a nearest neighbor, even when nothing in the collection is relevant; a query about the mass of the Moon put to an article about a film still returns three “similar” sentences (Alammar & Grootendorst, 2024, p. 234). In high dimensions, moreover, intuitions about which value counts as “high” become unreliable.

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Buckner, C. J. (2024). From deep learning to rational machines: What the history of philosophy can teach us about the future of artificial intelligence. Oxford University Press. https://doi.org/10.1093/oso/9780197653302.001.0001 §