Lesson 1 of 5 · 2 min
Meaning as numbers
An embedding is a list of numbers for a piece of text, learned so that similar meanings land close together. These tiny ones are written by hand to show the idea; real ones have hundreds or thousands of numbers.
import numpy as np
# dimensions: [money, time, place]
words = {"refund": [0.9, 0.2, 0.0], "money back": [0.85, 0.25, 0.0], "deadline": [0.1, 0.95, 0.0], "office": [0.0, 0.1, 0.9]}
def cosine(a, b):
a, b = np.array(a), np.array(b)
return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))
for name, v in words.items():
print(f"{name:11} similarity to refund: {cosine(words['refund'], v):.2f}")
