Machine Learning using JavaScript
What is Machine Learning?
Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it learn for themselves.
Why JavaScript?
JavaScript is everywhere, from enterprise to personal projects. It is enhanced by super set languages such as Typescript.
JavaScript is third most popular library in GitHub for Machine Learning. (Source: TechRepublic)
List of JavaScript Libraries for Machine Learning:
-> TensorFlow.js -> Brain.js -> Stdlib-js -> Machinelearn.js -> Math.js -> Face-api.js -> R-js -> Natural
As part of this Article, We will explore TensorFlow.js
What is TensorFlow.js ?
TensorFlow.js is a library for developing and training ML models in JavaScript, and deploying in browser or on Node.js
Basically there are four steps in TensorFlow.js:
1. Create Model
2. Define Input/output Tensor with Sample Data
3. Train Model
4. Prediction
Let's take simple use case to understand TensorFlow and Machine Learning
Use Case
There was a triangular series between 3 teams called as Team A, Team B and Team C. All three teams played n Number of matches with each other and we have result of it.
Now we need to create and train model which can predict match result based on selected team.
For example If Team A play against Team B, who will win the match ?
So let's begin our journey of learning with Machine Learning..!!
Step 1: Create Model
In machine learning, a model is a function with learnable parameters that maps an input to an output. The optimal parameters are obtained by training the model on data. A well-trained model will provide an accurate mapping from the input to the desired output.
In TensorFlow.js there are two ways to create a machine learning model:
- Using the Layers API where you build a model using layers.
- Using the Core API with lower-level ops such as tf.matMul(), tf.add(), etc
We will Use Layer API in our use case
The most common type of model is the Sequential model, which is a linear stack of layers.
Let's create our Model:
const model = tf.sequential();
Now we will add 3 layers to our model as below:
model.add(tf.layers.dense({ units: 100, inputShape: [2], activation: 'sigmoid' }));
model.add(tf.layers.dense({ units: 10, activation: 'sigmoid' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
Let's understand what we are doing here:
We are starting with 100 units in layer 1 and in layer 3 we have 3 units since our winning team is out of 3(Team A, Team B or Team C).
Input shape is 2 since at any point of time... only two team going to play (Team A vs Team B or Team B vs Team C or Team A vs Team B).
Last parameter is activation: We are using sigmoid in first two layer for binary classification and softmax in last layer since we need guaranteed output/Winner(probability sum of 1).
Now we need to compile our model to make it ready. We will use "categoricalCrossentropy" as loss since we have categorical data along with "adam" as optimizer.
model.compile({ loss: 'categoricalCrossentropy', optimizer: 'adam' });
Our Neural Networks Architecture:
Step 2: Define Input/Output Tensor with Sample Data
Let's give unique number to each of our team: Team A = 0, Team B = 1 and Team C = 2
We have now input and output data with two rounds of round robin matches between Teams.
// [0,1] => Team A vs Team B
// [1,2] => Team B vs Team C
// [0,2] => Team A vs Team C
const inputTensor = tf.tensor([[0, 1], [1, 2], [0, 2], [0, 1], [1, 2], [0, 2]]);
//Output Data- winner
const outputTensor = tf.oneHot(tf.tensor1d([0, 1, 2, 0, 1, 2), 'int32'),3);
For output we are using oneHot method which can convert output to a dense one-hot representation. Last digit represent number of layers/dense in our model.
Step 3: Train Model
At this point we have our model and sample data ready, let's train our model
model.fit(inputTensor, outputTensor, { epochs: 500 })
.then(loss => {
// TODO: Model is ready for prediction.
})
.catch(e => {
console.log(e.message);
});
Here an epoch is a full iteration over samples. The number of epochs is how many times the algorithm is going to run.
Step 4: Prediction
Now we have Machine Learning Model is ready and trained. Let's write Prediction function.
const predictTs = tf.tensor([[0, 1]]); // Input for prediction. for example Team A vs Team B([0, 1])
function prediction(predictTs) {
const t = model.predict(predictTs);
const pred = t.argMax(1).dataSync(); // get the class of highest probability
const winner = Array.from(pred).map(e => setLabel[e]);
console.log(winner); //Winner of Team A vs Team B Match
}
}
Here we will get output in winner variable.
Live Demo Application
You can experiment live in demo application which i developed using Angular 8 and TensorFlow.js
Here you can see sample data in form of pie chart, add more data and select different matches to see prediction using above design
Source Code
You can also access source code of demo application here