LLMs-from-scratch
by rasbt
Step-by-step code and notebooks that teach you to build a ChatGPT-like language model in PyTorch from scratch.
- Stars
- 109,089 stars
- Forks
- 17k forks
- Last push
- today
- Activity
- Active
# Download the code$ git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.gitCommands from the project's README. Run them after installing (see below).
Overview
Understand LLMs-from-scratch in 60 seconds
- What it does
- LLMs-from-scratch is the official code for Sebastian Raschka's book Build a Large Language Model (From Scratch). It walks you through coding, pretraining and fine-tuning a small GPT-like model in PyTorch, step by step.
- The problem it solves
- It's hard to understand how models like ChatGPT work by only using them. This repository builds one from the ground up, from text processing and attention to training and instruction fine-tuning, without external LLM libraries.
- Why choose it
- Choose it if you want to learn by building, with notebooks per chapter. The code runs on ordinary laptops. The book itself is sold separately; the code is free.
- Who should use it
- Python developers who want to understand LLMs deeply
- Students studying deep learning
- Engineers moving into AI who want hands-on practice
- Readers of the book
- Setup difficulty
You'll need:
- Strong Python skills
- Basic PyTorch helps (Appendix A introduces it)
- A regular laptop is enough; a GPU is used automatically if available
How LLMs-from-scratch works
The repository follows the book chapter by chapter: working with text data, coding attention mechanisms, implementing a GPT model, pretraining on unlabeled data, fine-tuning for text classification and fine-tuning to follow instructions. Each chapter has Jupyter notebooks and summary scripts plus exercise solutions.
Appendices cover an introduction to PyTorch, extras for the training loop, and parameter-efficient fine-tuning with LoRA. The method mirrors how large foundation models are built, scaled down for learning.
The main chapters are designed to run on conventional laptops in reasonable time. A setup folder explains how to install Python and packages, and there's a troubleshooting guide. A 17-hour companion video course and a sequel book on reasoning models are available separately.
Features
Key features
Build a GPT from scratch
Tokenisation, attention and the full model in PyTorch.
Pretraining
Train on unlabeled text.
Fine-tuning
Text classification and instruction following.
LoRA appendix
Parameter-efficient fine-tuning.
Runs on a laptop
No special hardware needed for main chapters.
Exercise solutions
Notebooks with solutions per chapter.
Strengths
- Clear, step-by-step learning path
- Code tested on Linux, Windows and macOS
- Popular, well-maintained companion code
- Apache-2.0 license
Limitations
- Best used alongside the paid book
- Models are small and educational, not production-ready
- Needs solid Python knowledge
Practical use cases
Learn how attention works
Work through chapter 3's notebooks.
Train a tiny GPT
Run the pretraining chapter on your laptop.
Practice fine-tuning
Fine-tune a model to follow instructions.
Installation
Install LLMs-from-scratch
Commands come from LLMs-from-scratch's README. AIRepoHub hasn't run them yet. Steps are as of Nov 25, 2026; projects change often, so if a command doesn't work, check the project's latest README. View source
Prerequisites
- Python
- Git
Supported systems
- Windows
- macOS
- Linux
- 1
Clone the repository
Shellgit clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git - 2
Set up Python and packages
Follow setup/README.md in the repository for Python and package installation.
Configuration and API keys
| Setting | What it's for |
|---|
First run
- 1
Open the chapter 2 notebook
ch02/01_main-chapter-code/ch02.ipynb covers working with text data.
LLMs-from-scratch's documentation doesn't include sample output, so we don't show any. Expect the results in your terminal.
Your first project
Build your first GPT model piece by piece
Go from text data to a working GPT model by following the chapters.
Before you start
- Repository cloned
- Python environment set up per setup/README.md
- 1
Chapter 2: work with text data
Open ch02/01_main-chapter-code/ch02.ipynb.
- 2
Chapter 3: code attention mechanisms
Open ch03/01_main-chapter-code/ch03.ipynb.
- 3
Chapter 4: implement the GPT model
Open ch04/01_main-chapter-code/ch04.ipynb or run gpt.py.
- 4
Chapter 5: pretrain it
Use ch05.ipynb or gpt_train.py.
What to expect
A small GPT-style model you built and trained yourself, generating text.
Next steps
- Fine-tune for classification in chapter 6
- Fine-tune to follow instructions in chapter 7
- Try LoRA in Appendix E
Costs and requirements
What it costs to run
- License
- Apache-2.0 for the code.Free
- Code
- Free.Free
- Book
- The book and video course are sold separately.Paid
Troubleshooting
Common setup issues
Environment setup problems
Read setup/README.md for Python and package installation.
Other errors
See troubleshooting.md in the repository.
Viewing the README offline
Use a Markdown previewer or view it on GitHub.
Alternatives
Similar AI repositories
ML-For-Beginners
microsoft
Free 12-week Microsoft course with 26 lessons that teaches classic machine learning to beginners.
AI-For-Beginners
microsoft
Free 12-week Microsoft course with 24 lessons that introduces artificial intelligence to beginners.
rea
morluto
Connects AI agents to reverse-engineering tools for inspecting binaries, apps and websites.
superpowers
obra
Collection of agent skills and a working method that guide AI coding agents through software development step by step.