forked from BrainJS/brain.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-example.js
41 lines (35 loc) · 966 Bytes
/
stream-example.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
var assert = require('assert');
var brain = require('../dist/brain');
var net = new brain.NeuralNetwork();
var xor = [
{ input: [0, 0], output: [0]},
{ input: [0, 1], output: [1]},
{ input: [1, 0], output: [1]},
{ input: [1, 1], output: [0]}
];
var trainStream = net.createTrainStream({
/**
* Write training data to the stream. Called on each training iteration.
*/
floodCallback: function() {
flood(trainStream, xor);
},
/**
* Called when the network is done training.
*/
doneTrainingCallback: function(obj) {
console.log('trained in ' + obj.iterations + ' iterations with error: '
+ obj.error);
var result = net.run([0, 1]);
console.log('0 XOR 1: ', result); // 0.987
}
});
// kick it off
flood(trainStream, xor);
function flood(stream, data) {
for (var i = 0; i < data.length; i++) {
stream.write(data[i]);
}
// let it know we've reached the end of the data
stream.write(null);
}