Reproduced MNIST handwritten digit classification, ground up. Intended to showcase how to rebuild foundational deep learning architectures and training pipelines from scratch, rather than relying solely on high-level frameworks.
The plan is to start by training models using PyTorch, and then systematically peel back the layers—replacing modules, the autograd engine, and the optimizer—until everything is running on pure NumPy.
- Phase 1: Raw PyTorch (Current)
- Basic MLP implemented manually using
nn.Parameterand raw matrix multiplications instead ofnn.Linear(mnist_mlp.py). - Custom training loop that directly parses the raw IDX files rather than using
torchvision.datasets(trainer_torch.py). - Custom CNN architectures built from scratch (
mnist_cnn.py), featuring multiple Conv2D implementations:- Brute force nested loops (
_convolve_brute) im2colmatrix multiplication variants:- Slow, loop-based implementation (
convolve_im2col_slow) - Fast, fully vectorized implementation (
convolve_im2col_fast)
- Slow, loop-based implementation (
- Brute force nested loops (
- Implement pooling layers (e.g., MaxPool2d, AvgPool2d) for CNNs.
- Basic MLP implemented manually using
- Phase 2: Dropping PyTorch Components
- Write a custom auto-grad engine in Python/NumPy to replace
loss.backward(). - Implement custom optimizers (e.g., AdamW, SGD) to replace
torch.optim.
- Write a custom auto-grad engine in Python/NumPy to replace
- Phase 3: Pure NumPy Engine
- Fully replace Tensors with
np.ndarrayand strip outtorchdependencies completely.
- Fully replace Tensors with
data/- Expects the raw MNIST dataset (*-idx*-ubytefiles).torch_version/- The PyTorch-based implementation.mnist_mlp.py- Custom MLP and barebones Linear layer implementation.mnist_cnn.py- Custom CNN architecture and Conv2D implementations (brute force, loop-based im2col, vectorized im2col).trainer.py- The main training loop, evaluation code, and logic to handle thestructparsing of MNIST's native IDX file format.
numpy_version/- The pure NumPy implementation (in progress).mnist_mlp.py- Custom MLP implementation.autograd.py- Custom autograd engine in Python/NumPy.
download.py- Script to download the raw MNIST dataset.
- Download the raw MNIST
.idxand.idx3/1-ubytebinaries into thedata/directory. Run with pythondownload.py - Run the PyTorch training loop:
python torch_version/trainer.py