Codiqo

Understanding AI · lesson 40 of 60 · about 6 minutes

Meaning as Closeness

AI gives each word a list of numbers so that words with similar meanings sit close together. See how.

What you'll learn

  • Explain what an embedding is
  • Measure how close two words are
  • Explain how closeness captures meaning

Words as points

Numbering words 1, 2, 3 says nothing about meaning. So language AI gives each word a list of numbers, called an embedding, chosen during training so that words used in similar ways get similar numbers.

Cat and dog end up close together, because they turn up in similar sentences. Car ends up far away.

Pretend embeddings with just two numbers each: how animal-like and how machine-like a word is. Real ones have hundreds of numbers.

vectors = {"cat": (9, 1), "dog": (8, 2), "car": (1, 9)}

def distance(a, b):
    return abs(a[0] - b[0]) + abs(a[1] - b[1])

print(distance(vectors["cat"], vectors["dog"]))
print(distance(vectors["cat"], vectors["car"]))

Output

2
16

Learned from use

Nobody types these numbers in. They're learned from billions of sentences: words that appear in similar places get pulled together. A famous result: the difference between king and queen turned out to be similar to the difference between man and woman.

Tip

Similar meaning, nearby numbers: that's the whole idea of embeddings.

Now try it yourself

This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.