We hear a lot about Machine Learning and Artificial Intelligence these days, so I thought it would be cool to look at a really simple example using Javascript.
We're going to use the Brain.js library to create our neural network, it takes care of the heavy lifting for us.
A neural network is an algorithmic construct that's loosely modelled after the human brain. They're built using layers. There's an input layer, an output layer and one or more layers in between to do the processing.
The layers contain the 'neurons'. As the data moves across the layers its value is multiplied by certain weights which determine how much impact each neuron has on the output.
We 'train' our network by adjusting these weights.
We provide our inputs to the input layer, the data from the input layer then travels through the neural network across all of its layers to the output layer, where it gives us the probability of each output being the correct answer.
Our network will approximate the output of the exclusive-or operation (XOR). We'll provide our network with two inputs and the network will predict whether the output should be a 1.
XOR is a binary operation that takes two inputs and produces one output. If the inputs are the same, it'll output a 0 and if the inputs are different it'll output a 1.
Below is the truth table for XOR.
A | B | Output -------------- 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 0
const brain = require('brain.js') var net = new brain.NeuralNetwork() var training_data = [ {input: [0, 0], output: [0]}, {input: [0, 1], output: [1]}, {input: [1, 0], output: [1]}, {input: [1, 1], output: [0]} ] net.train(training_data) console.log(net.run([0, 0])) console.log(net.run([1, 0]))
new brain.NeuralNetwork()
net.run()
When we run our code we get the following outputs:
Input: [0, 0] Output: [ 0.05875255912542343 ] Input: [1, 0] Output: [ 0.9338177442550659 ]
You can see our network is working correctly. When our inputs are 0,0 the output is 0.058 which means the probability the output is a 1 is almost 0, which is correct because the correct answer is 0.
And when our inputs are 1,0 the output probability is 93% which means the network is almost certain that the correct answer is 1.
That's a simple look at neural networks! Hopefully, you learned a lot!