forked from lvdmaaten/bhtsne
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtsne_bin.cpp
95 lines (74 loc) · 3.06 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
#include "tsne_core.cpp"
// 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) {
// Open file, read first 2 integers, allocate memory, and read the data
FILE *h;
if((h = fopen("data.dat", "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
*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;
}
// 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) {
// Open file, write first 2 integers and then the data
FILE *h;
if((h = fopen("result.dat", "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) {
// 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);
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);
// 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() {
// Define some variables
int N, D, no_dims, rand_seed;
double perplexity, theta, *data;
// Read the parameters and the dataset
if(load_data<double>(&data, &N, &D, &no_dims, &theta, &perplexity, &rand_seed)) {
run_tSNE_andSave<double>(data, N, D, no_dims, theta, perplexity, rand_seed);
free(data); data = NULL;
}
}