Implementing a Simple Neural Network

JavaScript neural networks libraries

Image of a biological neural network

Last week I compared a few different JavaScript neural network libraries my team was considering to create an automated grocery list generator. We settled on Synaptic.js for its combined power and ease of use. Here, I will briefly describe how to get started with Synaptic to create your own neural networks.

Synaptic has five primary modules:

  • Neurons: The basic building block of a network; analogous to individual biological neurons. Neurons connect to each other to form layers, which connect to form networks. You can activate a neuron, which causes it to act upon the input data you give to it and output a result. Training a neuron causes it to alter its activation function until it returns the expected output. You can either directly activate a neuron with an input parameter, or by connecting neurons together so that each output activates one or more additional neurons. This means that individual neurons can receive multiple outputs. However, each neuron will only produce one output, though it can pass this output on to many different neurons.
  • Layers: Most neural networks combine layers of neurons to create a network, as layers of neurons provide increased processing power (much like having multiple servers for an application).
  • Networks: Combinations of neurons and/or layers of neurons. The type of network depends upon its structure–combining neurons and layers of neurons results in a variety of different structures, called architectures.

The above named modules allow you to create custom neural network architectures to suit your needs.

  • Trainer: Before a network of neurons will give the output you expect for a set of inputs, you must train it will a set of known inputs and output for your data. These data must be correctly structured as an array of objects, whose values are arrays (see example below). You can either use the default settings for the trainer, or use your own to manipulate the speed and accuracy with which the network learns.
  • Architect: This module allows you to quickly create a neural network by using one of the predefined architectures it provides. This is the simplest way to start using a neural network, as you can avoid almost all of the complexities involved with the network. If you use one of these architectures, you can get started using a network with just a few lines of code, and avoid using the Neuron, Network, and Layer modules entirely.

Diagram of an artificial neural network

Sample Code to create the network shown above:

//create a new network using a built in architecture; 
// #neurons in input layer = 4
// #neurons in hidden layer = 5
// #neurons in output layer = 1
var network = new Architect.Perceptron(4, 5, 1);

//train the network with your known data
var trainer = new Trainer(network);
trainer.train([{input: [3,1,9,4], output [6]}, {input: [9,5,18,3], output: [18]}]);

//activate the network
network.activate([1, 8, 4, 9]) //output with some value as an array: [19]

The Synaptic documentation does a great job explaining how to use each of these modules, and I encourage you to read about each of them in detail. There are many more awesome functions provided by Synaptic, including functions to use web workers and to create a standalone neural network that doesn’t have any dependencies.

Written on September 5, 2015