Give every word a vector, and words with similar meanings end up pointing the same way. Find the word closest to "kitten" with cosine similarity.
AI · beginner · 10 min · 22 steps · free
What you'll learn
Describe an embedding as a list of numbers for meaning
Compute cosine similarity between two vectors
Use similarity to find the closest meaning
embeddings.py
import math
# Each word gets a vector: a list of numbers that describes its meaning.
# Real models learn hundreds of numbers per word. Here there are two: how animal-like, how food-like.
words = ["cat", "dog", "apple", "bread"]
kinds = ["animal", "animal", "food", "food"]
animal = [0.9, 0.7, 0.1, 0.2]
food = [0.1, 0.3, 0.9, 0.8]
# A new word, "kitten". Which known word means the most similar thing?
qa, qf = 0.8, 0.1
similarity = []
for a, f in zip(animal, food):
# Cosine similarity: 1 when two vectors point the same way, 0 when they have nothing in common.
cos = (a * qa + f * qf) / (math.sqrt(a * a + f * f) * math.sqrt(qa * qa + qf * qf))
similarity.append(round(cos, 3))
closest = words[similarity.index(max(similarity))]
print("kitten is closest to", closest)
Step by step
This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
Four words the model knows.
Two are animals and two are food. The model is never told this; it is here only to colour the chart.
The first number of each word's vector: how animal-like it is.
The second number: how food-like. Each word is now a point on the chart: animals bottom right, food top left.
A new word, "kitten": very animal-like, barely food-like. It is the star on the chart.
An empty list for one similarity score per word.
Compare kitten with each word in turn, starting with cat.
Cosine similarity: the dot product of the two vectors divided by their lengths. Cat and kitten point almost exactly the same way: 1.0.
Save it, rounded.
a changes from 0.9 to 0.7. f changes from 0.1 to 0.3.
Dog: 0.961. Also very close; dogs are animals too.
0.961 is added to the end of the list that similarity points to.
a changes from 0.7 to 0.1. f changes from 0.3 to 0.9.
Apple: 0.233. It points a very different way.
0.233 is added to the end of the list that similarity points to.
a changes from 0.1 to 0.2. f changes from 0.9 to 0.8.
Bread: 0.361. Low.
0.361 is added to the end of the list that similarity points to.
Python moves to the next item of the loop: for a, f in zip(animal, food).
The closest meaning is the word with the highest similarity: cat.
Kitten is closest to cat. Nobody wrote a rule saying kittens are cats: closeness of the vectors is the meaning. Real embeddings have hundreds of numbers, but the comparison is exactly this.
Common mistakes
Expecting each number in a real embedding to have a clear name: learned dimensions are rarely that tidy
Comparing embeddings made by different models
Best practices
Use cosine similarity, which ignores length, when comparing meaning
Key takeaways
An embedding is a list of numbers that stands for a meaning.
Similar meanings get vectors that point in similar directions.
Cosine similarity measures that: 1 is the same direction, 0 is unrelated.
Finding the closest vectors is how search by meaning works.
Check yourself
Two words have cosine similarity 0.98. What does that suggest?
Answer: Their meanings are very similar. Near 1 means the vectors point almost the same way, so the model uses the words in similar ways.
Why divide the dot product by the vectors' lengths?
Answer: So only the direction counts, not the size. Dividing by the lengths cancels out size, leaving how well the directions line up.