-
Notifications
You must be signed in to change notification settings - Fork 11
/
autoencoder.js
125 lines (106 loc) · 3.57 KB
/
autoencoder.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
/*
Copyright 2015 One Zero Capital
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Autoencoder = function(headers, splits, Ws, bs) {
this.headers = headers;
this.splits = splits;
this.Ws = Ws;
this.bs = bs;
}
Autoencoder.deserialize = function(data) {
return new Autoencoder(data['headers'],
data['splits'],
data['Ws'],
data['bs']);
}
Autoencoder.prototype.getOutput = function(row) {
var input = [];
for (var i = 0; i < this.splits.length; i++) {
var j = this.splits[i][0];
var x = this.splits[i][1];
if (row[j] == undefined)
input.push(0.0);
else if (row[j] < x)
input.push(1.0);
else if (row[j] > x)
input.push(-1.0);
}
// Normalize input
var k = input.map(Math.abs).reduce(function(a, b) { return a + b;}, 0);
var row = input.map(function(x) { return x * Math.pow(k+1, -0.5); });
for (var layer = 0; layer < this.Ws.length; layer++) {
// console.log(this.Ws[layer].length + ' * ' + this.Ws[layer][0].length);
// Compute row * Ws[layer] + bs[layer]
var nextRow = [];
for (var b = 0; b < this.bs[layer].length; b++)
nextRow.push(this.bs[layer][b]);
for (var a = 0; a < this.Ws[layer].length; a++)
for (var b = 0; b < this.Ws[layer][0].length; b++)
nextRow[b] += row[a] * this.Ws[layer][a][b];
if (layer < this.Ws.length - 1) {
for (var b = 0; b < this.bs[layer].length; b++)
nextRow[b] = Math.max(0, nextRow[b]); // relu
} else {
for (var b = 0; b < this.bs[layer].length; b++)
nextRow[b] = 1.0 / (1 + Math.exp(-nextRow[b]));
}
row = nextRow
}
return row;
}
Autoencoder.prototype.getCdfs = function(row) {
var row = this.getOutput(row);
var cdfs = [];
for (var i = 0; i < this.splits.length; i++) {
var j = this.splits[i][0];
var x = this.splits[i][1];
while (cdfs.length <= j)
cdfs.push({'xy': []});
cdfs[j].xy.push({'x': x, 'y': row[i]});
}
return cdfs;
}
Autoencoder.prototype.getPdfs = function(row, points) {
// We cheat a bit and define some smoothing
// Let's approximate the CDF as a sum of sigmoids:
// CDF = sum_i (row[i+1] - row[i]) * sigm((x' - x) / D)
// Where i2 is a fractional version of i
// Then take the derivative to get the PDF
var cdfs = this.getCdfs(row);
if (points == undefined)
points = 100;
var pdfs = [];
for (var j = 0; j < row.length; j++) {
pdfs.push({'xy': [], 'xyQuartile': []});
var D = (cdfs[j].xy[cdfs[j].xy.length-1].x - cdfs[j].xy[0].x) / cdfs[j].xy.length;
// console.log(cdfs[j]);
for (var p = 0; p < points; p++) {
var x = cdfs[j].xy[0].x + (cdfs[j].xy[cdfs[j].xy.length-1].x - cdfs[j].xy[0].x) * p / (points - 1);
var y = 0.0;
var yCdf = 0.0;
for (var i = 0; i < cdfs[j].xy.length; i++) {
var xp = cdfs[j].xy[i].x;
var d = cdfs[j].xy[i].y;
if (i > 0) d -= cdfs[j].xy[i-1].y;
var delta = (x - xp) / D;
var s = 1.0 / (1 + Math.exp(-delta));
y += d * s * (1 - s) / D;
yCdf += d * s;
}
pdfs[j].xy.push({'x': x, 'y': y});
if (yCdf > 0.25 && yCdf < 0.75) {
pdfs[j].xyQuartile.push({'x': x, 'y': y});
}
}
}
return pdfs;
}