-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsne.cpp
597 lines (501 loc) · 17.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
/*
* 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.
*
* Modified by Stephen ingram in 2013 to take truncated distance matrix input
* truncated distance matrices are arrays of hashtables, where distances are keyed by index
* arrays and hashtable data structures are built using GLib
*
*/
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <time.h>
#include "quadtree.h"
#include "tsne.h"
using namespace std;
const char *g_inputfile = NULL;
const char *g_outputfile = NULL;
const char *g_initstartfile = NULL;
TSNE::TSNE() {
dmat = NULL;
theta = 0.5;
perplexity = 3.0;
Y = NULL;
costs = NULL;
random_start = true;
max_iter = 3000;
stop_lying_iter=1000;
mom_switch_iter = 250;
}
// Perform t-SNE
void TSNE::run( ) {
int N = dmat->N;
Y = (double*) malloc(dmat->N * 2 * sizeof(double));
costs = (double*) calloc(dmat->N, sizeof(double));
if(Y == NULL || costs == NULL) { printf("A: Memory allocation failed!\n"); exit(1); }
printf("perplexity = %f, and theta = %f\n", perplexity, theta);
// Set learning parameters
float total_time = .0;
clock_t start, end;
//int max_iter = 1000, stop_lying_iter = 250, mom_switch_iter = 250;
//int max_iter = 3000, stop_lying_iter = 1000, mom_switch_iter = 250;
double momentum = .5, final_momentum = .8;
double eta = 200.0;
// Allocate some memory
int no_dims = 2;
// printf("N=%d no_dims=%d\n",N,no_dims);
double* dY = (double*) malloc(N * no_dims * sizeof(double));
double* uY = (double*) malloc(N * no_dims * sizeof(double));
double* gains = (double*) malloc(N * no_dims * sizeof(double));
if(dY == NULL || uY == NULL || gains == NULL) { printf("B: Memory allocation failed!\n"); exit(1); }
for(int i = 0; i < N * no_dims; i++) uY[i] = .0;
for(int i = 0; i < N * no_dims; i++) gains[i] = 1.0;
// Normalize input data (to prevent numerical problems)
printf("Computing input similarities...\n");
start = clock();
// Compute asymmetric pairwise input similarities
int* row_P; int* col_P; double* val_P;
computeGaussianPerplexity(&row_P, &col_P, &val_P);
// Symmetrize input similarities
symmetrizeMatrix(&row_P, &col_P, &val_P);
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 = clock();
// double lie_value = 4.00;
double lie_value = 12.00;
// Lie about the P-values
for(int i = 0; i < row_P[N]; i++) val_P[i] *= lie_value;
// Initialize solution (randomly)
if( init_startfile ) {
// load the file into Y
//load_init_startfile(Y);
}
else if( random_start ) {
for(int i = 0; i < N * no_dims; i++) Y[i] = randn() * .0001;
}
else{
VERTEX* tree = build_tree(make_leaves(dmat->N),make_edges(dmat));
dendrogram_traverse( tree, true, -1.0, -1.0, 2.0, 2.0 );
}
// Perform main training loop
printf("Done in %4.2f seconds (sparsity = %f)!\nLearning embedding...\n", (float) (end - start) / CLOCKS_PER_SEC, (double) row_P[N] / ((double) N * (double) N));
start = clock();
for(int iter = 0; iter < max_iter; iter++) {
// Compute (approximate) gradient
computeGradient(row_P, col_P, val_P, dY );
// Update gains
for(int i = 0; i < N * no_dims; i++) gains[i] = (sign(dY[i]) != sign(uY[i])) ? (gains[i] + .2) : (gains[i] * .8);
for(int i = 0; i < N * no_dims; i++) if(gains[i] < .01) gains[i] = .01;
// Perform gradient update (with momentum and gains)
for(int i = 0; i < N * no_dims; i++) uY[i] = momentum * uY[i] - eta * gains[i] * dY[i];
for(int i = 0; i < N * no_dims; i++) Y[i] = Y[i] + uY[i];
// Make solution zero-mean
zeroMean(Y, N, 2);
// 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] /= lie_value;
}
if(iter == mom_switch_iter) momentum = final_momentum;
// Print out progress
if((iter > 0 && iter % 50 == 0) || iter == max_iter - 1) {
end = clock();
double C = .0;
C = evaluateError(row_P, col_P, val_P); // doing approximate computation here!
if(iter == 0)
printf("Iteration %d: error is %f\n", iter + 1, C);
else {
total_time += (float) (end - start) / CLOCKS_PER_SEC;
printf("Iteration %d: error is %f (50 iterations in %4.2f seconds)\n", iter, C, (float) (end - start) / CLOCKS_PER_SEC);
}
start = clock();
}
}
end = clock(); total_time += (float) (end - start) / CLOCKS_PER_SEC;
// 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;
printf("Fitting performed in %4.2f seconds.\n", total_time);
}
void TSNE::zeroMean(double* X, int N, int D) {
// Compute data mean
double* mean = (double*) calloc(D, sizeof(double));
if(mean == NULL) { printf("C:Memory allocation failed!\n"); exit(1); }
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;
}
// Compute gradient of the t-SNE cost function (using Barnes-Hut algorithm)
void TSNE::computeGradient(int* inp_row_P, int* inp_col_P, double* inp_val_P, double* dC)
{
int D = 2;
int N = dmat->N;
// Construct quadtree on current map
QuadTree* tree = new QuadTree(Y, N);
// Compute all terms required for t-SNE gradient
double sum_Q = .0;
double* pos_f = (double*) calloc(N * D, sizeof(double));
double* neg_f = (double*) calloc(N * D, sizeof(double));
if(pos_f == NULL || neg_f == NULL) { printf("D:Memory allocation failed!\n"); exit(1); }
tree->computeEdgeForces(inp_row_P, inp_col_P, inp_val_P, N, pos_f);
for(int n = 0; n < N; n++) tree->computeNonEdgeForces(n, theta, neg_f + n * D, &sum_Q);
// Compute final t-SNE gradient
for(int i = 0; i < N * D; i++) {
dC[i] = pos_f[i] - (neg_f[i] / sum_Q);
}
free(pos_f);
free(neg_f);
delete tree;
}
// Evaluate t-SNE cost function (approximately)
double TSNE::evaluateError(int* row_P, int* col_P, double* val_P)
{
int N = dmat->N;
// Get estimate of normalization term
const int QT_NO_DIMS = 2;
QuadTree* tree = new QuadTree(Y, N);
double buff[QT_NO_DIMS] = {.0, .0};
double sum_Q = .0;
for(int n = 0; n < N; n++) tree->computeNonEdgeForces(n, theta, buff, &sum_Q);
// Loop over all edges to compute t-SNE error
int ind1, ind2;
double C = .0, Q;
for(int n = 0; n < N; n++) {
ind1 = n * QT_NO_DIMS;
for(int i = row_P[n]; i < row_P[n + 1]; i++) {
Q = .0;
ind2 = col_P[i] * QT_NO_DIMS;
for(int d = 0; d < QT_NO_DIMS; d++) buff[d] = Y[ind1 + d];
for(int d = 0; d < QT_NO_DIMS; d++) buff[d] -= Y[ind2 + d];
for(int d = 0; d < QT_NO_DIMS; d++) Q += buff[d] * buff[d];
Q = (1.0 / (1.0 + Q)) / sum_Q;
C += val_P[i] * log((val_P[i] + FLT_MIN) / (Q + FLT_MIN));
}
}
delete tree;
return C;
}
// Compute input similarities with a fixed perplexity using ball trees (this function allocates memory another function should free)
void TSNE::computeGaussianPerplexity(int** _row_P, int** _col_P, double** _val_P) {
int N = dmat->N;
// compute memory requirements
int i,j,m;
double w;
GList* listhead=NULL;
GList* indices=NULL;
int num_vals=0;
for( i = 0; i < dmat->N; i++ ) {
num_vals += g_hash_table_size(g_array_index(dmat->rows,GHashTable*,i));
}
// Allocate the memory we need
*_row_P = (int*) malloc((N + 1) * sizeof(int));
*_col_P = (int*) calloc(num_vals, sizeof(int));
*_val_P = (double*) calloc(num_vals, sizeof(double));
if(*_row_P == NULL || *_col_P == NULL || *_val_P == NULL) { printf("E:Memory allocation failed!%d,%d\n",N+1,num_vals); exit(1); }
int* row_P = *_row_P;
int* col_P = *_col_P;
double* val_P = *_val_P;
double* cur_P = (double*) malloc((N - 1) * sizeof(double));
if(cur_P == NULL) { printf("F:Memory allocation failed!\n"); exit(1); }
row_P[0] = 0;
for(int n = 0; n < N; n++) row_P[n + 1] = row_P[n] + g_hash_table_size(g_array_index(dmat->rows,GHashTable*,n));
for(int n = 0; n < N; n++) {
// 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;
int K;
GHashTable* row = g_array_index(dmat->rows,GHashTable*,n);
// Iterate until we found a good perplexity
int iter = 0; double sum_P;
while(!found && iter < 200) {
// Compute Gaussian kernel row
m = 0;
indices = g_hash_table_get_keys(row);
listhead = indices;
while(indices != NULL) {
w = *((float*)g_hash_table_lookup(row,indices->data));
cur_P[m] = exp(-beta * w);
m++;
indices = indices->next;
}
K = m;
g_list_free(listhead);
// Compute entropy of current row
sum_P = DBL_MIN;
for(m = 0; m < K; m++) sum_P += cur_P[m];
double H = .0;
m=0;
indices = g_hash_table_get_keys(row);
listhead = indices;
while(indices != NULL) {
w = *((float*)g_hash_table_lookup(row,indices->data));
H += beta * (w * cur_P[m]);
m++;
indices = indices->next;
}
H = (H / sum_P) + log(sum_P);
g_list_free(listhead);
// 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(m = 0; m < K; m++) cur_P[m] /= sum_P;
m=0;
indices = g_hash_table_get_keys(row);
listhead = indices;
while(indices != NULL) {
j = *((int*)(indices->data));
col_P[row_P[n] + m] = j;
val_P[row_P[n] + m] = cur_P[m];
m++;
indices = indices->next;
}
g_list_free(listhead);
}
// Clean up memory
free(cur_P);
}
void TSNE::symmetrizeMatrix(int** _row_P, int** _col_P, double** _val_P) {
int N = dmat->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) { printf("G: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
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) { printf("H: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] + row_counts[n];
// Fill the result matrix
int* offset = (int*) calloc(N, sizeof(int));
if(offset == NULL) { printf("I: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++) { // 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 || (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;
}
// Generates a Gaussian random number
double TSNE::randn() {
double x, y, radius;
do {
x = 2 * (rand() / ((double) RAND_MAX + 1)) - 1;
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;
y *= radius;
return x;
}
/* output data to a csv */
void TSNE::output_csv( const char* filename ) {
FILE* fp = NULL;
int i;
if((fp = fopen(filename,"w"))==NULL) {
fprintf(stderr,"Error: cannot open %s\n",filename);
exit(0);
}
for( i = 0; i < dmat->N; i++ )
fprintf(fp,"%f,%f\n",Y[i*2],Y[i*2+1]);
}
void TSNE::dendrogram_traverse( VERTEX* v, bool isVertical, double top, double left, double width, double height ) {
if( v->a == NULL && v->b == NULL) {
// pick a center spot in the rect
Y[(v->id)*2+0] = left + width*(rand() / ((double) RAND_MAX + 1));
Y[(v->id)*2+1] = top + height*(rand() / ((double) RAND_MAX + 1));;
}
else {
double proportion = ((double)(v->a->size)) / ((double)(v->size));
double a_top,b_top,a_left,b_left,a_width,b_width,a_height,b_height;
if( isVertical ) {
a_top = top;
a_left = left;
a_width = width;
a_height = height*proportion;
b_top = top+height*proportion;
b_left = left;
b_width = width;
b_height = height*(1.0-proportion);
}
else {
a_top = top;
a_left = left;
a_width = width*proportion;
a_height = height;
b_top = top;
b_left = left+width*proportion;
b_width = width*(1.0-proportion);
b_height = height;
}
dendrogram_traverse(v->a, !isVertical, a_top, a_left, a_width, a_height);
dendrogram_traverse(v->b, !isVertical, b_top, b_left, b_width, b_height);
}
}
void usage() {
printf("tsne [options]\n");
printf("\ti - input truncated distance matrix (vec format)\n");
printf("\to - input coordinates (csv format)\n");
printf("\tt - Barnes hut approximation factor theta [0.5]\n");
printf("\tp - perplexity (density) [3.0]\n");
printf("\tN - normal iterations\n");
printf("\tF - high energy iterations\n");
printf("\th - hierarchical start config [NO ARGUMENT]\n");
}
/* catch buggy input params */
void check_args() {
if( g_inputfile == NULL ) {
fprintf(stderr,"Need input filename, use -i\n");
exit(0);
}
}
/* parse user input */
void proc_command_args( int argc, char **argv, TSNE* tsne ) {
int i = 0;
char *argument = NULL;
while( i < argc ) {
if( ( argv[i][0] == '-' ) && (strlen( argv[i] ) > 1 ) ){
if( argv[i][2] != '\0')
argument = &(argv[i][2]);
else
argument = argv[i+1];
if( argv[i][1] == '?' ) {
usage(); exit(0);
} else if( argv[i][1] == 'i' ) {
g_inputfile = argument;
tsne->dmat = load_dmat( g_inputfile );
} else if( argv[i][1] == 't' ) {
tsne->theta = atof(argument);
} else if( argv[i][1] == 'p' ) {
tsne->perplexity = atof(argument);
} else if( argv[i][1] == 'N' ) {
tsne->max_iter = atoi(argument);
} else if( argv[i][1] == 'F' ) {
tsne->stop_lying_iter = atoi(argument);
} else if( argv[i][1] == 'o' ) {
g_outputfile = argument;
} else if( argv[i][1] == 'h' ) {
tsne->random_start = false;
} else if( argv[i][1] == 's') {
tsne->init_startfile = true;
}
}
i++;
}
check_args();
}
// Function that runs the Barnes-Hut implementation of t-SNE
int main(int argc, char** argv) {
TSNE* tsne = new TSNE();
srand(time(NULL));
proc_command_args(argc,argv,tsne);
// Read the parameters and the dataset
if(tsne->dmat != NULL) {
// Now fire up the SNE implementation
tsne->run( );
// write to disk
if( g_outputfile != NULL)
tsne->output_csv(g_outputfile);
}
}