Lesson 1 of 8 · Meaning as Vectors
What Is an Embedding?
Embeddings explained: how AI turns words, sentences and images into numbers where similar meanings sit close. See them on a map and try king - man + woman.
- 6 min read
- Intermediate
- Reviewed
- What an embedding is
- Why similar meanings end up close together
- How directions in embedding space can carry meaning
- Where embeddings are used: search, recommendations, RAG and more
Turning meaning into numbers
Computers are good with numbers and bad with meaning. An embedding bridges the gap: it turns a piece of text (or an image, or a sound) into a list of numbers chosen so that similar meanings get similar numbers.
A map of meaning
Imagine giving every word just two numbers: how much it is an animal (right) or a vehicle (left), and how big it is (up). Each word becomes a point on a map:
Cat and kitten are neighbours; car and bus are neighbours; cat and truck are far apart. Distance stands for difference in meaning. That one property is what makes embeddings so useful.
Real embedding models don't use hand-picked axes like "animal" or "size". They learn their dimensions from enormous amounts of text, and individual dimensions usually don't have a simple human meaning. But the principle is the same: related things end up near each other.
Directions mean something too
In a good embedding space, directions can carry meaning. A famous example from early word embeddings: take the vector for "king", subtract "man" and add "woman", and you land near "queen". Here it is with toy two-number embeddings, where the numbers are [royalty, gender]:
import math
# Toy 2-number embeddings: [royalty, gender]. Real ones have hundreds of numbers.
words = {
"king": [3.0, 2.0], "queen": [3.0, -2.0],
"prince": [2.5, 1.8], "princess": [2.5, -1.8],
"man": [0.2, 2.0], "woman": [0.2, -2.0],
"apple": [-2.5, 0.0],
}
def distance(a, b):
return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
def nearest(vector, exclude):
best, best_distance = None, None
for word in words:
if word in exclude:
continue
d = distance(vector, words[word])
if best is None or d < best_distance:
best, best_distance = word, d
return best
# Words with similar meanings are close together
print("Closest to 'king':", nearest(words["king"], exclude=["king"]))
# Directions carry meaning too: king - man + woman
k, m, w = words["king"], words["man"], words["woman"]
result = [k[0] - m[0] + w[0], k[1] - m[1] + w[1]]
print("king - man + woman =", result, "->", nearest(result, exclude=["king", "man", "woman"]))The direction from "man" to "woman" is the same as the direction from "king" to "queen", so the arithmetic works. In real embeddings it works only roughly and only for some relationships, but it shows that the space has structure, not just clusters.
Where embeddings are used
| Use | How embeddings help |
|---|---|
| Semantic search | Find documents whose meaning matches a query, even with different words |
| RAG | Find the passages to give an LLM as context for its answer |
| Recommendations | Suggest items close to ones a user liked |
| Clustering | Group thousands of support tickets or reviews by topic automatically |
| Classification | Label new texts by which labelled examples they are closest to |
| Duplicate detection | Spot questions or products that mean the same thing |
Common mistakes
Find the nearest word to a point in embedding space.
import math
words = {
"kitten": [2.5, -2.0], "cat": [2.6, -1.2], "dog": [2.2, -0.6],
"horse": [2.0, 2.2], "car": [-2.4, 0.8], "bus": [-2.6, 2.4],
}
target = [2.3, -0.8]
# Print the word whose embedding is closest to target (smallest distance)
In a good embedding space, what does it mean if two items' vectors are close together?
Summary
- An embedding is a vector of numbers representing the meaning of text, images or other data.
- Similar meanings are close together in embedding space; distance stands for difference in meaning.
- Directions can carry meaning, as in king − man + woman ≈ queen.
- Real embeddings have hundreds or thousands of dimensions, learned from data.
- They power semantic search, RAG, recommendations, clustering, classification and duplicate detection.
Next: how to measure how similar two vectors are.
Finished reading? Tick it off to track your progress.