Geometry of learned embeddings
Explain why tokens used in similar contexts end up with similar vectors. Explain how directions such as gender or plurality can be roughly linear (king − man + woman ≈ queen).
Builds on: Token embeddings as a lookup table, Count-based n-grams and the need for generalization
What the animation says
Nobody labeled it
Training a language model asks exactly one thing: predict the next token. Nobody tells it that cat and dog are similar, or what gender is. Yet look at the embedding table after training. Cat's row and dog's row sit close together. And king, minus man, plus woman, lands near queen. Two puzzles: why does similarity appear at all, and why does gender become a straight direction?
Similar contexts, similar pushes
A row of the embedding table changes only on steps where its token appears. Each time, training nudges it toward better predictions in that context. Cat and dog appear in nearly the same places: feed the blank, the blank was asleep. So they get nearly the same nudges and drift close together. The vector becomes a summary of where the token tends to appear. Now: training sees 'the dog was asleep' often, 'the cat was asleep' rarely. What about cat?
Subtracting cancels the offset
It carries over: cat sits near dog, so it predicts alike. Count-based n-grams can't do that. Now gender. Take a model with no layers. Each token has an input vector e, each context token an output vector u. Training pushes e dot u toward the log-probability that the context token appears nearby, plus an offset for that context token. Subtract two words' vectors. The offset is the same for both, so it cancels. Left over: the difference, dotted with u, equals the log of a probability ratio.
Checking the ratios
Made-up numbers. Context word she: queen point one, king point zero zero five. Ratio twenty. Woman point one two, man point zero zero six. Also twenty. Both logs are about three. Context word his: both ratios are one tenth, log about minus two point three. Context word crown: queen and king equal, woman and man equal. Tap the context word where both differences have dot product zero.
One offset, then search
Both differences agree on all three. If ratios match for every context word, they agree on every u. There are far more u vectors than dimensions. Assume they span the space: then agreeing with all of them means equal. So queen minus king about equals woman minus man: one gender offset. Test it with toy three-d vectors. Query: king minus man plus woman. Compare by cosine similarity, skipping the input words. Queen scores point nine nine nine. One catch. Without that skip, what often wins in real embeddings?
What to remember
Often king itself: the gender offset is small next to the word vectors, so excluding inputs is standard. So, embeddings are summaries of contexts. Tokens in similar contexts get similar nudges and end up close together. If a property like gender or plurality shifts every context's log-probability by the same amount, whatever the base word, it becomes a shared offset. That offset is only roughly linear, because real ratios only roughly match.
Similar contexts give similar pushes
Why differences become directions
Checking the ratios with numbers
Analogy search with cosine similarity
import numpy as np
E = {'king':[3,1,0.2], 'queen':[3,3,0.1], 'man':[1,1,0],
'woman':[1,3.1,0.1], 'prince':[2.5,0.9,0.4], 'apple':[0,0.5,3]}
E = {w: np.array(v, float) for w, v in E.items()}
q = E['king'] - E['man'] + E['woman'] # [3, 3.1, 0.3]
cos = lambda a, b: a @ b / np.linalg.norm(a) / np.linalg.norm(b)
cands = [w for w in E if w not in ('king', 'man', 'woman')]
print({w: round(float(cos(q, E[w])), 3) for w in cands})
# {'queen': 0.999, 'prince': 0.896, 'apple': 0.186}
print(max(cands, key=lambda w: cos(q, E[w]))) # queen
What to remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses