-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js.html
498 lines (442 loc) · 19.3 KB
/
index.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>index.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-leaf-detective_main.Biases.html">Biases</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Biases.html#fillRandom">fillRandom</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Biases.html#populate">populate</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html">Layer</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html#addBiases">addBiases</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html#addWeights">addWeights</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html#apply">apply</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html#connect">connect</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html#populate">populate</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Layer.html#run">run</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html">Network</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#addBiases">addBiases</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#addWeights">addWeights</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#backpropagate">backpropagate</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#calculateDeltas">calculateDeltas</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#connect">connect</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#feed">feed</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#populate">populate</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#predict">predict</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Network.html#run">run</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="module-leaf-detective_main.Weights.html">Weights</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Weights.html#fillRandom">fillRandom</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_main.Weights.html#populate">populate</a></span></li><li class="nav-heading">Modules</li><li class="nav-heading"><span class="nav-item-type type-module">M</span><span class="nav-item-name"><a href="module-leaf-detective_helpers.html">leaf-detective/helpers</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_helpers.html#.plus">plus</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_helpers.html#.shuffle">shuffle</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_helpers.html#.sigmoid">sigmoid</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="module-leaf-detective_helpers.html#.squaredError">squaredError</a></span></li><li class="nav-heading"><span class="nav-item-type type-module">M</span><span class="nav-item-name"><a href="module-leaf-detective_main.html">leaf-detective/main</a></span></li>
</nav>
<div id="main">
<h1 class="page-title">index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/** @module leaf-detective/main */
import { plus } from "./helpers.js";
import { sigmoid, Activation } from "./activations.js";
/**
* Class representing layer weights
* @extends Array
* @constructor
*/
export class Weights extends Array {
/**
* Creates weights matrix
* @param {Number} width width of weights matrix
* @param {Number} height height of weights matrix
*/
constructor(width, height) {
super(height)
.fill(0)
.forEach((_, i, arr) => (arr[i] = new Array(width).fill(0)));
}
/**
* Fills weights matrix with random numbers between 0 (included) and 1
* (excluded)
* @returns {Weights}
*/
fillRandom() {
return this.map(row => row.map(_ => Math.random()));
}
/**
* Fills given data into matrix
* @param {Array} data array of data to populate weights matrix with; must
* be of same width and height as matrix
* @returns {Weights}
*/
populate(data) {
return this.map((row, i) => row.map((_, j) => data[i][j]));
}
}
/**
* Class representing layer biases
* @extends Array
* @constructor
*/
export class Biases extends Array {
/**
* Creates a bias matrix
* @param {Number} height height of bias matrix; must correspond to output
* layers length
*/
constructor(height) {
super(height).fill(0);
}
/**
* Fills bias matrix with random numbers between -1 (included) and 1
* (excluded)
* @returns {Biases}
*/
fillRandom() {
return this.map(_ => Math.random() * 2 - 1);
}
/**
* Fills given data into matrix
* @param {Array} data array of data to populate bias matrix with; must be
* same width as matrix
* @returns {Biases}
*/
populate(data) {
return this.map((_, i) => data[i]);
}
}
/**
* Class representing layer matrix
* @constructor
* @example
* // Import constructors and helpers
* import { Layer, Weights, Biases } from "lib/index.js";
* import { sigmoid } from "lib/activations.js";
*
* // Create Layers
* let inputLayer = new Layer(5);
* let outputLayer = new Layer(2);
*
* // Create weights and biases
* let weights = new Weights(inputLayer.length, outputLayer.length).fillRandom();
* let biases = new Biases(outputLayer.length).populate([-10, 10]);
*
* // Connect weights and outputLayer to inputLayer
* inputLayer.addWeights(weights).connect(outputLayer);
* // Connect biases to outputLayer
* outputLayer.addBiases(biases);
*
* // Add data and run neural net
* inputLayer.populate([1, 1, 1, 1, 1]).run().apply(sigmoid);
*/
export class Layer {
/**
* Creates a layer with given amount of neurons
* @param {Number} length length / amount of neurons of layer
*/
constructor(length) {
this.neurons = new Array(length).fill(0);
this.rawNeurons = new Array(length).fill(0);
this.length = length;
this.weights = null;
this.biases = null;
this.next = null;
this.previous = null;
}
/**
* Adds Biases object to layer
* @param {Biases} biases biases object from Biases constructor; belongs to
* ouput layer
* @example
* let inputLayer = new Layer(5);
* let outputLayer = new Layer(2);
*
* outputLayer.addBiases(biases);
* @returns {Layer}
*/
addBiases(biases) {
if (!(biases instanceof Biases))
throw new Error("Please pass a bias object");
this.biases = biases;
return this;
}
/**
* Adds Weights object to layer
* @param {Weights} weights weights object from Weights constructor; belongs
* to input layer
* @example
* let inputLayer = new Layer(5);
* let outputLayer = new Layer(2);
*
* inputLayer.addWeights(weights);
* @returns {Layer}
*/
addWeights(weights) {
if (!(weights instanceof Weights))
throw new Error("Please pass a weights object");
else if (!weights.every(row => row.length === this.length))
throw new Error(
"Width of weight matrix must be equal to amount of neurons"
);
this.weights = weights;
return this;
}
/**
* Applys given activation function to all neurons of the layer
* @param {(Activation|object)} activation activation object with a
* `source` and `derivative` method
* @example
* import { sigmoid } from "lib/activations.js"
*
* outputLayer.apply(sigmoid);
* @returns {Layer}
*/
apply(activation) {
this.neurons = this.rawNeurons.map((neuron, i) =>
activation.source(neuron, i)
);
return this;
}
/**
* Connects an input layer with an output layer
* @param {Layer} layer output layer object
* @example
* let inputLayer = new Layer(5);
* let outputLayer = new Layer(2);
*
* inputLayer.connect(outputLayer)
* @returns {Layer}
*/
connect(layer) {
if (!(layer instanceof Layer)) throw new Error("Please pass a layer");
this.next = layer;
layer.previous = this;
return this;
}
/**
* Populates neurons with data
* @param {Array} data array of data to populate layer / neurons with; must
* be same length as layer
* @returns {Layer}
*/
populate(data) {
if (data.length !== this.length)
throw new Error(
`Please pass data of length ${this.neurons.length}`
);
this.neurons = [...data];
return this;
}
/**
* Runs connection between two layers by applying weights to input layer's
* neurons and biases to resulting output layer's neurons
* @example
* let inputLayer = new Layer(5);
* let outputLayer = new Layer(2);
*
* inputLayer.connect(outputLayer).run()
* @returns {Layer} connected output layer
*/
run() {
this.next.rawNeurons = this.next.rawNeurons
.map((_, row) =>
this.neurons
.map((neuron, col) => neuron * this.weights[row][col])
.reduce(plus, 0)
)
.map((output, row) => output + this.next.biases[row]);
return this.next;
}
}
/**
* Neural Network Class
* @extends Array
* @constructor
* @example
* // Import Constructors and helpers
* import { Network } from "lib/index.js";
* import { gaussian } from "lib/activations.js";
*
* // Create network
* let network = new Network([2048, 1024, 512, 256], gaussian);
* // Run network
* network
* .addWeights()
* .addBiases()
* .connect()
* .run()
*/
export class Network extends Array {
/**
* Creates a neural network with given layers
* @param {Array} layers array of Layer objects or numbers representing
* layer length
*/
constructor(layers, activation = sigmoid) {
if (!(activation.source && activation.derivative))
throw new Error(
"Activation function must have a source and an derivative"
);
super(...layers.map(el => (el instanceof Layer ? el : new Layer(el))));
this.activation = activation;
}
/**
* Adds randomly filled bias matrix with correct dimensions to every layer
* by calling Biases constructor
* @returns {Network}
*/
addBiases() {
this.forEach((layer, i) => {
if (i > 0) {
layer.addBiases(new Biases(layer.length).fillRandom());
}
});
return this;
}
/**
* Adds randomly filled weight matrix with correct dimensions to every layer
* by calling Weights constructor
* @returns {Network}
*/
addWeights() {
this.forEach((layer, i) => {
if (i < this.length - 1) {
layer.addWeights(
new Weights(layer.length, this[i + 1].length).fillRandom()
);
}
});
return this;
}
/**
* Connects every layers with its succedding layer by calling layer.connect
* @returns {Network}
*/
connect() {
this.forEach((layer, i) =>
i < this.length - 1 ? layer.connect(this[i + 1]) : ""
);
return this;
}
/**
* Runs the neural network and applies the sigmoid activation function
* @returns {Network}
*/
run() {
this.forEach(
layer =>
layer.next && layer.run() && layer.next.apply(this.activation)
);
return this;
}
/**
* Backpropagate input and adjust weights based on error
* @param {Array} targets targets for the current dataset; must be as long
* as output layer
* @param {Number} [learningRate=0.5] learning rate; must be between 0 (excluded)
* and 1 (included)
* @param {Number} [momentum=Math.random()] momentum; must be between 0 (included) and 1
* (excluded)
* @example
* network
* .run()
* .backpropagate([1, 0]);
* @returns {Network}
*/
backpropagate(targets, learningRate = 0.5, momentum = Math.random()) {
let deltas = this.calculateDeltas(targets);
for (let currLayer = 0; currLayer < this.length; currLayer++) {
const layer = this[currLayer];
if (currLayer < this.length - 1) {
layer.weights = layer.weights.map((weightsRow, output) =>
weightsRow.map(
(weight, input) =>
learningRate *
deltas[currLayer + 1][output] *
layer.neurons[input] +
momentum * weight
)
);
}
if (currLayer > 0) {
layer.biases = layer.biases.map(
(bias, index) =>
learningRate * deltas[currLayer][index] +
momentum * bias
);
}
}
return this;
}
/**
* Calculate deltas / "error term" for output layers and hidden layers
* @param {Array} targets targets for the current dataset; must be as long
* as output layer
* @returns {Array} array of delta-arrays with the width of the Network - 1
* (every layer except input layer); every delta-array has the height of the
* corresponding layer (number of neurons in that layer)
*/
calculateDeltas(targets) {
let deltas = new Array(this.length)
.fill(0)
.map((_, layer) => new Array(this[layer].length).fill(0));
for (let currLayer = this.length - 1; currLayer > 0; currLayer--) {
const layer = this[currLayer];
deltas[currLayer] = deltas[currLayer].map((_, currNeuron) => {
// Calculate delta for output layer
if (currLayer === this.length - 1) {
return (
(targets[currNeuron] - layer.neurons[currNeuron]) *
this.activation.derivative(layer.rawNeurons[currNeuron])
);
}
// Calculate delta for hidden layer
else {
const previousLayer = this[currLayer + 1];
return (
previousLayer.neurons
.map(
(_, index) =>
deltas[currLayer + 1][index] *
this[currLayer].weights[index][currNeuron]
)
.reduce(plus, 0) *
this.activation.derivative(layer.rawNeurons[currNeuron])
);
}
});
}
return deltas;
}
/**
* Populates input layer with data
* @param {Array} data array of data to populate input layer with; must be
* same length as layer
* @returns {Network}
*/
populate(data) {
this[0].populate(data);
return this;
}
/**
* Populates input layer with data (alias: {@link Network#populate})
* @param {Array} data array of data to populate input layer with; must be
* same length as layer
* @returns {Network}
*/
feed(data) {
return this.populate(data);
}
/**
* Run the network with input data and return the prediction
* @param {Array} data array of data to populate input layer with; must be
* same length as layer
* @returns {Number} prediction based on input data
*/
predict(data) {
this.populate(data).run();
return this[this.length - 1].neurons;
}
}
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> on Wed Apr 07 2021 14:48:24 GMT+0200 (Central European Summer Time) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>