-
Notifications
You must be signed in to change notification settings - Fork 3
/
catseye.js
258 lines (241 loc) · 5.42 KB
/
catseye.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
//---------------------------------------------------------
// Cat's eye
//
// ©2016 Yuichiro Nakada
//---------------------------------------------------------
var CATS = {
TYPE :0, // MLP, Conv
ACT :1, // activation function type
CHANNEL :2,
SIZE :3, // input size (ch * x * y)
XSIZE :4, // width
YSIZE :5, // height
KSIZE :6, // kernel size
STRIDE :7,
LPLEN :8, // length of layer params
};
// activation function
var act = {};
act[0] = function(x) { // identity
return x;
};
act[2] = function(x) { // sigmoid
return 1/(1+Math.exp(-x));
};
act[3] = function(x) { // tanh
return tanh(x);
};
act[5] = function(x) { // ReLU
return (x>0 ? x : 0);
};
act[6] = function(x) { // LeakyReLU
return (x>0 ? x : x*0.01);
};
var layer_forward = {};
layer_forward[0] = function(s, w, o, uu, u) // linear
{
var s_in = uu[CATS.SIZE]+1; // +1 -> for bias
var s_out = u[CATS.SIZE];
var a = u[CATS.ACT];
console.log("mlp:" + s_in +" "+ s_out +" "+ a);
for (var i=0; i<s_out; i++) {
var z = 0;
for (var j=0; j<s_in; j++) {
z += w[j*s_out+i]*s[j];
}
o[i] = act[a](z);
}
};
layer_forward[1] = function(s, w, o, uu, u) // convolutional
{
var m = Math.floor(u[CATS.KSIZE]/2)*2;
var sx = u[CATS.XSIZE] - m; // out
var sy = u[CATS.YSIZE] - m;
var k2 = u[CATS.KSIZE] * u[CATS.KSIZE];
var n = u[CATS.CHANNEL] * k2;
var size = uu[CATS.SIZE]/uu[CATS.CHANNEL];
var step = u[CATS.XSIZE]-u[CATS.KSIZE];
var a = u[CATS.ACT];
console.log(sx +" "+ sy +" "+ a);
//console.log(s);
/*var m = 0;
for (var c=0; c<u[CATS.CHANNEL]; c++) { // out
for (var y=0; y<sy; y++) {
for (var x=0; x<sx; x++) {
var z = 0;
for (var cc=0; cc<uu[CATS.CHANNEL]; cc++) { // in
var k = c*k2 + cc*n;
var p = (size*cc) + y*u[CATS.XSIZE]+x; // in
for (var wy=u[CATS.KSIZE]; wy>0; wy--) {
for (var wx=u[CATS.KSIZE]; wx>0; wx--) {
z += s[p++] * w[k++];
}
p += step;
}
}
o[m++] = act[a](z);
}
}
}*/
for (var c=u[CATS.CHANNEL]*sx*sy-1; c>=0; c--) { // out
o[c] = 0;
}
var oo = 0, z, ss = 0, p, r, k = 0;
for (var c=0; c<u[CATS.CHANNEL]; c++) { // out
r = 0;
for (var cc=0; cc<uu[CATS.CHANNEL]; cc++) { // in
for (var wy=u[CATS.KSIZE]; wy>0; wy--) {
for (var wx=u[CATS.KSIZE]; wx>0; wx--) {
p = ss++;
z = oo;
for (var y=0; y<sy; y++) {
for (var x=0; x<sx; x++) {
o[z++] += s[p++] * w[k];
}
p += m;
}
k++;
}
ss += step;
}
r += u[CATS.XSIZE] * u[CATS.YSIZE];
ss = r;
}
oo = z;
ss = 0;
}
for (var c=u[CATS.CHANNEL]*sx*sy-1; c>=0; c--) { // out
o[c] = act[a](o[c]);
}
console.log("min:"+Math.min.apply(null, o) +" max:"+ Math.max.apply(null, o));
};
layer_forward[2] = function(s, w, o, uu, u) // maxpooling
{
var sx = u[CATS.XSIZE];
var sy = u[CATS.YSIZE];
var m = 0;
for (var c=0; c<u[CATS.CHANNEL]; c++) {
for (var y=0; y<sy-1; y+=u[CATS.STRIDE]) {
for (var x=0; x<sx-1; x+=u[CATS.STRIDE]) {
var n = c*sx*sy + y*sx+x;
var a = s[n];
for (var wy=u[CATS.KSIZE]; wy>0; wy--) {
for (var wx=u[CATS.KSIZE]; wx>0; wx--) {
if (a<s[n]) a =s[n];
n++;
}
n += sx-u[CATS.KSIZE];
}
o[m++] = a;
}
}
}
};
function _CatsEye(w, u)
{
this.w = w;
this.u = u;
var n = 0;
this.o = [];
for (var uu in u) {
this.o[n++] = [];
}
}
_CatsEye.prototype = {
// caluculate forward propagation of input x
forward: function(x)
{
// calculation of input layer
//this.o[0][u[0][CATS.SIZE]] = 1;
x[u[0][CATS.SIZE]] = 1;
layer_forward[u[1][CATS.TYPE]](/*this.o[0]*/x, this.w[0], this.o[1], u[0], u[1]);
for (var i=1; i<u.length-1; i++) {
this.o[i][u[i][CATS.SIZE]] = 1;
layer_forward[u[i+1][CATS.TYPE]](this.o[i], this.w[i], this.o[i+1], u[i], u[i+1]);
}
},
predict: function(x)
{
// forward propagation
this.forward(x);
// biggest output means most probable label
var n = u.length-1;
var max = this.o[n][0];
var ans = 0;
for (var i=1; i<u[n][CATS.SIZE]; i++) {
console.log("ans:"+this.o[n][i]);
if (this.o[n][i] > max) {
max = this.o[n][i];
ans = i;
}
}
for (i=0; i<u[n][CATS.SIZE]; i++) this.o[n][i] /= max;
return ans;
}
};
function CatsEye(_in, _hid, _out, w1, w2) {
this.in = _in;
this.hid = _hid;
this.out = _out;
// input layer
this.xi1 = [];
this.xi2 = [];
this.xi3 = [];
// output layer
this.o1 = [];
this.o2 = [];
this.o3 = [];
// error value
this.d2 = [];
this.d3 = [];
// weights
this.w1 = w1;
this.w2 = w2;
};
CatsEye.prototype = {
// activation function
sigmoid: function(x)
{
return 1/(1+Math.exp(-x));
},
// caluculate forward propagation of input x
forward: function(x)
{
// calculation of input layer
x[this.in] = 1;
// caluculation of hidden layer
for (j=0; j<this.hid; j++) {
this.xi2[j] = 0;
for (i=0; i<this.in+1; i++) {
this.xi2[j] += this.w1[i*this.hid+j]*x[i];
}
this.o2[j] = this.sigmoid(this.xi2[j]);
}
this.o2[this.hid] = 1;
// caluculation of output layer
for (j=0; j<this.out; j++) {
this.xi3[j] = 0;
for (i=0; i<this.hid+1; i++) {
this.xi3[j] += this.w2[i*this.out+j]*this.o2[i];
}
this.o3[j] = this.xi3[j];
//this.o3[j] = this.sigmoid(this.xi3[j]);
}
},
predict: function(x)
{
// forward propagation
this.forward(x);
// biggest output means most probable label
max = this.o3[0];
ans = 0;
for (i=1; i<this.out; i++) {
if (this.o3[i] > max) {
max = this.o3[i];
ans = i;
}
}
for (i=0; i<this.out; i++) this.o3[i] /= max;
return ans;
}
};