tiny neural network
Only 123 lines and ~1 kB gzipped !
installation
npm install --save tiny-neural-network
examples
The neural network works with values between 0 and 1.
xor
// import the Neural Network; // how much to train// create a network with 2 input, 1 output and 2 layers of 3 and 1 neuron// The network class has 'learningRate' as second argument with default value of 0.1.// xor dataset to learn;;; // training loop// randomly select an input// capture result of what the network predicted// make the network learn from the errorfor ; i < maxIterations; i++ // after training create a table of all predictions and expectations// print the table on console;console.tabletable;
In this case, the more the network learns, the better the XOR prediction. This is because there is no unknown data to predict. In case you want to predict unknown data then more learning is not always better. Here is a an example that I have generated to demonstrate it:
The goal was to learn the sine wave using 20 evenly spaced points. The GIF starts with 20 iterations and goes up to 20e6 with a increase of factor 10. The best prediction is at 20e4 iterations and the two last ones (20e5 and 20e6) are garbage.
mnist
Mnist is a data set of handwriting. The objective is to learn recognize digits and then predict digits that the network hasn't seen before. First install the mnist data set:
npm install --save small-mnist
Then code the magic:
// import this lib and the small version of mnist;; // define the number of epochs which is number of times// that the network see all the data one time// 400 inputs because of 400 pixels,// 200 neurons for first layers and 10 neurons for output layer;; // since we are working with epochs we need// a generic shuffle function so that the network does not learn the sequence // trainfor ; i < epochs; i++ // benchmark en format results to show in a table// will print a table of only failed// the test set has 100 elements so if 10 are printed the performance is 90%console.table test .map .filterexpectation !== prediction;
This should take 1-2 min to train in order to have an accuracy above ~85%.
We can also do the inverse, make the network learn to write numbers (Node.JS only due pixel draw on terminal):
// import this lib and the a small version of mnist;;; // define the number of epochs which is number of times that the network see all the data once// 10 inputs because of 10 different input possibilities,// 200 neurons for first layers and 400 neurons for output;; // generic shuffle function so that the network does not learn the sequence // trainfor ; i < epochs; i++ // quick and dirty way to test the drawing skills of the network// create 10 inputs with their expected value as number to test the network// draw black for each pixel having value < 0.5 otherwise draw white.// canvas is 20x20 thus new line at each 20 pixels.Array.fromArray10.keys .map .map;``` ## develop Project was made with Node.JS v10.10.0 and TypeScript v3.0.3. Ulterior versions may work but are not guaranteed. ```bashgit clone https://github.com/dugagjin/tiny-neural-network.gitcd tiny-neural-network``` ### `npm run build` Compile the project in build folder. ### `npm start` Run the existing index file in the build folder. ### `npm run execute` Compile and then run the project. ## author Dugagjin Lashi ## license MIT