Projectabout 4 hours
Project: build a tiny GPT
After this project you can train a small transformer on a small text and sample from it.
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.
You build it in the starter, one step at a time. The finished project is there to read when you are stuck.
git clone https://github.com/FaydenAI/learn-nlp-transformers.git
cd learn-nlp-transformers/project/starter
uv syncGet the starterRead the finished project
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
- Run the testsRun it
- A number for each characterYour turn
- The textRun it
- 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.
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
- Attention, with its maskYour turn
- A turn for each placeYour turn
- The blockYour turn
- From the table to a score for every tokenYour turn
- Count the weightsRun it
When you have run it
How many weights does your whole model have?
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
- One token at a timeYour turn
- It writes before it learnsRun it
- Perplexity, on text it never sawYour turn
- One step of trainingYour turn
- Train itRun it
- It writes afterRun it
When you have run it
What perplexity does your model reach on the held-out tenth, after training?
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
- Your textOn your own
- Train on itOn your own
- 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 .
