Neural Network in JavaScript
Implementantion of a Perceptron neural network in JavaScript. It is a simple implementation that can serve as an example for learning, not for production use. It does not use GPU and the only activation function implemented is a sigmoid
function.
For a ready to use implementation please refer to BrainJS
Installation
npm install --save vt-neural-network
Usage
// Define the layer structureconst layers = 2 // This is the input layer 10 // Hidden layer 1 10 // Hidden layer 2 1 // Output const network = layers // Start trainingconst numberOfIterations = 20000 // Training data for a "XOR" logic gateconst trainingData = input : 00 output: 0 input : 01 output: 1 input : 10 output: 1 input : 11 output: 0 forvar i = 0; i < numberOfIterations; i ++ // Get a random training sample const trainingItem = trainingDataMath network; // After training we can see if it works// we call activate to set a input in the first layernetworkconst resultA = network networkconst resultB = network networkconst resultC = network networkconst resultD = networkconsoleconsoleconsoleconsole
If you want to see other logic gates implementations, check the test folder.
API
network.setLearningRate(0.3)
: Adjust the learning rate of the network,network.toJSON()
: returns the structure of the networknetwork.layers
: contains the different layers of the networklayer.neurons
: contains the different neurons on each layer
How to develop the application?
npm installnpm run watch# Open public/ directory in browser
Remove generated directory
If you would like to remove public/dist
directory (created by Webpack):
npm run clear
If you would like to remove node_modules/
and remove public/dist/
npm run clear:all
Count LOC (Lines of Code)
If you would like to know how many lines of code you write:
npm run count
Analysis of bundle file weight
If you would like to check how much a bundle file weight:
npm run audit
Information of interest
Backpropagation
https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
Neural networks
https://scrimba.com/g/gneuralnetworks https://franpapers.com/en/machine-learning-ai-en/2017-neural-network-implementation-in-javascript-by-an-example/ http://karpathy.github.io/neuralnets/