-
Notifications
You must be signed in to change notification settings - Fork 11
/
autoencoder.js
150 lines (129 loc) · 4.49 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
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
/*
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;
this.splitsByJ = [];
for (var i = 0; i < this.splits.length; i++) {
var j = this.splits[i][0];
var x = this.splits[i][1];
while (this.splitsByJ.length <= j) this.splitsByJ.push([]);
this.splitsByJ[j].push(x);
}
// Smoothing factors. These are pretty arbitrary numbers, mainly picked because they seem to work
this.Ds = [];
for (var j = 0; j < this.splitsByJ.length; j++) {
this.Ds.push([]);
var oldD = (this.splitsByJ[j][this.splitsByJ[j].length - 1] - this.splitsByJ[j][0]) / this.splitsByJ[j].length,
eps = 0.1;
for (var i = 0; i < this.splitsByJ[j].length; i++) {
var iLo = i > 0 ? i - 1 : i;
var iHi = i < this.splitsByJ[j].length - 1 ? i + 1 : i;
this.Ds[j].push((this.splitsByJ[j][iHi] - this.splitsByJ[j][iLo]) / (iHi - iLo) + eps * oldD);
}
}
}
Autoencoder.deserialize = function(data) {
return new Autoencoder(data['headers'],
data['splits'],
data['Ws'],
data['bs']);
}
Autoencoder.prototype.getOutput = function(row) {
var input = [];
for (var j = 0; j < this.splitsByJ.length; j++) {
for (var i = 0; i < this.splitsByJ[j].length; i++) {
var x = this.splitsByJ[j][i];
// Instead of encoding it as a -1 or +1 input, we smooth it using a tanh representation
// See description in getPdfs, it's the same idea
var D = this.Ds[j][i];
if (row[j] == undefined) {
input.push(0.0);
} else {
input.push(Math.tanh((x - row[j]) / D));
}
}
}
// Normalize input
var k = 0;
for (var j = 0; j < row.length; j++)
k += row[j] != undefined ? 1 : 0;
var row = input.map(function(x) { return x * (k > 0 ? Math.pow(k, -0.5) : 0.0); });
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': []});
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 D = this.Ds[j][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;
}