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

FunctionFormulaUse whenWatch out
dot productmagnitude carries signallarge-norm vectors dominate
cosine similaritydirection matters more than scaleloses confidence/frequency magnitude
Euclidean distanceabsolute coordinates are meaningfulsensitive to feature scaling
squared Euclideansmooth least-squares objectivegrows fast for outliers
Manhattan distancesparse or coordinate-wise deviationsnon-smooth at zero
learned scoretask-specific similarityharder 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?