With pictures and motion, we see which words in a sentence a small model trained for this book (not GPT) gathers information from, and how it chooses the next word to write a sentence.
In tab ① you follow Q, K, V → scores → mask → softmax weights → output numerically with small hand-set matrices, and check what dividing by √dk means and how the causal mask works. In tab ② you run a small Transformer trained for this book inside the browser and examine the next-token probabilities, how the distribution changes with temperature, top-k, and top-p, and whether the generated sentences are correct. Notice also that a high-probability sentence is not always a correct one. The small model was trained on Japanese text, so its tokens appear in Japanese; English glosses are given alongside.
When a small trained model reads a sentence, the position of each word gathers information from the words before it. Choose a word, and the thickness of the lines shows which words it gathers from and in what proportions (the attention weights).
These are actual computed values from a small model trained for this book only on synthetic data for teaching purposes (short sentences made from a table of material properties). It is not GPT.
The model shows the probabilities of the words likely to come next as bars, and writes a sentence by choosing one word at a time with a lottery that follows those probabilities.
This is a small model trained only on synthetic data for teaching purposes (it is not GPT). The written sentences are judged by checking them against the original table from which the training sentences were made.
softmax weights A (each row = one query; each row sums to 1 over the keys)
A = softmax(QKT/√dk + M). The output is Z = AV.
The weights are hand-set values. This is not a trained model. You can change the matrix values in the fields below (−5 to 5).
A small model trained for this book (28,384 parameters; not GPT). It was trained on a synthetic teaching corpus of Japanese sentences, so the tokens are shown in Japanese. All computation runs inside this browser.
Context and next-token distribution
Caution: The weights are the proportions in which information is gathered; they are not necessarily the reason for the model’s decision (Chapter 24, Section 24.10).
| Type | Decoder-only Transformer (pre-LN) |
|---|---|
| Vocabulary Vvoc (word level) | 67 |
| dmodel / nhead / dk | 32 / 2 / 16 |
| nlayer / context length Tmax | 2 / 24 |
| FFN hidden dimension | 128 (GELU) |
| Positional information | Learned position embedding WP |
| Output | Tied weights WU = WET |
| Number of parameters | 28,384 |
| of which, per layer: attention / FFN | 4,224 / 8,352 |
Training data: A synthetic teaching corpus. Short Japanese sentences were generated mechanically from a table of 27 materials (classification, electrical, thermal, crystal structure, magnet, and state at room temperature; only first-year university textbook facts) and 8 sentence templates; no real text was used. The number of sentences differs by material (6 frequent materials: 1000 sentences each; 8 medium: 100 each; 13 rare: 8 each). Training: 6211 sentences (56476 tokens); validation: 693 sentences.
Training conditions: Cross-entropy for next-token prediction. AdamW, learning rate 3×10⁻³ (warmup, then cosine), minibatch of 32 rows × 24 tokens, 1500 steps (about 20 passes over the training data), seed 1. 14 s on CPU in float32.
| Validation loss (nat/token) / perplexity | 0.501 / 1.65 |
|---|---|
| Perplexity of the bigram model (for comparison) | 2.52 |
| Top-1 accuracy for “<material> is …” (as a topic): frequent / medium / rare | 33/33 / 44/44 / 62/67 |
| Error rate in generation at τ = 1 with the material specified: frequent / medium / rare | 0.8 % / 1.3 % / 9.0 % |
The error rates and similar figures are measured values for this small model and this synthetic corpus. They are not the error rates of a real GPT.
The description of this simulator (procedure, formulas, conditions, questions) is on the explanation page “Explanation of the Attention and Next-Token Generation Simulator”.
Back to Machine Learning — From Probability to GPT (Contents) | Text: Chapter 24, Section 24.3, Chapter 26, Section 26.6 | Equations and explanation (explanation page) | Equations are typeset with the browser’s built-in MathML (no external library).