Build an LLM from scratch

Train a small language model on your own device and watch it learn to write short stories.

Seven short steps. You first see what a language model does, then peek at the maths and the design inside it, and finally build and train one yourself. No experience needed.

7 steps · about 90 minutes

  1. Step 1 See what a language model does

    A language model only ever guesses the next piece of text, and seeing that first makes the rest easier to follow.

    About 10 minutes

    Open step 1: See what a language model does
    Tick each one when it is true for you
  2. Step 2 Follow the maths inside

    Every guess comes from plain arithmetic on lists of numbers, and you can follow each calculation by hand.

    About 15 minutes

    Open step 2: Follow the maths inside
    Tick each one when it is true for you
  3. Step 3 Explore the design of a transformer

    A transformer is the kind of model inside most of today’s chatbots. Its design decides what the model can notice, so you will see which parts matter and why.

    About 20 minutes

    Open step 3: Explore the design of a transformer
    Tick each one when it is true for you
  4. Step 4 Get the training stories

    A model learns only from the text it reads, so you choose and load its reading material first.

    About 5 minutes

    Open step 4: Get the training stories
    Tick each one when it is true for you
  5. Step 5 Cut the text into tokens

    The model reads small pieces called tokens, not whole sentences, so the text must be cut up and numbered.

    About 5 minutes

    Open step 5: Cut the text into tokens
    Tick each one when it is true for you
  6. Step 6 Train your model

    Training is the model guessing, checking and improving many thousands of times until its guesses get good. The loss is a number for how wrong its guesses are: lower is better.

    About 30 minutes

    Open step 6: Train your model
    Tick each one when it is true for you
  7. Step 7 Write a story with it

    This is the payoff: your own model, trained on your device, writing text it has never seen.

    About 5 minutes

    Open step 7: Write a story with it
    Tick each one when it is true for you