albert-yu/mnist
Simple Zig mnist
Zig implementation of a simple neural network for mnist
First, follow the instructions at https://ziglang.org/download. I'm using version 0.13.0.
Then, download the dataset from http://yann.lecun.com/exdb/mnist/. The four files you need are:
train-images-idx3-ubyte: training set images
train-labels-idx1-ubyte: training set labels
t10k-images-idx3-ubyte: test set images
t10k-labels-idx1-ubyte: test set labels
Without renaming the files, put them in a data/
folder at the root of this project's directory.
zig build run
To actually make it go fast:
zig build -Doptimize=ReleaseFast run
On my machine, getting around 93% accuracy with 100 epochs.
To run tests:
zig build test
This project was a way for me to (1) learn the fundamentals of neural networks and (2) learn Zig.
I started with Michael Nielsen's excellent introduction to neural networks and the Python implementation within as a reference. But translating the Python code directly to Zig was a clumsy effort. By chance, I stumbled upon this blog post implementing mnist in Zig. Taking inspiration from there, I refactored my original implementation to hard-code the layers and feedforward/backprop them explicitly instead of using a loop.