Embeddings

Three-panel comic: one-hot vectors waste most slots, embeddings store compact learned vectors, and related words can become close in vector space
What You’ll Learn

You’ll learn why one-hot vectors are large and semantically blind, and how embeddings replace them with compact learned vectors. You’ll also see how lookup tables turn token IDs into vectors a transformer can use.

The simplest way to represent a token as numbers is a one-hot vector: a sparse vector of all zeros with a single 1. This works, but it has serious problems. Embeddings are the elegant fix.


The Problem with One-Hot

The most naive approach: give each token a unique position in a vector of zeros, then put a 1 at that position:

One-Hot Encoding

Step 1: Pick a token

Choose one token. A one-hot vector gives every vocabulary token its own slot.

This has three problems:

1. Dimension Explosion

With a real vocabulary of 50,000 tokens, each one-hot vector has 50,000 dimensions. That’s 200KB per token. A 1,000-token sequence would need 200MB just for inputs.

RepresentationDimensionsMemory per Token
One-hot (50K vocab)50,000200 KB
Embedding (256-dim)2561 KB
Savings195× smaller200× less memory

2. No Semantic Information

One-hot treats every pair of tokens as equally different. The distance between any two one-hot vectors is always the same:

PairSemantic RelationshipDistance
cat ↔ dogBoth mammals, petssame
cat ↔ fishLess relatedsame
cat ↔ quantumCompletely unrelatedsame

Every token is equally “far” from every other token. There’s no concept of similarity.

3. Wasteful Computation

When you multiply a weight matrix by a one-hot vector, most of the computation is wasted multiplying by zeros. The result is just one column of the matrix. Why do 50,000 multiplications when you could just look up the right column?


The Solution: Embedding Lookup

An embedding matrix stores a dense vector for each token. Instead of multiplying by a one-hot vector, we simply look up the row. Think of it like a dictionary where each token ID maps to a small, meaningful vector.

Try it yourself. Click a token below to see how its ID selects one row from the embedding matrix:

Level 1 · Table Lookup by ID Find and click the token that copies out Row 4 of the embedding matrix.
0

“the” → Row 0

0.05 d0
-0.12 d1
0.41 d2
0.08 d3
-0.35 d4
0.19 d5
0.02 d6
-0.08 d7

What to look for: Each token ID points to exactly one row. The output embedding is just that row copied out.

Quick Check
What does an embedding lookup do?
Your turn
Choose an answer to check your understanding.

How Embeddings Learn

The embedding matrix is not hand-designed. It’s initialized randomly and learned during training, just like any other weight matrix.

During backpropagation, gradients flow back through the network into the embedding vectors. But there’s a key difference: only the tokens that appeared in the current batch get updated.

What happensEmbedding rows affected
Batch contains tokens [5, 10, 15]Only rows 5, 10, 15 get gradient updates
Token 5 appears 3 times in batchRow 5 accumulates 3 gradient contributions
Token 42 not in batchRow 42 stays unchanged this step

After seeing millions of sentences like “the cat sat”, “the dog sat”, “the cat ran”:

Word PairSimilarityWhy
cat ↔ dogHighBoth appear in “the ___ sat”
sat ↔ ranHighBoth appear in “the cat ___“
cat ↔ ranLowerDifferent grammatical roles

Tokens that appear in similar contexts tend to get similar embeddings. This is the distributional hypothesis: “You shall know a word by the company it keeps.”

Fill in the Blanks
Embeddings place tokens with similar close together in vector space, because the model learns that they can often be for each other.
Fill in every blank, then check your answer.
Quick Check
Why use learned embeddings instead of one-hot vectors?
Your turn
Choose an answer to check your understanding.

Embeddings in Transformers

In a Transformer, embeddings are the first layer. Step through the pipeline to see how raw text becomes the vectors the model works with:

Embedding Pipeline

Tokenize
Input
The
cat
sat

What to look for: Step through all four stages. Raw words become token IDs, each ID gets looked up in the embedding matrix, and a position vector is added so the model knows order. These combined vectors enter the transformer layers.

Weight Tying

A clever trick used in GPT, BERT, and many others: the same embedding matrix is used for both input and output. At the input, you look up a row to get a vector. At the output, you compare the hidden state with token embeddings to get a score for every token in the vocabulary.

This halves the embedding parameters and ensures input/output live in the same semantic space. A token’s embedding is also its “target vector” for prediction.


Quick Summary

ConceptWhat HappensIntuition
One-hot encodingEach token becomes a sparse vector with a single 1Like giving every student a unique ID number, with no info about who they are
Embedding lookupToken ID indexes directly into a dense matrix rowLike looking up a student’s profile: compact and informative
Learning embeddingsGradients update only the rows used in each batchTokens in similar contexts get nudged toward similar vectors
Weight tyingSame matrix for input lookup and output predictionThe model asks “does my prediction look like this word’s meaning?”

Learning Checklist
  • I can explain the three problems with one-hot vectors (dimension explosion, no semantics, wasteful computation)
  • I understand that embedding lookup uses a token ID to select one learned row
  • I can describe how embeddings learn semantic relationships through training (distributional hypothesis)
  • I can explain weight tying and why sharing the embedding matrix between input and output makes sense

Next Steps

Now we know how tokens become vectors. Next we need the smallest neural network move: turning one vector into another with learned weights. Neural Network Basics →