-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
450 lines (400 loc) · 11.3 KB
/
main.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
let currentEpoch = 0
// you can toogle the learning_rate to see the impacts
// you can learn more about it reading the references at README
const learning_rate = .003
// initializing the weights
let w1 = initializeWeight()
let w2 = initializeWeight()
let w3 = initializeWeight()
let w4 = initializeWeight()
let w5 = initializeWeight()
let w6 = initializeWeight()
// initializing the bias
b1 = 0
b2 = 0
b3 = 0
b4 = 0
// linear functions
const f1 = x => w1 * x + b1
const f2 = x => w2 * x + b2
const f3 = x => w3 * x + b3
// activation function. You chan choose in the web
let a = getActivationFuncion('softplus')
// returns a cubic function with x range from -1 to 1
const { x, y } = getFakeData()
// run first foward propagation and return the first random prediction
let y_pred = getPredicts(x)
// this is the most important function!
// It will recive how many epochs you wish to run and for each epoch it will:
// - Do a backpropagation (Gradient Decentent)
// - update the weights and bieases
// - update the predictions points
// - call the function to update the chart
// - call the function to update the Network Graph
// you can learn more about it reading the references at README
function runEpochs(epochs) {
let epoch = 0
while (epoch < epochs) {
const dSSR_w1 = getGradient1(w4, f1)
const dSSR_w2 = getGradient1(w5, f2)
const dSSR_w3 = getGradient1(w6, f3)
const dSSR_w4 = getGradient2(f1)
const dSSR_w5 = getGradient2(f2)
const dSSR_w6 = getGradient2(f3)
const dSSR_b1 = getGradientBias1(w4, f1)
const dSSR_b2 = getGradientBias1(w5, f2)
const dSSR_b3 = getGradientBias1(w6, f3)
const dSSR_b4 = getGradientBias2()
const step_size_dSSR_w1 = dSSR_w1 * learning_rate
const step_size_dSSR_w2 = dSSR_w2 * learning_rate
const step_size_dSSR_w3 = dSSR_w3 * learning_rate
const step_size_dSSR_w4 = dSSR_w4 * learning_rate
const step_size_dSSR_w5 = dSSR_w5 * learning_rate
const step_size_dSSR_w6 = dSSR_w6 * learning_rate
const step_size_dSSR_b1 = dSSR_b1 * learning_rate
const step_size_dSSR_b2 = dSSR_b2 * learning_rate
const step_size_dSSR_b3 = dSSR_b3 * learning_rate
const step_size_dSSR_b4 = dSSR_b4 * learning_rate
w1 = w1 - step_size_dSSR_w1
w2 = w2 - step_size_dSSR_w2
w3 = w3 - step_size_dSSR_w3
w4 = w4 - step_size_dSSR_w4
w5 = w5 - step_size_dSSR_w5
w6 = w6 - step_size_dSSR_w6
b1 = b1 - step_size_dSSR_b1
b2 = b2 - step_size_dSSR_b2
b3 = b3 - step_size_dSSR_b3
b4 = b4 - step_size_dSSR_b4
y_pred = getPredicts(x)
epoch++
}
currentEpoch = currentEpoch + epoch
document.getElementById('epochs').textContent = currentEpoch
document.getElementById('loss').textContent = getLoss()
myChart.data.datasets[1].data = y_pred
myChart.update()
drawNetworkGrph()
}
// just to initialize the view
document.addEventListener("DOMContentLoaded", () => {
// just to plot the real data and the prediction
plotGraph()
// this will plot the network graph (input, output and neurons)
drawNetworkGrph()
document.getElementById('loss').textContent = getLoss()
});
// this function recieves the original x values and return the predicted y values
function getPredicts(x) {
return x.map(x => {
return a(f1(x)) * w4 + a(f2(x)) * w5 + a(f3(x)) * w6 + b4
})
}
// this functions will verify how good our network is using residual sum of squares
// it's not the best aproach because it can leave to local minima, but it's easier to explain
function getLoss() {
return sumArray(y.map((y, i) => Math.pow((y - y_pred[i]), 2)))
}
// this will generate a random value ranging from -1 to 1
function initializeWeight() {
return Math.random() * isPositive()
}
// function to change the activation function using the select from ui
// this do not change the gradients, but it's fun to test.
// ReLu shows great results in many cases
function setActivationFunction() {
var val = document.getElementById("select").value
a = getActivationFuncion(val)
}
function getGradient1(w, f) {
return sumArray(x.map((x, i) => {
return -2 * (y[i] - y_pred[i]) * w * x * Math.pow(Math.E, f(x)) / (1 + Math.pow(Math.E, f(x)))
}))
}
function getGradient2(f) {
return sumArray(x.map((x, i) =>
-2 * (y[i] - y_pred[i]) * a(f(x))
))
}
function getGradientBias1(w, f) {
return sumArray(x.map((x, i) =>
-2 * (y[i] - y_pred[i]) * w * Math.pow(Math.E, f(x)) / (1 + Math.pow(Math.E, f(x)))
))
}
function getGradientBias2() {
return sumArray(x.map((_, i) =>
-2 * (y[i] - y_pred[i])
))
}
function sumArray(arr) {
return arr.reduce((a, b) => a + b, 0)
}
// more about activation functions can be found in README.md
function getActivationFuncion(name) {
const functions = {
'sigmoid': x => 1 / (1 + Math.pow(Math.E, -x)),
'relu': x => x < 0 ? 0 : x,
'softplus': x => Math.log(1 + Math.pow(Math.E, x)),
'tanh': x => Math.tanh(x)
}
return functions[name]
}
function isPositive() {
return Math.random() > 0.5 ? -1 : 1
}
function getFakeData() {
let a = Math.random() * isPositive() * 1.1
let b = (Math.random() * isPositive()) / 3
let c = Math.random() * isPositive()
const range = function (start, stop, step) {
step = step || 1
let arr = []
for (let i = start; i < stop; i += step) {
arr.push(i)
}
return arr
}
const x = range(-1, 1, .01)
return {
x,
y: x.map(x => a * Math.pow(x, 3) + b * Math.pow(x, 2) + c)
}
}
function plotGraph() {
const data = {
labels: x,
datasets: [
{
label: 'Real Data',
data: y,
backgroundColor: 'rgb(255, 99, 132)',
},
{
label: 'Predicted',
data: y_pred,
backgroundColor: "rgba(0,255,0,.1)",
},
]
}
let ctx = document.getElementById('myChart').getContext('2d')
myChart = new Chart(ctx, {
type: 'line',
data: data,
options: {
responsive: true,
scales: {
x: {
type: 'linear'
},
}
},
})
}
// the next functions are used just to prety print the Network Graph
function normalize(val, max, min) {
return (val - min) / (max - min)
}
function normalize_array(arr) {
const hold_normed_values = []
const max = Math.max.apply(null, arr)
const min = Math.min.apply(null, arr)
arr.forEach((this_num) => {
const val = normalize(this_num, max, min) + .1
hold_normed_values.push(val > 1 ? 1 : val)
})
return hold_normed_values
}
function getSum(f) {
return sumArray(x.map(x => {
return a(f(x))
}))
}
function drawNetworkGrph() {
const [a1, a2, a3] = normalize_array([getSum(f1), getSum(f2), getSum(f3)])
const ws1 = normalize_array([w1 + b1, w2 + b2, w3 + b3])
const ws2 = normalize_array([w4, w5, w6])
const len = y.length
const [input, output] = [sumArray(normalize_array((y))) / len, sumArray(normalize_array((y_pred))) / len]
const elements = [{
"data": {
"id": "e121",
opacity: ws1[0],
"weight": 19,
"source": "input",
"target": "a(z1)",
label: "w1 * x + b1"
},
"position": {},
"group": "edges"
},
{
"data": {
"id": "e272",
opacity: ws1[2],
"weight": 77,
"source": "input",
"target": "a(z3)",
label: "w3 * x + b3"
},
"position": {},
"group": "edges"
},
{
"data": {
"id": "e295",
opacity: ws1[1],
"weight": 98,
"source": "input",
"target": "a(z2)",
label: "w2 * x + b2"
},
"position": {},
"group": "edges"
},
{
"data": {
"id": "aaa",
opacity: ws2[1],
"weight": 98,
"source": "a(z2)",
label: "w5",
"target": "output"
},
"position": {},
"group": "edges"
},
{
"data": {
"id": "bbb",
opacity: ws2[0],
"weight": 98,
"source": "a(z1)",
label: "w4",
"target": "output"
},
"position": {},
"group": "edges"
},
{
"data": {
"id": "c",
opacity: ws2[2],
"weight": 98,
"source": "a(z3)",
label: "w6",
"target": "output"
},
"position": {},
"group": "edges"
},
{
"data": {
"id": "input",
opacity: input,
"weight": 77
},
"position": {
"x": 150,
"y": 0
},
"group": "nodes"
},
{
"data": {
"id": "a(z3)",
opacity: a3,
"weight": 3
},
"position": {
"x": 250,
"y": 100
},
"group": "nodes"
},
{
"data": {
"id": "a(z2)",
opacity: a2,
"weight": 33
},
"position": {
"x": 150,
"y": 100
},
"group": "nodes"
},
{
"data": {
"id": "a(z1)",
opacity: a1,
"weight": 23
},
"position": {
"x": 50,
"y": 100
},
"group": "nodes"
},
{
"data": {
"id": "output",
opacity: output,
"weight": 65
},
"position": {
"x": 150,
"y": 200
},
"group": "nodes"
}
]
if (window.cy && window.cy.json) {
window.cy.json({ elements })
return
}
window.cy = cytoscape({
userZoomingEnabled: false,
userPanningEnabled: false,
boxSelectionEnabled: false,
container: document.getElementById('cy'),
layout: {
name: 'preset',
fit: true
},
style: [
{
selector: 'node',
style: {
'height': 12,
'width': 12,
"label": "data(id)",
'background-color': '#ff0000',
'opacity': "data(opacity)",
'text-valign':'center',
'text-halign':'left',
}
},
{
selector: 'edge',
style: {
'curve-style': 'haystack',
'haystack-radius': 0,
'width': 3,
'opacity': "data(opacity)",
"font-size": "10px",
'line-color': '#000000'
}
},
{
selector: "edge[label]",
css: {
"label": "data(label)",
"text-rotation": "autorotate",
color: 'black',
"text-margin-x": "11px",
"text-margin-y": "0px"
}
},
],
elements
})
}