Machine Learning in Javascript

There’s a good chance that you don’t think of JavaScript when you think of artificial intelligence and machine learning. Instead, you might think of Python and popular libraries like Google’s Tensorflow. While TensorFlow is very powerful and complex, it does require a steep learning curve. Personally, I’m just looking to get my feet wet in some simple AI programming. However, I came across a javascript library called Brain.js. This library makes machine learning in javascript plausible and testable.It’s very easy to get started and works within your browser (not recommended though) and NodeJS.

Start Node Project

Let’s start by initializing a new node project. Assuming you already have NodeJS installed, type the following in the terminal:

npm init

After going through the package setup, begin by installing the Brain.js module:

npm install brain.js --save

Make sure to include the save parameter so that it is listed in your devDependencies. Awesome, now create an index.js file in your project and you are ready to begin coding.

Javascript Artificial Intelligence

In your index.js file, begin by importing the brain.js module:

const brain = require('brain.js)

Now we need to create a network variable. Brain.js allows you to choose between a feedforward network and recurrent network. For this tutorial, we are just going to use a feedforward network. By default, it initializes with one hidden layer. You could specify more however that is not needed for this project.

var net = new brain.NeuralNetwork()

Collecting Data

Before we dive into coding, we need to setup some common understanding. So let’s pretend that we all agree that a viral video has the following characteristics: 100k Views, 10K Comments, and 9500 Likes – all within a 24 hour period. For simplicity, we are going to label this as maximum possible input we expect to get. Likewise, we are going to say a boring video gets 0 views, 0 comments, and 0 likes. This is going to be the least possible input value we can expect. Now we can also setup different classifications for more viral videos. We could also add that 90k views, 7700 comments, and 3k likes also count as a viral video. Likewise, we could add another data point that says 100 views, 10 comments and 2 likes also count as a boring video.

Neural Network Training

The next thing you need to do is feed data to your network. This data should consist of inputs along with their ideal outputs. However, neural networks are very picky when it comes to their accepted data input. You can only input and output data that is an array of values consisting of the numbers between 0 and 1 (or -1 and 1 in some cases, but not Brain.js).

An acceptable input and output could match one of these examples:

input: [0], output: [1]

input:[0.5], output: [0.7]

input: {r: 1, b:0}, output: {r:0.5,b:0.2}

If we were working with strings, then we would need to find a way to encode the strings so that they satisfy the input/output requirements.

So what I need to do is normalize my data (the number of likes, views and comments).  Essentially, I want the following:

input:{views: 100000, likes: 10000, comments: 95000}, output: {popular: 1}

Unfortunately, this does not satisfy the requirement and you will receive an error if you try to use those values as inputs. Therefore, we need to normalize the data. The best way to do this is put these numbers are percentages. So let’s a video received 25K views. Therefore, it received 0.25  (25000/100000) views. Got it? Good! Let’s continue coding.

Create an array that will hold objects of training data. I use the output of h for popular and b for boring :

var data = [{ input: { v: 1.0, l: 1.0, c: 1.0 }, output: { h: 1 } },
{ input: { v: 0.8, l: 0.75, c: 0.58 }, output: { h: 1 } }, //80k views, 7500 comments 5500 likes 
{ input: { v: 0.85, l: 0.9, c: 0.7 }, output: { h: 1 } },
{ input: { v: 0.92, l: 0.83, c: 0.79 }, output: { h: 1 } },
{ input: { v: 0.0, l: 0.0, c: 0.0 }, output: { b: 1 } },
{ input: { v: 0.2, l: 0.34, c: 0.3 }, output: { b: 1 } },
{ input: { v: 0.5, l: 0.1, c: 0.1 }, output: { b: 1 } },
{ input: { v: 0.3, l: 0.3, c: 0.1 }, output: { b: 1 } },
]

Now that you have the array, let’s send it to the network:

net.train(data, { log: true });

In the previous line, we sent the data array as the data for the network to train with. We also enabled logging so that we can see the output as it’s iterating.

Finally, let’s test the network. I want it to predict whether a video with 25k views, 1k likes and 5k comments is popular.

console.log(net.run({{ v: 0.15, l: 0.1, c: 0.35 )

The output I got is 95% boring and 5% hit. Therefore, this video is most likely going to be a boring video.

You can view the full code on GitHub