-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathneural-network-standalone.js
343 lines (315 loc) · 10.3 KB
/
neural-network-standalone.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const initHE = ['relu', 'leaky_relu', 'softmax'];
// sum all items in array 2d
function sumArr(arr) {
return arr.reduce((acc, curr) => acc + curr, 0);
}
// random in range [min,max]
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function randn() {
// Create random in range [0, 1)
let u1 = Math.random();
let u2 = Math.random();
// Box-Muller Transform
let z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
return z0;
}
/* weights look like:
[[w00,w01,w02],
[w10,w11,w12]]
transpose to
[[w00,w10],
[w01,w11],
[w02,w12]]
*/
function transpose(weights) {
let result = []
for (let i = 0; i < weights[0].length; i++) {
let r = [];
for (let j = 0; j < weights.length; j++) {
r.push(weights[j][i])
}
result.push(r);
}
return result;
}
function heInit(fanIn, fanOut) {
const scale = Math.sqrt(2 / fanIn);
scale * randn();
let result = [];
for (let i = 0; i < fanIn; i++) {
let wr = [];
for (let j = 0; j < fanOut; j++) {
wr.push(scale * randn())
}
result.push(wr);
}
return result;
}
function xavierInit(fanIn, fanOut) {
const scale = Math.sqrt(6 / (fanIn + fanOut));
let result = [];
for (let i = 0; i < fanIn; i++) {
let wr = [];
for (let j = 0; j < fanOut; j++) {
wr.push(scale * randn())
}
result.push(wr);
}
return result;
}
// sigmoid
function sigmoid(arr) {
return arr.map(x => 1.0 / (1 + Math.exp(-x)));
}
function sigmoidDerivative(arr) {
return arr.map(x => x * (1 - x));
}
// relu
function relu(arr) {
return arr.map(x => x > 0 ? x : 0);
}
function reluDerivative(arr) {
return arr.map(x => x > 0 ? 1 : 0);
}
function leakyRelu(arr, alpha = 0.001) {
return arr.map(x => x > 0 ? x : alpha * x);
}
function leakyReluDerivative(arr, alpha = 0.001) {
return arr.map(x => x > 0 ? 1 : alpha);
}
// softmax
function softmax(arr) {
let maxX = Math.max(...arr);
let expX = arr.map((v) => Math.exp(v - maxX));
let sum = expX.reduce((acc, curr) => acc + curr, 0);
return expX.map((v) => v / sum);
}
function softmaxDerivative(arr) {
let s = softmax(arr);
return arr.map((v, idx) => s[idx] * (1 - s[idx]));
}
// MSE loss
function mse(y_pred, y_true) {
return y_pred.map((_, idx) => (y_pred[idx] - y_true[idx]) ** 2);
}
function mseGradient(y_pred, y_true) {
return y_pred.map((_, idx) => (y_pred[idx] - y_true[idx]));
}
// Categorical Cross Entropy loss
function categoricalCrossEntropy(y_pred, y_true) {
return y_true.map((_, i) => (-y_true[i] * Math.log(y_pred[i])));
}
function categoricalCrossEntropyGradient(y_pred, y_true) {
return y_true.map((_, i) => (y_pred[i] - y_true[i]));
}
function binaryCrossEntropy(y_pred, y_true) {
let loss = [];
for (let i = 0; i < y_true.length; i++) {
let currentLoss = y_true[i] === 1 ? -Math.log(y_pred[i]) : -Math.log(1 - y_pred[i]);
loss.push(currentLoss);
}
return loss;
}
function binaryCrossEntropyGradient(y_pred, y_true) {
let gradients = [];
for (let i = 0; i < y_true.length; i++) {
if (y_true[i] === 1) {
gradients.push(-1.0 / y_pred[i]);
} else {
gradients.push(1.0 / (1 - y_pred[i]));
}
}
return gradients;
}
class Layer {
weights;
biases;
forward() {
throw Error('not implement')
}
backward() {
throw Error('not implement')
}
}
class Dense extends Layer {
inputs;
outputs;
gradBiases;
gradWeights;
gradInputs;
active;
activeFunc;
derivativeFunc;
constructor(input_size, output_size, active = 'sigmoid') {
super();
this.active = active
switch (this.active) {
case 'sigmoid':
this.activeFunc = sigmoid;
this.derivativeFunc = sigmoidDerivative;
break;
case 'relu':
this.activeFunc = relu;
this.derivativeFunc = reluDerivative;
break;
case 'leaky_relu':
this.activeFunc = leakyRelu;
this.derivativeFunc = leakyReluDerivative;
break;
case 'softmax':
this.activeFunc = softmax;
this.derivativeFunc = softmaxDerivative;
break;
default:
break;
}
if (initHE.includes(this.active)) {
this.weights = heInit(input_size, output_size);
} else {
this.weights = xavierInit(input_size, output_size);
}
// this.weights = Array.from({length: input_size}, () => Array.from({length: output_size}, () => getRandomArbitrary(-1,1)))
this.biases = Array.from({ length: output_size }, () => getRandomArbitrary(-1, 1))
}
forward(inputs) {
// save input for backward
this.inputs = inputs;
this.outputs = [];
for (const [i, bias] of this.biases.entries()) {
/* weights look like:
[[w00,w01,w02],
[w10,w11,w12]]*/
// so we need do it like: w[0][0] * input[0] + w[1][0] * input[1] + ... + w[n][0] * input[n] (n = inputs.length - 1)
let z = sumArr(this.inputs.map((input, j) => this.weights[j][i] * input)) + bias;
this.outputs.push(z);
}
this.outputs = this.activeFunc(this.outputs);
return this.outputs;
}
backward(grad, lr) {
this.gradInputs = new Array(this.inputs.length).fill(0); // calculate gradient for previous layer
this.gradWeights = this.inputs.map(() => new Array(this.outputs.length).fill(0)); // calculate gradient weights for this layer
this.gradBiases = new Array(this.outputs.length).fill(0); // calculate gradient biases for this layer
let derivativeOutputs = this.derivativeFunc(this.outputs);
for (let i = 0; i < this.inputs.length; i++) {
for (let j = 0; j < this.outputs.length; j++) {
let derivativeValue = derivativeOutputs[j];
this.gradInputs[i] += grad[j] * this.weights[i][j] * derivativeValue;
this.gradWeights[i][j] = grad[j] * this.inputs[i] * derivativeValue;
this.gradBiases[j] += grad[j] * derivativeValue;
// update weights
this.weights[i][j] -= lr * this.gradWeights[i][j];
if (i === 0) {
this.biases[j] -= lr * this.gradBiases[j];
}
}
}
return this.gradInputs; // return for previous layer using to calculate gradient
}
}
class Model {
loss;
earlyStoppingCb;
epochEndCb;
monitors;
outputs;
layers = [];
lossHistory = [];
lossFunc;
lossFuncGradient;
constructor(loss, earlyStoppingCb, epochEndCb, monitors) {
this.loss = loss;
this.earlyStoppingCb = earlyStoppingCb;
this.epochEndCb = epochEndCb;
this.monitors = monitors;
switch (loss) {
case 'categorical_crossentropy':
this.lossFunc = categoricalCrossEntropy;
this.lossFuncGradient = categoricalCrossEntropyGradient;
break;
case 'mse':
this.lossFunc = mse;
this.lossFuncGradient = mseGradient;
break;
case 'binary_crossentropy':
this.lossFunc = binaryCrossEntropy;
this.lossFuncGradient = binaryCrossEntropyGradient;
break;
default:
break;
}
}
add(layer) {
this.layers.push(layer);
}
forward(inputs) {
let outputs = [...inputs];
for (const layer of this.layers) {
outputs = layer.forward(outputs)
}
return outputs;
}
backward(targets, lr) {
let grad = this.lossFuncGradient(this.outputs, targets);
// calculate gradient from last layer to first layer
for (let i = this.layers.length - 1; i >= 0; i--) {
grad = this.layers[i].backward(grad, lr)
}
}
train(inputs, targets, epochs, learningRate) {
for (let epoch = 0; epoch < epochs; epoch++) {
let totalLoss = 0;
// training each epoch
for (let i = 0; i < targets.length; i++) {
// propagation
this.outputs = this.forward(inputs[i]);
totalLoss += sumArr(this.lossFunc(this.outputs, targets[i]));
this.backward(targets[i], learningRate);
}
// avg loss
let loss = totalLoss / inputs.length;
this.lossHistory.push(loss);
if (this.monitors && this.monitors.includes('loss')) {
console.log(`Epoch: ${epoch}, Loss: ${loss}`);
}
// callbacks
typeof this.epochEndCb === 'function' && this.epochEndCb(this, epoch, loss);
if (typeof this.earlyStoppingCb === 'function' && this.earlyStoppingCb(this, epoch, loss)) {
console.log('Early stopping !');
break;
};
}
}
summary() {
console.log(`====================`)
let totalParams = 0;
for (const layer of this.layers) {
let layerParams = layer.weights.length * layer.weights[0].length;
totalParams += layerParams
console.log(`${layer.constructor.name} (${layer.weights.length}, ${layer.weights[0].length}) Active: '${layer.active}' Params: ${layerParams}`)
}
console.log(`Total Params: ${totalParams}`)
console.log(`====================`)
}
}
const epochEndCallback = (model, epoch, loss) => {
if (epoch === 0 || loss < model.lossHistory[epoch - 1]) {
// model.save('./simple-model.any_extension');
}
}
const earlyStoppingCallback = (model, epoch, loss) => {
return loss < 0.0003;
}
let model = new Model('mse', earlyStoppingCallback, epochEndCallback, ['loss', 'progress']);
model.add(new Dense(2, 3, 'leaky_relu'));
model.add(new Dense(3, 1, 'sigmoid'));
model.summary()
let xTrain = [[0, 0], [0, 1], [1, 0], [1, 1]];
let yTrain = [[0], [1], [1], [0]];
model.train(xTrain, yTrain, 1000, 0.1);
for (let idx = 0; idx < yTrain.length; idx++) {
let output = model.forward(xTrain[idx]);
console.log(`Input: [${xTrain[idx]}],Expect: ${yTrain[idx]}, Predict: ${output}`)
}