forked from harthur/brain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-example.js
43 lines (34 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
42
43
var assert = require("assert"),
brain = require("./lib/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);
}