AI from Scratch in C
From Perceptrons to Large Language Models
Nearly every book that teaches machine learning teaches it in Python, and nearly every one of them reaches for a framework within the first fifty pages. The cost of that is that the machinery disappears, and you end up learning which function to call and in what order while the part where the thing actually learns stays behind an abstraction you never open. This book takes the other route, deriving every gradient on the page and then writing it out as arithmetic you can read, so that there is nowhere for the mechanism to hide.
C turned out to be the right language for that, because C hands you arrays of floats and loops over them and very little besides, which keeps the distance between the mathematics and the code short and stops anything from happening on your behalf that you did not ask for. It also runs on whatever you already own, whether that is a laptop, a Raspberry Pi, a low power RISC-V board or a microcontroller with a few hundred kilobytes of memory, and nothing in the book needs a GPU or takes longer than a few seconds to finish.
The chapters below are free to read.
Contents
- 1The Perceptron
- 2The Multi-Layer Perceptron
- 3Activation Functions
- 4Loss Functions
- 5Gradient Descent and Optimizers
- 6Overfitting and Regularization
- 7Hidden Representations
- 8Convolution and Filters
- 9CNN Architecture
- 10Embeddings
- 11Tokenization
- 12Sequence Problems
- 13Recurrent Neural Networks
- 14The Vanishing Gradient Problem
- 15LSTM
- 16GRU
- 17Bidirectional RNNs
- 18Sequence-to-Sequence
- 19Dot-Product Attention
- 20Query/Key/Value Attention
- 21Multi-Head Attention
- 22Positional Encoding
- 23Normalization Deep Dive
- 24The Transformer Block
- 25The Full Transformer
- 26Decoder-Only Transformer
- 27KV Cache
- 28Reinforcement Learning
- 29Q-Learning
- 30Deep Q-Networks
- 31Pretraining and Fine-Tuning
- 32Scaling Laws
- 33RLHF and Alignment
- 34LoRA
- 35Quantization
- 36Mixture of Experts
- 37A Complete GPT from Scratch
- AAppendix A: Derivative Reference
- BAppendix B: Building and Running the Code