Every network so far has taken numbers as input — pixels, coordinates, one-hot tokens. Text is made of words. Before any of the machinery from this part can touch a sentence, every word needs to become a vector — and not just any vector: one where distance means something.
Click any word. Its nearest neighbor lights up — and it's always the word that's closest in meaning, not spelling or length. "King" sits nearest to "prince," not to "man" or "queen," because this toy space has two axes: how royal, and which gender. Real embeddings have hundreds of dimensions learned from text, but the idea is identical — position encodes meaning, and nearby points mean similar things.
With word vectors and , distance is just ordinary Euclidean distance:
- — the Euclidean distance between two word vectors.
- — two word vectors being compared (e.g. the embedding coordinates of two different words).
- — the individual coordinate values of vector along each embedding dimension.
- — the individual coordinate values of vector along each embedding dimension.
- Nearest-neighbor search
A nearest-neighbor search is nothing more than computing this for every other word and keeping the smallest.
- The formula isn't the magic
What makes embeddings powerful isn't the distance formula — it's that a well-trained embedding space arranges words so this simple formula lines up with human judgments of similarity.
Click through several words and read the full ranked list. Notice the axes this space actually encodes: moving along one direction changes royalty (commoner → prince → king), moving along another changes gender (woman → man). Nobody labeled those axes by hand — they just happen to be the directions distance is measured along.
If position encodes meaning, then directions should too. Take king's vector, subtract man's, add woman's: .
- Subtract man from king
Removes whatever direction encodes "male royalty male."
- Add woman
Adds back "female" in its place — leaving pure "female royalty."
- Land on queen
In this space, that arithmetic lands exactly on queen's coordinates. This is the single most famous fact about word embeddings: analogies work as vector arithmetic, because the relationship between two words is encoded as a direction, and that direction transfers to other word pairs.
The × marks king − man + woman. Click the word you think it lands closest to.
This chapter hand-placed the vectors to make the geometry obvious. Real word embeddings (word2vec, GloVe — short for Global Vectors — and the embedding layer inside every modern language model) are learned — trained so that words appearing in similar contexts end up nearby, and this king/queen-style arithmetic emerges on its own, unplanned. Before any of that training can happen, though, a sentence first has to be cut into discrete pieces to assign vectors to. That's next: tokenization.