DEV Community

Cover image for Brain.js for Beginners
gfish94
gfish94

Posted on • Updated on

Brain.js for Beginners

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();

Enter fullscreen mode Exit fullscreen mode

Training

Image description

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] },
]);

Enter fullscreen mode Exit fullscreen mode

Run

Image description

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
Enter fullscreen mode Exit fullscreen mode

Saving your Trained Network

Image description

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');
Enter fullscreen mode Exit fullscreen mode
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);
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
citronbrick profile image
CitronBrick

You can implement syntax highlighting in this site using

(triple backtick) javascript
code
(triple backtick)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

```js
// code goes here
```

// code goes here
const example = {
  something: 'asdf',
  else: 8094,
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
citronbrick profile image
CitronBrick

Really surprised by the number of likes!!! Thank you all.

Collapse
 
wraith profile image
Jake Lundberg

This is pretty awesome, thank for sharing!

Collapse
 
maximprogramerxx profile image
Maximprogramerxx

intyresting post

Collapse
 
dshaw0004 profile image
Dipankar Shaw

So I can create logistic regression model using it.
Nice..

Collapse
 
cantillojo54982 profile image
Joiner David Cantillo Camargo

Thanks. Really great experience

Collapse
 
techliondev profile image
TechLionDev

Anyway to train a chat bot with this?

Collapse
 
bradley39e profile image
Bradley39e

This works with react hopefully 🤞🙏.

Collapse
 
silverium profile image
Soldeplata Saketos

JavaScript code usually works with React :D :D :D

Collapse
 
darioprazeres profile image
Dário Prazeres

intyresting post

Collapse
 
enshunt profile image
Comficker

essy to understand

Collapse
 
artxe2 profile image
Yeom suyun

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?

Collapse
 
munichdeveloper profile image
Johannes Hinkov

Hi this is a very interesting post, thanks!
Could i load for example weather data into brain.js or does it need the input to be a vector?

Collapse
 
saswat_design profile image
Saswat
Collapse
 
bhagatharsh profile image
BhagatHarsh

Very interesting!!