-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLearnAI.js
697 lines (631 loc) · 35.4 KB
/
LearnAI.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/* ********************************************************************************
These functions are used when running the network. Scroll down to the cost
and activation section to learn how to use them
******************************************************************************** */
function defaultCost(output, expected) {
let error = output - expected;
return error * error;
}
function defaultCostDerivative(output, expected) {
return 2 * (output - expected);
}
function sigmoid(x) {
return 1 / (1 + Math.exp(-x));
}
function sigmoidDerivative(x) {
let y = 1 / (1 + Math.exp(-x));
return y * (1 - y);
}
function TanH(x) {
let e2 = Exp(2 * x);
return (e2 - 1) / (e2 + 1);
}
function TanHDerivative(x) {
let e2 = Exp(2 * x);
let y = (e2 - 1) / (e2 + 1);
return 1 - y * y;
}
function ReLU(x) {
return Math.max(0, x);
}
function ReLUDerivative(x) {
return x > 0 ? 1 : 0
}
function SiLU(x) {
return x / (1 + Math.exp(-x));
}
function SiLUDerivative(x) {
let y = 1 / (1 + Math.exp(-x));
return x * y * (1 - y) + y;
}
// System rewrite is needed in order to be able to use these:
// function Softmax(x)
// function SoftmaxDerivative(x)
/* ********************************************************************************
This class is responsible for storing the weights and biases of a neural network,
as well as the gpu programs and other function needed to run the network and
evaluate it
Example usage: let nn = new NeuralNetwork(784, 100, 10).randomize();
******************************************************************************** */
class NeuralNetwork {
/* ********************************************************************************
This function explains the class so students can understand what it does
Example usage: NeuralNetwork.print();
******************************************************************************** */
static print() { // Use console.log to print the contents of the class instead
console.log(`This is the NeuralNetwork class, which stores all the data necessary to run a Neural Network!
Read the library to learn more about the inner workings of an AI!
List of methods in NeuralNetwork:
/* ********************************************************************************
These functions are used when running the network. Scroll down to the cost
and activation section to learn how to use them
******************************************************************************** */
function defaultCost(output, expected)
function defaultCostDerivative(output, expected)
function sigmoid(x)
function sigmoidDerivative(x)
function TanH(x)
function TanHDerivative(x)
function ReLU(x)
function ReLUDerivative(x)
function SiLU(x)
function SiLUDerivative(x)
/* ********************************************************************************
This constructor creates a neural network with undefined weights and biases
Example usage: let nn = new NeuralNetwork(784, 100, 10);
******************************************************************************** */
constructor(layer1, layer2, ...otherLayers)
/* ********************************************************************************
This function will be called to create the gpu kernels once the user has changed the
activation/cost functions to their satisfaction
Example usage: let nn = new NeuralNetwork(784, 100, 10).generateGPU();
******************************************************************************** */
generateGPU()
/* ********************************************************************************
This function will randomize all the weights in an existing network
Example usage: let nn = new NeuralNetwork(784, 100, 10).randomize();
******************************************************************************** */
randomize()
/* ********************************************************************************
This function will load a neural network from a save file
Example usage: let nn = new NeuralNetwork(784, 100, 10).fromFile("nn.txt");
******************************************************************************** */
fromFile(file)
/* ********************************************************************************
This function will save a neural network to a save file
Example usage: nn.saveToFile("nn.txt");
******************************************************************************** */
saveToFile(filename)
/* ********************************************************************************
The singleCost function calculates the cost of a single output of the network, can be set by user
to functions other than the default value, user must include derivative so that the network is trainable
Both functions have 2 inputs and a return value
Example usage: nn.setCostFunction(myfunc, myinverse);
******************************************************************************** */
singleCost = [defaultCost, defaultCostDerivative];
setCostFunction(normal, derivative)
/* ********************************************************************************
This function calculates the total cost of the network's output, not for user
Example usage: N/A
******************************************************************************** */
averageCost(output, expected)
/* ********************************************************************************
These functions run the network and return its final outputs
getNodeValues is used by the deep learner because it saves all intermittent values,
which are necessary for training
Example usage: N/A
******************************************************************************** */
runNetwork(data)
getAllValues(data)
/* ********************************************************************************
The activation function is applied to a node's input to find the nodes output, can be set by user
to functions other than the default value, user must include derivative so that the network is trainable
Both functions have 1 input and a return value
The gpu really doesn't like it when your function creates a variable, so only math in there ok?
Example usage: nn.setActivationFunction(myfunc, myinverse);
******************************************************************************** */
activation = [sigmoid, sigmoidDerivative];
setActivationFunction(normal, derivative)
outputactivation = [sigmoid, sigmoidDerivative];
setOutputActivationFunction(normal, derivative)`);
}
/* ********************************************************************************
This constructor creates a neural network with undefined weights and biases
Example usage: let nn = new NeuralNetwork(784, 100, 10);
******************************************************************************** */
constructor(layer1, layer2, ...otherLayers) {
//Gpu.js
this.gpu = new GPU();
// Layer sizes
this.layersizes = [layer1, layer2].concat(otherLayers);
this.totallayers = this.layersizes.length - 1; // Number of layers
// Layer weights
this.layerweights = Array(this.totallayers);
for (let i = 0; i < this.totallayers; i++) {
this.layerweights[i] = Array(this.layersizes[i]);
for (let j = 0; j < this.layersizes[i]; j++) {
this.layerweights[i][j] = Array(this.layersizes[i + 1]);
}
}
// Layer biases
this.layerbiases = Array(this.totallayers);
for (let i = 0; i < this.totallayers; i++) {
this.layerbiases[i] = Array(this.layersizes[i + 1]);
}
}
/* ********************************************************************************
This function will be called to create the gpu kernels once the user has changed the
activation/cost functions to their satisfaction
Example usage: let nn = new NeuralNetwork(784, 100, 10).generateGPU();
******************************************************************************** */
generateGPU() {
// Layer computation functions ( Note to self: if you print this array, everything will magically stop working )
this.layercomputers = [[], []];
for (let i = 0; i < this.totallayers; i++) {
// inputs => weighted_inputs
this.layercomputers[0].push(eval(`this.gpu.createKernel(function(inputs, biases, weights) {
let sum = biases[this.thread.x];
for (let ii = 0; ii < ${this.layersizes[i]}; ii++) {
sum += inputs[ii]*weights[ii][this.thread.x];
}
return sum;
}).setOutput([${this.layersizes[i + 1]}])`));
// weighted_inputs => outputs
this.layercomputers[1].push(eval(`this.gpu.createKernel(function(weighted_inputs) {
${this.activation[0]}
return ${this.activation[0].name}(weighted_inputs[this.thread.x]);
}).setOutput([${this.layersizes[i + 1]}])`));
}
this.outputlayercomputer = eval(`this.gpu.createKernel(function(weighted_inputs) {
${this.outputactivation[0]}
return ${this.outputactivation[0].name}(weighted_inputs[this.thread.x]);
}).setOutput([${this.layersizes[this.totallayers]}])`);
// Node computation functions (for backpropagation and gradient descent)
// These calculate the derivative of the cost with respect to weighted input
// cost => weighted_inputs
this.outputnodescomputer = eval(`this.gpu.createKernel(function(weighted_inputs, outputs, expected_outputs) {
${this.singleCost[1]}
${this.outputactivation[1]}
return ${this.singleCost[1].name}(outputs[this.thread.x],expected_outputs[this.thread.x])
* ${this.outputactivation[1].name}(weighted_inputs[this.thread.x]);
}).setOutput([${this.layersizes[this.layersizes.length - 1]}])`);
// weighted_inputs => weighted_inputs
this.hiddennodescomputers = [];
for (let i = 1; i < this.totallayers; i++) {
this.hiddennodescomputers.push(eval(`this.gpu.createKernel(function(weighted_inputs, weights, nodevalues) {
${this.activation[1]}
let value = 0;
for (let ii = 0; ii<${this.layersizes[i + 1]}; ii++)
{
value += weights[this.thread.x][ii] * nodevalues[ii];
}
return value * ${this.activation[1].name}(weighted_inputs[this.thread.x]);
}).setOutput([${this.layersizes[i]}])`));
}
return this;
}
/* ********************************************************************************
This function will randomize all the weights in an existing network
Example usage: let nn = new NeuralNetwork(784, 100, 10).randomize();
******************************************************************************** */
randomize() {
for (let i = 0; i < this.totallayers; i++) {
for (let j = 0; j < this.layersizes[i]; j++) {
let sqrt = Math.sqrt(this.layersizes[i]);
for (let k = 0; k < this.layersizes[i + 1]; k++) {
this.layerweights[i][j][k] = (Math.random()*2 - 1) / sqrt;
}
}
}
for (let i = 0; i < this.totallayers; i++) {
for (let j = 0; j < this.layersizes[i + 1]; j++) {
this.layerbiases[i][j] = 0;
}
}
return this;
}
/* ********************************************************************************
This function will load a neural network from a save file
Example usage: let nn = new NeuralNetwork(784, 100, 10).fromFile("nn.txt");
******************************************************************************** */
fromFile(file) {
this.layersizes = file.layersizes;
this.totallayers = this.layersizes.length - 1;
this.layerweights = file.weights;
this.layerbiases = file.biases;
// Cant do activation and cost yet because I didnt save them properly, I should stringify the function
console.log("Successfully loaded from file");
return this;
}
/* ********************************************************************************
This function will save a neural network to a save file
Example usage: nn.saveToFile("nn.txt");
******************************************************************************** */
saveToFile(filename) {
let a = document.createElement("a");
let file = new Blob([JSON.stringify({
activation: [JSON.stringify(this.activation[0]), JSON.stringify(this.activation[1])],
cost: [JSON.stringify(this.singleCost[0]), JSON.stringify(this.singleCost[1])],
layersizes: this.layersizes,
weights: this.layerweights,
biases: this.layerbiases})], {type: 'text:plain'});
a.href = URL.createObjectURL(file);
a.download = filename;
a.click();
a.remove();
}
/* ********************************************************************************
The singleCost function calculates the cost of a single output of the network, can be set by user
to functions other than the default value, user must include derivative so that the network is trainable
Both functions have 2 inputs and a return value
Example usage: nn.setCostFunction(myfunc, myinverse);
******************************************************************************** */
singleCost = [defaultCost, defaultCostDerivative];
setCostFunction(normal, derivative) {
this.singleCost = [normal, derivative];
return cost;
}
/* ********************************************************************************
This function calculates the total cost of the network's output, not for user
Example usage: N/A
******************************************************************************** */
averageCost(output, expected) {
/*let cost = [];
for (let i = 0; i < output.length; i++) {
cost.push(this.singleCost[0](output[i], expected[i]));
}
return cost;*/
let cost = 0;
for (let i = 0; i < output.length; i++) {
cost += this.singleCost[0](output[i], expected[i]);
}
return cost; // / output.length;
}
/* ********************************************************************************
This function calculates the cost of the network for a given datapoint or a set of datapoints
Example usage: N/A
******************************************************************************** */ // TODO code needs to be nicer, cleaner, easier to use, faster, and just better
/*Cost(data, targets, ...range) { // Takes an array of inputs and outputs and finds the cost of the neural network (average cost), range is [start,stop] for batchSize
let cost = 0.0;
// If its a dataset, calculate for each element, and then return average
if (Array.isArray(data[0])) {
// Find the start and stop of our data
let start = 0;
let end = data.length;
if (range != undefined) {
start = range[0];
end = range[1];
}
// Find the average cost in that range
for (let i = start; i < end; i++) {
cost += this.Cost(data[i], targets[i]);
}
return cost / (end - start);
}
// Single input
// Cost is difference between expected output and actual output
let output = this.runNetwork(data);
for (let i = 0; i < output.length; i++) {
cost += this.nodeCost(output[i], targets[i]);
}
return cost;
}*/
/* ********************************************************************************
These functions run the network and return its final outputs
getNodeValues is used by the deep learner because it saves all intermittent values,
which are necessary for training
Example usage: N/A
******************************************************************************** */
runNetwork(data) {
let currentLayer = data;
for (let i = 0; i < this.totallayers - 1; i++) {
currentLayer = this.layercomputers[0][i](currentLayer, this.layerbiases[i], this.layerweights[i]);
currentLayer = this.layercomputers[1][i](currentLayer);
}
currentLayer = this.layercomputers[0][this.totallayers - 1](currentLayer, this.layerbiases[this.totallayers - 1], this.layerweights[this.totallayers - 1]);
currentLayer = this.outputlayercomputer(currentLayer);
return currentLayer;
}
getAllValues(data) {
// nodeValues with store a bunch of pairs, one for every layer. The first item in the pair is the weighted inputs, and the second is the activations/outputs/inputs to next layer. Netowrk input has no weighted input
let nodeValues = [[null, data]];
for (let i = 0; i < this.totallayers - 1; i++) {
nodeValues.push([null, null]); // For clarity
nodeValues[i + 1][0] = this.layercomputers[0][i](nodeValues[i][1], this.layerbiases[i], this.layerweights[i]);
nodeValues[i + 1][1] = this.layercomputers[1][i](nodeValues[i + 1][0]);
}
nodeValues.push([null, null]); // For clarity
nodeValues[this.totallayers][0] = this.layercomputers[0][this.totallayers - 1](nodeValues[this.totallayers - 1][1], this.layerbiases[this.totallayers - 1], this.layerweights[this.totallayers - 1]);
nodeValues[this.totallayers][1] = this.outputlayercomputer(nodeValues[this.totallayers][0]);
return nodeValues;
}
/* ********************************************************************************
The activation function is applied to a node's input to find the nodes output, can be set by user
to functions other than the default value, user must include derivative so that the network is trainable
Both functions have 1 input and a return value
The gpu really doesn't like it when your function creates a variable, so only math in there ok?
Example usage: nn.setActivationFunction(myfunc, myinverse);
******************************************************************************** */
activation = [sigmoid, sigmoidDerivative];
setActivationFunction(normal, derivative) {
this.activation = [normal, derivative];
return this;
}
outputactivation = [sigmoid, sigmoidDerivative];
setOutputActivationFunction(normal, derivative) {
this.activation = [normal, derivative];
return this;
}
}
/* ********************************************************************************
This class is responsible for training the neural network it is assigned to
as well as the gpu programs and other function needed to run the network and
evaluate it
Example usage: let nn = new NeuralNetwork(784, 100, 10).randomize();
******************************************************************************** */
class DeepLearner {
/* ********************************************************************************
This function explains the class so students can understand what it does
Example usage: DeepLearner.print();
******************************************************************************** */
static print() { // Use console.log to print the contents of the class instead
console.log(`This is the Deepleaner class, which is used to train a neural netowkr on some data!
Read the library to learn more about the inner workings of an AI!
List of methods in DeepLearner:
/* ********************************************************************************
This function explains the class so students can understand what it does
Example usage: DeepLearner.print();
******************************************************************************** */
static print()
/* ********************************************************************************
This constructor generates all the arrays and data required to be able to train
a given neural netowrk. The network cannot be changed after initialization
See defaultSettings for a list of possible settings, or read this messy constructor
Example usage: let dl = new DeepLearner(nn, dataset, DeepLearner.defaultSettings);
******************************************************************************** */
constructor(network, dataset, settings)
/* ********************************************************************************
This object stores all the settings for the learner, which are explained in the object's comments
Example usage: let dl = new DeepLearner(nn, dataset, DeepLearner.defaultSettings);
******************************************************************************** */
static defaultSettings = {
learnrate: 0.1, // for gradient descent
batchsize: 100, // give it 100 samples at a time
batchsplit: 0.8, // 80% used, 20% saved for testing its learning on never before seen data
maxIncorrectGuessesToPrint: 1, // Print the first X incorrect guesses
regularization: 0.1, // Used for weight decay, too big and your network forgets what it learned
momentum: 0.9 // Weights remember how fast they were moving previously and accelerate down a slope, this is how much of the previous speed is retained (so 0.9 is losing 10% of the speed)
}
/* ********************************************************************************
This function creates an interval which will call trainOnce at a user defined frequency (in milliseconds)
This function is here for training until done
Example usage: let trainer = dl.train(2000);
******************************************************************************** */
train(milliseconds)
/* ********************************************************************************
This function will do one training iteration of the network, running this.batchsize
datapoints through the network and using the results to train the network
Example usage: dl.trainOnce();
******************************************************************************** */
trainOnce()
/* ********************************************************************************
This function will clear the gradient arrays and then calculate and add the gradient for a lot of datapoints
Together with the gpu programs, this is half the magic of deep learning (the other half is actually using the gradients)
Example usage: N/A
******************************************************************************** */
updateGradients()
/* ********************************************************************************
This function will use the gradients to train the network, using the gradient descent method
Using the gradients as a slope of a graph, our goal is to slide down the slope and find the lowest point on the graph
Example usage: N/A
******************************************************************************** */
applyGradients()`);
}
/* ********************************************************************************
This constructor generates all the arrays and data required to be able to train
a given neural netowrk. The network cannot be changed after initialization
See defaultSettings for a list of possible settings, or read this messy constructor
Example usage: let dl = new DeepLearner(nn, dataset, DeepLearner.defaultSettings);
******************************************************************************** */
constructor(network, dataset, settings) {
// Variables
this.network = network;
this.settings = settings;
this.totallayers = this.network.totallayers;
this.totaldata = dataset[0].length;
this.trainingset = [[], []];
this.testingset = [[], []];
this.batchindex = 0;
this.totalcorrect = 0; // To track total correct every epoch
this.incorrectguessesprinted = 0; // Every epoch we print the first 10 mistakes
this.epochscompleted = 0;
this.onEpoch = function () {};
this.onCost = function () {};
// Settings
if (!this.settings.hasOwnProperty("learnrate")) this.settings.learnrate = 0.1;
this.trainamount = this.totaldata * 0.8; // default setting
if (this.settings.hasOwnProperty("batchsplit")) this.trainamount = this.totaldata * this.settings.batchsplit;
if (!this.settings.hasOwnProperty("batchsize")) this.settings.batchsize = 100;
if (!this.settings.hasOwnProperty("maxIncorrectGuessesToPrint")) this.settings.maxIncorrectGuessesToPrint = 10;
// Fill the two sets using the batchsplit setting
this.trainingset[0] = dataset[0].slice(0, this.trainamount);
this.trainingset[1] = dataset[1].slice(0, this.trainamount);
this.testingset[0] = dataset[0].slice(this.trainamount, dataset[0].length);
this.testingset[1] = dataset[1].slice(this.trainamount, dataset[1].length);
// Weight gradients and velocities
this.wgradients = Array(this.network.layerweights.length);
this.wvelocities = Array(this.network.layerweights.length);
for (let i = 0; i < this.network.layerweights.length; i++) {
this.wgradients[i] = Array(this.network.layerweights[i].length);
this.wvelocities[i] = Array(this.network.layerweights[i].length);
for (let j = 0; j < this.network.layerweights[i].length; j++) {
this.wgradients[i][j] = Array(this.network.layerweights[i][j].length).fill(0);
this.wvelocities[i][j] = Array(this.network.layerweights[i][j].length).fill(0);
}
}
// Bias gradients
this.bgradients = Array(this.network.layerbiases.length);
this.bvelocities = Array(this.network.layerbiases.length);
for (let i = 0; i < this.network.layerbiases.length; i++) {
this.bgradients[i] = Array(this.network.layerbiases[i].length).fill(0);
this.bvelocities[i] = Array(this.network.layerbiases[i].length).fill(0);
}
}
/* ********************************************************************************
This object stores all the settings for the learner, which are explained in the object's comments
Example usage: let dl = new DeepLearner(nn, dataset, DeepLearner.defaultSettings);
******************************************************************************** */
static defaultSettings = {
learnrate: 0.1, // for gradient descent
batchsize: 100, // give it 100 samples at a time
batchsplit: 0.8, // 80% used, 20% saved for testing its learning on never before seen data
maxIncorrectGuessesToPrint: 1, // Print the first X incorrect guesses
regularization: 0.1, // Used for weight decay, too big and your network forgets what it learned
momentum: 0.9 // Weights remember how fast they were moving previously and accelerate down a slope, this is how much of the previous speed is retained (so 0.9 is losing 10% of the speed)
}
/* ********************************************************************************
This function will run all the testing data through the network and return the networks performance
Example usage: N/A (deprecated)
******************************************************************************** */
/*test() {
let totalcorrect = 0;
let totalwrong = 0;
for (let i = 0; i < this.testingset[0].length; i++) {
let outputs = this.network.runNetwork(this.testingset[0][i]);
let largest = 0;
for (let j = 1; j < outputs[1].length; j++) {
if (outputs[1][j] > outputs[1][largest])
largest = j;
}
if (this.testingset[1][i][largest] == 1) totalcorrect++;
else totalwrong++;
}
return "The network gets " + totalcorrect + "/" + this.testingset[0].length + " on testing data";
}*/
/* ********************************************************************************
This function creates an interval which will call trainOnce at a user defined frequency (in milliseconds)
This function is here for training until done
Example usage: let trainer = dl.train(2000);
******************************************************************************** */
train(milliseconds) {
// Loop
let trainer = setInterval(this.trainOnce.bind(this), milliseconds);
return trainer; // In case the user wants to edit this interval
}
/* ********************************************************************************
This function will do one training iteration of the network, running this.batchsize
datapoints through the network and using the results to train the network
Example usage: dl.trainOnce();
******************************************************************************** */
trainOnce() {
// Get the average gradient of all data points
this.updateGradients();
this.applyGradients();
}
/* ********************************************************************************
This function will clear the gradient arrays and then calculate and add the gradient for a lot of datapoints
Together with the gpu programs, this is half the magic of deep learning (the other half is actually using the gradients)
Example usage: N/A
******************************************************************************** */
updateGradients() {
// Track cost for user because we can
let cost = 0;
// For settings.batchsize amount of datapoints, calculate the gradients and add them (we will take the average later)
for (let i = 0; i < this.settings.batchsize; i++) {
// Run the data through the network and save all layers
let layerdata = this.network.getAllValues(this.trainingset[0][this.batchindex]); // Length is this.totallayers + 1
// Track cost and see if the network is correct, for statistics
cost += this.network.averageCost(layerdata[layerdata.length - 1][1], this.trainingset[1][this.batchindex]);
let largest = 0;
let index = this.totallayers - 1; // Layer index, used later for backpropagation
for (let i = 1; i < layerdata[index + 1][1].length; i++) {
if (layerdata[index + 1][1][i] > layerdata[index + 1][1][largest])
largest = i;
}
this.debugnow = false;
if (this.trainingset[1][this.batchindex][largest] == 1) this.totalcorrect++;
else if (this.incorrectguessesprinted < this.settings.maxIncorrectGuessesToPrint) {
this.incorrectguessesprinted++;
console.log("Incorrect guess", this.incorrectguessesprinted + ", network guessed", layerdata[index + 1][1], "for", this.trainingset[1][this.batchindex] + ", biggest is", largest);
if (this.debug) {
this.debugnow = true;
}
}
// Backpropagation begins
// Calculate output layer gradients
let nodevalues = this.network.outputnodescomputer(layerdata[index + 1][0], layerdata[index + 1][1], this.trainingset[1][this.batchindex]);
for (let j = 0; j < nodevalues.length; j++) {
// Calculate the gradients for weights
for (let k = 0; k < layerdata[index][1].length; k++) {
this.wgradients[index][k][j] += layerdata[index][1][k] * nodevalues[j];
}
// Calculate the gradient for bias
this.bgradients[index][j] += nodevalues[j];
}
if (this.debugnow) console.log("Output layer nodeValues", nodevalues);
// Calculate hidden layer gradients
for (index--; index >= 0; index--) {
nodevalues = this.network.hiddennodescomputers[index](layerdata[index + 1][0], this.network.layerweights[index + 1], nodevalues);
for (let j = 0; j < nodevalues.length; j++) {
// Calculate the gradients for weights
for (let k = 0; k < layerdata[index][1].length; k++) {
this.wgradients[index][k][j] += layerdata[index][1][k] * nodevalues[j];
}
// Calculate the gradient for bias
this.bgradients[index][j] += nodevalues[j];
}
if (this.debugnow) {
console.log("Hidden layer nodevalues", nodevalues, "other multiply thingy", layerdata[index][1]);
//console.log(nodevalues.length, layerdata[index][1].length);
}
}
if (this.debugnow) {
console.log("Gradients are w", this.wgradients, "b", this.bgradients);
console.log("Weights are w", this.network.layerweights, "b", this.network.layerbiases);
console.log("Heres the layerdata", layerdata);
}
// Increment the index
this.batchindex++;
if (this.batchindex == this.trainingset[0].length) {
this.epochscompleted++;
this.onEpoch(this.totalcorrect, this.trainingset[0].length);
console.log("Epoch", this.epochscompleted, "complete, network guessed", this.totalcorrect, "/", this.trainingset[0].length, "correct");
this.batchindex = 0;
this.totalcorrect = 0;
this.incorrectguessesprinted = 0;
}
}
this.onCost(cost / this.settings.batchsize);
console.log("Average cost on data is", cost / this.settings.batchsize);
}
/* ********************************************************************************
This function will use the gradients to train the network, using the gradient descent method
Using the gradients as a slope of a graph, our goal is to slide down the slope and find the lowest point on the graph
Example usage: N/A
******************************************************************************** */
applyGradients() {
// We use a little trick here, instead of taking the average of our gradients we use the sum and instead divide our learn rate
let learnrate = this.settings.learnrate / this.trainamount;
let weightDecay = (1 - this.settings.regularization * learnrate);
// Apply weights
for (let i = 0; i < this.wgradients.length; i++) {
for (let j = 0; j < this.wgradients[i].length; j++) {
for (let k = 0; k < this.wgradients[i][j].length; k++) {
let velocity = this.wvelocities[i][j][k] * this.settings.momentum - this.wgradients[i][j][k] * learnrate;
this.wvelocities[i][j][k] = velocity;
this.network.layerweights[i][j][k] = this.network.layerweights[i][j][k] * weightDecay + velocity;
}
this.wgradients[i][j].fill(0);
}
}
// Apply biases
for (let i = 0; i < this.bgradients.length; i++) {
for (let j = 0; j < this.bgradients[i].length; j++) {
let velocity = this.bvelocities[i][j] * this.settings.momentum - this.bgradients[i][j] * learnrate;
this.bvelocities[i][j] = velocity;
this.network.layerbiases[i][j] += velocity;
}
this.bgradients[i].fill(0);
}
}
}