Rafhael Oliveira · IA

Embeddings: from a meaningless integer to a vector that knows king and queen are related

The lookup table where meaning is born, and the tie that binds it to the model's output.

1 min read· chapter: 10 min

Landed here from LinkedIn? This is chapter 02 of the course GPT-1 and GPT-2, taken apart. It reads on its own; if you want the beginning, it is chapter 00.

To the model, cat is 4899 and dog is 5257. Feeding that into a neural network is a serious conceptual error. Those numbers are locker labels, not measurements. Locker 5257 is not bigger or warmer than 4899. But a neural network only knows arithmetic: given the two integers, it will compute differences and averages and extract relationships that do not exist. The fix was to give words coordinates. 768 of them, in GPT-2 small. And it is in that space that 'king' and 'queen' become relatives.

The three points

  1. The embedding matrix is a table: row 4899 is the vector for cat. It is learned along with everything else.
  2. There is a second embedding, for position, because attention alone does not know word order.
  3. Weight tying: the same matrix that turns a token into a vector turns a vector back into a token. It saves 38 million parameters in GPT-2 small.

From the chapter, the beginning

From a meaningless integer to a vector of 768 numbers that knows "king" and "queen" are related.

We left chapter 01 with a list of integers. Say gato (cat) = 4899 and cachorro (dog) = 5257. These numbers are locker labels, not measurements. Locker 5257 is not "bigger" or "hotter" than 4899. If the vocabulary were shuffled, cat would be 12 and dog would be 40,001, and nothing would change.

But a neural network only knows how to do arithmetic. If it receives 4899 and 5257, it will inevitably compute differences, averages, products — and it will extract relations that do not exist. Feeding raw IDs into a network is a serious conceptual error.

The classic way of saying "this is category 4899, and categories have no order" is the one-hot vector: a vector of 50,257 positions, all zero, except for a 1 at position 4899.

Keep reading chapter 02 →Ler em portuguêsThe PDFs by e-mail →
What the chapter covers · 8 sections · 10 min
  1. The problem: integers have no meaning
  2. The idea: give words coordinates
  3. In practice: the matrix is a lookup table
  4. The second embedding: position
  5. Putting it together: the model's real input
  6. The final knot: weight tying
  7. The code
  8. In the lab
All chapters of the course · GPT-1 and GPT-2, taken apart
  1. 00 · The seven fossils: why GPT-1, GPT-2 and RoBERTa are a historical cross-section, not a list
  2. Glossary · Course glossary: what token, layer and model mean in AI, and in the rest of IT
  3. 01 · Why GPT-2 sees neither letters nor words, and what that costs in Portuguese
  4. 02 · Embeddings: from a meaningless integer to a vector that knows king and queen are related
  5. 03 · Attention: the single operation that replaced twenty years of recurrent networks
  6. 04 · Twelve heads instead of one, and the single line of code that separates GPT from RoBERTa
  7. 05 · Why GPT-2 needs 48 floors to answer, and what happens inside each one
  8. 06 · What scale really buys: from 124 million to 1.5 billion, measured
  9. 07 · Temperature, top-k and top-p: the decision that changes the text more than switching models
  10. 08 · Perplexity: the one number that says what the model learned, without anecdotes
  11. 09 · GPT-1 to GPT-2: same size, half the perplexity. Four decisions explain it
  12. 10 · RoBERTa: when the advance is training better what already existed
  13. 11 · Encoder vs decoder: one line of code and every consequence that descends from it
  14. 12 · The detector: how a language model becomes a classifier, and why this one should decide nothing
  15. 13 · Reading GPT-2's original code: 174 lines against everything you learned
  16. 14 · Serving models: from function call to HTTP server, and what production does on top
  17. 15 · The lab guide: every command, every script, and everything that was measured
  18. 16 · The ceiling of this path: LeCun's argument, read from inside GPT-2

After this: the Antivision experiment

Rafhael Oliveira
Rafhael Oliveira

Writes the GPT-2 course and runs the Antivision experiment, measuring every run before publishing the number. The LinkedIn posts start here and end in the chapters.

Follow on LinkedIn →