rasbt /reasoning-from-scratch

Implement a reasoning LLM in PyTorch from scratch, step by step

★ 5,840⑂ 947👁 67⑃ 200● 58▤ 1
Jupyter NotebookApache-2.0Created 2025-03-04mng.bz/lZ5B
下载:v1.0v1.0 ↓1.02026-05-18
+9
日增长
5,840
stars
947
forks
67
watchers
200
prs
58
issues
1
releases

README

Build A Reasoning Model (From Scratch)

This repository contains the code for developing an LLM reasoning model and is the official code repository for the book Build a Reasoning Model (From Scratch).







(Printed in color.)



In Build a Reasoning Model (From Scratch), you will learn and understand how a reasoning large language model (LLM) works.

Reasoning is one of the most exciting and important recent advances in improving LLMs, but it’s also one of the easiest to misunderstand if you only hear the term reasoning and read about it in theory. This is why this book takes a hands-on approach. We will start with a pre-trained base LLM and then add reasoning capabilities ourselves, step by step in code, so you can see exactly how it works.

The methods described in this book walk you through the process of developing your own small-but-functional reasoning model for educational purposes. It mirrors the approaches used in creating large-scale reasoning models such as DeepSeek R1, GPT-5 Thinking, and others. In addition, this book includes code for loading the weights of existing, pretrained models.

- Link to the official source code repository
- Link to the book at Manning (the publisher's website)
- Link to the book page on Amazon.com
- ISBN 9781633434677






To download a copy of this repository, click on the Download ZIP button or execute the following command in your terminal:

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





 Tip:
 Chapter 2 provides additional tips on installing Python, managing Python packages, and setting up your coding environment.




 Table of Contents





- Troubleshooting Guide

| Chapter Title                                               | Main Code                                                    |
| ----------------------------------------------------------- | ------------------------------------------------------------ |
| Ch 1: Understanding reasoning Models                        | No code                                                      |
| Ch 2: Generating Text with a Pre-trained LLM                | - ch02_main.ipynb- ch02_exercise-solutions.ipynb |
| Ch 3: Evaluating Reasoning Models                           | - ch03_main.ipynb- ch03_exercise-solutions.ipynb |
| Ch 4: Improving Reasoning with Inference-Time Scaling       | - ch04_main.ipynb- ch04_exercise-solutions.ipynb |
| Ch 5: Inference-Time Scaling via Self-Refinement            | - ch05_main.ipynb- ch05_exercise-solutions.ipynb |
| Ch 6: Training Reasoning Models with Reinforcement Learning | - ch06_main.ipynb- ch06_exercise-solutions.ipynb |
| Ch 7: Improving GRPO for Reinforcement Learning             | - ch07_main.ipynb- ch07_exercise-solutions.ipynb |
| Ch 8: Distilling Reasoning Models for Efficient Reasoning   | - ch08_main.ipynb- ch08_exercise-solutions.ipynb |
| Appendix A: References and Further Reading                  | No code                                                      |
| Appendix 

Top issues

Discussions

Latest release

v1.0 v1.0 2026-05-18
State of the book code as it goes to print.

This release captures the final version of the code accompanying the print edition. (Future updates may include small fixes, compatibility updates, or clarifications, but this tag marks the stable reference point for readers following along with the book.)

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON