Let’s build a tiny language model together in Python—then remove its transformer layers and change its temperature to see what happens.
EPISODE 10 — LET’S BUILD IT
Go from random characters to word-like text with a small character-level Transformer in PyTorch. Build the data pipeline, tokenizer, model, generation loop and training loop. No paid API, pretrained model or separate dataset download is needed.
FULL CODE + BOTH EXPERIMENTS
https://github.com/waleedmousa/lets-build-it/tree/main/tiny-llm
GET STARTED
1. Install Python and Git, then open a terminal.
2. Clone the repository:
git clone https://github.com/waleedmousa/lets-build-it.git
cd lets-build-it/tiny-llm
3. Install PyTorch in your Python environment:
python -m pip install torch
4. Train and generate text:
python tiny_llm.py
5. Compare the experiments:
python experiments.py tiny_llm.py
python experiments.py step5_simple_model.py
The supplied reference run uses Python 3.13 and PyTorch 2.15.0. Setup and training time depend on your machine. Pause this short and build at your own pace.
WHAT YOU’LL LEARN
• How character tokenization turns text into numbers
• How a causal Transformer predicts the next character
• How cross-entropy loss and gradient updates train the model
• Why context matters—and what happens without it
• How temperature changes sampling without retraining
UNDER THE HOOD
29 character tokens · 48-character context · 3 Transformer layers · 345,725 trainable parameters. The code uses PyTorch’s TransformerEncoder with a causal mask to prevent looking ahead. “From scratch” means training random weights, while using PyTorch’s existing components.
READ THE RESULTS CORRECTLY
“Real words” is a custom vocabulary-overlap score: the share of generated alphabetic characters belonging to words of at least two letters found in the training text. It is NOT factual accuracy, comprehension, or a held-out language-model benchmark.
The video’s 1%, 89%, 9%, 99% and 10% are reported results from the recorded run. Samples and percentages vary with the Python manual, software version and execution environment. The rising montage uses selected checkpoints; real training can fluctuate. About 68 seconds was reported on the production CPU, not a guarantee for laptops.
This is a tiny educational language model, not a production assistant. It learns text patterns; it does not reliably answer questions or understand Python. The five blocks are a teaching framework, not a complete description of frontier-model systems. Email assistants, post planners and code reviewers need a capable model, suitable data, evaluation and additional application logic.
CONTINUE BUILDING
Previous episode — Vector Databases:
https://youtube.com/shorts/-yrsZVMTMKU
Explore the Let’s Build It playlist on our channel. For the underlying concepts, follow ML in 3 Minutes: tokens, attention, next-token prediction and training.
What should we build next? Complete this: “A language model for ___.” Tell us the task you want it to help with.
Created by Waleed Mousa · Runtime Explained
Technical concepts made visual. Built together, step by step.
#LLM #Python #PyTorch #MachineLearning #LetsBuildIt