Tiny GPT From Scratch

How it was built

Part 1: Tokenizer

Build a tiny character-level tokenizer with vocab, stoi/itos, and encode/decode helpers.

Part 2: NumPy and Softmax Foundations

Get fluent with NumPy arrays, indexing, broadcasting, reductions, and numerically stable softmax.

Part 3: Data Pipeline and Bigram Baseline

Load the corpus, build batched (X, Y) sequences, and train a counting-based bigram model as a baseline.

Part 4: Single-Layer Neural Bigram

Replace the count table with a learned weight matrix and derive cross-entropy, gradients, and SGD updates.

Part 5: Layer Primitives and Backprop

Implement forward and backward passes for linear, bias, ReLU, softmax+CE, and LayerNorm building blocks.

Part 6: Embeddings and Self-Attention

Add token and positional embeddings, then build masked single-head and multi-head self-attention with full backward passes.

Part 7: FFN, Blocks, and Full Model

Compose feed-forward networks, residual connections, and pre-LN Transformer blocks into the complete GPT forward/backward.

Part 8: Adam, Training Loop, and Generation

Implement Adam, wire up the full training and validation loop, then sample text with temperature and top-k decoding.