Mindket

Build a Large Language Model (From Scratch)

Newversion 2 · updated
Star108k

Overview

The official code for Sebastian Raschka’s book on building a GPT-like LLM in PyTorch, one chapter at a time. The notebooks cover text data, attention, pretraining and finetuning. The setup notes cover uv, pixi, Conda, Docker, Lightning and Colab.

Build a Large Language Model (From Scratch) is an open-source project on GitHub in the Education category, listed by Mindket. It is free. This is version 2, last updated on . Install it from GitHub as its README says. Get Mindket’s setup notes on your Mindket account, as a zip.

At a glance

Runs in
  • Your own computer: a uv, pixi, pip or Conda environment, with JupyterLab or VS Code
  • A Docker DevContainer in VS Code, with an option for an NVIDIA GPU
  • Lightning AI Studio: a persistent cloud environment with VS Code and JupyterLab on CPUs and GPUs
  • Google Colab: open a chapter notebook from GitHub and upload the files it imports
Start with
git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git
cd LLMs-from-scratch

Platform

VS CodeCode editor
DockerContainer

About

What it is

This is the official code repository for Sebastian Raschka’s book Build a Large Language Model (From Scratch), which Manning publishes. Its README says you build a GPT-like LLM in PyTorch step by step, from text data through pretraining to finetuning, without any external LLM libraries. The method for training this small model mirrors the approach used to build large models such as those behind ChatGPT. The repository holds the book’s notebooks, scripts and bonus material. The book itself is a separate product that the README links at Manning and Amazon.

  1. Set up Python and install the requirements
  2. Open a chapter’s notebook in JupyterLab
  3. Chapters 2 to 4: text data, attention, a GPT model
  4. Chapter 5: pretrain it, load GPT-2 weights
  5. Chapters 6, 7: finetune to classify and follow instructions

Who it suits

Programmers who know Python and want to see how an LLM works by coding one. The README calls a strong foundation in Python the most important prerequisite. Experience with deep neural networks helps, and PyTorch basics are useful but not required. Appendix A is a short introduction to PyTorch.

Who uses it, and how
IndustryWho uses itHow they use itExample
Self-taught developersProgrammers who know PythonRead a chapter, run its notebook and check the exercise solutionsA developer runs ch03.ipynb to code attention, then opens exercise-solutions.ipynb in the same folder.
Students and study groupsLearners in a machine learning courseGo chapter by chapter and test themselves with the free quiz PDF from ManningA study group finishes Chapter 4, then works through that chapter’s quiz questions in the PDF.
Machine learning engineersEngineers who call LLMs through APIsRead the bonus folders to see how newer architectures are built from scratchAn engineer opens the Qwen3 folder in chapter 5 to read a from-scratch Qwen3 model.
InstructorsTeachers and workshop leadsAssign a chapter notebook and its code-along videoAn instructor assigns Chapter 2 and plays the author’s video on working with text data.

Skip it if you want a ready model to call or a library to build products on. The README describes the model you train as small but functional, made for education.

Where to use it

The code is Jupyter notebooks and Python scripts that use PyTorch. Chapters 5 to 7 also use TensorFlow to read the GPT-2 weights. The README says the main chapters run on a conventional laptop with no special hardware, and use a GPU automatically when one is there. The setup page says an NVIDIA GPU in the cloud helps most in chapters 5 to 7.

  • Your own computer: a uv, pixi, pip or Conda environment, with JupyterLab or VS Code
  • A Docker DevContainer in VS Code, with an option for an NVIDIA GPU
  • Lightning AI Studio: a persistent cloud environment with VS Code and JupyterLab on CPUs and GPUs
  • Google Colab: open a chapter notebook from GitHub and upload the files it imports
  • Your browser, to read only: GitHub shows the files and renders the Markdown

How to set it up

Get it saves a zip of Mindket’s setup notes in your downloads folder, with each route’s steps from the repository’s setup pages. If Python is already installed, the setup page’s quickest route is one line from the repository’s root folder: pip install -r requirements.txt. The author’s own route adds a uv virtual environment first.

What you need first
NeedDetail
Python3.10 to 3.12 for every chapter. pyproject.toml allows >=3.10,<3.15 but installs TensorFlow, which chapters 5 to 7 use to load the GPT-2 weights, only below 3.13. The check script flags only versions below 3.9
GitTo clone the repository. The README also offers a Download ZIP button
A network connectionChapters 5 to 7 download the GPT-2 weights, and the notebooks load their pictures from the author’s website
An NVIDIA GPUOptional. The code uses one automatically when it finds it
OllamaFor chapter 7’s evaluation step, which the main chapter describes and ollama_evaluate.py summarizes, and for the chapter 7 bonus notebooks that use Ollama
  1. Clone the repository and open its folder.
  2. Make a virtual environment and activate it. The author uses uv for this.
  3. Install the requirements.
  4. Open a chapter in JupyterLab. Start JupyterLab and open the chapter’s notebook. You can run the environment check first.
git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git
cd LLMs-from-scratch
pip install uv
uv venv --python=python3.10
source .venv/bin/activate
uv pip install -r requirements.txt

On Windows, the setup tips say you may have to replace the activate line with source .venv/Scripts/activate or .venv/Scripts/activate. If a dependency fails, fall back to plain pip.

pip install -r requirements.txt
python setup/02_installing-python-libraries/python_environment_check.py
jupyter lab

For Windows the author recommends pixi over the native uv commands, because some dependencies in pyproject.toml cause problems there. On Google Colab, put this line at the top of a notebook:

pip install uv && uv pip install --system -r https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/refs/heads/main/requirements.txt

How to use it

Read a chapter, then open its notebook. A chapter notebook holds all the code as it appears in the book, and chapters 2 to 7 and Appendix A each have an exercise-solutions notebook. The README’s table gives each chapter’s main notebook and a folder of bonus material.

Chapters and their main notebooks
ChapterMain notebookFolder
2: Working with Text Datach02.ipynbch02/01_main-chapter-code
3: Coding Attention Mechanismsch03.ipynbch03/01_main-chapter-code
4: Implementing a GPT Model from Scratchch04.ipynbch04/01_main-chapter-code
5: Pretraining on Unlabeled Datach05.ipynbch05/01_main-chapter-code
6: Finetuning for Text Classificationch06.ipynbch06/01_main-chapter-code
7: Finetuning to Follow Instructionsch07.ipynbch07/01_main-chapter-code
Appendix A: Introduction to PyTorchcode-part1.ipynb, code-part2.ipynbappendix-A/01_main-chapter-code

Chapters also have standalone scripts that summarize them, such as gpt_train.py in chapter 5. Chapter 7’s main code README shows how to run two of its scripts, which sit in the ch07/01_main-chapter-code folder:

python gpt_instruction_finetuning.py
python ollama_evaluate.py --file_path instruction-data-with-response-standalone.json

The README’s sample output for the second one starts with “Ollama running: True”.

Example

You want to follow the attention chapter. You activate your environment, start JupyterLab from the repository folder and open ch03/01_main-chapter-code/ch03.ipynb.

cd LLMs-from-scratch
source .venv/bin/activate
jupyter lab

Run the cells from the top. When you finish the chapter, open exercise-solutions.ipynb in the same folder and compare it with your own work. The troubleshooting page says these main notebooks are kept in sync with the printed book.

For newcomers and experts

If you are new. A large language model, or LLM, is the kind of model behind chat tools such as ChatGPT. The book teaches how one works, so you can start without knowing. Read the setup recommendations in the README table. Chapter 1 has no code, so the first notebook is Chapter 2’s. If Python is new to you, learn it first. If PyTorch is new, read Appendix A. The README also links the author’s post on how to best read the book.

For experts. The optional llms-from-scratch package on PyPI imports the chapters’ code, such as MultiHeadAttention from llms_from_scratch.ch03, and has KV-cache versions of GPT-2, Llama 3 and Qwen3.

Bonus folders in chapter 4 cover grouped-query, multi-head latent and sliding window attention, Gated DeltaNet and Mixture-of-Experts. Chapter 5 adds from-scratch Llama 3.2, Qwen3, Gemma 3 and Olmo 3 models, and chapter 7 adds DPO. The bonus notebooks need more packages, which one uv line installs:

uv pip install --group bonus

Appendix A’s multi-GPU demo is a script, run as python DDP-script.py, with a torchrun version beside it. On an Apple Silicon Mac, the troubleshooting page says many scripts skip the mps backend on purpose, so rerun on cpu first if the losses diverge. The author’s sequel, Build A Reasoning Model (From Scratch), has its own repository, and the README links bonus material from it.

Good to know

The code and notebooks run on your machine. Installing the packages and the first run of chapters 5 to 7 need the network, and the notebooks load their pictures from the author’s website each time you open one.

  • Downloads: chapters 5 to 7 fetch the GPT-2 weights from openaipublic.blob.core.windows.net, with a backup on Backblaze, and the notebooks load their pictures from the author’s website
  • Cost: the repository is free. The book and the video course are on Manning’s site. Lightning and Colab are separate services, and the pages give no prices
  • Your prompts: the chapter 7 bonus evaluation has one notebook that uses OpenAI’s GPT-4 through its API and one that uses a Llama 3 model run locally with Ollama
  • Docker: the DevContainer runs as root by default. If its build fails, the guide says the likely cause is a machine without a compatible NVIDIA GPU, and to remove the runArgs line
  • Versions: the check script still accepts Python 3.9, but pyproject.toml requires >=3.10,<3.15. The setup tips recommend 3.10 or newer and give 3.10 or 3.11 as their example, and the native uv page’s example uses 3.11
  • Copy errors: the package README’s Chapter 4 import lacks a comma, and Appendix A misspells CUDA_VISIBLE_DEVICES. The native uv page’s Windows line ends in | more, which Mindket reads as a slip. The kit flags each
  • Contributions: the author cannot accept changes that extend the main chapter code, because it has to match the printed book
  • Removal: delete the folder. A uv environment is the .venv folder inside it, and the Docker page gives docker image rm for the container image. A Conda environment (LLMs) lives outside the folder, and the pages give no step to remove it
  • License: the Apache License 2.0 with a changed Source definition that leaves out books about the software and their images. GitHub’s license field reads “Other”. The kit holds Mindket’s notes, not the code or the book

README and files

README

Build a Large Language Model (From Scratch): setup notes

This kit holds setup notes only. Mindket wrote them. The code, notebooks and bonus material stay in Sebastian Raschka's repository: https://github.com/rasbt/LLMs-from-scratch

These notes are not made or endorsed by Sebastian Raschka or Manning. The README says the repository "contains the code for developing, pretraining, and finetuning a GPT-like LLM and is the official code repository for the book Build a Large Language Model (From Scratch)". The notes were written from the README, the setup pages and the files named under Sources, read on 3 November 2026.

License: the LICENSE.txt file is the Apache License 2.0 with one changed definition. Its "Source" form is "the preferred form for making modifications, explicitly excluding any books specific to this software and any related images". The copyright line names Sebastian Raschka, and GitHub's license field reads "Other". This kit holds neither the book nor its images. Source: https://github.com/rasbt/LLMs-from-scratch/blob/main/LICENSE.txt

What the repository is

The README says that in the book you "learn and understand how large language models (LLMs) work from the inside out by coding them from the ground up, step by step", and that the method for training "your own small-but-functional model for educational purposes" mirrors the approach used for large foundational models such as those behind ChatGPT. The book also has code for loading the weights of larger pretrained models for finetuning. The README links the book at Manning, the publisher, and on Amazon. Source: https://github.com/rasbt/LLMs-from-scratch/blob/main/README.md

The code uses PyTorch and, in the README's words, no external LLM libraries. The most important prerequisite is "a strong foundation in Python programming". Experience with deep neural networks helps, and PyTorch basics are useful but not required, because Appendix A is a concise introduction to PyTorch.

What is in it

| Chapter | Folder | Code |
|---|---|---|
| 1: Understanding Large Language Models | Not listed | None |
| 2: Working with Text Data | ch02 | Yes |
| 3: Coding Attention Mechanisms | ch03 | Yes |
| 4: Implementing a GPT Model from Scratch | ch04 | Yes |
| 5: Pretraining on Unlabeled Data | ch05 | Yes |
| 6: Finetuning for Text Classification | ch06 | Yes |
| 7: Finetuning to Follow Instructions | ch07 | Yes |
| Appendix A: Introduction to PyTorch | appendix-A | Yes |
| Appendix B: References and Further Reading | appendix-B | None |
| Appendix C: Exercise Solutions | appendix-C | Links to each chapter's solutions |
| Appendix D: Adding Bells and Whistles to the Training Loop | appendix-D | Yes |
| Appendix E: Parameter-efficient Finetuning with LoRA | appendix-E | Yes |

Each folder with code has a 01_main-chapter-code folder with the notebooks that match the book, and the chapter folders also hold numbered bonus folders beside it. Titles and folders are copied from the README's table. The main notebook of each chapter is listed in setup/chapter-map.md.

Ways to set it up

  • setup/uv-and-pip.md: your own computer with pip or uv's pip commands, the author's first suggestion.
  • setup/native-uv.md: uv's own commands, uv sync and uv run.
  • setup/pixi.md: pixi, which the author recommends to Windows users in place of uv's native uv add commands.
  • setup/conda.md: Conda through Miniforge, the fallback if the uv route fails.
  • setup/docker-dev-container.md: a Docker DevContainer in VS Code.
  • setup/colab-and-lightning.md: Lightning AI Studio and Google Colab in the cloud.
  • setup/vs-code-and-jupyter.md: the editor, its recommended extensions and JupyterLab tips.
  • setup/chapter-map.md: which notebook or script to open for each chapter, and how to run the scripts.
  • setup/pip-package.md: the optional llms-from-scratch package, which imports the chapters' code.
  • setup/troubleshooting.md: images that do not load, keeping your notebook changes, Apple Silicon and the traps in the setup pages.

Before you start

  • Python 3.10 or newer, which the Python setup tips recommend. The pyproject.toml file says >=3.10,<3.15, and its TensorFlow lines apply only below Python 3.13. See setup/troubleshooting.md for where the pages disagree.
  • Git, or the Download ZIP button on the repository page.
  • The README says the main chapters are "designed to run on conventional laptops within a reasonable timeframe" and need no specialized hardware, and that the code uses a GPU automatically if one is available. The setup page adds that an NVIDIA GPU in the cloud can substantially shorten chapters 5 to 7.
  • A network connection. Chapters 5 to 7 download the GPT-2 weights, and the notebooks load their pictures from the author's website. See setup/troubleshooting.md.

Source: https://github.com/rasbt/LLMs-from-scratch/blob/main/README.md and https://github.com/rasbt/LLMs-from-scratch/blob/main/setup/README.md

Get the code

The README gives two ways: the Download ZIP button, or this command. Source: https://github.com/rasbt/LLMs-from-scratch/blob/main/README.md

git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git

The Download ZIP link the README uses is https://github.com/rasbt/LLMs-from-scratch/archive/refs/heads/main.zip

If you want to keep your own changes and still receive updates, fork the repository first. See setup/troubleshooting.md.

Update or remove

The pages give no update step for a plain clone. The troubleshooting page adds an upstream remote for a fork, shown in setup/troubleshooting.md. For a clone you did not fork, git pull inside the folder updates it, which is Mindket's line and not the author's.

To remove it, delete the folder. A virtual environment made by uv lives in the .venv folder in it, and the native uv page says you can simply delete that folder to start from scratch. The Docker page gives docker image ls and docker image rm [IMAGE_ID_OR_NAME] for the DevContainer's image, as shown in setup/docker-dev-container.md.

Related material the README links

Sources

Files
README.md  8 KB
setup/chapter-map.md  5 KB
setup/colab-and-lightning.md  2 KB
setup/conda.md  3 KB
setup/docker-dev-container.md  5 KB
setup/native-uv.md  3 KB
setup/pip-package.md  3 KB
setup/pixi.md  3 KB
setup/troubleshooting.md  5 KB
setup/uv-and-pip.md  5 KB
setup/vs-code-and-jupyter.md  2 KB

11 files, 45 KB, downloaded as a zip.

What’s new

  • Version 2 · 2026-11-09

    Updated by Mindket to the current version of this kit.

  • Version 1 · 2026-11-03

Compare with similar

Listed GitHub projects in the same category that share the most topics with this one.

ProductStarsActivityLanguageLicenseLast change
Build a Large Language Model (From Scratch) This page107,934————
AI Engineering from ScratchShares from-scratch · deep-learning · generative-ai77,834——MIT—
Generative AI for BeginnersShares gpt · language-model · generative-ai123,169ActiveJupyter NotebookMIT
AI For BeginnersShares artificial-intelligence · deep-learning · ai70,626——MIT—
LLM CourseShares large-language-models · llm84,938——Apache-2.0—
Numbers as Mindket last read them, from 2026-11-07 to 2026-11-09. Activity is read from each project’s last change on GitHub: Active within 30 days, Quiet within 180, Stale after that.

Reviews

No ratings yet

Write a review

No reviews yet.

What people say

Posts and comments that name this project on Hacker News, Bluesky, X, Reddit, Facebook and YouTube, found by a weekly search and on Trendshift. Mindket doesn’t write or check them. Open one to read it where it was posted.

kuberwastakenDay 1 of trying to fit a Chatbot into a QR CodeImage for day 1: https://i.imgur.com/bQ3Oxc5.png After I tried to fit DOOM inside a QR code last time (https://news.ycombinator.com/item?id=43729683), I'm trying to continue this "series" to get an actually decent chatbot into a QR code. This is, of course, not as easy as the former. I could…1 point · 2 repliesmidudev · Miguel Ángel Durán¡Crea tu propio modelo de IA! Este repositorio contiene el código para desarrollar, pre-entrenar y ajustar un LLM como GPT desde cero: github.com/rasbt/LLMs-f...22 likes · 2 replies@danielishakutvWant to get really good at generative AI? Save these 10 GitHub repos. 👇 The diagram is where they lead: AI agents with memory, tools, and a human still in control.r/openagiThe best free &amp; open-source tools I've used this year that were 100% worth itdtfSebastian Raschka's from-scratch video course on building reasoning modelsThere's a note in Sebastian Raschka's LLMs-from-scratch github repository - "Recommendations for Getting the Most Out of a Technical Book". He recommends reading offline and code-free first (although chapter-by-chapter). https://github.com/rasbt/LLMs-from-scratch/blob/cdbd33e6d57a...midudev · Miguel Ángel Durán¡Crea tu propio modelo de IA! Este repositorio contiene el código para desarrollar, pre-entrenar y ajustar un LLM como GPT desde cero: github.com/rasbt/LLMs-f...10 likes · 1 reply@SanketShipsfoundations 01 micrograd: the tiny engine behind neural nets https://t.co/QObt0s4UwB 02 nn-zero-to-hero: karpathy's full course, zero to GPT https://t.co/0HJUmwgowe 03 nanoGPT: train your own small GPT https://t.co/5ax2mOJA42boredumbI were 17, I'd learn how to build LLMs from scratchI agree with mostly all of this, but personally I wrote a toy LLM almost 5 years ago and while it never saw much use outside of boring my wife with a shitty command line demo with glee it did help me understand how they worked and how to apply them, played a lot with JAX and pytorch, ended up…midudev · Miguel Ángel Durán¡Crea tu propio modelo de IA! Este repositorio contiene el código para desarrollar, pre-entrenar y ajustar un LLM como GPT desde cero: github.com/rasbt/LLMs-f...4 likes@antiAIvo用 Codex 整理了目前比较主流的 LLM 学习教程 偏动手写代码、理解模型原理和训练流程: 1. Zero to Hero|@Karpathy OpenAI 创始团队成员、前特斯拉 AI 负责人 从神经网络基础讲起带你手写语言模型、GPT 和分词器 https://t.co/RPfFZKjJaV 2. LLMs from Scratch|@rasbt 前威斯康星大学麦迪逊分校教授、机器学习畅销书作者 用 PyTorch 从零实现 GPT,覆盖注意力、预训练和微调 https://t.co/SZIV6Kx5vD 3. Happy-LLM|Datawhale 国内 AI 开源学习社区出品…JoeDaDudeTrain Your Own LLM from ScratchCoincidentally, I just started on Build a Large Language Model (From Scratch), a repo/book/course by Sebastian Raschka [0][1][2]. Maybe it is a good problem to have to have to decide which learning resource to use. [0] https://github.com/rasbt/LLMs-from-scratch [1]…wikitdBuild a Large Language Model (From Scratch) Code for developing, pretraining, and finetuning a GPT-like LLM and is the official code repository for the book Build a Large Language Model (From Scratch). ▶️ github.com/rasbt/LLMs-f... #ai #tutorials #freetutorials2 likes

Searched

More from Mindket

See all from Mindket

anaksor-ocr

Sokheng · Kit

Reads Khmer and English text from documents, receipts and photos of signs, using a detector for text lines and a recogniser for each line. Suits developers who…

New

Qwen2.5-7B-Instruct as GGUF files in eight quantization sizes, to chat with locally through llama.cpp. For people who want a capable 7.6B model on their own co…

New

ACE-Step 1.5 XL SFT

ACE-Step · Kit

Generates music from text with a 4B diffusion decoder, 50 steps and guidance control, aiming for higher audio quality than the base. For creators with a GPU of…

New

Similar projects

See all in Education

Rohit Ghumare’s free, MIT-licensed course that builds AI from raw math up to agents and production, in twenty phases. Tutor skills for Claude Code, Codex and C…

New77.8k

Microsoft Cloud Advocates’ free course on building generative AI apps, in 21 lessons. Learn lessons explain a concept, and Build lessons add Python and TypeScr…

New123k

Dify

langgenius · Kit

LangGenius’s open-source platform for building LLM apps: visual workflows, RAG, model management and agents with a sandbox. Its difyctl tool and skill let Clau…

New161k