latentspace.

Cosine Similarity


  • Cosine Similarity
    • Cosine similarity measures the similarity between two vectors by calculating the cosine of the angle between the two vectors.
    • Why cosine of the angle between \(x\) and \(y\) gives use the similarity?
      • Cosine function is \(1\) at the \(\theta = 0\) and \(-1\) at \(\theta = 180\). Therefore, if the two vectors overlap more perfectly, the cosine similarity approaches \(1\).

    • Calculation of cosine of the angle between \(x\) and \(y\) as: \[ \,\\ \cos(\theta) = \frac{x \cdot y}{\|x\| \, \|y\|} \,\\ \]
    • The above expression comes from the definition of the dot product:
      • The dot product of two vectors \(x\) and \(y\) is defined as: \[ \,\\ x \cdot y = \|x\| \, \|y\| \cos(\theta) \,\\ \]
      • Rearranging this equation to solve for \(\cos(\theta)\) gives us: \[ \,\\ \cos(\theta) = \frac{x \cdot y}{\|x\| \, \|y\|} \,\\ \]
      • This is equivalent to \(\cos(\theta) = \frac{\text{adjacent}}{\text{hypotenuse}}\) in a right-angled triangle, where \(\text{adjacent} = x \cdot y \) and \(\text{hypotenuse} = \|x\| \, \|y\| \)

  • Simple implementation