-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
344 lines (311 loc) · 13.9 KB
/
main.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* @fileoverview Functions for solving a Multidimensional Scaling problem with
* gradient descent (optionally with momentum) or the Gauss-Newton algorithm.
*
* The two central functions are getMdsCoordinatesWithGradientDescent(...) and
* getMdsCoordinatesWithGaussNewton(...).
*
* Dependencies:
* - The ml-matrix library for computing matrix manipulations, see
* https://github.com/mljs/matrix.
* Version used: 6.4.1
* - seedrandom, see https://github.com/davidbau/seedrandom.
* Version used: 3.0.5
*/
/**
* Solves a Multidimensional Scaling problem with gradient descent.
*
* If momentum is != 0, the update is:
*
* accumulation = momentum * accumulation + gradient
* parameters -= learning_rate * accumulation
*
* like in TensorFlow and PyTorch.
*
* In the returned object, coordinates is a matrix of shape (n, 2) containing
* the solution.
*
* @param {!mlMatrix.Matrix} distances - matrix of shape (n, n) containing the
* distances between n points.
* @param {!number} lr - learning rate to use.
* @param {!number} maxSteps - maximum number of update steps.
* @param {!number} minLossDifference - if the absolute difference between the
* losses of the current and the previous optimization step is smaller than
* this value, the function will return early.
* @param {!number} momentum - momentum of the gradient descent. Set this value
* to zero to disable momentum.
* @param {!number} logEvery - if larger than zero, this value determines the
* steps between logs to the console.
* @returns {{coordinates: mlMatrix.Matrix, lossPerStep: number[]}}
*/
function getMdsCoordinatesWithGradientDescent(distances,
{
lr = 1,
maxSteps = 200,
minLossDifference = 1e-7,
momentum = 0,
logEvery = 0
} = {}) {
const numCoordinates = distances.rows;
let coordinates = getInitialMdsCoordinates(numCoordinates);
const lossPerStep = [];
let accumulation = null;
for (let step = 0; step < maxSteps; step++) {
const loss = getMdsLoss(distances, coordinates);
lossPerStep.push(loss);
// Check if we should early stop.
if (lossPerStep.length > 1) {
const lossPrev = lossPerStep[lossPerStep.length - 2];
if (Math.abs(lossPrev - loss) < minLossDifference) {
return {coordinates: coordinates, lossPerStep: lossPerStep};
}
}
if (logEvery > 0 && step % logEvery === 0) {
console.log(`Step: ${step}, loss: ${loss}`);
}
// Apply the gradient for each coordinate.
for (let coordIndex = 0; coordIndex < numCoordinates; coordIndex++) {
const gradient = getGradientForCoordinate(
distances, coordinates, coordIndex);
if (momentum === 0 || accumulation == null) {
accumulation = gradient;
} else {
accumulation = mlMatrix.Matrix.add(
mlMatrix.Matrix.mul(accumulation, momentum),
gradient
);
}
const update = mlMatrix.Matrix.mul(accumulation, lr);
const updatedCoordinates = mlMatrix.Matrix.sub(
coordinates.getRowVector(coordIndex),
update);
coordinates.setRow(coordIndex, updatedCoordinates);
}
}
return {coordinates: coordinates, lossPerStep: lossPerStep};
}
/**
* Solves a Multidimensional Scaling problem with the Gauss-Newton algorithm.
*
* In the returned object, coordinates is a matrix of shape (n, 2) containing
* the solution.
*
* @param {!mlMatrix.Matrix} distances - matrix of shape (n, n) containing the
* distances between n points.
* @param {!number} lr - learning rate / alpha to use.
* @param {!number} maxSteps - maximum number of update steps.
* @param {!number} minLossDifference - if the absolute difference between the
* losses of the current and the previous optimization step is smaller than
* this value, the function will return early.
* @param {!number} logEvery - if larger than zero, this value determines the
* steps between logs to the console.
* @returns {{coordinates: mlMatrix.Matrix, lossPerStep: number[]}}
*/
function getMdsCoordinatesWithGaussNewton(distances,
{
lr = 0.1,
maxSteps = 200,
minLossDifference = 1e-7,
logEvery = 0
} = {}) {
const numCoordinates = distances.rows;
let coordinates = getInitialMdsCoordinates(numCoordinates);
const dimensions = coordinates.columns;
const lossPerStep = [];
for (let step = 0; step < maxSteps; step++) {
const loss = getMdsLoss(distances, coordinates);
lossPerStep.push(loss);
// Check if we should early stop.
if (lossPerStep.length > 1) {
const lossPrev = lossPerStep[lossPerStep.length - 2];
if (Math.abs(lossPrev - loss) < minLossDifference) {
return {coordinates: coordinates, lossPerStep: lossPerStep};
}
}
if (logEvery > 0 && step % logEvery === 0) {
console.log(`Step: ${step}, loss: ${loss}`);
}
// Apply the update.
const {residuals, jacobian} = getResidualsWithJacobian(
distances, coordinates);
const update = mlMatrix.pseudoInverse(jacobian).mmul(residuals);
for (let coordIndex = 0; coordIndex < numCoordinates; coordIndex++) {
for (let dimension = 0; dimension < dimensions; dimension++) {
const updateIndex = coordIndex * dimensions + dimension;
const paramValue = coordinates.get(coordIndex, dimension);
const updateDelta = lr * update.get(updateIndex, 0);
const updatedValue = paramValue - updateDelta;
coordinates.set(coordIndex, dimension, updatedValue);
}
}
}
return {coordinates: coordinates, lossPerStep: lossPerStep};
}
/**
* Initializes the solution by sampling from a uniform distribution, which
* only allows distances in [0, 1].
*
* @param {!number} numCoordinates - the number of points in the solution.
* @param {!number} dimensions - the number of dimensions of each point.
* @param {!number} seed - seed for the random number generator.
* @returns {mlMatrix.Matrix}
*/
function getInitialMdsCoordinates(numCoordinates, dimensions = 2, seed = 0) {
const randomUniform = mlMatrix.Matrix.rand(
numCoordinates, dimensions, {random: new Math.seedrandom(seed)});
return mlMatrix.Matrix.div(randomUniform, Math.sqrt(dimensions));
}
/**
* Returns the loss of a given solution to the Multidimensional Scaling
* problem by computing the mean squared difference between target distances
* and distances between points in the solution.
*
* @param {!mlMatrix.Matrix} distances - matrix of shape (n, n) containing the
* distances between n points, defining the MDS problem.
* @param {!mlMatrix.Matrix} coordinates - a matrix of shape (n, d) containing
* the solution, for example given by getMdsCoordinatesWithGaussNewton(...).
* d is the number of dimensions.
* @returns {number}
*/
function getMdsLoss(distances, coordinates) {
// Average the squared differences of target distances and predicted
// distances.
let loss = 0;
const normalizer = Math.pow(coordinates.rows, 2);
for (let coordIndex1 = 0;
coordIndex1 < coordinates.rows;
coordIndex1++) {
for (let coordIndex2 = 0;
coordIndex2 < coordinates.rows;
coordIndex2++) {
if (coordIndex1 === coordIndex2) continue;
const coord1 = coordinates.getRowVector(coordIndex1);
const coord2 = coordinates.getRowVector(coordIndex2);
const target = distances.get(coordIndex1, coordIndex2);
const predicted = mlMatrix.Matrix.sub(coord1, coord2).norm();
loss += Math.pow(target - predicted, 2) / normalizer;
}
}
return loss;
}
/**
* Returns the residuals and the Jacobian matrix for performing one step of the
* Gauss-Newton algorithm.
*
* The residuals are returned in a flattened vector as (target - predicted) /
* numCoordinates. The flattened vector is ordered based on iterating the
* matrix given by distances in row-major order. We divide by coordinates.rows,
* so that the sum of squared residuals equals the MDS loss, which involves a
* division by coordinates.rows ** 2.
*
* The element of the Jacobian at row i and column j should contain the
* partial derivative of the i-th residual w.r.t. the j-th coordinate. The
* coordinates are indexed in row-major order, such that in two dimensions,
* the 5th zero-based index corresponds to the second coordinate of the third
* point.
*
* @param {!mlMatrix.Matrix} distances - matrix of shape (n, n) containing the
* distances between n points, defining the MDS problem.
* @param {!mlMatrix.Matrix} coordinates - a matrix of shape (n, d) containing
* the current solution, where d is the number of dimensions.
* @returns {{jacobian: mlMatrix.Matrix, residuals: mlMatrix.Matrix}}
*/
function getResidualsWithJacobian(distances, coordinates) {
const residuals = [];
const numCoordinates = coordinates.rows;
const dimensions = coordinates.columns;
const jacobian = mlMatrix.Matrix.zeros(
numCoordinates * numCoordinates,
numCoordinates * dimensions);
for (let coordIndex1 = 0;
coordIndex1 < numCoordinates;
coordIndex1++) {
for (let coordIndex2 = 0;
coordIndex2 < numCoordinates;
coordIndex2++) {
if (coordIndex1 === coordIndex2) {
residuals.push(0);
// The gradient for all coordinates is zero, so we can skip
// this row of the Jacobian.
continue;
}
// Compute the residual.
const coord1 = coordinates.getRowVector(coordIndex1);
const coord2 = coordinates.getRowVector(coordIndex2);
const squaredDifferenceSum = mlMatrix.Matrix.sub(
coord1, coord2).pow(2).sum();
const predicted = Math.sqrt(squaredDifferenceSum);
const target = distances.get(coordIndex1, coordIndex2);
const residual = (target - predicted) / numCoordinates;
residuals.push(residual);
// Compute the gradient w.r.t. the first coordinate only. The
// second coordinate is seen as a constant.
const residualWrtPredicted = -1 / numCoordinates;
const predictedWrtSquaredDifferenceSum = 0.5 / Math.sqrt(squaredDifferenceSum);
const squaredDifferenceSumWrtCoord1 = mlMatrix.Matrix.mul(
mlMatrix.Matrix.sub(coord1, coord2), 2);
const residualWrtCoord1 = mlMatrix.Matrix.mul(
squaredDifferenceSumWrtCoord1,
residualWrtPredicted * predictedWrtSquaredDifferenceSum
);
// Set the corresponding indices in the Jacobian.
const rowIndex = numCoordinates * coordIndex1 + coordIndex2;
for (let dimension = 0; dimension < dimensions; dimension++) {
const columIndex = dimensions * coordIndex1 + dimension;
const jacobianEntry = jacobian.get(rowIndex, columIndex);
const entryUpdated = jacobianEntry + residualWrtCoord1.get(
0, dimension);
jacobian.set(rowIndex, columIndex, entryUpdated);
}
}
}
return {
residuals: mlMatrix.Matrix.columnVector(residuals),
jacobian: jacobian
};
}
/**
* Returns the gradient of the loss w.r.t. to a specific point in the
* given solution.
*
* The returned matrix has the shape (1, d), where d is the number of
* dimensions.
*
* @param {!mlMatrix.Matrix} distances - matrix of shape (n, n) containing the
* distances between n points, defining the MDS problem.
* @param {!mlMatrix.Matrix} coordinates - a matrix of shape (n, d) containing
* the current solution, where d is the number of dimensions.
* @param {!number} coordIndex - index of the point for which the gradient
* shall be computed.
* @returns {mlMatrix.Matrix}
*/
function getGradientForCoordinate(distances, coordinates, coordIndex) {
const coord = coordinates.getRowVector(coordIndex);
const normalizer = Math.pow(coordinates.rows, 2);
let gradient = mlMatrix.Matrix.zeros(1, coord.columns);
for (let otherCoordIndex = 0;
otherCoordIndex < coordinates.rows;
otherCoordIndex++) {
if (coordIndex === otherCoordIndex) continue;
const otherCoord = coordinates.getRowVector(otherCoordIndex);
const squaredDifferenceSum = mlMatrix.Matrix.sub(
coord, otherCoord).pow(2).sum();
const predicted = Math.sqrt(squaredDifferenceSum);
const targets = [
distances.get(coordIndex, otherCoordIndex),
distances.get(otherCoordIndex, coordIndex)
];
for (const target of targets) {
const lossWrtPredicted = -2 * (target - predicted) / normalizer;
const predictedWrtSquaredDifferenceSum = 0.5 / Math.sqrt(squaredDifferenceSum);
const squaredDifferenceSumWrtCoord = mlMatrix.Matrix.mul(
mlMatrix.Matrix.sub(coord, otherCoord), 2);
const lossWrtCoord = mlMatrix.Matrix.mul(
squaredDifferenceSumWrtCoord,
lossWrtPredicted * predictedWrtSquaredDifferenceSum
);
gradient = mlMatrix.Matrix.add(gradient, lossWrtCoord);
}
}
return gradient;
}