language-models

Understanding and Building Language Models 📘

How LLMs Work, Step by Step and from Scratch in Python

Book cover

by Hernán Díaz Rodríguez, PhD — Professor at the University of Oviedo · Former CERN researcher

Stars License: CC BY-NC 4.0 LinkedIn


📘 About this book

Understanding and Building Language Models explains how a language model works inside, like the ones behind today’s conversational assistants, by building a small one from the ground up. It starts by fitting a straight line to the prices of a few houses and ends by training, fine-tuning, evaluating and documenting a small GPT: gradient descent, bigrams, neural networks, tokenization with BPE, attention and the Transformer, training with method, text generation, fine-tuning to follow instructions, evaluation and a capstone project.

All the models are deliberately small: they train on the processor of an ordinary computer in a few minutes, with no graphics card and no model downloads.

This folder contains the 11 interactive notebooks of the English edition, one per chapter, ready to run in Google Colab.

📗 Part of the same series as Deep Learning with Python.

🇪🇸 ¿Prefieres la edición en español? Sus cuadernos están en la raíz del repositorio.


📂 Folder structure

language-models/en/
├── 1_How_a_model_learns.ipynb           ← One notebook per chapter
├── 2_Language_modeling.ipynb
├── ...
├── 11_Capstone_project.ipynb
├── notebook/N/                          ← Links from the book to each notebook in Colab
└── video/N/                             ← Links from the book to each chapter's video

The data the notebooks use are in the repository’s material/ folder.


🎯 What you will learn

The book and the notebooks are organized in 3 parts:

Part Topic Chapters
1. Foundations Gradient descent, language models from counts, from a table to a network, networks with embeddings 1–4
2. Building a GPT Tokenization with BPE, attention and the Transformer, training with method, generation and inference 5–8
3. Fine-Tuning, Evaluating and Applying From base model to assistant, evaluation and limits, capstone project 9–11

🚀 How to use the notebooks


📚 Notebooks and videos

Ch. Title Colab Video
1 How a Model Learns Colab YouTube
2 Language Modeling Colab YouTube
3 From Counting to a Network Colab YouTube
4 Networks to Predict Text Colab YouTube
5 Tokenization Colab YouTube
6 Attention and the Transformer Colab YouTube
7 Training and Data Colab YouTube
8 Generation and Inference Colab YouTube
9 From Base Model to Assistant Colab YouTube
10 Evaluation and Limits Colab YouTube
11 Capstone Project Colab YouTube

🗂️ Data

File Contents Notebooks
material/quixote.txt Full text of Don Quixote, in John Ormsby’s translation, in the public domain (Project Gutenberg) 6 to 10
material/gpt_quixote.pt The character-level GPT trained in chapter 7 8 to 10
material/corpus_chapters_1_4.txt Text of chapters 1 to 4 of the book, to train the tokenizer 5 and 10

👨‍🏫 About the author

Hernán Díaz Rodríguez is a Professor in the Department of Computer Science and Artificial Intelligence at the University of Oviedo, Spain, with a PhD in Computer Science from the same university. A graduate in Computer Engineering from the same institution, the author also holds an MBA from the Open University (United Kingdom).

With more than 20 years of experience across the public and private sectors, Hernán Díaz has worked on advanced technology and research projects, including 5 years at CERN, the European Organization for Nuclear Research — one of the world’s largest scientific laboratories.

💼 LinkedIn · 📧 Email · 📕 Amazon author page


⭐ Found this useful?

If these notebooks are helping you understand language models, you can support the project:

Thank you! 🙏


📜 License

This material is distributed under Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0).

✅ Allowed: teaching, studying, personal notes; adaptations for educational purposes, citing the author. ❌ Not allowed: commercial or for-profit use; redistribution in commercial products without permission.


📖 How to cite

If you use this material in academic work, please cite:

@book{diazrodriguez2026languagemodels,
  author    = {Hern{\'a}n D{\'\i}az Rodr{\'\i}guez},
  title     = {Understanding and Building Language Models: How LLMs Work, Step by Step and from Scratch in Python},
  year      = {2026},
  publisher = {Independently published},
  url       = {https://github.com/HernanDiaz/language-models}
}

✉️ Contact

Found an error or have a suggestion? I’d love to hear from you: 📧 hernan.diaz.rodriguez@gmail.com

For institutional adoption of the book in courses or programs, please mention your institution and course in the email — I’m happy to support educators.