forked from lnug/AIJavascript-meetup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainNet.js
51 lines (37 loc) · 1.22 KB
/
trainNet.js
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
const brain = require('brain.js/index')
const trainingData = require('./multiLabelTrainingData.js')
const fs = require('fs')
/*
Brain.js accepts training data as an array of objects of the shape:
{
input: [],
output: [],
}
This step takes processes our training data into that form to feed into brain.js
*/
const preparedTrainingData = trainingData.map(set => {
return {
input: set.slice(0, 9),
output: set.slice(9)
}
})
console.log('📈 Data prepared\n')
/*
Brain.js takes some config when you instantiate the NeuralNet
more details can be found in the docs https://github.com/brainjs/brain.js
These are the defaults. Maybe they're rubbish defaults...
*/
const config = {
binaryThresh: 0.5,
hiddenLayers: [3],
activation: 'sigmoid'
};
const net = new brain.NeuralNetwork(config);
console.log('🏃♀️ Start training - this could take some time...\n')
net.train(preparedTrainingData);
/*
We can output our trained Neural net as either a function or json and write it to a file.
*/
fs.writeFileSync('trainedNet.js', `export default ${ net.toFunction().toString() };`);
console.log('🏁 Training finished - model created\n')
process.exit(1);