-
Notifications
You must be signed in to change notification settings - Fork 3
/
vanilla_js.html
278 lines (230 loc) · 6.66 KB
/
vanilla_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
<html>
<!DOCTYPE html>
<html lang="en-CA">
<head>
<title>CCPN</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
</body>
<script>
var width = 100;
// create canvas element
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = width;
// create imageData object
var idata = ctx.createImageData(width, width);
canvas.style.height = "80vh";
document.body.appendChild(canvas);
const MAXTIME = 80;
var input = [];
var H = 8;
var w1 = [];
var w2 = [];
var w3 = [];
var wOut = [];
var h1 = [];
var h2 = [];
var h3 = [];
var output = [];
var rgbCoeffs = [255,255,255]//[80, 100, 128];
var rgbOffset = [0,0,0]//[0, 50, 127];
function generateInput() {
// generate input (x,y, 0)
for (let i = 0; i<width; i++) {
for (let j = 0; j<width; j++) {
input.push([i/width, j/width, 0]); //i*j/width/width
}
}
}
async function updateInput(t) {
// update input (x,y, t)
for (let i = 0; i<width*width; i++) {
input[i][2] = t; //Math.sin(t);
}
}
function generateWeights() {
// first layer weights
for (let i = 0; i<H; i++) {
let tempArr = [];
for (let j = 0; j<3; j++) {
tempArr.push(Math.random()*2-0.5);
}
w1.push(tempArr);
}
// second layer weights
for (let i = 0; i<H; i++) {
let tempArr = [];
for (let j = 0; j<H; j++) {
tempArr.push(Math.random()*2-0.5);
}
w2.push(tempArr);
}
// third layer weights
for (let i = 0; i<H; i++) {
let tempArr = [];
for (let j = 0; j<H; j++) {
tempArr.push(Math.random()*2-0.5);
}
w3.push(tempArr);
}
// output layer weights
for (let i = 0; i<3; i++) {
let tempArr = [];
for (let j = 0; j<H; j++) {
tempArr.push(Math.random()*2-0.5);
}
wOut.push(tempArr);
}
}
async function forwardProp() {
h1 = [];
h2 = [];
h3 = [];
output = [];
// input layer to first hidden layer
for (let i = 0; i<input.length; i++) {
let tempArr = [];
for (let j = 0; j<H; j++) {
let accumulator = 0;
for (let k = 0; k<3; k++) {
accumulator += input[i][k]*w1[j][k];
}
tempArr.push(Math.cos(accumulator));
}
h1.push(tempArr);
}
// first to second hidden layer
for (let i = 0; i<input.length; i++) {
let tempArr = [];
for (let j = 0; j<H; j++) {
let accumulator = 0;
for (let k = 0; k<H; k++) {
accumulator += h1[i][k]*w2[j][k];
}
tempArr.push(Math.tanh(accumulator));
}
h2.push(tempArr);
}
// second to third hidden layer
for (let i = 0; i<input.length; i++) {
let tempArr = [];
for (let j = 0; j<H; j++) {
let accumulator = 0;
for (let k = 0; k<H; k++) {
accumulator += h2[i][k]*w3[j][k];
}
tempArr.push(Math.cos(accumulator));
}
h3.push(tempArr);
}
// // third hidden layer to RGBA array
// for (let i = 0; i<input.length; i++) {
// let accumulator = 0;
// for (let k = 0; k<H; k++) {
// accumulator += h3[i][k]*wOut[0][k];
// }
// output.push(Math.floor((Math.sin(accumulator)/2+0.5)*80)); //r
// accumulator = 0;
// for (let k = 0; k<H; k++) {
// accumulator += h3[i][k]*wOut[1][k];
// }
// output.push(Math.floor((Math.sin(accumulator)/2+0.5)*128 + 127));
// accumulator = 0;
// for (let k = 0; k<H; k++) {
// accumulator += h3[i][k]*wOut[2][k];
// }
// output.push(Math.floor((Math.sin(accumulator)/2+0.5)*100+50));
// output.push(255); // alpha value
// }
// third hidden layer to RGBA array
for (let i = 0; i<input.length; i++) {
for (let j = 0; j<3; j++) { //rgb
let accumulator = 0;
for (let k = 0; k<H; k++) {
accumulator += h3[i][k]*wOut[j][k];
}
output.push(Math.floor((Math.sin(accumulator)/2+0.5)*rgbCoeffs[j]+rgbOffset[j]));
}
output.push(255); // alpha value
}
// // third hidden layer to RGBA array
// for (let i = 0; i<input.length; i++) {
// for (let j = 0; j<3; j++) { //rgb
// let accumulator = 0;
// for (let k = 0; k<H; k++) {
// accumulator += h3[i][k]*wOut[j][k];
// }
// output.push(Math.floor((Math.sin(accumulator)/2+0.5)*255));
// }
// output.push(255); // alpha value
// }
return(Uint8ClampedArray.from(output));
}
// // third hidden layer to output layer
// for (let i = 0; i<input.length; i++) {
// let tempArr = [];
// for (let j = 0; j<3; j++) {
// let accumulator = 0;
// for (let k = 0; k<H; k++) {
// accumulator += h3[i][k]*wOut[j][k];
// }
// tempArr.push(Math.floor((Math.sin(accumulator)/2+0.5)*255));
// }
// //tempArr.push(255); // alpha value
// output.push(tempArr);
// }
// // Output layer 2d array to RGBA array
// for(let pos = 0; pos < width*width; pos++) {
// buffer[pos*4 ] = output[pos][0]; // some R value
// buffer[pos*4+1] = output[pos][1]; // some G value
// buffer[pos*4+2] = output[pos][2]; // some B value
// buffer[pos*4+3] = 255; // set alpha channel
// }
async function updateCanvas(buffer) {
// set our buffer as source
idata.data.set(buffer);
// update canvas with new data
ctx.putImageData(idata, 0, 0);
}
generateWeights();
generateInput();
// var t=0;
// var outer = setInterval(function() {
// if(t > 8*Math.PI){
// clearInterval(outer);
// }
// else{
// t = t + 0.015;
// updateInput(t);
// buffer = forwardProp();
// updateCanvas(buffer);
// //console.log(t);
// }
// }, 5);
var t=0;
async function drawFrame() {
//t = performance.now()*SPEED;
t = t+0.015;
//console.log(t);
await updateInput(t);
buffer = await forwardProp();
await updateCanvas(buffer);
if(t < MAXTIME){
window.requestAnimationFrame(drawFrame);
}
else {
console.table( tf.memory() );
//tf.memory('numBytesInGPU')['numBytesInGPU']/1e6
}
}
window.requestAnimationFrame(drawFrame);
// run when the document is loaded
// document.addEventListener('DOMContentLoaded', function() {
// end of onload function
// }, false);
</script>
</html>