Teach Your Computer to Write
Build and Train an LLM from Zero
Download as PDF or EPUB, or read the whole book on one page. The code, exercises, and 17 Colab notebooks are on GitHub, and instructors can use the class-by-class course plan, 17 lecture decks, and short course-adoption form.
Book text CC BY-NC-ND 4.0 · code MIT licensed · formerly titled Building an LLM from Zero
Planned, written, edited, illustrated, and tested locally by the author, with AI assistance; the author takes full responsibility. See the Preface.
Why This Book?
There are already a lot of good resources for learning about AI and large language models. This one tries to find a balance: enough theory to understand what you are building, paired with real, runnable code you can execute on the machine in front of you.
The entire book was written and tested on a five-year-old ThinkPad T14 Gen 1 with no GPU. Training completes in about 20-30 minutes. The subject here is the machine itself: what the numbers are doing, why the thing invents facts, and what it can and cannot learn. Prompting strategy, deployment and governance are real subjects, and separate ones; this book stays inside the model.
About the Book
A step-by-step tutorial for building a GPT-style language model entirely from scratch using Python and PyTorch, written for students, professionals, and non-specialists across disciplines. By the end, you will have trained a model that generates Shakespeare-like text.
Every chapter has the same parts in the same order: the idea in plain language with a picture and an everyday comparison, a few numbered lines of code, what the computer printed when the code ran, one small change to try, what the idea means at work, and a 15-minute assignment.
Who Is This For?
- Students and non-specialists across disciplines who use AI tools every week and want to understand what is inside them
- Instructors and professors looking for a complete course package (17 lecture decks, 15-minute assignments, and browser notebooks)
- Managers, analysts, and IT/IS professionals who evaluate, supervise, or build with AI tools
- Beginners who learn by building and parents who want to show their kids how AI actually works
Quick Facts
| Chapters | 17 (3 modules) |
| Figures | 70 diagrams & plots |
| Course kit | 17 decks + 17 notebooks + 17 assignments |
| Model size | 824,832 parameters |
| Dataset | Tiny Shakespeare |
| Training time | ~20-30 min (CPU) |
| Framework | PyTorch (CPU or Colab) |
| License | MIT / CC BY-NC-ND 4.0 |
Table of Contents
Front Matter
| Preface: Why This Book Exists | |
| Introduction: Five Phases, One Loop |
| Ch 01 | Environment Setup |
| Ch 02 | What Is a Language Model? |
| Ch 03 | Tensors and PyTorch |
| Ch 04 | Tokenization |
| Ch 05 | Embeddings |
| Ch 06 | Self-Attention |
| Ch 07 | Multi-Head Attention |
| Ch 08 | Feed-Forward and Layer Norm |
| Ch 09 | The Transformer Block |
| Ch 10 | The Full GPT Architecture |
| Ch 11 | Causal Language Modeling |
Module 3: Training and Generation
| Ch 12 | Dataset and DataLoader |
| Ch 13 | The Training Loop |
| Ch 14 | Checkpointing |
| Ch 15 | Greedy and Sampling |
| Ch 16 | Temperature and Top-k |
| Ch 17 | Putting It All Together |
Appendices & Course Resources
| A | Glossary |
| B | Ten Key Papers |
| Teach With This Book · Lecture Slides |
ROMEO:
Praviour soul to shall that that are the and not,
Generated by the 824,832-parameter model trained in Chapter 13. It has learned character names, line breaks, and word shapes, without yet learning meaning.