-
Notifications
You must be signed in to change notification settings - Fork 13
/
train.js
261 lines (208 loc) · 7.33 KB
/
train.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var fs = require('fs');
var nj = require('numjs');
var syllable = require("./syllable-breaker");
var training_data = require('./data.js');
// stop words - List of words (syllable) that are
// too common, noisy and less important
var ignore_words = require('./ignore');
console.log('------');
console.log(training_data.length + " sentences in training data");
console.log('------');
/* === Training Data Preparation === */
var words = [];
var classes = [];
var documents = [];
for(var i in training_data) {
var pattern = training_data[i];
var w = tokenize(pattern['sentence']);
for(var ii in w) {
words.push(w[ii]);
}
documents.push([w, pattern['class']]);
if(!classes.includes(pattern['class'])) {
classes.push(pattern['class']);
}
}
// filter ignore words and duplicates
words = words.filter(function(w, i, self) {
return !ignore_words.includes(w) && self.indexOf(w) === i;
});
// removing duplicates
classes = classes.filter(function(c, i, self) {
return self.indexOf(c) === i;
});
console.log(documents.length + " documents");
console.log(classes.length + " classes");
// console.log(classes);
console.log(words.length + " unique words (syllables)");
// console.log(words);
console.log('------');
var training = [];
var output = [];
var output_empty = [];
for(var i in classes) {
output_empty.push(0);
}
for(var i in documents) {
var doc = documents[i];
var bag = [];
var pattern_words = doc[0];
for(var ii in words) {
if(pattern_words.includes(words[ii])) {
bag.push(1);
} else {
bag.push(0);
}
}
training.push(bag);
var output_row = output_empty.slice(0);
output_row[ classes.indexOf(doc[1]) ] = 1;
output.push(output_row);
}
// console.log("# words " + words.length);
// console.log("# classes " + classes.length);
// var i = 0;
// var w = documents[i][0];
// console.log(w);
// console.log(training[i]);
// console.log(output[i]);
/* === Training Model === */
function train(X, y, hidden_neurons, alpha, epochs, dropout, dropout_percent) {
var start_time = new Date();
var X_arr = X.tolist();
console.log("training with " + hidden_neurons + " neurons, alpha: " + alpha);
console.log("input matrix: " + X_arr.length + "x" + X_arr[0].length);
console.log("output matrix: 1x" + classes.length);
console.log('------');
var last_mean_error = 1;
var synapse_0 = nj.array( rand(X_arr[0].length, hidden_neurons) );
var synapse_1 = nj.array( rand(hidden_neurons, classes.length) );
var prev_synapse_0_weight_update = nj.zeros(synapse_0.shape);
var prev_synapse_1_weight_update = nj.zeros(synapse_1.shape);
var synapse_0_direction_count = nj.zeros(synapse_0.shape);
var synapse_1_direction_count = nj.zeros(synapse_1.shape);
for(var j = 0; j < epochs + 1; j++) {
var layer_0 = X;
var layer_1 = nj.sigmoid(nj.dot(layer_0, synapse_0));
if(dropout) {
// I don't understand what this does yet
// layer_1 *= nj.random.binomial([np.ones((len(X),hidden_neurons))], 1-dropout_percent)[0] * (1.0/(1-dropout_percent));
}
var layer_2 = nj.sigmoid(nj.dot(layer_1, synapse_1));
var layer_2_error = y.subtract(layer_2);
if( (j % 10000) == 0 && j > 5000 ) {
// if this 10k iteration's error is greater than
// the last iteration, break out
if (nj.mean(nj.abs(layer_2_error)) < last_mean_error) {
console.log("delta after " + j + " iterations:" + nj.mean(nj.abs(layer_2_error)) );
last_mean_error = nj.mean(nj.abs(layer_2_error));
} else {
console.log ("break:" + nj.mean(nj.abs(layer_2_error)) + ">" + last_mean_error );
break;
}
}
var layer_2_delta = layer_2_error.multiply( curve(layer_2) );
var layer_1_error = layer_2_delta.dot(synapse_1.T);
var layer_1_delta = layer_1_error.multiply( curve(layer_1) );
var synapse_1_weight_update = (layer_1.T.dot(layer_2_delta));
var synapse_0_weight_update = (layer_0.T.dot(layer_1_delta));
if(j > 0) {
synapse_0_direction_count = synapse_0_direction_count.add(
nj.abs(
binary_array(synapse_0_weight_update).subtract(
binary_array(prev_synapse_0_weight_update)
)
)
);
synapse_1_direction_count = synapse_1_direction_count.add(
nj.abs(
binary_array(synapse_1_weight_update).subtract(
binary_array(prev_synapse_1_weight_update)
)
)
);
}
synapse_1 = synapse_1.add( synapse_1_weight_update.multiply(alpha) );
synapse_0 = synapse_0.add( synapse_0_weight_update.multiply(alpha) );
prev_synapse_0_weight_update = synapse_0_weight_update;
prev_synapse_1_weight_update = synapse_1_weight_update;
}
// Saving trained synapses to file
var synapse_file = "synapses.json";
var synapse = JSON.stringify({
'synapse0': synapse_0.tolist(),
'synapse1': synapse_1.tolist(),
'words': words,
'classes': classes
}, null, 4);
fs.writeFileSync(synapse_file, synapse, "utf8");
console.log('------');
console.log("saved synapses to:" + synapse_file);
console.log('------');
// Calculating trining time
var end_time = new Date();
var training_time = (end_time - start_time) / 1000;
if(training_time > 60) {
var min = Math.floor(training_time / 60);
var sec = Math.floor(training_time % 60);
console.log("completed in: " + min + " minutes " + sec + " seconds");
} else {
console.log("completed in: " + training_time + " seconds");
}
console.log('------');
}
/* === Math and Helper functions === */
// Break sentence to syllable and remove whitespaces
function tokenize(sentence) {
var result = syllable(sentence).filter(function(item) {
return !!item.trim();
});
return result;
}
// NumJs come with sigmoid function.
// But no sigmoid derivative. This is it.
function curve(nums) {
nums = nums.tolist();
var result = [];
for(var i = 0; i < nums.length; i++) {
result[i] = [];
for(var ii=0; ii<nums[i].length; ii++) {
result[i][ii] = nums[i][ii] * (1 - nums[i][ii]);
}
}
return nj.array(result);
}
// Random number between 1 and -1
// return as 2D array
function rand(rows, cols) {
var result = [];
for(var i=0; i<rows; i++) {
result[i] = [];
for(var ii=0; ii<cols; ii++) {
result[i][ii] = 2 * Math.random() - 1;
}
}
return result;
}
// Return 2D array with 1 for positive and 0 for negative
function binary_array(matrix) {
var arr = matrix.tolist();
var nx = arr.length;
var ny = arr[0].length;
// Loop over all cells
for (var i = 0; i < nx; ++i) {
for (var j = 0; j < ny; ++j) {
if( arr[i][j] > 0 ) {
arr[i][j] = 1;
} else {
arr[i][j] = 0;
}
}
}
return nj.array(arr);
}
/* === Training === */
var X = nj.array(training);
var y = nj.array(output);
// X, y, hidden_neurons, alpha, epochs, dropout, dropout_percent
train(X, y, 20, 0.01, 100000, false, 0.2);