From character IDs to a learned context
Updated
A character ID tells us which character we have, but its numerical size has no linguistic meaning. An embedding lookup replaces that arbitrary ID with a trainable vector. A network can then combine several such vectors to predict what comes next.
This is a focused explanation of the lookup-table and shape reasoning in my makemore MLP notebook. An MLP is a multilayer perceptron.
A lookup, not an ordering
Let the vocabulary size be and the embedding width be . A matrix stores one vector per token. Looking up token returns row ; multiplying the integer by a weight would impose an unwanted numerical ordering on the tokens.
For a context of tokens, concatenate the selected rows into . With hidden width , a simple model is
where and . Softmax turns the logits in into a next-token distribution.
Follow an illustrative batch
For an illustrative batch size , context , embedding width and hidden width , the shapes are:
- Token IDs: .
- Looked-up embeddings: .
- Concatenated context: .
- Hidden activations: .
- Output logits: .
Concatenation preserves the order of the context positions. It is not averaging the three embeddings. The same lookup matrix is shared across positions, so repeated token IDs accumulate gradient contributions into the same row.
What learning an embedding does—and does not—mean
Backpropagation updates the selected rows along with the other parameters. Their geometry becomes useful for the prediction objective. Nearby vectors are not guaranteed to be synonyms, particularly in a character model. The original word-level motivation should not be transferred uncritically to characters.
The model still sees only preceding tokens. A low training loss alone cannot show that its representation generalizes. Choose hyperparameters using validation data and evaluate the final model on held-out examples.
See Bengio and colleagues' A Neural Probabilistic Language Model for the original word-level model, and Karpathy's makemore for the character-level learning exercise.