Skip to content
Multi-layer Neural Network in Javascript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


A simple implementation of a multi-layer neural network using backpropagation algorithm in javascript. See this library in action here !


To instantiate a new neural network, just require the network class in the library.

var neural = require('../lib/network')
var network = new neural.Network()

Next, you can add layers to your neural network thanks to the addLayer method. This method takes two parameters :

  • numNeurons : number of neurons to create for the given layer
  • numInputs : optional, determines the number of input for each given neurons in the layer. If this argument is not specified, the number of input will be the number of neurons from the previous layer.

For instance :

network.addLayer(10, 20) // Hidden layer, 10 neurons, 20 inputs
network.addLayer(2)      // Output layer, 2 neurons

It is now time to train the neural network with training data. The training method uses the backpropagation algorithm. Careful ! this method can take time ...

Two stop conditions are implemented :

  • The mean square error is below a threshold (errThreshold)
  • We iterated over 100 000 times
  // inputs   outputs
  [  zero,    [0, 0]  ],
  [  one,     [0, 1]  ],
  [  two,     [1, 0]  ],
  [  three,   [1, 1]  ]

Once the neural network is trained, you can pass it input data and get the output with the process method :

var outputs = network.process(zero);


I made a website that uses this neural network to predict a drawn digit between 0 and 3. You can check it out here !


Instead of training the neural network everytime, I added a function to serialize/deserialize the neural network for later use.


// The deserialization needs to know how many inputs the neural network has. This is the first argument of the method.


Feel free to contribute !

You can’t perform that action at this time.