r/learnmachinelearning • • 14d ago

I built a GPT from scratch in NumPy only – 34 notebooks, no PyTorch

I wanted to understand LLMs below the framework level, so I wrote 
a 34-chapter tutorial that starts from a single neuron and builds 
up to a ~10.8M-parameter GPT — tensors, computational graphs, 
autograd, attention, pre-training, sampling, KV cache, SFT and 
DPO — using nothing but NumPy. Every chapter is a runnable 
Jupyter Notebook, everything trains on CPU, and the book is free 
in English and Chinese.

**How is this different from nanoGPT / micrograd?** 

Those projects are excellent and this book stands on their 
shoulders. The difference is scope and starting point: it starts 
from neural-network basics, builds its own tensor / autograd 
framework in NumPy, and only then gets to GPT — covering pre-
training, sampling, KV cache, SFT and DPO in 34 self-contained 
notebooks. It is a textbook, not just a training script.

Book: https://gptmini.github.io/from-neuron-to-gpt
Code: https://github.com/gptmini/from-neuron-to-gpt

---

## What the book covers

Part 1 (16 chapters) introduces the fundamentals of neural 
networks: multilayer perceptrons, loss functions, gradient 
descent, autograd, learning rates, iterations, batch sizes, 
epochs, hidden layers, and activation functions — followed by 
discussions on encoding, embeddings, cross-entropy, recurrent 
neural networks (RNNs), the Adam optimizer, and learning rate 
scheduling. After completing this part, you will have built a 
small, working deep learning framework (`src/core`).

Part 2 (18 chapters) introduces large language models (LLMs): 
tokens and positional embeddings; self-attention, causal 
attention, weighted attention, and multi-head attention; residual 
connections, normalization, dropout, and model persistence; 
sampling strategies (temperature, top-k, top-p), stopping 
criteria, KV caching, and batch inference; and finally, SFT and 
DPO data preprocessing and training.

Each chapter is a self-contained, runnable Jupyter notebook, and 
most chapters end with practice exercises. No GPU required: the 
default model (context window 256, 6 Transformer modules, 6 
attention heads, approximately 10.8 million parameters) can be 
trained on a laptop’s CPU. The training data uses Karpathy’s 
tinyshakespeare.

## Who it is for

1. Self-learners and career changers who seeking a complete 
learning path rather than fragmented tutorials.
2. Engineers already using PyTorch who cannot explain autograd or 
attention mechanisms in detail. You can read relevant chapters 
(such as autograd and KV caching) separately to reinforce your 
knowledge.
3. Teachers looking for experimental materials. Licensed under 
the MIT License, each chapter is executable and includes 
exercises, no GPU budget required.

This book is not intended to replace PyTorch. Its purpose is to 
encourage you to read it before relying on any framework — so 
that when you actually use a framework, you’ll understand how it 
works under the hood. Frameworks come and go, but the underlying 
principles remain constant.

If you find this book helpful, please star the repository; this 
will help others discover it. Feel free to submit issues and pull 
requests, especially for chapters that are unclear — that’s how 
this book continues to improve.
0 Upvotes

15 comments sorted by

3

u/See_it_do_it_done 14d ago

building the autograd yourself is the part nanoGPT skips, and that's where silent bugs hide. a finite difference gradient check per op would settle it. how long does the 10.8M model train on CPU?

1

u/SLiXG 14d ago

I'm using a Mac mini M4 16/256. Pre-training took 50 minutes.

1

u/See_it_do_it_done 14d ago

50 minutes on an M4 is quick for pure NumPy. are the arrays float32 or still the float64 default?

1

u/SLiXG 14d ago

float32

1

u/See_it_do_it_done 14d ago

that fits the 50 minutes. one catch, finite difference checks get noisy at seven digits of precision, so I'd run the gradient check alone in float64. is dtype set in one place?

1

u/SLiXG 14d ago

Yes

1

u/See_it_do_it_done 14d ago

then the float64 check is a one line flip. with central differences and a step near 1e-5, relative error should sit around 1e-7. anything stuck near 1e-3 is a real bug. which op scores worst?

1

u/SLiXG 13d ago

Actually, this is an interesting topic, I plan to include gradCheck as an appendix to the tutorial.

1

u/See_it_do_it_done 13d ago

appendix suits it. I'd plant one flipped sign in a backward pass and let readers watch gradcheck catch it.

1

u/See_it_do_it_done 13d ago

handy. finite differences cost two forward passes per weight, so sample a few per tensor. how many params?

1

u/SLiXG 13d ago

actually, I used a tiny model to do gradCheck, 1995 weights, about 4000 forward passes, so every element is checked.
for a real model, you are right, sampling a few random elements per tensor is the practical choice.

2

u/Able-Contact9097 14d ago

Get this slop out of here

1

u/kdrozzi 14d ago

looks good

1

u/Ok-Pension3366 13d ago

this will teach you more about how transformers actually work than blindly fine-tuning another hugging face model ever will