-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (41 loc) · 1.04 KB
/
index.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
"use strict";
var fs = require("fs-extra");
var eos = require("then-eos");
var ndjson = require("ndjson");
var brain = require("brain.js");
var NeuralNetwork = brain.NeuralNetwork;
module.exports = {
load: load,
save: save,
run: run,
train: train,
};
function load(location) {
if(!location)
return Promise.resolve(new NeuralNetwork());
return fs.readFile(location, "utf8").then(function(json){
var parsed = JSON.parse(json);
var net = new NeuralNetwork();
net.fromJSON(parsed);
return net;
});
}
function save(location, net) {
var data = net.toJSON();
var json = JSON.stringify(data);
return fs.writeFile(location, json, "utf8");
}
function run(net, data) {
return fs.readFile(data).then(function(json){
var parsed = JSON.parse(json);
return net.run(parsed);
});
}
function train(location, net) {
var inputStream = fs.createReadStream(location, "utf8");
var parsed = inputStream.pipe(ndjson.parse());
var trainStream = net.createTrainStream();
return eos(parsed.pipe(trainStream)).then(function () {
return net;
});
}