Tokens, Embeddings, and Attention
Word embeddings
Turning words into vectors that carry meaning
A computer has no idea what "king" means. So we do something audacious: we represent each word as a list of numbers - a point in a high-dimensional space - and we learn those numbers so that words used in similar ways land near each other. Once meaning is a set of coordinates, similarity is an angle, and analogies become addition and subtraction. This is a preview built directly on Vectors (L8) and the Dot Product (L9).
One-hot: every word a lonely island
The naive encoding gives each word its own slot. With a vocabulary of words, "king" is a vector of all zeros with a single in its slot - a vector living in :
It is honest but useless for meaning. Every pair of distinct one-hot vectors is orthogonal: their dot product is exactly . So the geometry insists that "king" is precisely as related to "queen" as it is to "banana" - no notion of similarity survives. And with often above 50,000, the vectors are enormous and almost entirely zeros.
The distributional hypothesis
Nobody hands the model the meaning of each coordinate; the numbers are learned by gradient descent so that the geometry pays off. The remarkable result: individual directions in that space line up with human-recognisable concepts - a "gender" direction, a "royalty" direction, a "plural" direction. That is what makes the toy below possible.
Meaning becomes geometry
Below is a hand-built 2-D embedding space. The x-axis behaves like a gender dimension and the y-axis like a royalty / seniority dimension. The dashed cross marks the neutral centre of the space. Real embeddings have 300 dimensions and are learned, not placed - but the geometry works exactly like this.
king − man + woman → queen (cos 1)
The magic moment
The arithmetic of meaning
Two operations do all the work, and you already met both in Lessons 8 and 9.
Analogy = vector arithmetic. Subtracting two word vectors isolates a relationship; adding it elsewhere applies that relationship:
The two formulas
The famous analogy is just addition and subtraction of vectors, then a search for the closest real word:
Similarity = angle, not distance. To decide which real word a result is closest to, we use cosine similarity - the cosine of the angle between two vectors (Lesson 9). It ignores length and keeps only direction, so it reads as "how aligned in meaning are these?":
A value near means "point the same way" (synonyms), near means "unrelated" (orthogonal, as the one-hot vectors were), and near means "opposite". Switch the demo to Neighbours and click king: its top match is prince, because their vectors point in nearly the same direction from the centre.
# E is the embedding matrix: one learned row per word (V rows, d cols)
# "looking up" a word is literally indexing into a row by its token id
king = E[ vocab["king"] ]
man = E[ vocab["man"] ]
woman = E[ vocab["woman"] ]
def cosine(a, b): # similarity = angle, not distance
return (a @ b) / (norm(a) * norm(b))
target = king - man + woman # vector arithmetic isolates + applies "royalty"
nearest = max(vocab, key=lambda w: cosine(target, E[vocab[w]]))
# nearest -> "queen"Check yourself
In an embedding space, cosine similarity between two word vectors measures their:
Reading king - man + woman = queen, the vector (king - man) captures the idea of:
Recall: why can a model do arithmetic like king - man + woman and land on queen?
Try to state it, then check.
Lock it in
- One-hot vectors are orthogonal and enormous (one slot per vocabulary word), so they carry no similarity - "king" is as unrelated to "queen" as to "banana".
- The distributional hypothesis - know a word by the company it keeps - lets gradient descent learn short dense vectors () where words sharing contexts land near each other.
- Similarity is angle, not distance: cosine similarity reads meaning off direction and ignores length, landing in .
- Consistent relationships become consistent directions, so analogies are plain vector arithmetic: .
- These learned vectors are the embedding matrix - the first layer of every LLM, where a token id simply indexes its row.
Primary source
Ask your teacher