Token embeddings as a lookup table
Explain the embedding as row x_t of W_E, which equals a one-hot vector times W_E. Explain that these vectors are learned during training like any other weights.
Builds on: Byte-pair encoding tokenizers, Weights, parameters and layers, What training does
What the animation says
Ids are just names
The tokenizer hands the model plain integers, like 464, 3290, and 318. But every layer multiplies vectors by matrices. It needs a list of numbers per token, not a single number. And the size of an id means nothing. Token 3290 is no closer to 3291 than it is to 7. So each token needs d numbers that can carry meaning. Where do they come from? Predict: how does token id 2 become its vector?
A table, one row per token
Answer: read a row. Keep a table with V rows, one per token, and d columns. Row v is token v's vector. Embedding a token means reading its row, with no arithmetic. Every token owns an independent row, so the order of the ids stops mattering. The rows start random. Training nudges them like other weights, so tokens used alike end up with similar rows. Here V is 4 and d is 3. Embed the ids 2, 0, 2: X gets one row per position. Your turn: tap the table row that fills X's third row.
Lookup = one-hot × table
The same lookup can be written as a multiply. Make a one-hot row: all zeros, with a 1 at the token's id. For token 2, that's zero, zero, one, zero. Times the table, each row gets weighted by its entry. Rows 0, 1, and 3 are multiplied by zero and vanish. Row 2 survives, exactly. Stack one one-hot row per position into H, and X equals H times the table. That's a linear layer with no bias. Indexing just skips the zeros. Now a six-row table and the ids 3, 1, 3. When training, which rows get a nonzero gradient?
Both views, and the gradient
Only rows 1 and 3. In code, indexing and the one-hot multiply give the same X. Since X is H times the table, the table's gradient is H transposed times X's gradient. Row 3 appeared twice, so it collects the sum of both positions' gradients. Row 1 gets one. Rows of absent tokens get exactly zero, as long as the table only embeds the inputs. So an embedding is a learned row per token, read by index, which equals a one-hot multiply, and it is trained like any weight.
A table with one learned row per token
Lookup equals one-hot times $W_{E}$
Embedding [2, 0, 2] with V=4, d=3
Both views, and how training updates rows
import numpy as np
V, d = 6, 4
rng = np.random.default_rng(0)
W_E = rng.normal(size=(V, d)) # learned table, starts random
ids = np.array([3, 1, 3]) # token ids, T=3
X_lookup = W_E[ids] # [T, d] by indexing
H = np.eye(V)[ids] # [T, V] one-hot rows
X_matmul = H @ W_E # [T, d] by matrix multiply
print(np.allclose(X_lookup, X_matmul)) # True
g_X = rng.normal(size=(3, d)) # stand-in gradient per position
g_W = H.T @ g_X # [V, d] gradient for the table
print(np.abs(g_W).sum(axis=1)) # nonzero only for rows 1 and 3
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses