This repo holds the code to train a simple digit recognizing feed foward neural network with the MNIST database digits and also a website which allows the user to draw digits and visualize the neural network prediction of the drawings.
- Draw digit on the "Digit" canvas
- Click on node to highlight it's weights
- Check table for camera controls in the 3D visualizer
- Space to reset weights and camera
First, clone the repository:
git clone https://github.com/g-otn/number-guesser.git
cd number-guesser
If you do not wish to train a new neural network, you can simply serve the client/
folder:
npm i -g serve
serve client
orcd train && npm run-script serve
If you wish to run the training script to generate a new model, follow these steps. Steps 1-3 are optional, but if you wish to skip them, please read Note 2 below:
- Navigate to the
train/
folder:cd train
- BEFORE creating
node_modules/
(i.enpm i
) in thetrain/
folder, download these two files from the MNIST database:- train-images-idx3-ubyte.gz: training set images (9912422 bytes)
- train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
- Extract the two files into the
data/
folder:
data
├───train-images-idx3-ubyte
└───train-labels-idx1-ubyte
Note: Notice the -
character in the file names.
You may need to rename them (replacing the .
for -
) if you get an file not found error in the next step.
-
Install the packages:
npm i
. A post-install npm script should do all the work with the files. -
Run
npm start
ornode train.js
-
After training ends the model file will already be in the right path to be loaded by the browser.
Note 2: If something goes wrong during steps 1-3 you can skip it, however you will need to edit the train.js
file and lower the mnist set to a sum <= 10000. (i.e mnist.set(8000, 2000)
. Otherwise the training might log NaN
s and save an invalid model.
If you already have an exported brain.js model, you can replace the client/model.json
file so the client can use your model instead.
Because of hard-coded code, and since it's using a poorly modified version of brain-browser.js
, the client requires the model to have this structure for the visualization to work:
- Type: feed forward (
brain.NeuralNetwork
) - Layers: 784 nodes input layer (28*28 image pixels), at least one hidden layer, 10 nodes output layer (digits 0-9)
- Activation function:
sigmoid
This repo was highly motivated by 3Blue1Brown's videos on Neural Networks.
Most of the training code was based on Ralph's Blog article on "Machine Learning with brain.js and Tensorflow.js".
Thanks to MSc Adriana Natividad Lopez Valverde for assigning me the project related to this repo.
This repo uses a modified version of a BrainJS/brain.js file, licensed under MIT.