Embeddings

What Embeddings Are and Why They Matter

A clear introduction to embeddings, the vector representations that let machines compare meaning in text, images and other data, with practical uses.

On this page
  1. The basic idea
  2. Why not just use the raw data
  3. How embeddings are learned
  4. Measuring similarity
  5. What embeddings are used for
  6. Vector databases and search at scale
  7. Limits worth knowing
  8. Frequently Asked Questions

Computers work with numbers, but much of the information we care about is words, pictures and sounds. An embedding is a way to bridge that gap. It turns a piece of data into a list of numbers in such a way that similar things end up with similar lists.

That simple idea sits underneath search engines, recommendation systems, and the language models that power modern assistants. Once you understand embeddings, a lot of current AI tooling becomes easier to reason about.

The basic idea

An embedding is a vector, which is just an ordered list of numbers, that represents an item. The item could be a word, a sentence, a document, an image, a user or a product. A typical embedding might have a few hundred or a few thousand numbers.

The key property is geometric. Items that are similar in meaning are placed close together in this numeric space, and dissimilar items are placed far apart. The word "doctor" would sit near "nurse" and far from "bicycle."

You can think of it as a map. A paper map places cities in two dimensions so that nearby cities are close together. An embedding does the same for meaning, but in many more dimensions.

Why not just use the raw data

Consider a simple way to represent words: give each word in a vocabulary its own position in a very long list, with a 1 in that position and 0 everywhere else. This is called one-hot encoding.

It has two big problems. The vectors are huge and mostly empty. Worse, they carry no sense of similarity. In one-hot form, "cat" is exactly as different from "kitten" as it is from "airplane." Every pair of words is equally unrelated.

Embeddings fix this. They are compact and dense, and the distances between them mean something. A model can generalize: if it learned something about "kitten," it can apply part of that knowledge to "cat."

How embeddings are learned

Embeddings are not written by hand. They are learned from data, usually as a side effect of training a model on some task.

Early word embedding methods learned from a simple principle: words that appear in similar contexts tend to have similar meanings. If "coffee" and "tea" keep showing up near words like "cup," "hot" and "morning," their vectors drift toward each other during training.

Modern systems use neural networks, often transformers, trained on enormous collections of text, images or both. The network is asked to predict missing pieces or match related items, and the internal representations it builds become the embeddings. Some models produce a different embedding for the same word depending on the sentence around it, which helps with words that have several meanings.

Measuring similarity

Once items are vectors, comparing them becomes a matter of arithmetic. The most common measure is cosine similarity, which looks at the angle between two vectors. A small angle means the items are similar.

import numpy as np

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

A score near 1 means the vectors point in nearly the same direction. A score near 0 means they are unrelated. Other measures, such as Euclidean distance and the dot product, are also used depending on the model.

What embeddings are used for

Embeddings turn up in many everyday systems:

  • Semantic search: instead of matching exact keywords, a search system embeds the query and the documents, then returns the documents whose vectors are closest. A search for "how to fix a flat tire" can find a page about "repairing a punctured wheel."
  • Recommendations: users and items can both be embedded, and the system suggests items near a user's tastes.
  • Clustering and deduplication: grouping similar documents or finding near-duplicates becomes a distance calculation.
  • Classification: a small classifier trained on top of embeddings can sort support tickets or label sentiment with little data.
  • Retrieval for language models: a common pattern fetches relevant passages by embedding similarity and gives them to a language model as context before it answers.
  • Multimodal matching: some models place text and images in the same space, so a text description can retrieve matching pictures.

Vector databases and search at scale

Comparing a query against millions of vectors one by one is slow. Specialized indexes use approximate nearest neighbor search to find close vectors quickly, trading a tiny amount of exactness for a large gain in speed. Libraries and database extensions built for this are now common, but the underlying concept stays the same: store vectors, then find the nearest ones.

Limits worth knowing

Embeddings are powerful but not perfect. They reflect the data they were trained on, so they can absorb biases present in that data. Two items being close in vector space does not guarantee they are related in the way you need. A model trained on general text may not understand specialized vocabulary in medicine or law as well as one tuned for it.

Embeddings from different models are also not interchangeable. Vectors made by one model cannot be compared with vectors from another, and switching models usually means re-embedding all your data. Dimensions are not individually interpretable either, so you generally cannot say what a single number in the vector means.

Frequently Asked Questions

Are embeddings the same as the weights of a model?

No. Weights are the learned parameters inside a model. An embedding is the output representation for a particular input, although in some models a table of learned embeddings is itself stored as part of the weights.

How many dimensions should an embedding have?

It depends on the model and the task. More dimensions can capture finer detail but cost more to store and search. Most people use the size produced by the model they select rather than choosing it themselves.

Can I embed things other than text?

Yes. Images, audio clips, code, users, products and graph nodes can all be embedded. The requirement is a model trained to map that type of data into a vector space where closeness reflects similarity.