Neural Network with Brain.js

Hello, It’s Javascript

Himanshu Prajapati
3 min readAug 22, 2020

--

There are lots of programming languages, and of course, you can do anything in those languages. But javascript shares more value when it comes to popularity, flexibility, and easy to learn.

It is a language that browsers can understand and the only language for the WEB. It is lightweight and interpreted.

Initially, javascript was supposed to run on the server but it gets more popularity when integrated to the browser. Now, it is again become a part of the server-side, thanks to some run time environments like NodeJS.

But its power not remain there. We can do many more things with Javascript. We can create large web applications, simple 2D graphs to the high-end 3D graphics, 3D games, artificial intelligence applications, just to point a few.

Today we will create a simple neural network experiment by using a great neural network library Brain.js.

Brain.js

Brain.js is a GPU accelerated library of neural networks written in Javascript for browsers and run time environments. It learns the patterns and relationships between the data provided in order to make predictions.

Neural Network

A neural network is a mathematical model that mimics the behavior of biological neural networks.

Here we are not going in a deep understanding of the theoretical concepts of the AI or Javascript.

So let’s start

Today we are building a simple neural network experiment that predicts the time when input meal is provided.

You just need to be familiar with javascript to follow this article:

create NodeJS project

npm init

Provide all the information or you can skip those

Install Brain.js

npm install brain.js

create index.js file

add the following code

import the Brain.js library

let brain = require(“brain.js”);

create a neural network

let neuralNetwork = new brain.NeuralNetwork();

Our sample data to train this is a network contains foods: milk, tea, bread, chapati, curd, rice, veggies, and there is four possible output: morning, afternoon, evening and night.

Here we are using only four sample inputs for training which is very less to get high accuracy, but you can add as many as you want. This is just to implement the concept. The value 1 means we have that and 0 means we haven’t, the output is the time. You can add as many samples as you want to improve the high accuracy.

Training Data

train the neural network

neuralNetwork.train(trainingData);

Now our network is trained with the data. Let’s provide input for prediction.

run neural network

The output is

predicted output

Here the most probable output is evening. The value is very low at accuracy because we have very little data for prediction. For demonstration purposes, it’s OK to have fewer data.

So we have just done with this experiment in javascript.

It’s very easy, Right!

You just created an awesome and simple neural network experiment to predict the time based on the meal.

You can find the full source code on Github

I hope you enjoyed it.

--

--