Skip to content
cd /learn/tiny-language-model

Step 3: How training sets the numbers

Inside a tiny language model · Step 3 of 4

How 5,000 rounds of guess, score, and nudge turned 10.65 million random numbers into a Shakespeare imitator, with real data from my training run.

The big picture

Steps 1 and 2 showed the model's parts: rows of numbers for each character, and attention heads that look back. Every one of those numbers started out random. Training is the loop that set them. It has one goal: make the next-character guess a little less wrong, over and over, 5,000 times.

My model is built on Andrej Karpathy's nanoGPT, open source under the MIT license, and trained on his Tiny Shakespeare text from char-rnn. Every number and chart on this page comes from my own training run of that code, unless it's labelled as an illustration.

Words to know

Loss

One number for how wrong the guesses were. Lower is better. On this page: how surprised the model was by the right answers.

Training step

One pass through the loop: guess, score, nudge. My run did 5,000.

Batch

The chunks of text used in one step. Mine: 64 chunks of 256 characters each.

Nudge direction

For each number, whether raising or lowering it slightly would lower the loss, and how much it matters. Its textbook name is the gradient.

Learning rate

How big each nudge is allowed to be. Mine peaked at 0.001.

Held-back text

The last 10% of the Shakespeare file, never used for nudging, only for checking. Its textbook name is validation data.

Overfitting

Getting better on the training text while getting worse on text it never saw. In plain words: memorizing.

Loss: how surprised the model was

Every guess is a share for each of the 65 characters. Loss only looks at one of them: the share the model gave to the character that actually came next. Give the right answer nearly 100% and the surprise is close to 0. Give it 1% and the surprise is large. The loss for a step is the average surprise over all its guesses.

The real points on the chart come from one line of the training text, "…hear me spea". Before training, the model gave the right answer, k, only 0.9% and scored a surprise of 4.70. By step 500 it gave k 98.2%, a surprise of 0.02. A model that guessed evenly, 1 in 65 for every character, would score 4.17 every time. That's the "know nothing" baseline. My untrained model actually started a bit worse, at 4.28 on held-back text, because its random numbers made it slightly lopsided rather than perfectly even.

A loss number like 1.67 is hard to picture, so the chart turns each held-back loss into a count of choices: the model is as unsure as if it were picking among that many equally likely characters. The untrained model was as lost as picking from 72. By step 2,250 it was down to about 4. Then it got worse again, back to 5.3 by the end. That reversal is the memorizing problem, covered below.

One training step

Swipe sideways to see the rest of this figure.

Before training, the file is cut in two. The model only ever learns from the first 90%. In each step it grabs 64 chunks of 256 characters from random places in that part. Inside a chunk, every character is a question: guess the next one. Because of the look-back-only rule from Step 2, all 256 guesses in a chunk happen at once without peeking, so one step makes 64 × 256 = 16,384 guesses.

Chunks per step64
Characters per chunk256
Guesses per step16,384
Steps5,000
Guesses in total81,920,000
Times through the training textabout 82, on average

The model then works out the loss for all 16,384 guesses and makes one round of nudges. It never learns from a single guess on its own. Averaging over a batch smooths out flukes, so one odd chunk can't drag the numbers somewhere strange. Over the whole run it made 81,920,000 guesses, so on average every character of the training text came up about 82 times.

The nudge, and how big to make it

After scoring, the model works out, for every one of its 10,745,088 numbers (the 10.65 million from Step 2 plus the 98,304 position numbers), whether raising or lowering it a hair would lower the loss, and by how much. It does this with calculus, automatically, in one backward pass through the model. Then every number moves a little in its helpful direction. In the step I tested, all 10,745,088 got a nonzero nudge: nothing sits out.

I ran a side experiment to show why the nudge has to be small. I took the model saved at step 500, computed one nudge direction on 32 training chunks, and moved all the numbers by different total amounts along it. The chart shows the loss on those same chunks afterward. A small move (0.1) lowered the loss from 1.565 to 1.531. A bigger move (0.3) made it worse, and a move of 1 nearly doubled it to 2.996. Moves of 3, 10, and 30 sent the loss to 8.98, 31.7, and 64.0, far worse than random guessing. The nudge direction is only accurate close to where you are; go too far and you overshoot. This experiment used a plain step, not the run's own method, and it isn't part of the training run.

The chart shows my run's real learning rate at each step. It wasn't fixed. For the first 100 steps it ramped up from almost 0 to its peak of 0.001 (warm-up). Then it eased down along a smooth curve to 0.0001 by step 5,000. Big steps early, while the numbers are far from good, and small careful steps at the end. The run's method (an optimizer called AdamW) also scales each number's nudge on its own, and it caps any single step from being too large.

The real loss curve

Every 250 steps, the run paused and measured the loss on 200 random batches from each part of the file. Both lines start at the "know nothing" level (4.29 and 4.28). The biggest drop happens in the first 500 steps. After that, learning slows.

The two lines tell different stories. The training-text line keeps falling all the way to 0.6468. The held-back line bottoms out at 1.4833 at step 2,250, then climbs back to 1.6747 by step 5,000. After step 2,250, more training made the model better at text it had seen and worse at text it hadn't.

How the writing improved

Eight panels of text the model wrote after "To be, or not to be:" at steps 0, 50, 100, 250, 500, 1,000, 2,500 and 5,000.

Each panel shows the first lines of a real 200-character sample and the held-back loss at that step (4.28, 2.50, 2.46, 2.06, 1.72, 1.53, 1.50, 1.67). Step 0 is random symbols. Step 50 has spaces, line breaks and capitalized names with colons. Step 250 has short words and "GLOUCESS". Step 1,000 has "GLOUCESTER:" and lines like "My lord, heart that I will have as delay'd." Steps 2,500 and 5,000 bring in ROMEO, BENVOLIO and MENENIUS.

Swipe sideways to read this sample.

At each saved step, the run had the model write 200 characters after "To be, or not to be:", with the same random seed each time so the only thing changing is the model. At step 0 it's random symbols. By step 50 it has spaces, line breaks, and names in capitals followed by a colon. By step 250 there are real short words and a name that's almost right (GLOUCESS). By step 1,000 it writes proper character names and lines that read like Shakespeare, even if they make no sense. Steps 2,500 and 5,000 are smoother still, with ROMEO, BENVOLIO, and MENENIUS appearing.

I checked whether it was just copying. The longest word-for-word stretch from the training text in any sample is 24 characters (at step 1,000: a line break, "GLOUCESTER:", then "My lord, "). At step 5,000 it's 20 characters, mostly the name "MENENIUS:". These samples aren't copied passages, even though the model was memorizing by then. They're only 200 characters each, though, so this is a spot check, not proof.

Where Steps 1 and 2 came from

The checkpoints are the saved steps (0, 50, 100, 250, 500, 1,000, 2,500, 5,000), so the spacing isn't even. Nobody built the previous-character head from Step 2. The same nudges that lowered the loss turned Layer 1, Head 3 into one: 9.8% of its attention went to the previous character at step 100, 47.7% at step 250, 81.2% at step 500, and 94.4% by step 1,000. In the same window, steps 250 to 500, the share the model gave to k after "spea" jumped from 2.9% to 98.2%. The two happened at the same time. That fits the idea that knowing the last few letters is what spelling needs, but this data alone doesn't prove one caused the other.

Step 1's grouping came from the same loop. The average similarity between each letter and its own capital went from −0.006 at step 0 to 0.161 at step 250 and 0.330 by step 1,000, then levelled off. Most of the structure on Steps 1 and 2 was in place by step 1,000, a fifth of the way through the run.

Memorizing: the train vs held-back gap

The chart shows how much worse the model does on held-back text than on training text. At step 0 there's no gap: the model knows nothing about either. By step 2,250 the gap was 0.46. By step 5,000 it was 1.03 (1.6747 against 0.6468). Between step 2,250 and the end, the training loss fell by 37% while the held-back loss rose by 13%.

That's overfitting. The model saw the training text about 82 times over, and with 10.7 million numbers it had room to start storing particular passages instead of general patterns. The run already had one guard against this: during training, 20% of the signals inside the model were switched off at random each step (dropout), so it couldn't rely on any single path. It still overfit. The model I studied in Steps 1 and 2 is the one from step 5,000, past its best point. The step-2,250 model wasn't saved; the nearest saved one is step 2,500, with a held-back loss of 1.50.

Where this method came from

Timeline from 1847 to 2026: gradient descent, backpropagation, char-rnn, the Transformer, GPT, nanoGPT, my run.

Seven stacked entries. 1847, Cauchy: nudging downhill. 1986, Rumelhart, Hinton and Williams: backpropagation. 2015, Karpathy: char-rnn and Tiny Shakespeare. 2017, Vaswani et al.: the Transformer. 2018, OpenAI: GPT. 2022, Karpathy: nanoGPT. 2026, my run: nanoGPT on my desktop machine, 455.2 seconds.

  1. 1847

    Nudging downhill

    Cauchy: step each number a little in the direction that lowers the error

  2. 1986

    Backpropagation

    Rumelhart, Hinton, Williams: work out every nudge in one backward pass

  3. 2015

    char-rnn and Tiny Shakespeare

    Karpathy: a character-level model, and the text file I used

  4. 2017

    The Transformer

    Vaswani et al., Google: attention instead of reading one step at a time

  5. 2018

    GPT

    OpenAI: a Transformer trained to predict the next token

  6. 2022

    nanoGPT

    Karpathy: a small, readable teaching version

  7. 2026

    My run

    nanoGPT on my desktop machine, 455.2 seconds

Nobody invented this all at once. The idea at the bottom of the loop, nudge each number a little in the direction that lowers the error, is the oldest piece. The mathematician Augustin-Louis Cauchy described it in 1847 as a general way to solve equations. Today it's called gradient descent.

Nudging only works if you can figure out the direction for every number, and in a model with millions of numbers stacked in layers that's the hard part. In 1986, David Rumelhart, Geoffrey Hinton, and Ronald Williams published backpropagation in Nature: start from the error at the output and pass it backward through the layers, so one backward sweep gives every number its nudge. Earlier versions of the math existed: Seppo Linnainmaa described the backward method in his 1970 master's thesis, and Paul Werbos applied it to neural networks in his 1974 PhD thesis. The 1986 paper is what made it the standard way to train these models. It's the "one backward pass" from the nudge section above.

The model design is much newer. In 2017, a team of eight researchers, mostly at Google (Ashish Vaswani and colleagues), published "Attention Is All You Need", introducing the Transformer: a model built around the attention from Step 2, which handles all positions at once instead of reading text one step at a time. In 2018, OpenAI (Alec Radford and colleagues) trained a Transformer to predict the next token over a large pile of books and called it GPT, short for Generative Pre-trained Transformer. My model is a tiny GPT. In December 2022, Andrej Karpathy released nanoGPT, a short, readable version of that recipe meant for learning; its Shakespeare setup is the one I ran. The text file itself comes from his 2015 char-rnn project.

YearWorkWhat it gave us
1847Cauchy, Méthode générale pour la résolution des systèmes d'équations simultanéesThe nudge: gradient descent
1970, 1974Linnainmaa (master's thesis); Werbos (PhD thesis)Early versions of the backward method
1986Rumelhart, Hinton, Williams, Learning representations by back-propagating errors (Nature 323)Working out every nudge at once
2015Karpathy, char-rnnThe Tiny Shakespeare file
2017Vaswani et al., Attention Is All You NeedThe Transformer design
2018Radford et al., Improving Language Understanding by Generative Pre-Training (OpenAI)GPT: train it to guess the next token
2022Karpathy, nanoGPTThe code I ran

Sources:

Cauchy's 1847 paper in English translation · the backpropagation paper · Linnainmaa and Werbos: Baydin et al., "Automatic Differentiation in Machine Learning: a Survey", JMLR 18, 2018, section 3.3 · the Transformer paper · the GPT paper · nanoGPT · char-rnn

What training can and can't do

Can

  • Set all 10.7 million numbers automatically, starting from random.
  • Grow useful habits nobody designed, like the previous-character head and the letter–capital link.
  • Show its own progress, through the loss on held-back text.

Can't

  • Learn anything that isn't in the training text. It has never seen modern English, code, or security writing.
  • Tell on its own when to stop. The held-back check has to be watched, and this run kept going past its best point.
  • Give the model understanding. It only gets better at predicting the next character.
  • Explain why a number ended up where it did.
Setting from my runValue
Chunks per step (batch size)64
Characters per chunk256
Peak learning rate0.001
Final learning rate0.0001
Warm-up steps100
Signals switched off at random (dropout)20%
Steps5,000
Training time455.2 seconds

Previous: Step 2, how the model uses context.