Embeddings Explained: How Machines Measure Meaning

⚡ TL;DR: An embedding is a list of numbers representing meaning. Similar things get similar vectors, so you can measure how related two pieces of text are with a dot product — which is how semantic search, recommendations and RAG all work.

Tokenisation gave us integer IDs, and those IDs mean nothing — token 5771 is no closer to 5772 than to 90000. Embeddings fix that by replacing each ID with a vector positioned so that closeness in space means closeness in meaning.

This is where your linear algebra stops being theory. Everything below is vectors, distances and dot products.

What an embedding looks like

An embedding is simply a vector — typically 384, 768 or 1,536 numbers. The word “dog” might become [0.21, −0.44, 0.87, …]. On its own that list is meaningless. What matters is where it sits relative to every other vector.

Embeddings diagram showing man, woman, king, queen, boy and girl positioned so the same relationship points in the same direction
A 2-D sketch of a many-dimensional space. Related pairs are separated by a similar direction and distance.

The famous demonstration is that king − man + woman lands near queen. The gender relationship is a consistent direction in the space, so you can add and subtract it like any other vector.

⚠️ The analogy is a real effect, but it is often overstated

It works cleanly for a handful of well-chosen examples and much less cleanly in general. The result is usually only the nearest neighbour once you exclude the input words themselves, and many published analogies fall apart without that exclusion. Treat it as a good illustration of structure, not proof that the space is neatly organised.

Measuring similarity

Two vectors are compared by the angle between them, using cosine similarity:

$$\text{similarity} = \frac{A \cdot B}{\|A\| \, \|B\|}$$

That is the dot product divided by both magnitudes — exactly the formula from your linear algebra pages, now doing real work.

✅ Reading the number: 1 means the same direction — near-identical meaning. 0 means perpendicular, unrelated. −1 means opposite. Cosine ignores magnitude and compares direction only, which is what you want: a long document and a short one about the same topic should score as similar. Try it in the cosine similarity calculator.

Why embeddings beat keyword search

Search for “how do I fix a puncture” and keyword matching finds documents containing those exact words. A document titled “repairing a flat tyre” contains none of them, and is exactly what you wanted.

Embedding both and comparing vectors solves this — the two phrases land close together because they mean the same thing. That is semantic search, and it is the mechanism behind retrieval-augmented generation, recommendation systems, duplicate detection and clustering of documents.

Word embeddings versus sentence embeddings

TypeOne vector perLimitation
Static (word2vec, GloVe)Word, fixed forever“Bank” gets one vector for both the river and the money sense
Contextual (BERT-style)Word in its sentenceMore compute; needs the surrounding text
Sentence / documentWhole passageThe practical choice for search and RAG

Contextual embeddings are what attention produces inside a modern model: the vector for “bank” is shaped by the words around it, so the two senses separate properly.

Where embeddings come from

They are learned, not designed. Nobody decided which dimension means “royalty” — and no individual dimension cleanly does. The vectors are parameters, adjusted by backpropagation like any others, under a training objective that rewards putting words used in similar contexts near each other.

The underlying assumption is old and linguistic: words appearing in similar contexts tend to mean similar things. Embeddings are that idea made numerical.

Two practical cautions

⚠️ Before you build on embeddings

  • They inherit bias from training text. If the corpus associates certain professions with certain genders, the vectors encode that association — and any system built on them repeats it. See bias in machine learning.
  • Never mix models. Vectors from two different embedding models are not comparable, even at the same dimensionality. Re-embed everything when you change model.

🔑 Key Takeaways

  • An embedding is a vector whose position encodes meaning.
  • Cosine similarity — a dot product over two magnitudes — measures how related two texts are.
  • Embeddings find matches that share no words at all, which keyword search cannot.
  • Contextual embeddings solve the “bank” problem that static ones cannot.
  • They carry the biases of their training text, and vectors from different models never mix.

Further reading

The Sentence Transformers library is the practical starting point for generating your own embeddings in a few lines of Python, and Google’s embeddings course module covers how the vectors are learned.

Where to go next

Scroll to Top