-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtsne_p.cu
219 lines (192 loc) · 6.25 KB
/
tsne_p.cu
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
#include <time.h>
#include <float.h>
#include "nvmatrix.cuh"
#include "tsne_p.cuh"
#include "mex.h"
/* Implementation of t-SNE in CUDA (designed for Matlab).
*
*
* (C) Laurens van der Maaten, 2010
* University of California, San Diego
*
*/
void tsne(float* inp_P, unsigned int N, float* mappedX, unsigned int no_dims) {
/* Initialize some variables */
int max_iter = 1000;
float initial_momentum = 0.5f;
float final_momentum = 0.8f;
int momentum_switch_iter = 250;
int lie_switch_iter = 100;
float momentum = initial_momentum;
float eta = 500.0f;
/* Fire up cublas */
checkCudaErrors(cudaSetDevice(gpuGetMaxGflopsDeviceId()));
cublasStatus status = cublasInit();
if(status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! cublas initialization failed\n");
}
NVMatrix::initDeviceProps();
NVMatrix::initRandom(time(0));
/* Print memory information */
size_t freeMem, totalMem;
cudaMemGetInfo(&freeMem, &totalMem);
fprintf(stdout, "Running CUDA implementation of t-SNE...\n");
fprintf(stdout, " - GPU memory is %d bytes (%d available).\n", static_cast<unsigned int>(totalMem), static_cast<unsigned int>(freeMem));
fprintf(stdout, " - NOTE: This implementation does not show intermediate plots.\n");
/* Copy data onto device, and make sure it is normalized */
NVMatrix* Q = new NVMatrix(N, N);
NVMatrix* P = new NVMatrix(true, inp_P, N, N);
P->zeroDiagonal();
Q->copyFromDevice(*P);
P->add(Q->getTranspose());
P->scale(1.0f / P->sum());
P->addScalar(FLT_MIN);
P->scale(4.0f);
/* Initialize the solution */
NVMatrix* Y = new NVMatrix(N, no_dims, false);
Y->apply(NVMatrix::ZERO);
Y->addGaussianNoise(0.0001f);
/* Allocate some memory */
NVMatrix* Qnum = new NVMatrix(N, N);
NVMatrix* sum_Q = new NVMatrix(1, N);
NVMatrix* sum_Y = new NVMatrix(N, 1);
NVMatrix* square_Y = new NVMatrix(N, no_dims);
NVMatrix* dY = new NVMatrix(N, no_dims);
NVMatrix* diffY = new NVMatrix(N, no_dims);
NVMatrix* incY = new NVMatrix(N, no_dims);
NVMatrix* gains = new NVMatrix(N, no_dims);
NVMatrix* gains_update1 = new NVMatrix(N, no_dims);
NVMatrix* gains_update2 = new NVMatrix(N, no_dims);
incY->apply(NVMatrix::ZERO);
gains->apply(NVMatrix::ONE);
/* Perform updates */
for(int iter = 0; iter < max_iter; iter++) {
/* Create transposes, and stop early stopping */
NVMatrix* Y_trans = &Y->getTranspose();
NVMatrix* sum_Y_trans = &sum_Y->getTranspose();
if(iter == lie_switch_iter) {
P->scale(0.25f);
}
if(iter == momentum_switch_iter) {
momentum = final_momentum;
}
/* Compute pairwise similarity matrix for the map */
square_Y->copyFromDevice(*Y);
square_Y->apply(NVMatrix::SQUARE);
square_Y->sum(1, *sum_Y);
Y->rightMult(*Y_trans, -2.0f, *Qnum);
Qnum->addVector(*sum_Y);
Qnum->addVector(*sum_Y_trans);
Qnum->apply(NVMatrix::STUDENT);
Qnum->zeroDiagonal();
Q->copyFromDevice(*Qnum);
Q->scale(1.0f / Q->sum());
/* Clean up memory */
delete Y_trans;
delete sum_Y_trans;
/* Compute gradient */
Q->add(*P, -1.0f, 1.0f);
Q->eltWiseMult(*Qnum);
Q->sum(0, *sum_Q);
Q->scale(-1.0f);
Q->setDiagonal(*sum_Q);
Q->rightMult(*Y, *dY);
/* Update gains */
dY->compareSigns(*incY, *gains_update1);
gains_update1->addScalar(-1.0f);
gains_update1->scale(-1.0f);
dY->compareSigns(*incY, *gains_update2);
gains_update2->eltWiseMult(*gains);
gains_update2->scale(0.8f);
gains->addScalar(0.2f);
gains->eltWiseMult(*gains_update1);
gains->add(*gains_update2);
/* Perform map update */
dY->eltWiseMult(*gains);
incY->add(*dY, momentum, -eta);
Y->add(*incY);
/* Print out progress */
if((iter + 1) % 100 == 0) {
Q->copyFromDevice(*Qnum);
Q->scale(1.0f / Q->sum());
Q->addScalar(FLT_MIN);
P->eltWiseDivide(*Q, *Q);
Q->apply(NVMatrix::LOG);
Q->eltWiseMult(*P);
Q->zeroDiagonal();
float C = Q->sum();
fprintf(stdout, "Iteration %d of %d: KL(P||Q) = %f\n", iter + 1, max_iter, C);
}
}
/* Copy low-dimensional map to host */
cudaThreadSynchronize();
Y->getTranspose().copyToHost(mappedX, no_dims, N);
/* Clean up some memory */
delete Y;
delete P;
delete Q;
delete Qnum;
delete sum_Q;
delete sum_Y;
delete square_Y;
delete dY;
delete diffY;
delete incY;
delete gains;
delete gains_update1;
delete gains_update2;
/* Shut down cublas */
NVMatrix::destroyRandom();
status = cublasShutdown();
if(status != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "!!!! error while shutting down cublas\n");
}
cudaThreadExit();
}
/* Function call:
*
* mappedX = tsne_p(P, labels, no_dims);
*
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
/* Check inputs */
if(nrhs < 1) {
mexErrMsgTxt("Function requires at least one input.");
}
if(!mxIsDouble(prhs[0])) {
mexErrMsgTxt("Error in input (input data should be doubles).");
}
if(nrhs > 1 && !mxIsDouble(prhs[1])) {
mexErrMsgTxt("Error in input (number of dimensions should be a double).");
}
/* Process inputs */
double* inp_P = mxGetPr(prhs[0]);
int no_dims = nrhs > 2 ? (int) *mxGetPr(prhs[2]) : 2;
if(mxGetN(prhs[2]) > 1 || mxGetM(prhs[2]) > 1) {
mexErrMsgTxt("Specification of an initial solution is not supported by the CUDA implementation of t-SNE.");
}
int N = mxGetM(prhs[0]);
if(mxGetN(prhs[0]) != N) {
mexErrMsgTxt("Input similarities P should be a square matrix.");
}
/* Convert data to float */
float* P = (float*) malloc(N * N * sizeof(float));
for(int i = 0; i < N * N; i++) {
P[i] = (float) inp_P[i];
}
/* Perform t-SNE */
float* mappedX = (float*) malloc(N * no_dims * sizeof(float));
tsne(P, N, mappedX, no_dims);
/* Construct output matrix (transpose) */
mwSize dims[2] = {N, no_dims};
plhs[0] = mxCreateNumericArray(2, dims, mxSINGLE_CLASS, mxREAL);
float* matlab_mappedX = (float*) mxGetPr(plhs[0]);
for(int i = 0; i < N; i++) {
for(int j = 0; j < no_dims; j++) {
matlab_mappedX[j * N + i] = mappedX[i * no_dims + j];
}
}
/* Clean up memory */
free(P);
free(mappedX);
}