All projects

TinyGPTResearch

A small language model I built and trained from scratch, to watch it go from random noise to writing simple children's stories.

ML & ResearchLanguage Models2026
PythonPyTorchNumPy
TinyGPT screenshot
13.9M
parameters, written from scratch
45 min
of training on an Apple M4
6.1
final perplexity, down from thousands

TinyGPT is a small language model, the same kind of system as ChatGPT but about 12,000 times smaller than GPT-3, that I built and trained from scratch for my deep learning course. The goal was to watch what training actually does: how a model goes from producing random noise to writing a simple story, and what it still gets wrong at the end.

The setup

The model learns from TinyStories, a collection of short children's stories in simple English, made by researchers at Microsoft to test how small a model can be and still write coherently. I used 100,000 of them.

I wrote the model myself in PyTorch: six layers of attention, the mechanism that lets each word look back at the words before it, and about 14 million parameters in all. Before the real run, I checked that it could memorize one small batch perfectly, a quick test that learning works at all. Then it trained for about 45 minutes on an Apple M4 chip, reading 25 million tokens (pieces of words).

Watching it learn

I gave it the same opening line at three points in training and let it carry on. This progression is the whole point of the project.

Prompt

Once upon a time, there was a little girl named Lily.

  1. Before training

    0 tokens

    vehicles piece sitting nect freed te triangles unus milesœIfier lampulpadingivity curiosity grapes yoga bugs neighboureseGra day cro Slowly Maybe brighterfterKatie4olin gif squeezing…

  2. Halfway

    12.5M tokens

    She had long hair and many friends she had a big dress that she loved to play with. […] One day, Lily's mom said it was time for her to go to the park. Lily didn't want to leave the park yet, but she knew it was time to leave. […] When the sun came home, Lily got tired, but she knew she had to go home.

  3. Finished

    25M tokens

    She had many toys, but her favorite teddy bear was Lily. Lily loved to play with her doll and teddy bear. One day, Lily went to the park with her mom. […] "Wow, that's a good ride! Do you want to try it?" Timmy asked. Lily smiled and said, "Yes, I do. Let's play!"

Before training, it strings random pieces of words together. By halfway it writes real sentences and knows how a story opens, but it loses the thread: Lily is told to go to the park while she's already there, and then "the sun came home." By the end the grammar is clean and the characters talk to each other, but it still slips. Lily's teddy bear is also called Lily.

What the numbers say

The chart at the top shows the model's error falling as it reads more. It finishes with a perplexity of about 6, which roughly means that at each step it's choosing between six equally likely next words, down from thousands at the start.

A lower number didn't fix everything, though. Across the three openings I tested, characters change names partway through, and a red ball becomes a green ball and then a yellow one. Small models learn grammar long before they learn to keep track of who's who.

Under the hood

Everything is built from basic PyTorch operations, including the attention layers, with a tokenizer trained on the same stories. One command runs the whole pipeline, from preparing the data to training, generating the samples and writing the final report.