A handwritten digit classifier written from scratch in a single C++ file with no libraries. It is a plain fully connected feed forward neural network (a multilayer perceptron) trained with backpropagation on the MNIST dataset of 28x28 digit images.
Download MNIST once before anything else:
mkdir -p data && cd data
for f in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte; do
curl -L -O "https://ossci-datasets.s3.amazonaws.com/mnist/$f.gz"
gunzip "$f.gz"
done
Run any of them from anywhere, they build the program first.
./build.shcompiles only./train.shtrains with the default settings and saves the weights to model.bin./test.shloads model.bin, prints the results and draws 10 mistakes./quick.shtrains a small network for a fast look./check.shchecks the backpropagation math against numerical gradients
data/ holds the four MNIST files, mnist is the compiled program and model.bin is the saved weights. All three are ignored by git and can be deleted and remade at any time.