Representations

Embedding space

An embedding space is the high-dimensional space where a model places every token or sentence as a point, so that similar meanings land close together. Directions in the space carry meaning too, which is what makes vector arithmetic on words work.

Words become points in a 3D space where nearness means similar meaning. Watch the clusters form, trace king to its nearest neighbours, see directions carry meaning, and find out what any flat picture of an embedding hides.

By Film published 1:41 watch

Analogy
Loading 3D view

nearest to king, by cosine

A space of meaningTo compare words, a model turns each one into a point, placed so that nearness means similar meaning: here are forty-two of them, in three dimensions.

Narrated with James Phoenix's AI voice.

king
Word
uncle0.99
Nearest by cosine
-
Analogy lands on
42
Words

king sits in the people cluster; its nearest word by angle is uncle, at a similarity of 0.99. Drag to orbit, click a point to pick it.

An embedding turns a word into a list of numbers, which makes it a point in a space. The useful property is that the space is arranged by meaning: words used in similar ways end up near each other, so a model can compare words by comparing points.

The film starts with forty-two words and lets them gather into their groups: people, countries, capitals, animals and professions. Then it traces one word. Ask for the nearest neighbours of king and uncle, prince and father come back, each with a similarity above 0.98.

The surprising part is that directions carry meaning as well as positions. The arrow from man to woman is the same arrow as the one from king to queen, so king minus man plus woman lands exactly on queen. Every country to its capital is one more copy of a single arrow, which is why "Paris minus France plus Germany" lands on Berlin.

Similarity is measured by angle, not distance. Draw an arrow from the origin (the gold knob on the stage) to each word and compare the angle between arrows. King and queen are four units apart and forty-four degrees apart, a similarity of 0.72, while prince sits just ten degrees from king and scores 0.98. Length mostly tracks how often a word appears, so the angle is where the meaning lives.

The failure mode is the picture itself. Real embeddings have hundreds or thousands of dimensions, and any plot drops almost all of them. The film squashes just one axis and every capital lands on its country and every queen on her king, because those relations lived in the dimension that was thrown away. Two points that look close in a plot can be far apart in the dimensions you cannot see.

So rank neighbours by cosine in the full space, and use a picture like this one only to see the shape of the groups. After the film, pick any word to see its neighbours, or an analogy to see it drawn as arrows, and drag to orbit.

A necessary caveat: the vectors here are illustrative, hand-built in three dimensions so the geometry survives being drawn. That is also why every analogy lands with a similarity of exactly 1.00; in a real model the answer is merely the nearest word, often at 0.6 to 0.8.

The maths

Cosine similarity
cos(a,b)  =  abab\cos(\mathbf{a}, \mathbf{b}) \;=\; \frac{\mathbf{a} \cdot \mathbf{b}}{\lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert}

The measure behind the nearest-neighbour readout. It compares direction only, ignoring length, which is why embeddings are usually compared this way rather than by Euclidean distance.

Analogy arithmetic
v  =  ab+c,answer  =  argmaxw{a,b,c}cos(v,w)\mathbf{v} \;=\; \mathbf{a} - \mathbf{b} + \mathbf{c}, \qquad \text{answer} \;=\; \arg\max_{w \notin \{a,b,c\}} \cos(\mathbf{v}, \mathbf{w})

The parallelogram the presets draw. king minus man plus woman lands near queen because the man-to-woman offset and the king-to-queen offset are approximately the same vector.

Why a picture hides structure
xdrawn  =  Px,PR3×d,d3\mathbf{x}_{\text{drawn}} \;=\; P\,\mathbf{x}, \qquad P \in \mathbb{R}^{3 \times d}, \quad d \gg 3

Any plot of an embedding is a projection P from d dimensions down to the three you can see, and whatever varies only in the dropped directions vanishes. The film squashes one axis here and Paris lands exactly on France, because the capital relation lived entirely in that axis.

Transcript

The narration, chapter by chapter. A timestamp opens the film at that moment.

0:00A space of meaning
To compare words, a model turns each one into a point, placed so that nearness means similar meaning: here are forty-two of them, in three dimensions.
0:11Clusters
Train that space well and the points gather by meaning: the animals sit together, the professions sit together, and the capitals hover just above their countries.
0:22Neighbours
Trace one word: ask for the nearest neighbours of king, and uncle, prince and father come back, each with a similarity above 0.98.
0:34Directions
The surprise is that directions carry meaning: man to woman is the same arrow as king to queen, so king minus man plus woman lands exactly on queen, and every country to its capital is one more parallel arrow.
0:52Angle, not gap
Similarity is the angle between arrows from the origin, not the gap between the points: king and queen are four units apart and forty-four degrees apart, a similarity of 0.72, while prince, at ten degrees, scores 0.98.
1:12What pictures hide
The catch: real embeddings have hundreds or thousands of dimensions, and every picture drops most of them, so flatten just one here and each capital lands on its country, and each queen on her king.
1:28The takeaway
So rank neighbours by cosine, in the full space, and use a picture like this only for the shape of the groups, never for how close two words really are.

Also available as captions (WebVTT), the video file and a Markdown copy of this page.

Related terms

More visualisations

Embed this film

Paste this into a post, a course page or a newsletter. The film plays in place, with the same controls as here, and credits the source.

Building with language models?

These explainers come out of the work. If you want the same thinking applied to your own system, that is what I do.

See how I can help