Triplet Loss

Cosine Similarity

Overview
Cosine Similarity is the most common metric used to measure how similar two documents (vectors) are, irrespective of their size. It measures the cosine of the angle between two vectors projected in a multi-dimensional space.

Formula

$$\text{similarity} = \cos(\theta) = \frac{A \cdot B}{|A| |B|}$$
  • Range: -1 to 1.
  • 1: Vectors point in exactly same direction (Identical meaning).
  • 0: Vectors are orthogonal (90 degrees, Unrelated).
  • -1: Vectors are opposite (180 degrees, Opposite meaning).

Why not Euclidean Distance?
Euclidean distance measures the *magnitude*.
- Document A: "I like app."
- Document B: "I like app. I like app. I like app."
- Euclidean: Far apart (B is much longer).
- Cosine: Identical (Same angle/topic).

For text search, we usually care about the *topic* (angle), not the *frequency* (length), making Cosine Similarity superior.

Optimization
If vectors are normalized (length = 1), then $|A| = 1$ and $|B| = 1$.
The formula simplifies to just the Dot Product ($A \cdot B$), which is extremely fast to compute on hardware.

Take triplet loss further

Ask the copilot about this term, or have our engineers assess it against your process.