Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Inside a language model

Embeddings: giving every token a meaning

We gave every token a number. But why should hare be closer to rabbit than to Tuesday?

Can we feed the ids into the network?

Last time we turned text into a list of ids, and numbers are what a network takes. So let us simply feed them into the network.

Try the IDs below. “Hat” and “hare” happen to be neighbours; “hare” and “rabbit” are far apart.

The numbers are labels, not measurements of meaning. Feeding them in as quantities would suggest relationships that the vocabulary never promised.

Token ids do not carry meaningA number line of token ids. The ids of "hat" (3,071) and "hare" (3,072) are next to each other, though the words are unrelated. "hare" and "rabbit" (40,311) are related, but their ids are far apart.Token ids on a number linehare and rabbit: alike, far apart025,00050,000rabbit 40,311hat 3,071 and hare 3,072next-door ids, nothing in common
What to notice: These invented IDs put “hat” beside “hare” and “rabbit” far away. An ID is a label, not a measure of similarity.

We do not want to give the model a false picture of what the tokens mean. So instead of deciding the meaning for it, we let the model learn its own representation of each token.

A value that the network learns

Give each token an entry in an embedding table. The ID tells us where to look.

Start the entries randomly, then let prediction errors train them along with the rest of the network.

A value for each token, learned by backpropagationThe embedding table gives each token id a value. The value for the token "_cat" is looked up and goes into the network, which makes a guess. The guess is wrong, so the error flows back as backpropagation and nudges the value in the table.Embedding tableNetworkGuess12at0.8813th0.0514the0.4217_cat−0.31⋮⋮⋮network✗ wrongbackpropagation nudges the value“_cat”: −0.31 becomes, say, −0.12
What to notice: An ID selects a learned entry. Prediction errors update that entry during training; the lookup itself just reads it.

So the network learns a useful representation of each token, rather than treating its ID as a meaningful measurement. Looking a token up is just reading its entry: nothing is multiplied or added in the lookup itself.

One number is not enough

A single number can only place a token along a line, and tokens are alike in many ways at once. Two numbers make a map, and more numbers leave room for more kinds of meaning.

One number, two numbers, many numbers for each tokenWith one number, the words horse, hare, bike and bus sit on a line, and bus ends up far from both horse and bike. With two numbers, a map with animal-to-vehicle across and small-to-big up, all four sit near their partners. With many numbers, each token is a column of values.One number per tokenhorseharebikebusbus is big like horse, a vehicle likebike, and yet far from bothTwo numbers: a mapharehorsebikebusanimal ← → vehiclesmall ↓big ↑Many numbers: a columnharebushorse64? 768? 8,192?the designer’s choice7,168
What to notice: One coordinate puts tokens on a line; two make a map. Try following the different similarities. Real embeddings use many more coordinates.

The number of coordinates is the model’s width, also called its hidden size. Designers choose it before training.

A token’s list of coordinates is its embedding. The table holds one such list per vocabulary entry.

What the model learns

Here is a real embedding table, but a tiny one: 60 tokens (here, whole words), each with 64 numbers, learned by a small model from made-up sentences. Watch a sentence become an input table: one column for each token of the sentence.

From tokens to an input table

Interactive

Explore the map below. Do tokens used in similar settings end up near one another?

This is a two-dimensional projection of much larger vectors. Neighbours are a clue, not a guarantee of similar meaning.

Tokens that are used alike end up close together

Interactive

The input embedding stays the same for a token in every sentence. Later layers build its context-dependent representation.

How big is the embedding table?

The table has one column for every token in the vocabulary, and each column is as tall as the width. So its size follows from the two choices we have met: how many tokens, and how many numbers for each.

The size of the embedding table in five modelsFive rectangles to the same scale, one for each model's embedding table. The width is the number of tokens and the height is the number of numbers for each token. Llama 3.1 70B: 128,256 tokens by 8,192 numbers, 2.10 GB of memory. DeepSeek: 129,280 by 7,168, 1.85 GB. Gemma 2 9B: 256,000 by 3,584, 1.84 GB. gpt-oss-120b: 201,088 by 2,880, 1.16 GB. GPT-2: 50,257 by 768, 77 MB.Embedding tables of real modelsone column for every token, all to the same scaleLlama 3.1 70B2.10 GB of memory8,192numbersper token128,256 tokensDeepSeek1.85 GB of memory7,168numbersper token129,280 tokensGemma 2 9B1.84 GB of memory3,584numbersper token256,000 tokensgpt-oss-120b1.16 GB of memory2,880numbersper token201,088 tokensGPT-277 MB of memory768numbersper token50,257 tokens
What to notice: Table area represents parameter count: vocabulary size times model width. Memory assumes 16-bit numbers. Compare width as well as vocabulary size.

The embedding table is large before anything has been learned: over a gigabyte in four of these five models (at 2 bytes per number), and about 150 MB in GPT-2. And a narrow model with a huge vocabulary (Gemma) can end up with a smaller embedding table than a wide model with a smaller one (Llama).

The output needs a table of the same shape. Some models reuse the input table; others learn a separate one.

GPT-2 and Gemma share it. Llama 3.1 and DeepSeek-V3 keep separate tables.

Now we can build a language model

We now have both ends of the network.

  • In: an input table, one column for every token.
  • Out: one output neuron for every token, predicting the next one.

In between, we want a very large network. But stack many layers and the learning signal fades. The fix was the residual stream: run the data straight through on one line, and hang each layer off it as an offshoot that adds to it.

So the input table is the stream. Hang layer after layer off it, then read the last column for the prediction.

A sentence on its way through a language model

From text to a vote over the vocabularyA sentence is cut into four tokens, each token has an id, and each id is an index that picks one column of the embedding table. The four columns are copied into the input table. The input table is the residual stream: it runs straight down to the end, with layers as offshoots that read it and add their result back. At the end only the last column goes on to one output neuron for every token in the vocabulary, which give a probability for each.textThe mouse eats thetokensidsThe464_mouse9,114_eats7,330_the262embedding tableone column for everytoken in the vocabularyinput tableinput tableone column for everyinput tokenthe residualstreamlayer 1+layer 2+layer N+⋮last columnonly this one isused for the voteoutput neurons: one per tokencheese62%grass14%bread9%7,1687,16861 layers
What to notice: Each ID selects an embedding column. Follow those vectors through the layers, then read the last position’s next-token distribution. IDs and probabilities are illustrative.

The output gives every token a score; softmax turns those scores into probabilities. We choose the next token, append it and continue.

But why read only the last column? That is the next story.

Sources and assumptions

Table sizes count vocabulary entries × hidden width, at two bytes per number, before serving overhead. The embedding demonstration uses a small trained model and made-up sentences; the map is a projection of its vectors, not a map of a commercial model’s knowledge.

← Browse all subjectsBack to the beginning ↑