Skip to content
cd /learn

Inside a tiny language model

I work in cybersecurity, and math is not my strong suit. I wanted to see what actually happens inside a language model, the kind of thing behind today's AI chat tools, without taking anyone's word for it. So I trained a tiny one on Shakespeare and took it apart.

This series is for people like me: technical, curious, and not interested in wading through equations. Every idea gets plain words, a picture, and a comparison to something from security work, plus a note on where that comparison stops working. Every number comes from my own model. Anything drawn as an example is labeled as one.

The model

  • What it learned from: one file, 1,115,394 characters of Shakespeare.
  • What it reads: single characters. It knows 65 of them: letters, punctuation, space, and line break.
  • How big it is: about 10.7 million adjustable numbers (parameters, the values training tunes). Today's large models have many billions.
  • How it learned: 5,000 rounds of guess, check, and adjust. The whole run took 455.2 seconds, about seven and a half minutes, on my desktop machine.
  • Its one job: guess the next character. Everything it writes is that guess, repeated.

The steps

  1. How text becomes numbers. How letters become ID numbers, then rows of 384 numbers, and what training did to those rows.
  2. How the model uses context. How each character looks back at earlier ones to decide what's likely next. This is called attention.
  3. How training sets the numbers. The guess, measure, nudge loop, and what "loss" means.
  4. How the model writes. One character at a time: the dice roll, temperature, and the window it can see.

Credits

  • The model is based on Andrej Karpathy's nanoGPT (MIT license).
  • The training text is the Tiny Shakespeare dataset from Karpathy's char-rnn.

I built this series with help from an AI assistant.