-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.js
228 lines (208 loc) · 9.24 KB
/
helper.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
var helperjs = helperjs || { REVISION: 'ALPHA' };
(function(global) {
"use strict";
// syntax sugar from karpathy
var getopt = function(opt, field, defaultval) {
if(opt.hasOwnProperty(field)) {
return opt[field];
} else {
return defaultval;
}
}
var helper = function(opt) {
var opt = opt || {};
this.perplexity = getopt(opt, "perplexity", 30); // effective number of nearest neighbors
this.dim = getopt(opt, "dim", 2); // by default 2-D tSNE
this.epsilon = getopt(opt, "epsilon", 10); // learning rate
this.iter = 0;
}
var mapRange = function(from, to, s) {
return to[0] + (s - from[0]) * (to[1] - to[0]) / (from[1] - from[0]);
};
helper.prototype = {
normalizeBetweenLayers: function(network, points, collapse_input) {
var count = 0;
var idx=0;
var layers = network.layers.length;
var links = [];
var start = 0;
var end = 0;
for (var l=0; l<(layers); l++) {
var L = network.layers[l];
var neurons = L.out_depth*L.out_sx*L.out_sy;
if (L.layer_type != "fc" && L.layer_type != "conv") {
// Good current layer
console.log("Layer "+l+": "+L.layer_type+" this: "+neurons);
var next_l = 1;
if (l < layers-1) {
while (network.layers[l+next_l].layer_type == "fc"
|| network.layers[l+next_l].layer_type == "conv") {
next_l++;
}
var nl = l+next_l;
var Ln = network.layers[nl];
var next = Ln.out_depth*Ln.out_sx*Ln.out_sy;
if (L.layer_type == "input" && collapse_input > 0) {
neurons = collapse_input;
}
start = links.length;
end = start;
for (var n=0; n<neurons; n++) {
if (n==0) {
console.log("\tto Layer: "+nl+" type: "+Ln.layer_type+" next: "+next);
}
for (var n2=0; n2<next; n2++) {
var val = 1.0-Math.abs(points[count+neurons+n2]-points[count+n]);
links.push(val);
end++;
}
}
}
var min = Math.min.apply(null, links.slice(start, end));
var max = Math.max.apply(null, links.slice(start, end));
console.log("BETWEEN layer "+l+" neur: "+neurons+" from: "+start+" :: "+end);
console.log("\told min: "+min+" max: "+max);
for (var n=start; n<end; n++) {
links[n] = mapRange([min, max], [0, 1], links[n]);
}
min = Math.min.apply(null, links.slice(start, end));
max = Math.max.apply(null, links.slice(start, end));
console.log("\tnew min: "+min+" max: "+max);
console.log("\t"+count+" neurons");
count = count + neurons;
}
}
console.log("Links data: "+links.length);
return links;
},
normalizeByLayers: function(network, data, start, end) {
var layers = network.layers.length;
var x = 0;
var last = Math.min(end, layers);
for (var l=start; l<last; l++) {
var L = network.layers[l];
if (L.layer_type != "fc" && L.layer_type != "conv" && L.layer_type != "regression") {
var neurons = L.out_depth*L.out_sx*L.out_sy;
var l_norm = 0;
var min = Math.min.apply(null, data.slice(x,x+neurons));
var max = Math.max.apply(null, data.slice(x,x+neurons));
//console.log(data.slice(x,x+neurons)+" min: "+min);
console.log("By Layers:"+ l+" "+neurons+" min: "+min+" max: "+max);
for (var n=0; n<neurons; n++) {
data[x+n] = mapRange([min, max], [0, 1], data[x+n]);
}
min = Math.min.apply(null, data.slice(x,x+neurons));
max = Math.max.apply(null, data.slice(x,x+neurons));
console.log("\tmin: "+min+" max: "+max);
x = x+neurons;
}
}
return data;
},
dataToSankey: function(network, points, links, opts, filename, collapse_input, slim_thresh) {
var data = {"nodes":[], "links":[], "meta":[], "opt":opts};
// for each layer, add nodes for each neuron
var count = 0;
var layer_count = 0;
var idx=0;
var used=0;
var layers = network.layers.length;
for (var l=0; l<(layers); l++) {
var L = network.layers[l];
var neurons = L.out_depth*L.out_sx*L.out_sy;
if (L.layer_type != "fc" && L.layer_type != "conv") {
console.log("Layer "+l+": "+L.layer_type+" this: "+neurons);
// Good current layer
if (L.layer_type == "input" && collapse_input > 0) {
neurons = collapse_input;
}
//else {
for (var n=0; n<neurons; n++) {
var name = "Class "+n;
//console.log(name + " "+points[count+n]+" "+count+" "+n+ " ");
data.nodes.push({"name":name,"layer":L.layer_type,"col":layer_count,"value":points[count+n],"num":count+n});
data.meta.push({"size":10+n%5,"pos":points[count+n]});
var next_l = 1;
if (l < layers-1) {
while (network.layers[l+next_l].layer_type == "fc"
|| network.layers[l+next_l].layer_type == "conv") {
next_l++;
}
var nl = l+next_l;
var Ln = network.layers[nl];
var next = Ln.out_depth*Ln.out_sx*Ln.out_sy;
if (n==0) {
console.log("\tto Layer: "+nl+" type: "+Ln.layer_type+" next: "+next);
}
for (var n2=0; n2<next; n2++) {
if (L.layer_type == "input" ){ //&& collapse_input > 0) {
var val = 0.6;
data.links.push({"source":count+n,
"target":count+neurons+n2,
"value":val,
"v1":points[count+n],
"v2":points[count+neurons+n2],
"idx":idx});
}
else if (Ln.layer_type == "regression") {
var val = links[idx];
var val = Math.random();
if (val > slim_thresh) {
data.links.push({"source":count+n,
"target":count+neurons+n2,
"value":val,
"v1":points[count+n],
"v2":points[count+neurons+n2],
"idx":idx});
used++;
}
}
else {
var val = links[idx];
if (val > slim_thresh) {
data.links.push({"source":count+n,
"target":count+neurons+n2,
"value":val,
"v1":points[count+n],
"v2":points[count+neurons+n2],
"idx":idx});
used++;
}
}
idx++;
}
}
}
//}
console.log("\t"+count+" neurons");
count = count + neurons;
layer_count++;
}
}
count += next;
console.log("\t"+count+" neurons TOTAL");
console.log("\t"+(idx-1)+" connections TOTAL");
console.log("\t"+links.length+" normalized links");
console.log("\t"+used+" connections used");
var fs = require('fs');
var str = JSON.stringify(data, null, 2);
fs.writeFile(filename, str, function(err) {
if (err) {
console.log(err);
} else {
console.log("json saved to "+filename);
}
});
}
}
global.helper = helper;
})(helperjs);
// export the library to window, or to module in nodejs
(function(lib) {
"use strict";
if (typeof module === "undefined" || typeof module.exports === "undefined") {
window.helperjs = lib; // in ordinary browser attach library to window
} else {
module.exports = lib; // in nodejs
}
})(helperjs);