All courses

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

▶ Watch this lesson Start the whole course

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.

Why does a token id need a vector?

The tokenizer turns text into integers like 464, 3290, 318. Layers, though, multiply **vectors** by matrices. The size of an id means nothing: token 3290 is no more like 3291 than like 7. So how does each id become a vector of $d$ numbers that can carry useful meaning, and where do those numbers come from?

A table with one learned row per token

Keep a table with $V$ rows and $d$ columns. Row $v$ is token $v$'s vector. To embed a token, read its row. No arithmetic is involved. Every token gets its own independent vector, so the arbitrary order of ids doesn't matter. The rows start random, and training nudges them like any other weights. Tokens that are used in similar ways tend to end up with similar vectors.

Lookup equals one-hot times $W_{E}$

Let $e\in\mathbb{R}^{1\times V}$ be one-hot: $e_{x_t}=1$, all other entries 0. Then $$(eW_E)_j=\sum_i e_i\,(W_E)_{ij}=(W_E)_{x_t,j}$$ so $eW_E$ is row $x_t$ of $W_E\in\mathbb{R}^{V\times d}$. Stack $T$ one-hot rows into $H\in\mathbb{R}^{T\times V}$ to get $X=HW_E\in\mathbb{R}^{T\times d}$. The embedding is a linear layer with no bias. Indexing just skips the multiplications by zero. The table holds $V\cdot d$ parameters.

Embedding [2, 0, 2] with V=4, d=3

$W_E$ rows: 0: $[0.1,0.5,-0.2]$, 1: $[0.9,0,0.3]$, 2: $[-0.4,0.7,0.6]$, 3: $[0.2,-0.1,0.8]$. Token 2 gives $e=[0,0,1,0]$, so $eW_E=0\cdot r_0+0\cdot r_1+1\cdot r_2+0\cdot r_3=[-0.4,0.7,0.6]$. The ids $[2,0,2]$ give $X=\begin{bmatrix}-0.4&0.7&0.6\\0.1&0.5&-0.2\\-0.4&0.7&0.6\end{bmatrix}$. The same token gets the same vector wherever it appears.

Both views, and how training updates rows

Indexing and the one-hot multiply give the same $X$. Because $X=HW_E$, the gradient for the table is $H^\top$ times the gradient for $X$. Each row collects the **sum** of the gradients at the positions where its token appeared. Rows of tokens absent from the batch get exactly zero gradient. This assumes the table is used only to embed the inputs and is not reused elsewhere in the model.
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

A token's embedding is row $x_t$ of $W_E$, which is the same as its one-hot vector times $W_E$. The embedding is just a bias-free linear layer. Its $V\cdot d$ numbers start random and are learned by training like every other weight. Through this lookup, a row gets a nonzero gradient only when its token appears in the batch.

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.