techtree / Transformer
Next-token prediction and sampling
Turn the last vector into a score per token, softmax, and pick one.
Transcript
After the last block, take the vector at the last position. Multiply it by one more matrix, with one row for every token in the vocabulary. Often this is the embedding table itself, reused.
Out comes one score for every token. Softmax turns the scores into probabilities: how likely each token is to come next.
Now pick one. Always taking the top one is called greedy. Or draw at random, following the probabilities. That is sampling.
A setting called temperature divides the scores first. Low temperature makes the top token more likely. High temperature spreads the chances out.
The chosen token is added to the end of the text, and the whole model runs again, for the token after that.
One token at a time, that is how the text gets written.
Needs
- Stacking blocks video
The same block design, repeated many times, each with its own weights. - Exp and softmax video
Turn any list of scores into positive weights that add up to one. - Matrix times vector video
Each output entry is a dot product of one row with the vector.
Everything this builds on (22 topics, in order)
- Vectors
- Tokens
- Adding and scaling vectors
- Dot product
- Matrices
- Exp and softmax
- Nonlinearity (ReLU)
- Mean, variance, and normalization
- Length, angle, and cosine similarity
- Matrix times vector
- Embeddings
- A neural network layer
- Position information
- Queries, keys, and values
- MLP
- Residual connections
- Attention scores
- Self-attention
- Causal masking
- Multi-head attention
- The transformer block
- Stacking blocks
Leads to
- What is a transformer? video
The whole machine, end to end.