DEV Community

Cover image for Intro to Brain.js, Creating Neural Networks in Javascript
Evan Loria
Evan Loria

Posted on

Intro to Brain.js, Creating Neural Networks in Javascript

Introduction

Brain.js is a Javascript library that is used to create neural networks using Javascript. The Brain.js library makes it easier to understand neural networks by supplying methods that hide all the complex computations that are done inside of a network. I will talk more specifically about what a neural network is, the methods Brain supplies, and how the library simplifies the process of creating a neural network.

Neural Network

A neural network is a machine learning algorithm that uses a series of connected nodes in order to process data and makes decisions. This type of algorithm is inspired by how the human brain makes decisions using neurons that fire information to each other. Networks have three different types of layers, which contain nodes that pass information to and from each other. Functions are run on the input passed into nodes in order to determine whether or not a node should continue to pass along the information it received. You can find a more in depth explanation of neural networks and their nodes here.

Brain.js

Brain.js makes it possible to create neural networks using Javascript, and helps users to understand how neural networks work in the process. The library can be installed using npm.
npm install Brain.js
Once installed, a neural network can be created in the same way a new Javascript class is created:

const network = new brain.NeuralNetwork(options)
Enter fullscreen mode Exit fullscreen mode

brain.NeuralNetwork takes in an optional hash table, which tells the network how to behave.
A few optional properties to supply the network:

  • inputSize: The number of input nodes
  • outputSize: The number of output nodes
  • learningRate: Controls the step size during training iterations: The max number of training sessions in a network
  • activation: The activation function used in network training. Activation Functions
  • hiddenLayers: An array of numbers that specifies the number of hidden layers, and number of nodes on each layer.
const options = {
inputSize: 4,
outputSize: 4,
learningRate: 0.3
activationFunction: 'sigmoid',
hiddenLayers: [3, 4]
}
Enter fullscreen mode Exit fullscreen mode

The properties have default values if they are not defined.

Training a Network

Networks have a train() method to train a network what an output should be when given a certain input. train() takes in an array of data for the network to learn from. Each training pattern in the array has an input and an output property. Input and output can be either an array of numbers 0 through 1, or a hash table of numbers 0 through 1.
Let's look at an example that trains a network to output either 1 or 0 based on an input array.

const network = new brain.NeuralNetwork(options);
network.train([
  {input: [0,0], output: [0] },
  {input: [0,1], output: [1] },
  {input: [1,0], output: [1] },
  {input: [1,1], output: [0] }
])
Enter fullscreen mode Exit fullscreen mode

The input property tells the network what is being supplied. The output property tells the network what the output should be when given the corresponding input.
The output of train() is a hash table containing information of how the training went.

{
  error: 0.0039139985510105032,  // training error
  iterations: 406                // training iterations
}
Enter fullscreen mode Exit fullscreen mode

Brain.js run()

Once a network is trained, the run() method can be used to output a networks prediction when a certain value is passed in. Using the same trained network from above, we can pass in an array to the run method, and the array will output its prediction based on what it learned during the training. The return value of run() is a hash table with properties that correspond to the possible outputs. The values of these properties are a number that represent how likely it is that that property is the output that should be supplied with the current input.

let result = network.run([1,0]); 
/* result will be: 
{"zero":0.06577067077159882,"one":0.9343253374099731}
*/
Enter fullscreen mode Exit fullscreen mode

The network tells us that there is a 93% chance that the output of the input [1,0] is 1. We are also able to use object property access on the result just like we would with a normal Javascript object.

console.log(result["zero"]; // 0.06577067077159882
console.log(result["one"]; // 0.9343253374099731
Enter fullscreen mode Exit fullscreen mode

Other NeuralNetwork Methods

network.forecast(input, count)

Returns an array of predictions for the input. Count is how many predictions are given.

network.toJSON()

Returns the network converted into JSON format.The network can then be saved to a file or moved into a database.

network.fromJSON()

Unstringifies a network so that it can be used for predictions.

Additional Information

This blog only mentions the very basic capabilities of the Brain.js library. Brain.js is capable of creating different types of neural networks (RNNTimeStep, LSMTimestep, GRUTimeout) which have unique behaviors. The library also allows a network to be trained asynchronously with certain network types by using trainAsync() in place of train(). If you find this post interesting, I encourage you to look at the Brain.js documentation, provided in the sources below, to get a deeper understanding of the library's capabilities.

Conclusion

Brain.js simplifies the complex topic of neural networks and machine learning. Networks are able to be created using the 'new' keyword, and then can be interacted with using methods supplied by the library. By hiding all the complexities of neural networks behind the scenes, a user can more easily understand how the complex algorithms work.

Sources:

Brain.js Documentation
Medium
W3 Schools

Top comments (0)