Getting started with Brain.js

Original article can be found here (source): Artificial Intelligence on Medium

Getting started with Brain.js

What is it?

Brain.js is a library for neural networks. Neural networks are computer algorithms designed to work like animal brains. This can be the first step into real machine learning and artificial intelligence. You should definitely check out how these networks actually work to gather a better understanding of this powerful tool we now have, but you don’t need to fully understand to get started using them. In this article i’m going to show you a quick example of building neural network and training it in brain.js.

Setup

All you are going to need for this example is NodeJS and the Brain.js package. You can install the package using:

npm install brain.js

next just setup a .js file and we can start by requiring that package we installed so something like this:

const brain = require('brain.js');

Building and Training

Next we need to actually make a new neural network.

const network = new brain.NeuralNetwork();

this makes a new neural network for us to start training with our data sets. Training a neural network, in case this is A unfamiliar term is giving the network data as well as the “answers” that we want from that particular data set. So for example if we wanted to teach the network to tell me if a sentence was a question or a statement I could give it my data so “What is your name?” followed by the answer “question”. Simple enough but if this still does not make sense the training data should clear it up.

To trains we use the method train so it looks like this:

network.train([])

Here we can start adding our training data. For this example i’m going to give the network 3 numbers in an array, the answer of these 3 numbers is going to be the first number. No where do we specify that we are looking for first number we expect the network to figure this out through that training data.

So here is our training method with the data:

network.train([
{ input: [0, 1, 0], output: [0] },
{ input: [0, 1, 1], output: [0] },
{ input: [0, 0, 1], output: [0] },
{ input: [1, 1, 0], output: [1] },
{ input: [1, 0, 0], output: [1] },
{ input: [1, 0, 1], output: [1] },
{ input: [1, 1, 1], output: [1] },
])

I purposely left out the [0, 0, 0] combination because this is the one we are going to through into the network to test. To run our network lets use the run method along with a console.log to see our results.

const output = network.run([0, 0, 0]);console.log(`Prob: ${output}`);

When we run this we get

Prob: 0.1834053248167038

we can think of this is a probability it is right from 0 to 1 so its way closer to 0 then to one because of our training data if we put in [1, 0 ,0] instead of [0, 0, 0] we get:

Prob: 0.9575356841087341

So its almost at 1 this time if we add more training data that number gets better and better and if we remove training data it gets less accurate.

Conclusion

I hope to make more complex examples of neural networks in future blogs. This was a pretty simple and boring example of a neural network but I think it shows the real potential in this technology the thing is learning!