Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Brain.js Tutorial to Train Neural Networks with Data in Node.js Using Javascript

Posted on November 20, 2022

 

 

Welcome folks today in this blog post we will be using the brain.js library in node.js to train the neural networks with data using javascript. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

In order to get started you need to initialize a new node.js project by using the below command as shown below

 

 

npm init -y

 

 

Now we we need to install the brain.js library using the below command as shown below

 

 

npm i brain.js

 

 

This will install this neural network library for machine learning in node.js. Now we need to make the index.js file inside the root directory as shown below

 

 

index.js

 

 

First of all guys we will be training the neural network using the numbers such as 0 and 1. For this we will be first of all importing the brain.js library as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const brain = require('brain.js')
 
// make a neural network
 
const network = new brain.NeuralNetwork()
 
// train the neural network
 
network.train([
    {
        input:[0,0,0],output:[0]
    },
    {
        input:[0,0,1],output:[0]
    },
    {
        input:[0,1,1],output:[0]
    },
    {
        input:[1,0,1],output:[1]
    },
    {
        input:[1,1,1],output:[1]
    }
])
 
const output = network.run([1,1,0])
 
console.log(output)

 

 

As you can see we are importing the library and then we are making the new neural network using the brain.neutralNetwork() method. And then we are training the neural network  using the train() method. Here in this method we are providing the series of array of objects which contain two properties which is the input and output. Input and output is required for the neural network to train and based upon that data it will give you the prediction. Now as you can see we are providing the series of numbers of inputs (combination of 0 and 1) and then based upon that we are having the output. The logic is that whatever is the first number of the array will be the output.

 

And after that training set is there we are now asking the neural network the question using the run() method. And inside this we are passing the array of values. Based upon the pattern in the input data set it will give you the output.

 

 

 

 

As you can see it is giving us the value which is nearer to 1. Because of the pattern of the input data set the output should be 1. Now the neural network is correctly predicting the output based upon the input data provided.

 

 

Providing JSON Array of Objects

 

 

Now guys we will be taking the example where we need to submit the string data to train the neural networks. For this we will store the information in the separate file called data.json and inside it we will be storing the array of objects. Each object will contain two properties text and category. Based upon the text we will have the category as shown below

 

Just make the data.json file inside the root directory and copy paste the below code

 

 

data.json

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
[
  {
    "text": "my unit test failed",
    "category": "software"
  },
  {
    "text": "tried the program, but it was buggy",
    "category": "software"
  },
  {
    "text": "i need a new power supply",
    "category": "hardware"
  },
  {
    "text": "the drive has a 2TB capacity",
    "category": "hardware"
  },
  {
    "text": "unit-tests",
    "category": "software"
  },
  {
    "text": "program",
    "category": "software"
  },
  {
    "text": "power supply",
    "category": "hardware"
  },
  {
    "text": "drive",
    "category": "hardware"
  },
  {
    "text": "it needs more memory",
    "category": "hardware"
  },
  {
    "text": "code",
    "category": "software"
  },
  {
    "text": "i found some bugs in the code",
    "category": "software"
  },
  {
    "text": "i swapped the memory",
    "category": "hardware"
  },
  {
    "text": "i tested the code",
    "category": "software"
  },
  {
    "text": "i love computers",
    "category": "hardware"
  },
  {
    "text": "programming",
    "category": "software"
  },
  {
    "text": "i broke my pc",
    "category": "hardware"
  },
  {
    "text": "computer",
    "category": "hardware"
  },
  {
      "text": "buy me a laptop",
      "category": "hardware"
  },
  {
      "text": "i love to play games",
      "category": "software"
  }
]

 

 

And now we will import this data and train this time the recurrent neural network which will be the LSTM (which is long short term memory) neural network.

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const brain = require('brain.js');
const data = require('./data.json');
 
const network = new brain.recurrent.LSTM();
 
const trainingData = data.map(item => ({
  input: item.text,
  output: item.category
}));
 
network.train(trainingData, {
  iterations: 2000
});
 
const output = network.run('The code has some bugs');
 
console.log(`Category: ${output}`);

 

 

As you can see when we are training this neural network with the string data we are providing the iterations parameter in the second argument.

 

 

Recent Posts

  • Angular 14/15 JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • Build a JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • React-Admin Example to Create CRUD REST API Using JSON-Server Library in Browser Using Javascript
  • Javascript Papaparse Example to Parse CSV Files and Export to JSON File and Download it as Attachment
  • Javascript Select2.js Example to Display Single & Multi-Select Dropdown & Fetch Remote Data Using Ajax in Dropdown
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme