-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComputeP.cpp
422 lines (355 loc) · 14.5 KB
/
ComputeP.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <iostream>
#include <boost/program_options.hpp>
#include "vptree.h"
using namespace std;
namespace po = boost::program_options;
/* Functions taken from Laurens van der Maaten's original implementation of t-SNE */
/* Source: https://github.com/lvdmaaten/bhtsne */
static void computeGaussianPerplexity(double* X, int N, int D, unsigned int** _row_P, unsigned int** _col_P, double** _val_P, double perplexity, int K);
static void symmetrizeMatrix(unsigned int** _row_P, unsigned int** _col_P, double** _val_P, int N);
static void computeSquaredEuclideanDistance(double* X, int N, int D, double* DD);
static void zeroMean(double* X, int N, int D);
/**********************************************************************************/
static bool load_data(string infile, double **data, int &num_instances, int &num_features) {
FILE *fp = fopen(infile.c_str(), "rb");
if (fp == NULL) {
cout << "Error: could not open data file " << infile << endl;
return false;
}
uint64_t ret;
ret = fread(&num_instances, sizeof(int), 1, fp);
ret = fread(&num_features, sizeof(int), 1, fp);
*data = (double *)malloc(num_instances * num_features * sizeof(double));
if (*data == NULL) {
cout << "Error: memory allocation of " << num_instances << " by "
<< num_features << " matrix failed" << endl;
return false;
}
uint64_t nelem = (uint64_t)num_instances * num_features;
size_t batch_size = 1e8;
double *ptr = *data;
ret = 0;
for (uint64_t remaining = nelem; remaining > 0; remaining -= batch_size) {
if (remaining < batch_size) {
batch_size = remaining;
}
ret += fread(ptr, sizeof(double), batch_size, fp);
ptr += batch_size;
}
if (ret != nelem) {
cout << "Error: reading input returned incorrect number of elements (" << ret
<< ", expected " << nelem << ")" << endl;
return false;
}
fclose(fp);
return true;
}
static void truncate_data(double *X, int num_instances, int num_features, int target_dims) {
size_t i_old = 0;
size_t i_new = 0;
for (int r = 0; r < num_instances; r++) {
for (int c = 0; c < num_features; c++) {
if (c < target_dims) {
X[i_new++] = X[i_old];
}
i_old++;
}
}
}
static bool run(double *X, int num_instances, int num_features, double perplexity, string outfile) {
// Apply lower bound on perplexity from original t-SNE implementation
if (num_instances - 1 < 3 * perplexity) {
cout << "Error: target perplexity (" << perplexity << ") is too large "
<< "for the number of data points (" << num_instances << ")" << endl;
return false;
}
printf("Processing %d data points, %d features with target perplexity %f\n",
num_instances, num_features, perplexity);
// Normalize input data (to prevent numerical problems)
zeroMean(X, num_instances, num_features);
cout << "Normalizing the features" << endl;
double max_X = 0;
for (size_t i = 0; i < num_instances * num_features; i++) {
if (fabs(X[i]) > max_X) {
max_X = fabs(X[i]);
}
}
for (size_t i = 0; i < num_instances * num_features; i++) {
X[i] /= max_X;
}
// Compute input similarities for exact t-SNE
double* P; unsigned int* row_P; unsigned int* col_P; double* val_P;
// Compute asymmetric pairwise input similarities
cout << "Computing conditional distributions" << endl;
computeGaussianPerplexity(X, num_instances, num_features,
&row_P, &col_P, &val_P, perplexity, (int) (3 * perplexity));
// Symmetrize input similarities
cout << "Symmetrizing matrix" << endl;
symmetrizeMatrix(&row_P, &col_P, &val_P, num_instances);
double sum_P = .0;
for (int i = 0; i < row_P[num_instances]; i++) {
sum_P += val_P[i];
}
for (int i = 0; i < row_P[num_instances]; i++) {
val_P[i] /= sum_P;
}
cout << "Saving to " << outfile << endl;
FILE *fp = fopen(outfile.c_str(), "wb");
if (fp == NULL) {
cout << "Error: could not open output file " << outfile << endl;
return false;
}
fwrite(&num_instances, sizeof(int), 1, fp);
fwrite(row_P, sizeof(unsigned int), num_instances + 1, fp);
fwrite(col_P, sizeof(unsigned int), row_P[num_instances], fp);
fwrite(val_P, sizeof(double), row_P[num_instances], fp);
free(row_P);
free(col_P);
free(val_P);
return true;
}
int main(int argc, char **argv) {
// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("input-file", po::value<string>()->value_name("FILE")->default_value("data.dat"), "name of binary input file (see prepare_input.m)")
("output-file", po::value<string>()->value_name("FILE")->default_value("P.dat"), "name of output file to be created")
("perp", po::value<double>()->value_name("NUM")->default_value(30, "30"), "set target perplexity for conditional distributions")
("num-dims", po::value<int>()->value_name("NUM"), "if provided, only the first NUM features in the input will be used")
;
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).run(), vm);
po::notify(vm);
if (vm.count("help")) {
cout << "Usage: ComputeP [options]" << endl;
cout << desc << "\n";
return 1;
}
double perplexity = vm["perp"].as<double>();
string infile = vm["input-file"].as<string>();
string outfile = vm["output-file"].as<string>();
double *data;
int num_instances;
int num_features;
if (!load_data(infile, &data, num_instances, num_features)) {
return 1;
}
cout << infile << " successfully loaded" << endl;
if (vm.count("num-dims")) {
int num_dims = vm["num-dims"].as<int>();
cout << "Using only the first " << num_dims << " dimensions" << endl;
truncate_data(data, num_instances, num_features, num_dims);
num_features = num_dims;
}
if (!run(data, num_instances, num_features, perplexity, outfile)) {
return 1;
}
cout << "Done" << endl;
return 0;
}
// Compute input similarities with a fixed perplexity using ball trees (this function allocates memory another function should free)
// Source: https://github.com/lvdmaaten/bhtsne
static void computeGaussianPerplexity(double* X, int N, int D, unsigned int** _row_P, unsigned int** _col_P, double** _val_P, double perplexity, int K) {
if(perplexity > K) printf("Perplexity should be lower than K!\n");
// Allocate the memory we need
*_row_P = (unsigned int*) malloc((N + 1) * sizeof(unsigned int));
*_col_P = (unsigned int*) calloc(N * K, sizeof(unsigned int));
*_val_P = (double*) calloc(N * K, sizeof(double));
if(*_row_P == NULL || *_col_P == NULL || *_val_P == NULL) { printf("Memory allocation failed!\n"); exit(1); }
unsigned int* row_P = *_row_P;
unsigned int* col_P = *_col_P;
double* val_P = *_val_P;
double* cur_P = (double*) malloc((N - 1) * sizeof(double));
if(cur_P == NULL) { printf("Memory allocation failed!\n"); exit(1); }
row_P[0] = 0;
for(int n = 0; n < N; n++) row_P[n + 1] = row_P[n] + (unsigned int) K;
// Build ball tree on data set
VpTree<DataPoint, euclidean_distance>* tree = new VpTree<DataPoint, euclidean_distance>();
vector<DataPoint> obj_X(N, DataPoint(D, -1, X));
for(int n = 0; n < N; n++) obj_X[n] = DataPoint(D, n, X + n * D);
tree->create(obj_X);
// Loop over all points to find nearest neighbors
printf("Building tree...\n");
vector<DataPoint> indices;
vector<double> distances;
for(int n = 0; n < N; n++) {
if(n % 10000 == 0) printf(" - point %d of %d\n", n, N);
// Find nearest neighbors
indices.clear();
distances.clear();
tree->search(obj_X[n], K + 1, &indices, &distances);
// Initialize some variables for binary search
bool found = false;
double beta = 1.0;
double min_beta = -DBL_MAX;
double max_beta = DBL_MAX;
double tol = 1e-5;
// Iterate until we found a good perplexity
int iter = 0; double sum_P;
while(!found && iter < 200) {
// Compute Gaussian kernel row
for(int m = 0; m < K; m++) cur_P[m] = exp(-beta * distances[m + 1] * distances[m + 1]);
// Compute entropy of current row
sum_P = DBL_MIN;
for(int m = 0; m < K; m++) sum_P += cur_P[m];
double H = .0;
for(int m = 0; m < K; m++) H += beta * (distances[m + 1] * distances[m + 1] * cur_P[m]);
H = (H / sum_P) + log(sum_P);
// Evaluate whether the entropy is within the tolerance level
double Hdiff = H - log(perplexity);
if(Hdiff < tol && -Hdiff < tol) {
found = true;
}
else {
if(Hdiff > 0) {
min_beta = beta;
if(max_beta == DBL_MAX || max_beta == -DBL_MAX)
beta *= 2.0;
else
beta = (beta + max_beta) / 2.0;
}
else {
max_beta = beta;
if(min_beta == -DBL_MAX || min_beta == DBL_MAX)
beta /= 2.0;
else
beta = (beta + min_beta) / 2.0;
}
}
// Update iteration counter
iter++;
}
// Row-normalize current row of P and store in matrix
for(unsigned int m = 0; m < K; m++) cur_P[m] /= sum_P;
for(unsigned int m = 0; m < K; m++) {
col_P[row_P[n] + m] = (unsigned int) indices[m + 1].index();
val_P[row_P[n] + m] = cur_P[m];
}
}
// Clean up memory
obj_X.clear();
free(cur_P);
delete tree;
}
// Symmetrizes a sparse matrix
// Source: https://github.com/lvdmaaten/bhtsne
static void symmetrizeMatrix(unsigned int** _row_P, unsigned int** _col_P, double** _val_P, int N) {
// Get sparse matrix
unsigned int* row_P = *_row_P;
unsigned int* col_P = *_col_P;
double* val_P = *_val_P;
// Count number of elements and row counts of symmetric matrix
int* row_counts = (int*) calloc(N, sizeof(int));
if(row_counts == NULL) { printf("Memory allocation failed!\n"); exit(1); }
for(int n = 0; n < N; n++) {
for(int i = row_P[n]; i < row_P[n + 1]; i++) {
// Check whether element (col_P[i], n) is present
bool present = false;
for(int m = row_P[col_P[i]]; m < row_P[col_P[i] + 1]; m++) {
if(col_P[m] == n) present = true;
}
if(present) row_counts[n]++;
else {
row_counts[n]++;
row_counts[col_P[i]]++;
}
}
}
int no_elem = 0;
for(int n = 0; n < N; n++) no_elem += row_counts[n];
// Allocate memory for symmetrized matrix
unsigned int* sym_row_P = (unsigned int*) malloc((N + 1) * sizeof(unsigned int));
unsigned int* sym_col_P = (unsigned int*) malloc(no_elem * sizeof(unsigned int));
double* sym_val_P = (double*) malloc(no_elem * sizeof(double));
if(sym_row_P == NULL || sym_col_P == NULL || sym_val_P == NULL) { printf("Memory allocation failed!\n"); exit(1); }
// Construct new row indices for symmetric matrix
sym_row_P[0] = 0;
for(int n = 0; n < N; n++) sym_row_P[n + 1] = sym_row_P[n] + (unsigned int) row_counts[n];
// Fill the result matrix
int* offset = (int*) calloc(N, sizeof(int));
if(offset == NULL) { printf("Memory allocation failed!\n"); exit(1); }
for(int n = 0; n < N; n++) {
for(unsigned int i = row_P[n]; i < row_P[n + 1]; i++) { // considering element(n, col_P[i])
// Check whether element (col_P[i], n) is present
bool present = false;
for(unsigned int m = row_P[col_P[i]]; m < row_P[col_P[i] + 1]; m++) {
if(col_P[m] == n) {
present = true;
if(n <= col_P[i]) { // make sure we do not add elements twice
sym_col_P[sym_row_P[n] + offset[n]] = col_P[i];
sym_col_P[sym_row_P[col_P[i]] + offset[col_P[i]]] = n;
sym_val_P[sym_row_P[n] + offset[n]] = val_P[i] + val_P[m];
sym_val_P[sym_row_P[col_P[i]] + offset[col_P[i]]] = val_P[i] + val_P[m];
}
}
}
// If (col_P[i], n) is not present, there is no addition involved
if(!present) {
sym_col_P[sym_row_P[n] + offset[n]] = col_P[i];
sym_col_P[sym_row_P[col_P[i]] + offset[col_P[i]]] = n;
sym_val_P[sym_row_P[n] + offset[n]] = val_P[i];
sym_val_P[sym_row_P[col_P[i]] + offset[col_P[i]]] = val_P[i];
}
// Update offsets
if(!present || (present && n <= col_P[i])) {
offset[n]++;
if(col_P[i] != n) offset[col_P[i]]++;
}
}
}
// Divide the result by two
for(int i = 0; i < no_elem; i++) sym_val_P[i] /= 2.0;
// Return symmetrized matrices
free(*_row_P); *_row_P = sym_row_P;
free(*_col_P); *_col_P = sym_col_P;
free(*_val_P); *_val_P = sym_val_P;
// Free up some memery
free(offset); offset = NULL;
free(row_counts); row_counts = NULL;
}
// Compute squared Euclidean distance matrix
// Source: https://github.com/lvdmaaten/bhtsne
static void computeSquaredEuclideanDistance(double* X, int N, int D, double* DD) {
const double* XnD = X;
for(int n = 0; n < N; ++n, XnD += D) {
const double* XmD = XnD + D;
double* curr_elem = &DD[n*N + n];
*curr_elem = 0.0;
double* curr_elem_sym = curr_elem + N;
for(int m = n + 1; m < N; ++m, XmD+=D, curr_elem_sym+=N) {
*(++curr_elem) = 0.0;
for(int d = 0; d < D; ++d) {
*curr_elem += (XnD[d] - XmD[d]) * (XnD[d] - XmD[d]);
}
*curr_elem_sym = *curr_elem;
}
}
}
// Makes data zero-mean
// Source: https://github.com/lvdmaaten/bhtsne
static void zeroMean(double* X, int N, int D) {
// Compute data mean
double* mean = (double*) calloc(D, sizeof(double));
if(mean == NULL) { printf("Memory allocation failed!\n"); exit(1); }
int nD = 0;
for(int n = 0; n < N; n++) {
for(int d = 0; d < D; d++) {
mean[d] += X[nD + d];
}
nD += D;
}
for(int d = 0; d < D; d++) {
mean[d] /= (double) N;
}
// Subtract data mean
nD = 0;
for(int n = 0; n < N; n++) {
for(int d = 0; d < D; d++) {
X[nD + d] -= mean[d];
}
nD += D;
}
free(mean); mean = NULL;
}