0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09A language model we built ourselves
This is a language model. Not a big one: just a hundred and ten thousand parameters, trained in three and a half minutes on a laptop. And it's ours: we built it from scratch, and trained it on Brightcart's own text. At the start, it types random characters. After two hundred and fifty steps, it's babbling something that looks a lot like a Brightcart message. And after a thousand steps, it writes a complete, sensible customer reply, and a sentence that never appeared anywhere in its training data. Today we build a tiny GPT, piece by piece, and every number you'll see comes from that real training run.
You know every part already. Now you'll see them work together.
0:56Where we are: build a tiny GPT
Season Three, Module Eleven, stop six: building a tiny GPT from scratch.
1:04The data: characters and windows
First, the data: two thousand six hundred lines of Brightcart text, support replies and product descriptions, about a hundred and ninety thousand characters. We keep ten percent aside as a validation set. To keep things simple, our tokeniser works on single characters rather than subwords. There are forty-six different characters, so our vocabulary has forty-six tokens. Each training example is a window of sixty-four characters taken from a random place in the text. The target is the same window shifted along by one character, so at every position the model learns to predict the next character.
Thirty-two windows make one batch.
1:49The model: a decoder-only transformer
Now the model, a decoder-only transformer, exactly the architecture from the end of Season Two, just small. Each character's ID looks up a sixty-four-number embedding, and we add a learned position embedding, so the model knows where each character sits in the window. Then come two transformer blocks. Each one normalises, applies multi-head self-attention with four heads and the causal mask, so no position can peek at the future, adds a residual connection, then normalises again and applies a feed-forward network that expands to two hundred and fifty-six numbers and back, with another residual connection.
Finally, a last normalisation and a linear layer turn each position's vector into forty-six scores, one for each possible next character. All in, a hundred and ten thousand, one hundred and twenty-six parameters. A large language model has tens of thousands of times more, but the blueprint is the same.
2:51The training loop
The training loop is the one from episode twenty-six. Grab a batch of windows. Run the model forward to get scores. Measure cross-entropy against the next characters. Backpropagate. Clip the gradient so a single bad batch can't explode it. And take a step with AdamW. The learning rate warms up over the first hundred steps, then follows a cosine curve down to zero by step two thousand, the schedule from episode twenty-seven. The whole program, data, model and training, fits in under a hundred lines. It's in the companion notebook.
3:30Baselines: what does a good loss mean?
Before training, let's set some baselines, so we know what the numbers mean. If the model guessed uniformly among forty-six characters, its loss would be the natural log of forty-six: three point eight three. Our untrained model starts at three point nine eight, very close. The sanity check passes. A much smarter baseline is a bigram model: just count how often each character follows each other character. It reaches a validation loss of two point zero three. It knows, say, that a space or a new line usually follows a full stop, but nothing more.
It can't remember a product mentioned twenty characters earlier. For our transformer to be worth anything, it needs to beat two point zero three by a long way.
4:23Watch it learn to write
Let's press play. At step zero, the output is random characters: "Hi", then gibberish. After a hundred steps, the loss has dropped to about two point zero four, roughly the bigram level. The model has learned which characters tend to follow which. By step two hundred and fifty, something remarkable has happened. The loss is down to about zero point five six, far below the bigram, and the text has structure. "Hi Chen, sorroury teanday." It's learned that messages start with a greeting and a customer's name, that sentences end with full stops, and the shapes of real words, even if it still garbles them.
By step one thousand, the loss is under zero point two, and the sample is a perfect Brightcart message: "Hi Chen, sorry your cushion arrived cracked. We will send a new one on Monday." From there, improvement slows: zero point one eight by step two thousand. And notice that the training and validation curves stay together, with no sign of overfitting.
5:29How it generates text
How does it actually write? Exactly as we learned in episode forty-one. Give it a prompt, like "Hi". Run it forward, take the scores for the next character, divide by a temperature of zero point eight, apply softmax, and sample a character. Add it to the text, and repeat, always looking back at the last sixty-four characters. Give it "Customer: where is my", and it continues: "mug? Agent: it left the warehouse on Friday." Give it "The", and it writes: "The bowl is soft and easy to clean. It ships in two days."
6:07Did it memorise, or create?
Now a fair question. Is it just copying lines it has seen? Let's search the training text. "Hi Chen, sorry your cushion arrived cracked. We will send a new one on Monday." Zero matches. "The bowl is soft and easy to clean. It ships in two days." Zero matches. These sentences never appeared in the training data. The model learned the patterns, a greeting, a name, a product, a problem, a day, and combined them in new ways. But "Customer: where is my mug? Agent: it left the warehouse on Friday." appears six times in the corpus.
When a pattern is common, the model can reproduce it exactly. Large models show both behaviours too: mostly recombining, sometimes reproducing text they saw many times. And to be honest about our result: this corpus is small and very repetitive, so reaching a low loss is far easier here than on real, varied language. That's exactly why real models need so much more data, and so many more parameters.
7:18From tiny to large
What separates our tiny GPT from the models behind today's chat assistants? Scale, at every level. Subword tokens instead of characters. Billions of parameters instead of a hundred thousand. Dozens of layers instead of two. Contexts of thousands of tokens or more instead of sixty-four characters. Trillions of tokens of training data instead of about a hundred and ninety thousand characters. Weeks on thousands of accelerators instead of three and a half minutes on a laptop. And then post-training to turn the base model into an assistant.
But the blueprint is the same: embeddings, causal self-attention, feed-forward layers, residuals and normalisation; a next-token cross-entropy loss; AdamW with warm-up and decay; gradient clipping; and temperature sampling. You've now built every one of those pieces yourself. One caveat: this corner of AI moves quickly. These examples were current when we made this video, and they may look different on the day you watch it.
8:28Pause & Predict: where are the 110,126 parameters?
Our model has a hundred and ten thousand, one hundred and twenty-six parameters. Can we account for every one of them, with the formulas from episode thirty-seven? Pause and estimate which part is largest. The embeddings are small: under three thousand for the characters and about four thousand for positions. Each layer's attention has sixteen thousand six hundred and forty. The feed-forward network is the biggest: thirty-three thousand and eighty-eight per layer. Add the layer norms and the output head, and the total is exactly a hundred and ten thousand, one hundred and twenty-six, matching the training script's own count.
No mystery parameters anywhere.
9:14Losses as perplexities
Let's translate the losses into perplexities, so they're easier to feel. Guessing at random among forty-six characters: a perplexity of forty-six. The bigram baseline, which only looks at the previous character: about seven point six. Our tiny transformer, after two thousand steps: about one point two. On this text, it's usually almost certain what the next character will be.
9:42Why our loss is so low
Now an honest caveat. That loss is remarkably low, and the reason is our data. The corpus was written for this course, so it's small, tidy and full of repeated templates, like "Hi so-and-so, sorry your item arrived damaged". Real-world text is far more varied, so a model trained on it would have a much higher loss. That's also a general lesson: never compare losses across different datasets or different tokenisers, because the numbers measure different things.
10:15Inputs and targets, by hand
Here's how the training pairs are made. Take a window of text, say "Hi Chen". The inputs are every character except the last, and the targets are the same text shifted one place to the left. So "H" must predict "i", "i" must predict the space, the space must predict "C", and so on. With our context of sixty-four characters, every random window gives sixty-four predictions at once, and the causal mask makes sure no position can peek at its own answer.
10:50Why characters, not subwords?
Why did we use characters instead of the subword tokens from episode thirty-five? Size. With a fifty-thousand-token vocabulary, the embedding table alone would have three point two million parameters, twenty-nine times our whole model. Characters keep the tables tiny, and any word can be spelled. The price is longer sequences, and the model has to learn spelling from scratch. For a tiny model on a small corpus, characters are the sensible choice.
11:22Experiments to try yourself
Now it's your turn. The whole training script is just fifty-five lines, and it runs on a laptop in a few minutes. Try lowering the temperature to zero point three and raising it to one point two, and compare the samples. Remove the position embeddings and see what breaks. Shrink the context to sixteen characters. Stop training after two hundred steps. And here's the most important habit: before every experiment, write down what you expect to happen. That's how intuition is built.
11:57Pause & Predict: is it overfitting?
At the end of training, the training loss is zero point one eight four and the validation loss is zero point one seven nine. Is the model overfitting? Pause. No. The two curves stayed together the whole way, unlike episode twenty-six, where the validation loss turned upward. Validation can even come out slightly lower, because both figures are estimates from a few random batches, and the validation text may simply be a little easier. A tiny gap in either direction is just noise. If we trained a much bigger model, or for far longer, on this small corpus, we'd expect the curves to part, and that would be our signal to stop.
12:46Myth vs Fact
Myth: building a language model takes secret, incomprehensible technology. Fact: the core ideas are published and, as you've just seen, fit in under a hundred lines. What's genuinely hard at the frontier is the scale, the quality of the data, the engineering of training across thousands of chips, and post-training, which is why only a handful of organisations train the largest models.
13:13Recap
Let's recap. We split Brightcart's text into characters and sixty-four-character windows, with targets shifted by one. We built a decoder-only transformer: embeddings, two causal transformer blocks and an output head, a hundred and ten thousand parameters. We trained it for two thousand steps with AdamW, a warm-up and cosine schedule, and gradient clipping. Its validation loss fell from almost four, past the bigram baseline at about two, to under zero point two. And its writing went from noise, to babble, to correct sentences it had never seen.
One sentence to remember: same blueprint, different scale. You've built the engine of a language model.
13:59Next: beyond the basic transformer
Mission complete. We built and trained a GPT from scratch, and it writes brand-new Brightcart messages. Next, the final stop in this module: how frontier models stretch the basic transformer. Mixture-of-experts models that activate only part of themselves for each token, tricks for handling very long inputs, and alternative architectures that might one day challenge attention itself. See you on the map.