Building Language Models from Scratch · Wanglong Lu · Public reading edition
The book contains ten chapters and 71 sections, with reader PDFs, illustrations, and 70 numbered exercises with supplied checks.
The book and illustrations use CC BY-NC-ND 4.0; teaching code uses PolyForm Noncommercial 1.0.0. See LICENSE.md and NOTICE.md for the scope of each license.
Start with the English reading snapshot, with the Chinese edition alongside it. Follow EXERCISES.md in numerical order. Section 03.04 is conceptual and has no separate exercise. Every assigned function is a fresh TODO, so this is not a complete solution package. Supplied drivers test the illustrated cases, not universal correctness. Later drivers can stop in an unfinished prerequisite.
The exercise baseline is Python 3.11, PyTorch 2.0.0 and NumPy 1.x. CPU execution is sufficient. From this package's root:
python3.11 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python exercises/01_01_text_to_tokens.py --check encodeThe first run raises NotImplementedError, the exercise's starting point. Read the contract and hints, implement the region marked START LEARNER CODE, and rerun the check. If a later exercise reports an unfinished prerequisite, complete that earlier assignment first. All supplied examples run on CPU.
The package also includes a complete CPU example and paired synthetic data. Read the English walkthrough, with Simplified Chinese alongside it, then run:
python tools/check_package.py
python examples/generate_teaching_data.py --check
python examples/train_bigram.py --checkThe package check prints status: PASS on success. The 76 assigned functions remain TODOs; the complete example supplies its own implementation. This example uses one character of context. See the data guide for its source, generator and fixed split.
The integrity check applies to the files as downloaded. After implementing an exercise, use that exercise's supplied check driver.
For corrections or questions, include the relevant chapter or exercise.
This GitHub source tree keeps large PDFs in the versioned release assets. The complete ZIP and both PDFs belong to the same companion release.