Generative AI
Embeddings & Vector Space
Words are points in a space where distance means similarity and direction carries meaning — and every picture of that space is a projection that hides some of it.
Stage 1 of 4: Space and Similarity
PCA projection at yaw 45 degrees, tilt 10 degrees. The two axes on screen carry 61 percent of the variance. The most compressed pair is prince and puppy: 2.89 apart in the embedding, 0.45 apart on screen. No word is pinned.
- Word vector
Spin the cloud. At ±90° you are looking down the widest axis.
Raise or lower the viewpoint.
Twenty words as points in three dimensions. Orbit with Yaw and Tilt, or drag a gridline. Points close together share meaning; the axes themselves mean nothing — only the relative positions do.
Check your understanding
Words become points in a space where distance means similarity and direction carries meaning. Every picture of that space is a projection — and the projection is allowed to lie about distance, but never about meaning.
Neural networks do not process letters. To a language model a word is a point in a space of a few hundred to a few thousand dimensions — an embedding — and everything the model knows about that word is its position relative to every other word.
Two consequences, and this lab is about both. Nearby points mean similar things, so similarity becomes a measurement rather than a judgement. And the directions between points carry meaning of their own, which is why you can do arithmetic with words.
The space here is three-dimensional and hand-built, twenty words in five groups, so that the whole thing fits on a screen. Everything computed from it is real: cosine similarities, the PCA basis, the analogy.
Similarity is an angle
Cosine similarity is the cosine of the angle between two vectors: A·B / (‖A‖‖B‖). It is 1 when they point the same way, 0 when they are perpendicular, −1 when they are opposed. It ignores length entirely, which is usually what you want — a word being "more strongly" itself should not make it less similar to its neighbours.
Pin a word and the lab reports its three nearest, with the number that ranked them. dog gives cat at 0.980, puppy at 0.968, kitten at 0.957.
Directions have meanings
If the step from man to woman is a direction that means "change the gender", then adding that same step to king should land somewhere near queen. It does: king − man + woman produces a point that is no word at all, and the nearest actual word to it is queen, at similarity 0.996.
The lab computes that rather than assuming it. The operands are excluded from the search, which is not a technicality — without it an analogy will happily return one of its own inputs and you will have learned nothing.
paris − france + italy is the same trick on a different relation, and it lands on rome exactly.
What the picture costs
You are looking at three dimensions through a two-dimensional window, and one direction has to go. Whichever direction you are looking along is the one you cannot see — so the more the cloud varies along your line of sight, the more the picture is hiding.
Variance on screen is that, as a number: the fraction of the cloud's total variance carried by the two axes you can see. And the compressed pair names the current casualty — the two words whose real distance this view understates by the largest factor.
Note that there is always one. At the clearest available angle it is woman and queen, understated forty-sevenfold. No flat picture of a curved-up space is faithful to every pair at once; the readout tells you which pair you are currently being misled about, not that a perfect view exists.
PCA picks the best available compromise: the two orthogonal directions along which the data varies most, so the picture discards the least total variance. At yaw 0 that is 87% on screen. Turn to look straight down the first principal component — yaw ±90° — and it collapses to 34%, whole clusters slide behind each other, and kitten and italy land a fifth of a unit apart while being four and a half apart in the embedding.
The one thing the projection may not do
Watch the readouts while you do all that. They do not move.
Nearest neighbours and similarities in this lab are computed from the embedding, never from the picture — and that is a correctness requirement, not a preference. Cosine similarity survives rotating a space and does not survive shifting it, because it is an angle measured at the origin. A PCA projection subtracts the mean, so it moves the origin. Compute neighbours in that basis instead and you get answers like girl's nearest neighbour being france, which is not a fact about language.
This is the habit worth taking away. Every t-SNE or UMAP plot of an embedding you have ever seen is one of these views. The clusters in it are real evidence and the distances in it are not measurements — if you want a distance, ask the vectors.
Reference
- Vector
- An array of numbers locating a word in the embedding space.
- Cosine Similarity
- cos(θ) = A·B / (||A|| ||B||) — the angle between two vectors. 1 is the same direction, 0 orthogonal, −1 opposite.
- Dot Product
- A·B = Σ(aᵢ bᵢ) — how much one vector projects onto another.
- Rotation vs translation
- cosine similarity survives rotating the space and does **not** survive shifting it, which is why neighbours are computed in the embedding rather than in a mean-centred projection.
- Semantic Arithmetic
- v(king) − v(man) + v(woman) lands nearest v(queen), at similarity 0.996.
- PCA
- the orthogonal directions of greatest variance, found here by power iteration on the covariance matrix with deflation.
- Variance on screen
- the fraction of the cloud’s total variance carried by the two axes you can see. The rest is hidden along your line of sight.
- Compressed pair
- the two words whose true distance the current view understates by the largest factor. There is always one — no flat picture is faithful to every pair.
Break it on purpose
Orbit until you are looking straight down the first principal component — yaw near ±90° — and the two axes on screen carry only a third of the variance instead of seven eighths. Whole clusters slide behind each other: `kitten` and `italy` sit 4.5 apart in the embedding and land a fifth of a unit apart on screen. Nothing has changed about the words. The picture has stopped being able to show you what it knows.