New Tool Radar

AI Concepts

How to Normalise Embedding Vectors Before Cosine Similarity

Normalise every vector to length 1 before storing it, so cosine similarity becomes a simple dot product and rankings stay comparable.

Schematic with three steps: Check whether your model normalises; Normalise before storing; Check the ranking is unchanged on a.

Cosine similarity measures the angle between two vectors, so it should not depend on how long they are. Normalising a vector means dividing it by its length, so the result has length 1 and only its direction remains. OpenAI notes that its embeddings are normalised to length 1, which means cosine similarity can be computed slightly faster using just a dot product, and that cosine similarity and Euclidean distance then give identical rankings. If your model does not normalise for you, do it yourself before storing the vectors.

What you need

  • Embeddings for your chunks, from any model
  • A note of whether your model already normalises its vectors
  • The vector store and the similarity function it uses

Step 1: Check whether your model normalises

Read the model docs or test one vector: compute its length, and if it is not 1, the model does not normalise. OpenAI third-generation embeddings are normalised; many local models are not.

Step 2: Normalise before storing

Divide each vector by its own length before you insert it into the store, and do the same to every query vector. Store the normalised vectors, not the raw ones, so the store and the query always agree.

Step 3: Check the ranking is unchanged on a test set

Run the same test questions before and after normalising and compare the top results. If the ranking changes, the old results were distorted by vector length; if it does not, normalising was harmless.

Worked example: synthetic normalisation check on 3 vectors

The table below is a synthetic example. It shows the expected result of normalising vectors; no vectors were computed.

Vector Length before Length after
Chunk A 2.40 1.00
Chunk B 0.80 1.00
Query 1.60 1.00
Ranking check Same top result Pass

Expected result (UNTESTED). all three vectors have length 1 after normalising, and the top result is unchanged. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • After truncating a Matryoshka embedding, the result is no longer normalised, so re-normalise after truncating.
  • Normalising throws away vector length, which is the right call only when length carries no meaning in your data.
  • The query and the stored vectors must be normalised the same way, or the comparison is meaningless.
  • Some stores normalise for you when you choose cosine similarity; check the store docs before adding your own step.

Sources

See all