Teach Your Computer to Write

Build and Train an LLM from Zero

by Truong (Jack) Luu, PhD.

Free & Open Source · No GPU Required · Python + PyTorch

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
Adopting this book for a course? Every chapter comes with a 75-minute lecture deck, a 15-minute assignment, and a browser notebook (Course Plan · Lecture Slides). Fill out this quick form so I can document classroom adoption:
Questions? hello@jackluu.io

Quick Facts

Chapters17 (3 modules)
Figures70 diagrams & plots
Course kit17 decks + 17 notebooks + 17 assignments
Model size824,832 parameters
DatasetTiny Shakespeare
Training time~20-30 min (CPU)
FrameworkPyTorch (CPU or Colab)
LicenseMIT / CC BY-NC-ND 4.0

Table of Contents

Module 3: Training and Generation

Ch 12Dataset and DataLoader
Ch 13The Training Loop
Ch 14Checkpointing
Ch 15Greedy and Sampling
Ch 16Temperature and Top-k
Ch 17Putting It All Together

Appendices & Course Resources

AGlossary
BTen Key Papers
 Teach With This Book · Lecture Slides
Sample output after 25 minutes on a laptop CPU:
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.