Vector Norm and Similarity
Vector norms and similarity functions define how embeddings, features, gradients, and coordinates are compared. They appear in Attention, retrieval, contrastive learning, clustering, docking scores, and evaluation.
For vectors , the dot product is:
The Euclidean norm is:
The distance induced by this norm is:
More generally, the -norm is:
Common special cases are:
Cosine similarity normalizes away vector magnitude:
Common Choices
- Dot product: magnitude and direction both affect similarity.
- Cosine similarity: direction matters more than magnitude.
- Euclidean distance: absolute position in feature space matters.
- Squared distance: convenient for gradients and least-squares objectives.
- Learned score: a model replaces a fixed geometry with task-specific scoring.
Choice Table
| Function | Formula | Use when | Watch out |
|---|---|---|---|
| dot product | magnitude carries signal | large-norm vectors dominate | |
| cosine similarity | direction matters more than scale | loses confidence/frequency magnitude | |
| Euclidean distance | absolute coordinates are meaningful | sensitive to feature scaling | |
| squared Euclidean | smooth least-squares objective | grows fast for outliers | |
| Manhattan distance | sparse or coordinate-wise deviations | non-smooth at zero | |
| learned score | task-specific similarity | harder to interpret and calibrate |
Metric Conditions
A distance is a metric if it satisfies:
Cosine similarity is a similarity score, not a distance metric by itself. Some systems convert it into a distance-like quantity, but metric assumptions should be checked before using nearest-neighbor indexes or evaluation metrics.
In Attention
Scaled dot-product attention uses dot products between queries and keys:
The factor keeps logits from growing too large as the key dimension increases.
In Representation Learning
If embeddings are normalized:
then dot product and cosine similarity coincide:
This matters for retrieval, clustering, contrastive losses, and nearest-neighbor evaluation.
Scaling and Normalization
Distance-based methods are sensitive to feature scale:
If rescales coordinates unevenly, the geometry changes. This is why feature normalization, whitening, learned projections, and embedding normalization can change retrieval or clustering behavior without changing the downstream model.
Checks
- Are embeddings normalized before similarity is computed?
- Is the score a dot product, cosine similarity, distance, or learned function?
- Does vector magnitude carry meaningful confidence, frequency, or scale information?
- Is the metric aligned with the training loss and evaluation metric?
- Are features on comparable scales before distance-based methods are used?
- Does the nearest-neighbor backend assume metric properties?
- Is normalization removing useful magnitude information?