-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
64 lines (55 loc) · 1.68 KB
/
util.js
File metadata and controls
64 lines (55 loc) · 1.68 KB
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
// contains various utility functions
var cnnutil = (function(exports){
// a window stores _size_ number of values
// and returns averages. Useful for keeping running
// track of validation or training accuracy during SGD
var Window = function(size, minsize) {
this.v = [];
this.size = typeof(size)==='undefined' ? 100 : size;
this.minsize = typeof(minsize)==='undefined' ? 20 : minsize;
this.sum = 0;
}
Window.prototype = {
add: function(x) {
this.v.push(x);
this.sum += x;
if(this.v.length>this.size) {
var xold = this.v.shift();
this.sum -= xold;
}
},
get_average: function() {
if(this.v.length < this.minsize) return -1;
else return this.sum/this.v.length;
},
reset: function(x) {
this.v = [];
this.sum = 0;
}
}
// returns min, max and indeces of an array
var maxmin = function(w) {
if(w.length === 0) { return {}; } // ... ;s
var maxv = w[0];
var minv = w[0];
var maxi = 0;
var mini = 0;
for(var i=1;i<w.length;i++) {
if(w[i] > maxv) { maxv = w[i]; maxi = i; }
if(w[i] < minv) { minv = w[i]; mini = i; }
}
return {maxi: maxi, maxv: maxv, mini: mini, minv: minv, dv:maxv-minv};
}
// returns string representation of float
// but truncated to length of d digits
var f2t = function(x, d) {
if(typeof(d)==='undefined') { var d = 5; }
var dd = 1.0 * Math.pow(10, d);
return '' + Math.floor(x*dd)/dd;
}
exports = exports || {};
exports.Window = Window;
exports.maxmin = maxmin;
exports.f2t = f2t;
return exports;
})(typeof module != 'undefined' && module.exports); // add exports to module.exports if in node.js