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.
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:
|
|
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.
This is the code to initialize the neural network:
|
|
inputs
is the number of inputs, our x
of the equation.outputs
is the number of ouputs, our y
of the equation.task
is the type of machine learning task we want our model to perform, in this case a regression
.debug
is a boolean value that indicates whether or not we want to see some logs and messages about
the training process in the console.layers
is an array of objects that defines the structure and configuration of each layer in our
neural network. Each object can have properties such as:
type
is the type of layer. For example:
'dense'
means a fully connected layer where each node receives inputs from all nodes in
the previous layer.'convolutional'
means a layer that applies filters on local regions of inputs (usually used
for image processing).'pooling'
means a layer that reduces the size of inputs by applying some aggregation function (e.g. max or average) on local regions (usually used for image processing).units
is the number of nodes in the layer.activation
is the activation function applied by each node in the layer.
'linear'
means no activation function (the input and the output are equal).'sigmoid'
means an S-shaped function that maps any input value between 0 and 1.learningRate
is the rate at which our model updates its weights during the training. A higher learning rate means faster learning but also more risk of overshooting the optimal solution. A lower learning rate means slower learning but also more precision.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:
|
|
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:
|
|
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:
|
|
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.
|
|
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:
|
|
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:
|
|
After running the neural network I get 20.7562153339386
as a result, which is very close to 21.
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