-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
190 lines (161 loc) · 4.66 KB
/
ui.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
// Visual SVD
// Scott Clayton
var RefreshMatrices = function () {};
var rows = 5;
var cols = 5;
var features = 3;
var matrix = [
[0,9,0,0,10],
[3,0,0,5,0],
[0,1,0,0,0],
[0,5,1,0,3],
[0,0,0,0,7]
];
var predictions = [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
];
var error = [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
];
var latentWide = [
[0.899, 0.762, 0.870],
[0.922, 0.771, 0.439],
[0.767, 0.939, 0.877],
[0.498, 0.691, 0.301],
[0.985, 0.067, 0.241]
];
var latentTall = [
[0.571, 0.171, 0.634],
[0.466, 0.181, 0.921],
[0.841, 0.648, 0.492],
[0.728, 0.671, 0.091],
[0.649, 0.721, 0.699]
];
var $matrixActual = $("#matrixActual");
var $matrixPredict = $("#matrixPredict");
var $matrixError = $("#matrixError");
var $rmse = $("#rmse");
var $iterations = $("#iterations");
$(document).ready(function() {
CreateMatrixInputs($matrixActual, rows, cols);
CreateMatrixElements($matrixPredict, rows, cols, features);
CreateMatrixElements($matrixError, rows, cols, 0);
RefreshMatrices = function () {
// Update the known values matrix
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
UpdateMatrixValue($matrixActual, r, c, matrix[r][c]);
}
}
// Update the latent feature matrices
for (var r = rows; r < rows + features; r++) {
for (var c = 0; c < cols; c++) {
UpdateMatrixValue($matrixPredict, r, c, latentWide[c][r - rows]);
}
}
for (var r = 0; r < rows; r++) {
for (var c = cols; c < cols + features; c++) {
UpdateMatrixValue($matrixPredict, r, c, latentTall[r][c - cols]);
}
}
// Update the predictions matrix
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
UpdateMatrixValue($matrixPredict, r, c, predictions[r][c]);
if (matrix[r][c] == 0) {
AddMatrixClass($matrixPredict, r, c, "m-predict");
} else {
RemoveMatrixClass($matrixPredict, r, c, "m-predict");
}
}
}
// Update the error matrix
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
ChangeMatrixValue($matrixError, r, c, error[r][c]);
if (error[r][c] != 0) {
RemoveMatrixClass($matrixError, r, c, "m-empty");
AddMatrixClass($matrixError, r, c, "m-error");
}
}
}
};
RefreshMatrices();
});
$("#btnGo").click(function() {
$("#btnGo").hide();
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
matrix[r][c] = +$matrixActual.children().eq(r).children().eq(c).val();
}
}
RefreshMatrices();
var maxTraining = 100;
var loop = setInterval(function() {
svd.factorize(matrix, latentWide, latentTall, predictions, error, 1);
RefreshMatrices();
$rmse.html(svd.rmse);
$iterations.html(svd.totalIterations);
if (svd.totalIterations >= maxTraining) {
clearInterval(loop);
console.log("Done!");
//$("#btnGo").show();
}
}, 1000);
});
// Create the <div> elements that make up a matrix
var CreateMatrixElements = function ($matrix, rows, cols, features) {
for (var r = 0; r < rows; r++) {
var newRow = $("<div></div>");
for (var c = 0; c < cols + features; c++) {
var newCol = $("<div class=\"m-cell" + (c >= cols ? " m-feat" : " m-empty") + "\">x</div>");
newRow.append(newCol);
}
$matrix.append(newRow);
}
for (var r = 0; r < features; r++) {
var newRow = $("<div></div>");
for (var c = 0; c < cols; c++) {
var newCol = $("<div class=\"m-cell m-feat\">x</div>");
newRow.append(newCol);
}
$matrix.append(newRow);
}
};
// Create the <input> elements that make up a matrix
var CreateMatrixInputs = function ($matrix, rows, cols) {
for (var r = 0; r < rows; r++) {
var newRow = $("<div></div>");
for (var c = 0; c < cols; c++) {
var newCol = $("<input type=\"text\" class=\"m-cell" + (c >= cols ? " m-feat" : " m-empty") + "\" />");
newRow.append(newCol);
}
$matrix.append(newRow);
}
};
// Set the value and appropriate class of a cell in the matrix
var UpdateMatrixValue = function ($matrix, row, col, value) {
ChangeMatrixValue($matrix, row, col, value);
RemoveMatrixClass($matrix, row, col, "m-empty");
AddMatrixClass($matrix, row, col, value != 0 ? "m-good" : "m-empty");
};
// Set the value of a specific cell in a matrix
var ChangeMatrixValue = function ($matrix, row, col, value) {
$matrix.children().eq(row).children().eq(col).html(+value.toFixed(2)).val(+value.toFixed(2));
};
// Set a class of a specific cell in a matrix
var AddMatrixClass = function ($matrix, row, col, value) {
$matrix.children().eq(row).children().eq(col).addClass(value);
};
// Remove a class of a specific cell in a matrix
var RemoveMatrixClass = function ($matrix, row, col, value) {
$matrix.children().eq(row).children().eq(col).removeClass(value);
};