forked from maximsch2/bhtsne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsne_bin.cpp
164 lines (141 loc) · 4.75 KB
/
tsne_bin.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
#include "tsne_core.cpp"
#include <unistd.h>
#include <ctype.h>
// Function that loads data from a t-SNE file
// Note: this function does a malloc that should be freed elsewhere
template<typename T>
bool load_data(T** data, int* n, int* d, int* no_dims, T* theta, T* perplexity, int* rand_seed, int* max_iter, char* infile) {
// Open file, read first 2 integers, allocate memory, and read the data
FILE *h;
if((h = fopen(infile, "r+b")) == NULL) {
printf("Error: could not open data file.\n");
return false;
}
fread(n, sizeof(int), 1, h); // number of datapoints
fread(d, sizeof(int), 1, h); // original dimensionality
fread(theta, sizeof(T), 1, h); // gradient accuracy
fread(perplexity, sizeof(T), 1, h); // perplexity
fread(no_dims, sizeof(int), 1, h); // output dimensionality
fread(max_iter, sizeof(int),1,h);
*data = (T*) malloc(*d * *n * sizeof(T));
if(*data == NULL) { printf("Memory allocation failed!\n"); exit(1); }
fread(*data, sizeof(T), *n * *d, h); // the data
*rand_seed = 0;
if(!feof(h)) fread(rand_seed, sizeof(int), 1, h); // random seed
fclose(h);
printf("Read the %i x %i data matrix successfully!\n", *n, *d);
return true;
}
template<typename T>
bool load_data(T** data, int* n, int* d, char* infile) {
FILE *h;
if ((h=fopen(infile,"r+b"))==NULL) {
printf("Error: could not open data file.\n");
return false;
}
fread(n,sizeof(int),1,h);
fread(d,sizeof(int),1,h);
*data = (T*) malloc(*d * *n * sizeof(T));
if (*data==NULL) { printf("Memory allocation failed!\n"); exit(1); }
fread(*data, sizeof(T),*n * *d,h);
fclose(h);
printf("Read the %i x %i data matrix successfully!\n",*n,*d);
return true;
}
// Function that saves map to a t-SNE file
template<typename T>
void save_data(T* data, int* landmarks, T* costs, int n, int d, char* outfile) {
// Open file, write first 2 integers and then the data
FILE *h;
if((h = fopen(outfile, "w+b")) == NULL) {
printf("Error: could not open data file.\n");
return;
}
fwrite(&n, sizeof(int), 1, h);
fwrite(&d, sizeof(int), 1, h);
fwrite(data, sizeof(T), n * d, h);
fwrite(landmarks, sizeof(int), n, h);
fwrite(costs, sizeof(T), n, h);
fclose(h);
printf("Wrote the %i x %i data matrix successfully!\n", n, d);
}
template<typename T>
void run_tSNE_andSave(T *inputData, int N, int D, int no_dims, T theta, T perplexity, int rand_seed, int max_iter, char* outfile) {
// Allocate memory for the output
T* Y = (T*) malloc(N * no_dims * sizeof(T));
if(Y == NULL) { printf("Memory allocation failed!\n"); exit(1); }
int res = run_tSNE(inputData, Y, N, D, no_dims, theta, perplexity, rand_seed, true, max_iter);
if (res > 0)
exit(res);
// Make dummy landmarks and costs
int* landmarks = (int*) malloc(N * sizeof(int));
if(landmarks == NULL) { printf("Memory allocation failed!\n"); exit(1); }
for(int n = 0; n < N; n++) landmarks[n] = n;
T* costs = (T*) calloc(N, sizeof(T));
if(costs == NULL) { printf("Memory allocation failed!\n"); exit(1); }
// Save the results
save_data(Y, landmarks, costs, N, no_dims, outfile);
// Clean up the memory
free(Y); Y = NULL;
free(costs); costs = NULL;
free(landmarks); landmarks = NULL;
}
// Function that runs the Barnes-Hut implementation of t-SNE
int main(int argc, char* argv[]) {
// Define some variables
char* infile = "data.dat";
char* outfile = "result.dat";
int no_dims = 0;
int rand_seed = 0;
int max_iter = 1000;
double perplexity = 30;
double theta = 0.5;
double *data;
int N, D;
int c;
opterr = 0;
while ((c=getopt(argc,argv,"i:o:d:r:m:p:t:")) != -1)
switch (c)
{
case 'i':
infile = optarg;
break;
case 'o':
outfile = optarg;
break;
case 'd':
no_dims = atoi(optarg);
break;
case 'r':
rand_seed = atoi(optarg);
break;
case 'm':
max_iter = atoi(optarg);
break;
case 'p':
perplexity = atof(optarg);
break;
case 't':
theta = atof(optarg);
break;
case '?':
if (isprint(optopt))
fprintf(stderr,"Unknown option character '\\x%x'.\n",optopt);
return 1;
default:
abort();
}
//if no_dims is not provided, then assume old format data file and read that
if (no_dims<1) {
// Read the parameters and the dataset
if(load_data<double>(&data, &N, &D, &no_dims, &theta, &perplexity, &rand_seed, &max_iter, infile)) {
run_tSNE_andSave<double>(data, N, D, no_dims, theta, perplexity, rand_seed, max_iter, outfile);
free(data); data = NULL;
}
} else {
if (load_data<double>(&data,&N,&D,infile)) {
run_tSNE_andSave<double>(data,N,D,no_dims,theta,perplexity,rand_seed,max_iter,outfile);
free(data); data=NULL;
}
}
}