Brain.js
Brain.js is a JavaScript library used to create neural networks that is very easy to learn and implement. Neural networks are a data structure inspired by and named after the human brain. Neural networks are comprised of layers of nodes each with associated weight thresholds. If a nodes output is above the threshold then that neuron is activated and the data is sent to the connected node in the next layer. In Brain.js the process of initializing this data structure is as simple as loading in the module and calling a constructor function.
const brain = require('brain.js');
//initialize neural network
const net = new brain.NeuralNetwork();
Training
This newly constructed object needs to be given a dataset in order to make predictions. Brain.js makes the process very simple all you need to do is call the .train
method and pass in an array of objects -- each with key value pairs of inputs and outputs. The basic network constructor only supports an array of 0s and 1s, or a hash of 0s and 1s; however the are other more complex models that allow for other datatypes to be entered in the dataset.
//input training dataset
net.train([
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] },
]);
Run
Now that you have input a dataset and trained your artificial brain you need to call the .run
method on your neural network. This will return an array with decimal value corresponding to the probability that your expected output is equal to 1. The higher the decimal point value the more likely the neural network believes the output should be a 1.
//outputs a probability
const output1 = net.run([1, 0]);// [ 0.9327429533004761 ]
const output2 = net.run([0, 0]);//) [ 0.05645085498690605 ]
const output3 = net.run([1, 1]);// [ 0.08839469403028488 ]
console.log('output 1:', output1);
console.log('output 2:', output2);
console.log('output 3:', output3);
console.log(Math.round(output1[0]));// => 1
console.log(Math.round(output2[0]));// => 0
console.log(Math.round(output3[0]));// => 0
Saving your Trained Network
The networks ability to predict the expected output is made more accurate by supplying a larger dataset; however with a larger dataset or if you are implementing one of the more complex network models --which require iterating over your dataset a set number of time in order to train -- the amount of time the code takes to execute also grows. This can be circumvented by utilizing brain.js's .toJSON
and .fromJSON
to store the pretrained network to a JSON. Here is an example of how to use node.js's file system module to save and load a network.
const brain = require('brain.js');
const data = require('./data.json');
const fs = require('fs');
//init neural network
const net = new brain.recurrent.LSTM();
const trainingData = data.map(item => ({
input: item.message,
output: item.response
}));
//input training data and configuration object
net.train(trainingData, {
log: (err) => console.log(err),
iterations: 500
});
//save trained network to json
const networkState = net.toJSON();
fs.writeFileSync('network_state.json',
JSON.stringify(networkState),
'utf-8');
const brain = require('brain.js');
const fs = require('fs');
//init neural network
const net = new brain.recurrent.LSTM();
//load trained network to json
const networkState = JSON.parse(
fs.readFileSync('network_state.json',
'utf-8'));
net.fromJSON(networkState);
Conclusion
Brain.js is a fun and simple way to learn and implement neural networks in JavaScript. I hope that this inspires more people to go out and implement machine learning algorithms in JavaScript.
Top comments (16)
You can implement syntax highlighting in this site using
(triple backtick) javascript
code
(triple backtick)
```js
// code goes here
```
Really surprised by the number of likes!!! Thank you all.
This is pretty awesome, thank for sharing!
intyresting post
So I can create logistic regression model using it.
Nice..
Thanks. Really great experience
Anyway to train a chat bot with this?
This works with react hopefully 🤞🙏.
JavaScript code usually works with React :D :D :D
intyresting post
essy to understand
I have heard of machine learning technologies in bits and pieces, but they are unfamiliar to me in detail.
What are the differences between TensorFlow and Brain.js?