Vector embeddings and semantic space
A vector embedding represents a piece of content as a list of numbers. Search compares a query's vector with document vectors to choose candidates. Here you'll score the same points three ways and see how the comparison rule changes their ranking.
In the previous lesson, we chose two features ourselves. A learned embedding has many more coordinates, usually without names we can interpret. Semantic space is our name for the resulting geometry when those representations capture useful relationships in meaning.
That geometry depends on training. It is not a universal map of what every sentence means.
Three rulers for the same points
Consider a query vector Q = [1, 0], and two document vectors: A = [2, 0] and B = [1, 1]. These numbers are invented so that you can follow the arithmetic.
Euclidean distance measures straight-line separation. Q is one unit from A and one unit from B. It declares a tie.
The dot product multiplies matching coordinates and adds the results. Q · A is 1 × 2 + 0 × 0 = 2. Q · B is 1 × 1 + 0 × 1 = 1. If we rank by larger dot products, A wins.
Cosine similarity divides the dot product by the lengths of the two vectors. It compares their directions while removing their lengths. Q and A point the same way, so their cosine similarity is 1. B points diagonally, giving a cosine similarity of about 0.707.
The dashed segments have equal length. The arrows have different directions. A distance and an angle answer different questions.
Before calculating anything else, predict what happens if we replace A with [20, 0]. Which of the three scores stays the same?
Cosine similarity stays at 1: the direction hasn't changed. The dot product grows from 2 to 20. Euclidean distance grows from 1 to 19. A large vector can therefore win a dot-product ranking even when it is far away in Euclidean distance.
Calculate rather than guess
Vector length comes from the same geometry: square the coordinates, add them, and take a square root. For B, that's sqrt(1² + 1²) = sqrt(2).
This standard-library Python calculates all three comparisons. It rejects a zero vector for cosine because a zero-length vector has no direction.
The function assumes equal dimensions. In application code, validate that assumption before comparing anything.
When the rulers agree
To normalize a nonzero vector, divide every coordinate by its length. The result has length one. Normalizing [3, 4], whose length is 5, gives [0.6, 0.8].
If both query and document vectors have length one, dot product and cosine similarity are identical. Their Euclidean rankings agree too: squared Euclidean distance equals 2 − 2 × cosine_similarity. A larger cosine therefore means a smaller distance.
Try the endpoints. Equal unit vectors have cosine 1, giving squared distance zero. Opposite unit vectors have cosine −1, giving squared distance 4 and distance 2.
This equivalence explains why a system can normalize embeddings once and then use dot products for retrieval.
It does not mean you should normalize every model's output. Some models are trained for scores that use vector length. Follow the model's documented scoring and query/document encoding rules.
More coordinates, more work
A vector's dimensionality is its number of coordinates. Comparing two 768-coordinate vectors requires more coordinate operations than comparing two 128-coordinate vectors. Storing them at the same precision also requires six times as much coordinate storage.
Extra dimensions give a representation more room, but don't guarantee better retrieval. Training data, objective, language coverage, and your questions matter. A model's internal token-vector width is also not necessarily its exported sentence-embedding size.
A useful comparison holds the evaluation questions fixed and measures relevant results, latency, and memory. It doesn't choose the biggest dimension count on a feature table.
A score isn't a verdict
Suppose a search returns “Delete your account” for “I can't log in.” The texts share an account-related topic. That does not make the result helpful.
Likewise, a cosine score of 0.9 does not mean a 90% chance that a document answers the question. Treat thresholds as settings to evaluate on your data, not probabilities to display to users.
For a final check, imagine switching only the query encoder to a new model with the same output dimensions. Would that preserve these rankings?
There is no such guarantee. The two models may use entirely different coordinate systems. Re-encode the documents with a compatible model setup and compare the new system on the same questions.
Lesson notes
Compare distance, dot product, and cosine on the same two documents, then decide which comparisons your embedding model supports.
Key ideas
- Semantic space: distance encodes meaning
- Dimensionality trade-off: expressiveness vs compute
- Cosine similarity vs Euclidean distance vs dot product