Project

Train a tiny GPT, and watch it learn to write

You build a GPT of one and a half million weights from the course's own parts, trained on your own computer until it writes like a play, then trained again on a text you choose.

About 4 hours. You need your own computer, with uv; Windows, Linux or a Mac with Apple silicon, since PyTorch no longer ships for Intel Macs; about 700 MB of disk for PyTorch; a few minutes of your computer's time for each training run, and no graphics card.

You build it in the starter, one step at a time. The finished project is there to read when you are stuck.

Terminal
git clone https://github.com/FaydenAI/learn-nlp-transformers.git
cd learn-nlp-transformers/project/starter
uv sync

Get the starterRead the finished project

  1. Part 1

    The text, as tokens

    A model never reads text. It reads numbers, one for each token. Real models use pieces of words, learned by merging. In a model this small, a list of thousands of pieces would hold most of its weights, and a short text would show it each piece too seldom. So here each token is one character: 65 of them, for all of Shakespeare. The price is the one the tokenization lesson named: more tokens for the same text. Then you cut the text into stretches, each with its answers.

    UsesTokenizationLanguage modeling

    1. Run the testsRun it
    2. A number for each characterYour turn
    3. The textRun it
    4. A question at every placeYour turn

    When you have run it

    How many different characters does Tiny Shakespeare use? That is how many tokens your model's list holds.

  2. Part 2

    The model

    Now the GPT itself, from the parts the course took apart: the table, attention with its mask, RoPE, the block, and the last step, which turns the stream into a score for every token. Each is a few lines. The model's size is in `config.py`: 4 blocks, 4 heads, and 192 numbers in each token's vector.

    UsesWord embeddingsAttentionQueries, keys, values and headsPositionThe transformer blockEncoders and decoders

    1. Attention, with its maskYour turn
    2. A turn for each placeYour turn
    3. The blockYour turn
    4. From the table to a score for every tokenYour turn
    5. Count the weightsRun it

    When you have run it

    How many weights does your whole model have?

  3. Part 3

    Train it, and watch it write

    A model that has never trained gives every token about the same chance. You make it write before training, then train it on the first nine tenths of the text, and check it on the last tenth, which it never trains on. Then it writes again. Training runs on your computer's processor, in a few minutes.

    UsesLanguage modelingTokenization

    1. One token at a timeYour turn
    2. It writes before it learnsRun it
    3. Perplexity, on text it never sawYour turn
    4. One step of trainingYour turn
    5. Train itRun it
    6. It writes afterRun it

    When you have run it

    What perplexity does your model reach on the held-out tenth, after training?

  4. Part 4

    Your own text

    This part is yours. Train the same model on a text you choose, and see what it learns to write. There is no starter code: `mine.py` only says what goes in it.

    UsesTokenizationLanguage modelingPosition

    1. Your textOn your own
    2. Train on itOn your own
    3. Change one thingRun it

    When you have run it

    Before training, your model's held-out perplexity on your text is close to which number?

Run on Python 3.13, PyTorch 2.15.0, NumPy 2.5.5, pytest 9.2.0, uv 0.11.15. Checked on .