Part XI — Sequence Models: RNNs, LSTMs, Attention & The Transformer Block · Chapter 6

Word embeddings (Word2Vec & GloVe)

Hook

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.

Intuition
kingqueenprinceprincessmanwoman
nearest to "king" is "prince" — distance 1.00

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.

Formalize

With word vectors aa and bb, distance is just ordinary Euclidean distance:

d(a,b)=(a1b1)2+(a2b2)2+d(a, b) = \sqrt{(a_1-b_1)^2 + (a_2-b_2)^2 + \dots}
  • d(a,b)d(a, b) — the Euclidean distance between two word vectors.
  • a,ba, b — two word vectors being compared (e.g. the embedding coordinates of two different words).
  • a1,a2,a_1, a_2, \dots — the individual coordinate values of vector aa along each embedding dimension.
  • b1,b2,b_1, b_2, \dots — the individual coordinate values of vector bb along each embedding dimension.
  1. Nearest-neighbor search

    A nearest-neighbor search is nothing more than computing this for every other word and keeping the smallest.

  2. 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.

Play
kingqueenprinceprincessmanwoman
ranked by distance from "queen": princess (1.00), king (2.00), prince (2.24), woman (3.00), man (3.61)

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.

Worked example

If position encodes meaning, then directions should too. Take king's vector, subtract man's, add woman's: kingman+woman\text{king} - \text{man} + \text{woman}.

  1. Subtract man from king

    Removes whatever direction encodes "male royalty \to male."

  2. Add woman

    Adds back "female" in its place — leaving pure "female royalty."

  3. 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.

Checkpoint

The × marks kingman + woman. Click the word you think it lands closest to.

?kingqueenprinceprincessmanwoman
click a word
Click a word to try it
Summary
kingman+womanqueen\text{king} - \text{man} + \text{woman} \approx \text{queen}

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.