Simple AI

In this tutorial we’re going to learn how to create an AI, more precisely a neural network which is a type of machine learning model that consists of layers of interconnected nodes (or neurons) that can learn from data and perform complex tasks.
The scope of this first AI is to solve a simple equation y=2x+3, for doing so we will provide it with some training data. In this case, the data are some x values and their y calculated values. If done correctly, the AI will learn what is the relationship from the input and the output and it will be able to calculate the y of new values that have not been given to it during the training phase.

Setup ml5js

For this tutorial we’re going to use ml5js a friendly machine learning library for the web.
To use it we just need to add:

1
<script src="https://unpkg.com/ml5@latest/dist/ml5.min.js"></script>

And we’re ready to go.
For the next part you can choose to use another file or just add the code inside a ‘script’ tag.

Let’s create the Neural network

This is the code to initialize the neural network:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Create a neural network object with custom options
let nn = ml5.neuralNetwork({
  inputs: 1,
  outputs: 1,
  task: 'regression',
  debug: true,
  layers: [
    {
      type: 'dense',
      units: 1,
      activation: 'linear'
    }
  ],
  learningRate: 0.25
});

How to add training data

In order to train our neural network, we need some data points that represent the input-output relationship we want our model to learn. For example, if we want our model to learn a simple linear equation such as y = 2x + 3, we can create some data points that follow this equation.

To add data points to our neural network object, we need to use the nn.addData() function and pass two objects as arguments that specify the input and output values for each data point. For example, if we want to add a data point where x = 1 and y = 5, we can write:

1
2
// Add a data point
nn.addData({x: 1}, {y: 5});

We can repeat this process for as many data points as we want. Alternatively, we can use a for loop to generate and add data points dynamically based on some logic. For example, if we want to create and add 20 data points where x ranges from 0 to 19 and y follows the equation y = 2x +3, we can write:

1
2
3
4
5
6
7
for (let i = 0; i < 20; i++) {
  // Calculate x and y values based on the equation
  let x = i;
  let y = 2 * x + 3;
  // Add the data point to the neural network
  nn.addData({x: x}, {y: y});
}

How to normalize and train the model

Before training our model, it is a good practice to normalize our data so that all input and output values are scaled between -1 and +1. This helps our model learn faster and more accurately by reducing numerical instability². To normalize our data, we need to use the nn.normalizeData() function:

1
2
// Normalize the data
nn.normalizeData();

To train our model, we need to use the nn.train() function and pass two arguments, an object that specifies some options for training and a callback function that runs when the training is done.

1
2
nn.train({ epochs: 50 }, finishedTraining);

For this tutorial we set the epochs to 50, meaning that our model goes through all the training data while training for 50 times.
We also set the callback function that runs when the training is done. In this function we can make predictions with our trained model using the nn.predict() function. For example, if we want to predict the output value for an input value of x = 9, we can write:

1
2
3
4
5
// Define a callback function for when the training is done
function finishedTraining() {
  // Make predictions with the trained model
  nn.predict({x: 9}, Results);
}

We also need to define another callback function that runs when the prediction is done. In this function we can access and use the results of our prediction. For example, if we want to log the predicted output value to the console, we can write:

1
2
3
4
5
6
7
8
9
// Define a callback function for when the prediction is done
function Results(error, results) {
  if (error) {
    console.error(error);
    return;
  }
  // Log the results
  console.log(results[0].value);
}

After running the neural network I get 20.7562153339386 as a result, which is very close to 21.

Conclusion

In this tutorial, we learned how to create and train a neural network with ml5.js. We saw how to use different options and functions to configure our model, add data points, normalize data, train our model, and make predictions.

I hope you found this tutorial helpful!
If you have any questions or feedback, please let me know on Twitter! @CodeSailer