How a transformer picks the next word
This is a real model with only 6,400 parameters, trained on 41 short sentences about AI. Everything below is computed live in your browser.
Tokens
The text is split into tokens. Here each word is one token with an ID from a vocabulary of 60 words.
Embeddings + position
Each token becomes 16 numbers (an embedding) describing its meaning. A position vector is added so the model knows word order.
Query · Key · Value
Every token makes a Query ("what am I looking for?"), a Key ("what do I contain?") and a Value ("what I'll share"). Columns 1–8 belong to head 1, columns 9–16 to head 2.
Attention
Each word compares its Query with the Keys of earlier words to decide how much to look at each one. Rows are the word looking and columns are the word looked at. No peeking ahead. Pick a head, or click a row.
MLP (feed-forward)
Each token then goes through a small neural network on its own (16 → 32 → 16). The bright cells are "neurons" that fired. Steps 3–5 repeat for each of the 2 layers.
Next-word probabilities
The last token's vector becomes a score for every word, and softmax turns the scores into probabilities. Temperature divides the scores first: lower sharpens the odds, higher flattens them.