Getting started with Brain.js

Gordon Styron
Mar 16 · 3 min read
Image for post
Image for post

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.


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 train we use the method train so it looks like this:


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:

{ 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 =[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.


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!

Analytics Vidhya

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Gordon Styron

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem

Gordon Styron

Written by

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store