Skip to main content

Embedding

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/embedding

In short

An embedding is a list of numbers, called a vector, that represents the meaning of text, images, or other data so that similar items end up close together.

What is an embedding?

An embedding turns something a computer can't easily compare, such as a sentence or a photo, into a fixed-length list of numbers called a vector. An embedding model is trained so that items with similar meaning get similar vectors. For example, 'How do I reset my password?' and 'I forgot my login' produce vectors that are close together, even though they share almost no words.

You can picture embeddings as points on a map, except the map has hundreds or thousands of dimensions instead of two. To measure how related two items are, you compare their vectors, most often with cosine similarity, which looks at the angle between them. A score close to 1 means very similar meaning, while a score near 0 means the items are unrelated.

Embeddings power semantic search, recommendations, duplicate detection, clustering, and RAG systems. They are typically stored in a vector database, or in a regular database with a vector index, which can quickly find the stored vectors nearest to a query vector.

Semantic search with embeddings is different from keyword search. Keyword search matches exact words, while embedding search matches meaning, so it can find relevant results even when they use different wording. Many systems combine both approaches, which is called hybrid search.

At a glance

Words placed as points by their embeddings, drawn here in two dimensions. Words with similar meanings sit close together: king and queen, cat and dog, Paris and London. A search for kitten turns the query into a point too and finds its nearest neighbors, cat and puppy.kingqueenprincecatdogpuppyParisLondonBerlinkittennearest neighborscat → [0.21, −0.43, 0.88, …]hundreds of numbers per text
An embedding turns text into a list of numbers, so “similar meaning” becomes “close together”, and search becomes finding the nearest points.

Key takeaways

  • An embedding is a vector of numbers that captures meaning.
  • Items with similar meaning have vectors that are close together.
  • Cosine similarity is a common way to compare two embeddings.
  • Embeddings enable semantic search, recommendations, and RAG.
  • Only compare embeddings produced by the same model.

Example

Comparing embeddings with cosine similaritypython
import math

def cosine_similarity(a, b):
    # 1.0 = same direction (similar meaning), near 0 = unrelated
    dot = sum(x * y for x, y in zip(a, b))
    return dot / (math.hypot(*a) * math.hypot(*b))

# Tiny made-up embeddings (real ones have hundreds of dimensions)
cat = [0.9, 0.1, 0.3]
kitten = [0.85, 0.15, 0.35]
car = [0.1, 0.9, 0.2]

print(cosine_similarity(cat, kitten))  # about 0.996 (very similar)
print(cosine_similarity(cat, car))     # about 0.27 (not similar)

Readers ask

What is a vector database?

A vector database stores embeddings and can quickly find the vectors closest to a query vector, which is called similarity or nearest-neighbor search. It is a core building block of semantic search and RAG applications.

What is the difference between an embedding and a token?

A token is a chunk of text that a language model reads, while an embedding is a vector of numbers that represents meaning. Inside an LLM each token is converted into an embedding, and dedicated embedding models produce a single vector for a whole sentence or document.

How many dimensions does an embedding have?

It depends on the model; common sizes range from a few hundred to a few thousand numbers. More dimensions can capture more nuance but need more storage and computing power.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings