▲ 104 today4.2k starsJupyter NotebookHN ×5
Learn to Build a GPT from Scratch, Fully Explained
What is how-to-train-your-gpt?
A 12-chapter Jupyter notebook walks you through writing every component of a modern transformer language model, from tokenizer to inference. All code is runnable and heavily commented so even beginners can see how each piece works.
Who it is for
Developers and ML hobbyists who want to understand how models like ChatGPT work and be able to build their own should check it out.
How how-to-train-your-gpt works
It implements a decoder-only Transformer that follows the LLaMA-style design. The model is built from a BPE tokenizer, token embeddings, RoPE positional encoding, multi-head attention with KV cache, RMSNorm, SwiGLU activation and residual connections. Training uses cross-entropy loss, AdamW optimizer, cosine warm-up and mixed-precision arithmetic. Inference applies temperature scaling, top-k/p sampling and optional beam search. The code is written in pure Python with PyTorch and is fully annotated.
What you can do with it
- Train a tiny GPT model on a small dataset in minutes on a laptop.
- Explore how tokenization, embeddings and attention work by editing the code.
- Run inference to generate text with configurable sampling parameters.
Good to know
- Full-scale 151 M-parameter configuration requires a GPU to train in reasonable time.
- The repository is meant for learning, not for deploying production-grade models.
- Only Python and PyTorch are supported; no other deep-learning frameworks are included.
Written from the project's own README.
Try it in 60 seconds
No one-line install. The README has the setup steps.
New projects are not vetted. Skim the code, and run it in a fresh folder or a container before giving it your files or keys.
The story so far
Early Signal first spotted raiyanyahya/how-to-train-your-gpt on December 4, 2026, when it had 4.2k GitHub stars. It now has 4.2k. On its busiest day it gained 104 stars. It has been discussed in 5 Hacker News threads, the top one with 4 points. Early Signal files it under LLMs.