Search⌘ K

Cosine Similarity

Explore how cosine similarity quantifies the relationship between word embedding vectors in natural language processing. Understand its mathematical basis, interpreting similarity values, and implement a function to calculate cosine similarities using TensorFlow. This lesson helps you grasp key techniques for comparing word meanings through their vector representations.

Chapter Goals:

  • Learn about cosine similarity and how it's used to compare embedding vectors

  • Create a function that computes cosine similarities for a given word

A. Vector comparison

In mathematics, the standard way for comparing vector similarity is through cosine similarity. Since word embeddings are just vectors of real numbers, we can use also cosine similarity to compare embeddings for different words.

For two vectors, u and v, the equation for cosine similarity is

where v2∣∣v∣∣_2​ represents the L2-norm of vector vv, and represents the dot product operation.

We refer to the quantity vv2\frac{v}{||v||_2} ...