Sub topic 2
Cosine Similarity and Why Embeddings Are Normalized
This chapter assumes basic Python and PyTorch tensor operations, vector norms, dot products, and neural-network embeddings. You should also know that an embedding is a vector of numbers used to represent an item, such as an image or a sentence.
Cosine similarity
Cosine similarity measures the angle between two vectors. It is defined as:
cosine similarity = (a · b) / (||a|| ||b||)
Here, a · b is the dot product, and ||a|| is the vector's length, or norm. The result usually ranges from -1 to 1.
- A value near 1 means the vectors point in nearly the same direction.
- A value near 0 means they are unrelated by direction.
- A value near -1 means they point in opposite directions.
CLIP uses this measure to compare image and text embeddings. A matching image and caption should point in similar directions, even though their original inputs are different types of data.
| Relationship between vectors | Cosine similarity |
|---|---|
| Same direction | 1 |
| Perpendicular directions | 0 |
| Opposite directions | -1 |
Why normalize embeddings?
Normalization changes a vector so that its length becomes 1. For a vector a, the normalized vector is:
a_norm = a / ||a||
After both vectors are normalized, their dot product equals their cosine similarity:
anorm · bnorm = cosine similarity(a, b)
This makes comparisons depend on direction rather than magnitude. Without normalization, a vector with a large length could receive a high dot product even when its direction is not especially close.
For example, consider two image embeddings. One may have a larger magnitude because of details unrelated to the matching task. Normalization removes that scale difference before comparison.
import torch
import torch.nn.functional as F
image_emb = torch.tensor([[3.0, 4.0]])
text_emb = torch.tensor([[6.0, 8.0]])
image_unit = F.normalize(image_emb, dim=-1)
text_unit = F.normalize(text_emb, dim=-1)
cosine = image_unit @ text_unit.T
print(image_unit)
print(text_unit)
print(cosine.item()) # 1.0The two original vectors have different lengths, but they point in the same direction. Their cosine similarity is therefore 1.
Normalization in CLIP
CLIP has two encoders:
- An image encoder maps an image to an image embedding.
- A text encoder maps text to a text embedding.
The encoders produce vectors with the same number of dimensions. CLIP normalizes both vectors, then computes their dot product. This produces a similarity score for every image-text pair in a batch.
The resulting scores can rank captions for an image or images for a text query. A higher score means the two embeddings are more aligned in the learned joint space.
Practical checks
When using CLIP embeddings, verify that:
- The image and text vectors have matching dimensions.
- Both vectors are normalized along the embedding dimension.
- You compare vectors from the same model or compatible model versions.
- You use cosine similarity consistently during retrieval and evaluation.
Exercise
Why can an unnormalized dot product rank a less-aligned vector above a better-aligned vector?
Show answer
Dot products depend on both angle and vector length. A vector with a much larger length can produce a larger dot product even when its direction is less similar.