-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainTst.js
89 lines (81 loc) · 2.23 KB
/
mainTst.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
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
79
80
81
82
83
84
85
86
87
88
89
var json;
var dataset = [];
var net = new brain.recurrent.LSTM();
if (json) {
net.fromJSON(json);
}
const trainStream = new brain.TrainStream({
neuralNetwork: net,
/**
* Write training data to the stream. Called on each training iteration.
*/
floodCallback: function() {
readInputs(trainStream, dataset);
},
/**
* Called when the network is done training.
*/
doneTrainingCallback: function(obj) {
document.getElementById("trans").addEventListener("click", function () {
console.log(net.toFunction());
console.log(net.toJSON());
var input = document.getElementById("tr").value;
var output = net.run(input);
document.getElementById("out").innerHTML = input+output;
console.log(output);
});
}
});
// kick it off
//readInputs(trainStream, xor);
var xsy = 0;
function readInputs(stream, data) {
var x = function () {
stream.write(data[xsy].toString());
xsy++;
if (xsy == data.length) {
stream.endInputs();
}
console.log(Math.floor((xsy/data.length)*100)+"% Done");
window.requestAnimationFrame();
};
x();
}
var iter = 0;
var load = function (d) {
d = d.toString().toLowerCase();
console.log("Finding sentences...");
dataset = d.toString().split("\t").join(" ").toString().split("\n");
console.log("Loaded dataset...");
console.log(dataset);
dataset.sort();
if (!brain) {
console.log("Missing dependencie 'brain.js'");
return;
}
document.getElementById("train").addEventListener("click", function () {
console.log("Training...");
/*var x = iter;
var y = iter + parseInt(window.prompt("Training Size:"));
iter = y;
net.train(dataset.slice(x, y), {
iterations: 300,
errorThresh: 0.02,
log: true,
logPeriod: 1,
learningRate: 0.2,
momentum: 0.7,
callback: null,
callbackPeriod: 10,
timeout: 1000*60,
});*/
readInputs(trainStream, dataset);
});
};
var xhr = new XMLHttpRequest();
xhr.open("GET", "jpn.txt");
xhr.addEventListener("load", function (x) {
console.log("Loaded file...");
load(this.responseText);
});
xhr.send();