-
Notifications
You must be signed in to change notification settings - Fork 229
/
tsne.cpp
648 lines (552 loc) · 19.9 KB
/
tsne.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* tsne.cpp
* Implementation of both standard and Barnes-Hut-SNE.
*
* Created by Laurens van der Maaten.
* Copyright 2012, Delft University of Technology. All rights reserved.
*
* Multicore version by Dmitry Ulyanov, 2016. [email protected]
*/
#include <cmath>
#include <cfloat>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#ifdef _OPENMP
#include <omp.h>
#endif
// #include "quadtree.h"
#include "splittree.h"
#include "vptree.h"
#include "tsne.h"
#ifdef _OPENMP
#define NUM_THREADS(N) ((N) >= 0 ? (N) : omp_get_num_procs() + (N) + 1)
#else
#define NUM_THREADS(N) (1)
#endif
/*
Perform t-SNE
X -- double matrix of size [N, D]
D -- input dimensionality
Y -- array to fill with the result of size [N, no_dims]
no_dims -- target dimentionality
*/
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
void TSNE<treeT, dist_fn>::run(double* X, int N, int D, double* Y,
int no_dims, double perplexity, double theta ,
int num_threads, int max_iter, int n_iter_early_exag,
int random_state, bool init_from_Y, int verbose,
double early_exaggeration, double learning_rate,
double *final_error) {
if (random_state != -1) {
srand(random_state);
}
if (N - 1 < 3 * perplexity) {
perplexity = (N - 1) / 3;
if (verbose)
fprintf(stderr, "Perplexity too large for the number of data points! Adjusting ...\n");
}
#ifdef _OPENMP
omp_set_num_threads(NUM_THREADS(num_threads));
#if _OPENMP >= 200805
omp_set_schedule(omp_sched_guided, 0);
#endif
#endif
/*
======================
Step 1
======================
*/
if (verbose)
fprintf(stderr, "Using no_dims = %d, perplexity = %f, and theta = %f\n", no_dims, perplexity, theta);
// Set learning parameters
float total_time = .0;
time_t start, end;
int stop_lying_iter = n_iter_early_exag, mom_switch_iter = n_iter_early_exag;
double momentum = .5, final_momentum = .8;
double eta = learning_rate;
// Allocate some memory
double* dY = (double*) malloc(N * no_dims * sizeof(double));
double* uY = (double*) calloc(N * no_dims , sizeof(double));
double* gains = (double*) malloc(N * no_dims * sizeof(double));
if (dY == NULL || uY == NULL || gains == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int i = 0; i < N * no_dims; i++) {
gains[i] = 1.0;
}
// Normalize input data (to prevent numerical problems)
if (verbose)
fprintf(stderr, "Computing input similarities...\n");
start = time(0);
zeroMean(X, N, D);
double max_X = .0;
for (int i = 0; i < N * D; i++) {
if (X[i] > max_X) max_X = X[i];
}
if (max_X == .0) {
free(dY);
free(uY);
free(gains);
return;
}
for (int i = 0; i < N * D; i++) {
X[i] /= max_X;
}
// Compute input similarities
int* row_P; int* col_P; double* val_P;
// Compute asymmetric pairwise input similarities
computeGaussianPerplexity(X, N, D, &row_P, &col_P, &val_P, perplexity, (int) (3 * perplexity), verbose);
// Symmetrize input similarities
symmetrizeMatrix(&row_P, &col_P, &val_P, N);
double sum_P = .0;
for (int i = 0; i < row_P[N]; i++) {
sum_P += val_P[i];
}
for (int i = 0; i < row_P[N]; i++) {
val_P[i] /= sum_P;
}
end = time(0);
if (verbose)
fprintf(stderr, "Done in %4.2f seconds (sparsity = %f)!\nLearning embedding...\n", (float)(end - start) , (double) row_P[N] / ((double) N * (double) N));
/*
======================
Step 2
======================
*/
// Lie about the P-values
for (int i = 0; i < row_P[N]; i++) {
val_P[i] *= early_exaggeration;
}
// Initialize solution (randomly), unless Y is already initialized
if (init_from_Y) {
stop_lying_iter = 0; // Immediately stop lying. Passed Y is close to the true solution.
}
else {
for (int i = 0; i < N * no_dims; i++) {
Y[i] = randn();
}
}
// Perform main training loop
start = time(0);
for (int iter = 0; iter < max_iter; iter++) {
bool need_eval_error = (verbose && ((iter > 0 && iter % 50 == 0) || (iter == max_iter - 1)));
// Compute approximate gradient
double error = computeGradient(row_P, col_P, val_P, Y, N, no_dims, dY, theta, need_eval_error);
for (int i = 0; i < N * no_dims; i++) {
// Update gains
gains[i] = (sign(dY[i]) != sign(uY[i])) ? (gains[i] + .2) : (gains[i] * .8 + .01);
// Perform gradient update (with momentum and gains)
uY[i] = momentum * uY[i] - eta * gains[i] * dY[i];
Y[i] = Y[i] + uY[i];
}
// Make solution zero-mean
zeroMean(Y, N, no_dims);
// Stop lying about the P-values after a while, and switch momentum
if (iter == stop_lying_iter) {
for (int i = 0; i < row_P[N]; i++) {
val_P[i] /= early_exaggeration;
}
}
if (iter == mom_switch_iter) {
momentum = final_momentum;
}
// Print out progress
if (need_eval_error) {
end = time(0);
if (iter == 0)
fprintf(stderr, "Iteration %d: error is %f\n", iter + 1, error);
else {
total_time += (float) (end - start);
fprintf(stderr, "Iteration %d: error is %f (50 iterations in %4.2f seconds)\n", iter + 1, error, (float) (end - start) );
}
start = time(0);
}
}
end = time(0); total_time += (float) (end - start) ;
if (final_error != NULL)
*final_error = evaluateError(row_P, col_P, val_P, Y, N, no_dims, theta);
// Clean up memory
free(dY);
free(uY);
free(gains);
free(row_P); row_P = NULL;
free(col_P); col_P = NULL;
free(val_P); val_P = NULL;
if (verbose)
fprintf(stderr, "Fitting performed in %4.2f seconds.\n", total_time);
}
// Compute gradient of the t-SNE cost function (using Barnes-Hut algorithm)
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
double TSNE<treeT, dist_fn>::computeGradient(int* inp_row_P, int* inp_col_P, double* inp_val_P, double* Y, int N, int no_dims, double* dC, double theta, bool eval_error)
{
// Construct quadtree on current map
treeT* tree = new treeT(Y, N, no_dims);
// Compute all terms required for t-SNE gradient
double* Q = new double[N];
double* pos_f = new double[N * no_dims]();
double* neg_f = new double[N * no_dims]();
double P_i_sum = 0.;
double C = 0.;
if (pos_f == NULL || neg_f == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return std::numeric_limits<double>::infinity();
}
#ifdef _OPENMP
#pragma omp parallel for reduction(+:P_i_sum,C)
#endif
for (int n = 0; n < N; n++) {
// Edge forces
int ind1 = n * no_dims;
for (int i = inp_row_P[n]; i < inp_row_P[n + 1]; i++) {
// Compute pairwise distance and Q-value
double D = .0;
int ind2 = inp_col_P[i] * no_dims;
for (int d = 0; d < no_dims; d++) {
double t = Y[ind1 + d] - Y[ind2 + d];
D += t * t;
}
// Sometimes we want to compute error on the go
if (eval_error) {
P_i_sum += inp_val_P[i];
C += inp_val_P[i] * log((inp_val_P[i] + FLT_MIN) / ((1.0 / (1.0 + D)) + FLT_MIN));
}
D = inp_val_P[i] / (1.0 + D);
// Sum positive force
for (int d = 0; d < no_dims; d++) {
pos_f[ind1 + d] += D * (Y[ind1 + d] - Y[ind2 + d]);
}
}
// NoneEdge forces
double this_Q = .0;
tree->computeNonEdgeForces(n, theta, neg_f + n * no_dims, &this_Q);
Q[n] = this_Q;
}
double sum_Q = 0.;
for (int i = 0; i < N; i++) {
sum_Q += Q[i];
}
// Compute final t-SNE gradient
for (int i = 0; i < N * no_dims; i++) {
dC[i] = pos_f[i] - (neg_f[i] / sum_Q);
}
delete tree;
delete[] pos_f;
delete[] neg_f;
delete[] Q;
C += P_i_sum * log(sum_Q);
return C;
}
// Evaluate t-SNE cost function (approximately)
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
double TSNE<treeT, dist_fn>::evaluateError(int* row_P, int* col_P, double* val_P, double* Y, int N, int no_dims, double theta)
{
// Get estimate of normalization term
treeT* tree = new treeT(Y, N, no_dims);
double* buff = new double[no_dims]();
double sum_Q = .0;
for (int n = 0; n < N; n++) {
tree->computeNonEdgeForces(n, theta, buff, &sum_Q);
}
delete tree;
delete[] buff;
// Loop over all edges to compute t-SNE error
double C = .0;
#ifdef _OPENMP
#pragma omp parallel for reduction(+:C)
#endif
for (int n = 0; n < N; n++) {
int ind1 = n * no_dims;
for (int i = row_P[n]; i < row_P[n + 1]; i++) {
double Q = .0;
int ind2 = col_P[i] * no_dims;
for (int d = 0; d < no_dims; d++) {
double b = Y[ind1 + d] - Y[ind2 + d];
Q += b * b;
}
Q = (1.0 / (1.0 + Q)) / sum_Q;
C += val_P[i] * log((val_P[i] + FLT_MIN) / (Q + FLT_MIN));
}
}
return C;
}
// Compute input similarities with a fixed perplexity using ball trees (this function allocates memory another function should free)
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
void TSNE<treeT, dist_fn>::computeGaussianPerplexity(double* X, int N, int D, int** _row_P, int** _col_P, double** _val_P, double perplexity, int K, int verbose) {
if (perplexity > K) fprintf(stderr, "Perplexity should be lower than K!\n");
// Allocate the memory we need
*_row_P = (int*) malloc((N + 1) * sizeof(int));
*_col_P = (int*) calloc(N * K, sizeof(int));
*_val_P = (double*) calloc(N * K, sizeof(double));
if (*_row_P == NULL || *_col_P == NULL || *_val_P == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
/*
row_P -- offsets for `col_P` (i)
col_P -- K nearest neighbors indices (j)
val_P -- p_{i | j}
*/
int* row_P = *_row_P;
int* col_P = *_col_P;
double* val_P = *_val_P;
row_P[0] = 0;
for (int n = 0; n < N; n++) {
row_P[n + 1] = row_P[n] + K;
}
// Build ball tree on data set
VpTree<DataPoint, dist_fn>* tree = new VpTree<DataPoint, dist_fn>();
std::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
if (verbose)
fprintf(stderr, "Building tree...\n");
int steps_completed = 0;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int n = 0; n < N; n++)
{
std::vector<double> cur_P(K);
std::vector<DataPoint> indices;
std::vector<double> distances;
// Find nearest neighbors
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 = 0;
while (!found && iter < 200) {
// Compute Gaussian kernel row
for (int m = 0; m < K; m++) {
cur_P[m] = exp(-beta * 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] * 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 (int m = 0; m < K; m++) {
cur_P[m] /= sum_P;
}
for (int m = 0; m < K; m++) {
col_P[row_P[n] + m] = indices[m + 1].index();
val_P[row_P[n] + m] = cur_P[m];
}
// Print progress
#ifdef _OPENMP
#pragma omp atomic
#endif
++steps_completed;
if (verbose && steps_completed % (N / 10) == 0)
{
#ifdef _OPENMP
#pragma omp critical
#endif
fprintf(stderr, " - point %d of %d\n", steps_completed, N);
}
}
// Clean up memory
obj_X.clear();
delete tree;
}
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
void TSNE<treeT, dist_fn>::symmetrizeMatrix(int** _row_P, int** _col_P, double** _val_P, int N) {
// Get sparse matrix
int* row_P = *_row_P;
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) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
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;
break;
}
}
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
int* sym_row_P = (int*) malloc((N + 1) * sizeof(int));
int* sym_col_P = (int*) malloc(no_elem * sizeof(int));
double* sym_val_P = (double*) malloc(no_elem * sizeof(double));
if (sym_row_P == NULL || sym_col_P == NULL || sym_val_P == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
// 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] + row_counts[n];
// Fill the result matrix
int* offset = (int*) calloc(N, sizeof(int));
if (offset == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int n = 0; n < N; n++) {
for (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 (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 || (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;
}
// Makes data zero-mean
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
void TSNE<treeT, dist_fn>::zeroMean(double* X, int N, int D) {
// Compute data mean
double* mean = (double*) calloc(D, sizeof(double));
if (mean == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int n = 0; n < N; n++) {
for (int d = 0; d < D; d++) {
mean[d] += X[n * D + d];
}
}
for (int d = 0; d < D; d++) {
mean[d] /= (double) N;
}
// Subtract data mean
for (int n = 0; n < N; n++) {
for (int d = 0; d < D; d++) {
X[n * D + d] -= mean[d];
}
}
free(mean); mean = NULL;
}
// Generates a Gaussian random number
template <class treeT, double (*dist_fn)( const DataPoint&, const DataPoint&)>
double TSNE<treeT, dist_fn>::randn() {
double x, radius;
do {
x = 2 * (rand() / ((double) RAND_MAX + 1)) - 1;
double y = 2 * (rand() / ((double) RAND_MAX + 1)) - 1;
radius = (x * x) + (y * y);
} while ((radius >= 1.0) || (radius == 0.0));
radius = sqrt(-2 * log(radius) / radius);
x *= radius;
return x;
}
extern "C"
{
#ifdef _WIN32
__declspec(dllexport)
#endif
extern void tsne_run_double(double* X, int N, int D, double* Y,
int no_dims = 2, double perplexity = 30, double theta = .5,
int num_threads = 1, int max_iter = 1000, int n_iter_early_exag = 250,
int random_state = -1, bool init_from_Y = false, int verbose = 0,
double early_exaggeration = 12, double learning_rate = 200,
double *final_error = NULL, int distance = 1)
{
if (verbose)
fprintf(stderr, "Performing t-SNE using %d cores.\n", NUM_THREADS(num_threads));
if (distance == 0) {
TSNE<SplitTree, euclidean_distance> tsne;
tsne.run(X, N, D, Y, no_dims, perplexity, theta, num_threads, max_iter, n_iter_early_exag,
random_state, init_from_Y, verbose, early_exaggeration, learning_rate, final_error);
}
else {
TSNE<SplitTree, euclidean_distance_squared> tsne;
tsne.run(X, N, D, Y, no_dims, perplexity, theta, num_threads, max_iter, n_iter_early_exag,
random_state, init_from_Y, verbose, early_exaggeration, learning_rate, final_error);
}
}
}