-
Notifications
You must be signed in to change notification settings - Fork 0
/
ktsne.cpp
697 lines (545 loc) · 27.9 KB
/
ktsne.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include <algorithm>
#include <cmath>
#include <fstream>
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
error "Missing the <filesystem> header."
#endif
#include <iostream>
#include <limits>
#include <random>
#include <string>
#include <unordered_set>
#include <vector>
#include <getopt.h>
#include <unistd.h>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <falconn/lsh_nn_table.h>
using point_t = falconn::DenseVector<double>;
namespace Eigen {
using MatrixXdr = Matrix<double, Dynamic, Dynamic, RowMajor>; // this is not FORTRAN
}
void print_vector(std::vector<double> const& v) {
std::cout << "[ ";
for(auto const& e: v) { std::cout << e << ' '; }
std::cout << "]\n";
}
/* reads data from csv format. assumes that data consists of floating
* point numbers only (as identified by strtof). works with header or
* without. probably really fragile, use with caution */
std::vector<point_t> read_data(char* fname, char delim=',') {/*{{{*/
std::ifstream fin{ fname };
if(!fin) {
std::cerr << "invalid file name: " << fname << '\n';
std::exit(-1);
}
std::vector<point_t> data;
size_t d = -1;
std::string line;
while(std::getline(fin, line)) {
if(d == -1) { d = std::count(line.begin(), line.end(), delim) + 1; }
std::istringstream iss{ line };
size_t i = 0;
point_t pi(d);
std::string token;
while(std::getline(iss, token, delim)) {
char* ptr = nullptr;
double f = std::strtof(token.data(), &ptr);
/* did not read to end, assume that token is not double and skip token. */
if(ptr != token.data() + token.size()) { continue; }
pi(i++) = f;
}
/* only append if d elements were read */
if(i == d) { data.push_back(pi); }
}
std::cerr << "[read_data] read " << data.size() << " points of dimension " << d << '\n';
return data;
}/*}}}*/
std::vector<int> read_labels(char* fname) {/*{{{*/
std::ifstream fin{ fname };
if(!fin) {
std::cerr << "invalid file name: " << fname << '\n';
std::exit(-1);
}
std::vector<int> labels;
int x;
while(fin >> x) { labels.push_back(x); }
std::cerr << "[read_labels] read " << labels.size() << " labels\n";
return labels;
}/*}}}*/
/* normalize all points to unit vector norm */
void normalize(std::vector<point_t>& data) {/*{{{*/
for(auto& p: data) { p.normalize(); }
}/*}}}*/
/* center points around origin */
point_t center(std::vector<point_t>& data) {/*{{{*/
point_t center = data[0];
for(size_t i = 1; i < data.size(); ++i) { center += data[i]; }
center /= data.size();
for(auto& p: data) { p -= center; }
return center;
}/*}}}*/
int mathematically_correct_sign(double x) {/*{{{*/
if(x < std::numeric_limits<double>::epsilon()) { return 0; }
return std::signbit(x) ? 1 : -1;
}/*}}}*/
Eigen::MatrixXdr compute_sq_dist_slow(Eigen::MatrixXdr const& X, Eigen::MatrixXdr const& Y) {/*{{{*/
Eigen::MatrixXdr sq_dist{ X.rows(), Y.rows() };
for(size_t i = 0; i < X.rows(); ++i) {
for(size_t j = 0; j < Y.rows(); ++j) {
sq_dist(i, j) = (X.row(i) - Y.row(j)).squaredNorm();
}
}
return sq_dist;
}/*}}}*/
// binomial form: (X - Y)^2 = -2*X@Y + X^2 + Y^2
// this is faster due to optimized matrix multiplication and vectorization
// possibilities
Eigen::MatrixXdr compute_sq_dist_binomial(Eigen::MatrixXdr const& X, Eigen::MatrixXdr const& Y) {/*{{{*/
Eigen::MatrixXd D(X.rows(), Y.rows());
D = (
(X * Y.transpose() * -2).colwise()
+ X.rowwise().squaredNorm()
).rowwise()
+ Y.rowwise().squaredNorm().transpose();
return D;
}/*}}}*/
double compute_procrustes(Eigen::MatrixXdr const& X, Eigen::MatrixXdr const& Y) {
return (X - Y).array().square().sum();
}
double tune_beta(std::vector<double> const& dist_sq_one_point, size_t const perp, double const tol=1e-5) {/*{{{*/
double beta = 1.0, min_beta = std::numeric_limits<double>::lowest(), max_beta = std::numeric_limits<double>::max();
std::vector<double> P; P.resize(dist_sq_one_point.size());
double log_perp = std::log2(perp);
size_t j = 0;
while(j++ < 200) {
for(size_t i = 0; i < dist_sq_one_point.size(); ++i) { P[i] = std::exp(-beta * dist_sq_one_point[i]); }
double sum_P = std::accumulate(P.begin(), P.end(), std::numeric_limits<double>::min());
double H = 0.0;
for(size_t i = 0; i < dist_sq_one_point.size(); ++i) { H += beta * (dist_sq_one_point[i] * P[i]); }
H = (H / sum_P) + std::log2(sum_P);
double H_diff = H - log_perp;
if(std::abs(H_diff) < tol) { break; }
if(H_diff > 0) {
min_beta = beta;
if(max_beta == std::numeric_limits<double>::max()) { beta *= 2; }
else { beta = (beta + max_beta) / 2; }
} else {
max_beta = beta;
if(min_beta == std::numeric_limits<double>::lowest()) { beta /= 2; }
else { beta = (beta + min_beta) / 2; }
}
}
return beta;
}/*}}}*/
Eigen::SparseMatrix<double> high_dimensional_affinities(std::vector<point_t> const& data, size_t perp, size_t num_hash_tables, size_t bits, int num_probes, bool use_hyperplane=false) {/*{{{*/
falconn::LSHConstructionParameters params = falconn::get_default_parameters<point_t>(
data.size(),
data[0].size(),
falconn::DistanceFunction::EuclideanSquared,
true
);
params.l = num_hash_tables;
if(use_hyperplane) {
params.lsh_family = falconn::LSHFamily::Hyperplane;
params.k = bits; // unclear for cross polytope
}
auto table = falconn::construct_table<point_t>(data, params);
std::vector<Eigen::Triplet<double>> triplets; triplets.reserve(data.size()*3*perp);
size_t count_not_enough = 0;
// find k nearest neighbors for every point. k is equal to 3*perp as per the paper
for(size_t i = 0; i < data.size(); ++i) {
std::vector<int32_t> result; result.reserve(3*perp + 1);
auto query = table->construct_query_object(/*num_probes*/ num_probes != -1 ? params.l + num_probes : num_probes, /*max_num_candidates*/ -1);
query->find_k_nearest_neighbors(data[i], 3*perp + 1, &result);
result.erase(std::remove(result.begin(), result.end(), i)); // remove self from neighbors
if(result.size() != 3*perp) { count_not_enough += 1; }
// compute neighbor distance for every neighbor point
std::vector<double> dist_sq_one_point; dist_sq_one_point.reserve(result.size());
for(size_t j = 0; j < result.size(); ++j) { dist_sq_one_point.push_back((data[i] - data[result[j]]).squaredNorm()); }
// determine sigma for this point based on its neighbors
double beta = tune_beta(dist_sq_one_point, perp);
// recompute P
std::vector<double> P; P.resize(dist_sq_one_point.size());
for(size_t j = 0; j < dist_sq_one_point.size(); ++j) { P[j] = std::exp(-beta * dist_sq_one_point[j]); }
double sum_P = std::accumulate(P.begin(), P.end(), std::numeric_limits<double>::min());
// row normalize and add to coefficients
for(size_t j = 0; j < result.size(); ++j) { triplets.push_back({static_cast<int>(i), result[j], P[j] / sum_P}); };
}
if(count_not_enough) {
std::cerr << __func__ << " [INFO] not enough neighbors were returned for " << count_not_enough << " points.\n"
<< "consider enabling multiprobing or decreasing the perplexity.\n";
}
Eigen::SparseMatrix<double> P_j_given_i(data.size(), data.size());
P_j_given_i.setFromTriplets(triplets.begin(), triplets.end());
return P_j_given_i;
}/*}}}*/
template <class RNG>
void initialize_gaussian(Eigen::MatrixXdr& A, double sigma, RNG&& gen) {
std::normal_distribution<double> dist{ 0, sigma };
for(size_t i = 0; i < A.size(); ++i) { *(A.data() + i) = dist(gen); }
}
void initialize_PCA(Eigen::MatrixXdr& A, std::vector<point_t> const& data) {
Eigen::MatrixXdr X(data.size(), data[0].size());
for(size_t i = 0; i < data.size(); ++i) { X.row(i) = data[i]; }
Eigen::MatrixXdr X_centered = X.rowwise() - X.colwise().mean();
Eigen::MatrixXdr X_cov = X_centered.adjoint() * X_centered;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXdr> eig(X_cov);
size_t dim = A.cols();
A = X * eig.eigenvectors().rightCols(dim);
}
/* kmeans++ initialization strategy
* (1) choose first centroid at random
* (2) for each point x, calculate the distance to the nearest previously chosen
* centroid D(x)
* (3) select the next centroid such that the probability to choose some centroid
* x is proportional to D(x)^2
* (4) repeat steps 2 and 3 until k centroids have been selected */
template <class RNG>
Eigen::MatrixXdr kmeanspp_initialize(Eigen::MatrixXdr const& Y, size_t const k, RNG&& gen) {/*{{{*/
std::unordered_set<size_t> centroid_idxs;
std::uniform_int_distribution<size_t> dist{ 0, static_cast<size_t>(Y.rows() - 1)};
centroid_idxs.insert(dist(gen));
while(centroid_idxs.size() != k) {
std::vector<double> dist_sq; dist_sq.reserve(centroid_idxs.size());
for(size_t i = 0; i < Y.rows(); ++i) {
double smallest_dist_sq = INFINITY;
for(size_t centroid_idx: centroid_idxs) {
double centroid_dist_sq = (Y.row(centroid_idx) - Y.row(i)).squaredNorm();
if(centroid_dist_sq < smallest_dist_sq) { smallest_dist_sq = centroid_dist_sq; }
}
assert(smallest_dist_sq != INFINITY);
dist_sq.push_back(smallest_dist_sq);
}
std::discrete_distribution<size_t> centroid_dist{ dist_sq.begin(), dist_sq.end() };
centroid_idxs.insert(centroid_dist(gen));
}
Eigen::MatrixXdr centroids{ k, Y.cols() };
auto it = centroid_idxs.begin();
for(auto i = 0; i < k; ++i, ++it) { centroids.row(i) = Y.row(*it); }
return centroids;
}/*}}}*/
template <class RNG>
Eigen::MatrixXdr kmeans_initialize_random(Eigen::MatrixXdr const& Y, size_t const k, RNG&& gen) {/*{{{*/
std::unordered_set<size_t> centroid_idxs;
std::uniform_int_distribution<size_t> dist{ 0, static_cast<size_t>(Y.rows() - 1)};
while(centroid_idxs.size() != k) { centroid_idxs.insert(dist(gen)); }
Eigen::MatrixXdr centroids{ k, Y.cols() };
auto it = centroid_idxs.begin();
for(auto i = 0; i < k; ++i, ++it) { centroids.row(i) = Y.row(*it); }
return centroids;
}/*}}}*/
// std::unordered_multimap can erase() an entire key, an iterator to a value or
// a range, but not a specific value. so here is a helper to find an value and
// erase it.
template <class K, class V>
void multimap_remove_single_value(std::unordered_multimap<K, V>& m, K const& key, V const& value) {/*{{{*/
auto range = m.equal_range(key);
auto it = range.first;
for(/**/; it != range.second; ++it) {
if(std::equal_to<V>{}(value, it->second)) { break; }
}
assert(it != range.second);
m.erase(it);
}/*}}}*/
Eigen::MatrixXdr::Index find_min(Eigen::MatrixXdr const& mat, int row) {
double mini = INFINITY;
int mini_index;
for(int j = 0; j < mat.cols(); ++j) {
if(mat(row, j) < mini) {
mini = mat(row, j);
mini_index = j;
}
}
return mini_index;
}
template <class RNG>
std::tuple<Eigen::MatrixXdr, Eigen::VectorXd, std::vector<size_t>> do_kmeans(Eigen::MatrixXdr const& Y, size_t const k, size_t const max_iter, RNG&& gen, bool compute_objective=false) {/*{{{*/
std::unordered_set<size_t> centroid_idxs;
std::uniform_int_distribution<size_t> dist{ 0, static_cast<size_t>(Y.rows() - 1)};
while(centroid_idxs.size() != k) { centroid_idxs.insert(dist(gen)); }
Eigen::MatrixXdr centroids = kmeans_initialize_random(Y, k, gen);
// Eigen::MatrixXdr centroids = kmeanspp_initialize(Y, k, gen);
std::vector<std::vector<size_t>> cluster_assignments; cluster_assignments.resize(k);
for(size_t it = 0; it < max_iter; ++it) {
for(auto& vec: cluster_assignments) { vec.clear(); vec.reserve(Y.rows()/k); } // clear should not change capacity(), so this should only allocate the very first time!
// assign
Eigen::MatrixXdr sq_dist = compute_sq_dist_binomial(Y, centroids);
for(int i = 0; i < Y.rows(); ++i) {
Eigen::MatrixXdr::Index nearest_centroid_idx = find_min(sq_dist, i);
cluster_assignments[nearest_centroid_idx].push_back(i);
}
// for every cluster that is empty, find a replacement point which is
// farthest away from its assigned centroid. more than one cluster can
// be empty, so we make sure that a point is not chosen as a replacement
// more than once. this implies an ordering to the replacements
for(size_t n = 0; n < k; ++n) {
std::unordered_set<size_t> replacement_points;
if(cluster_assignments[n].size() == 0) {
double farthest_dist = 0;
size_t taken_from = -1;
int farthest_idx = -1;
for(size_t nn = 0; nn < k; ++nn) {
if(n == nn || cluster_assignments[nn].size() < 2) { continue; }
auto range = std::make_pair(cluster_assignments[nn].begin(), cluster_assignments[nn].end());
for(auto it = range.first; it != range.second; ++it) {
double dist = (centroids.row(nn) - Y.row(*it)).squaredNorm();
if(dist > farthest_dist && !replacement_points.count(*it)) {
farthest_dist = dist;
farthest_idx = *it;
taken_from = nn;
}
}
}
replacement_points.insert(farthest_idx);
std::remove(cluster_assignments[taken_from].begin(), cluster_assignments[taken_from].end(), farthest_idx);
cluster_assignments[n].push_back(farthest_idx);
}
}
// update
for(size_t n = 0; n < k; ++n) {
Eigen::VectorXd new_centroid = Eigen::VectorXd::Zero(Y.cols());
auto range = std::make_pair(cluster_assignments[n].begin(), cluster_assignments[n].end());
for(auto it = range.first; it != range.second; ++it) { new_centroid += Y.row(*it); }
new_centroid /= cluster_assignments[n].size();
centroids.row(n) = new_centroid;
}
assert(!centroids.hasNaN());
}
Eigen::VectorXd num_assigned{ k };
for(size_t n = 0; n < k; ++n) { num_assigned(n) = cluster_assignments[n].size(); }
// reverse cluster_assignments, needed to later compute the objective by selecting the appropiate
// centroid for every point
std::vector<size_t> point_assignments;
if(compute_objective) {
point_assignments.resize(Y.rows());
for(size_t n = 0; n < k; ++n) {
auto range = std::make_pair(cluster_assignments[n].begin(), cluster_assignments[n].end());
for(auto it = range.first; it != range.second; ++it) { point_assignments[*it] = n; }
}
}
return std::make_tuple(centroids, num_assigned, point_assignments);
}/*}}}*/
double momentum(size_t iteration) { return iteration < 250 ? 0.5 : 0.8; }
void load_matrix(Eigen::MatrixXdr& X, std::string const& fname) {/*{{{*/
std::ifstream fin{ fname };
if(!fin) {
std::cerr << "could not open " << fname << '\n';
std::abort();
}
double x;
for(size_t i = 0; i < X.rows(); ++i) {
for(size_t j = 0; j < X.cols(); ++j) {
if(!fin) { std::cerr << "fUCK\n"; }
fin >> x;
X(i, j) = x;
}
}
}/*}}}*/
void print_csv(std::string filename, Eigen::MatrixXdr const& Y, std::vector<int> labels={}) {/*{{{*/
std::ofstream fout{ filename };
if(!fout) {
std::cerr << "could not open outfile " << filename << "!\n";
std::abort();
}
fout << "x,y" << (labels.size() ? ",label" : "") << '\n';
for(size_t i = 0; i < Y.rows(); ++i) {
for(size_t j = 0; j < Y.cols(); ++j) {
fout << Y(i, j) << (j == Y.cols()-1 ? "" : ",");
}
if(labels.size()) { fout << "," << labels[i]; }
fout << '\n';
}
}/*}}}*/
struct config_t {
double eta = 200;
unsigned int perplexity = 30;
double early_exaggeration = 12;
double late_exaggeration = 12;
unsigned int num_hash_tables = 100;
int num_hash_bits = -1;
int num_probes = -1;
unsigned int kmeans_lo = 30;
unsigned int kmeans_hi = 30;
unsigned int seed = 666;
size_t max_iter = 1000;
int compute_objective = 0;
int print_intermediate = 0;
int use_hyperplane = 0;
int random_init = 0;
};
void print_usage(std::string const& program) {
config_t default_config;
std::cerr << "usage: " << program << " opts FILE\n"
<< "where opt in opts is one of the following:\n\n"
<< " -p ... perplexity (effective number of neighbors per point). tunable parameter, default = " << default_config.perplexity << '\n'
<< " -n ... stepsize eta of gradient descent, default = " << default_config.eta << '\n'
<< " -x, --early-exaggeration ... early exaggeration value, default = " << default_config.early_exaggeration << '\n'
<< " -X, --late-exaggeration ... late exaggeration value, default = " << default_config.late_exaggeration << '\n'
<< " -i, --max-iter ... number of gradient descent iterations, default = " << default_config.max_iter << '\n'
<< " -s, --seed ... random seed\n\n"
<< " -k, --k-lo ... lower bound for k-means k, default = " << default_config.kmeans_lo << '\n'
<< " -K, --k-hi ... upper bound for k-means k, default = " << default_config.kmeans_hi << "\n\n"
<< " --random-init ... initialize using a Gaussian distribution with stddev 10e-4\n"
<< " --pca-init ... initialize with PCA\n\n"
<< " --num-hash-tables ... number of hash tables for FALCONN lsh. tunable parameter, default = " << default_config.num_hash_tables << '\n'
<< " --num-hash-bits ... number of hash bits, controls number of buckets per table. automatically set to max(16, log2(n)) if -1 is passed, default = " << default_config.num_hash_bits << '\n'
<< " --num-probes ... number of probes for multi-probe LSH. tunable parameter (inverse relation to L), default = " << default_config.num_probes << "\n\n"
<< " --[no-]compute-objective ... compute objective in every iteration, default = " << (default_config.compute_objective ? "on" : "off") << '\n'
<< " --[no-]print_intermediate ... print intermediate embedding to file every iteration (for creating GIFs), default = " << (default_config.print_intermediate ? "on" : "off") << "\n\n"
<< " --use-hyperplane ... use hyperplane LSH (instead of cross-polytope), default = " << (default_config.use_hyperplane ? "on" : "off") << '\n'
<< " --use-cross-polytope ... use cross-polytope LSH (instead of hyperplane LSH), default = " << (!default_config.use_hyperplane ? "on" : "off") << "\n\n"
<< " -h ... this message\n\n"
<< "and FILE is a csv file.\n\n"
<< "ktsne is an accelerated approximative version of tsne which uses LSH and kmeans in its computation.\n";
}
int main(int argc, char** argv) {
config_t config;
struct option long_opts[] = {
{"compute-objective", no_argument, &config.compute_objective, 1},
{"no-compute-objective", no_argument, &config.compute_objective, 0},
{"print-intermediate", no_argument, &config.print_intermediate, 1},
{"no-print-intermediate", no_argument, &config.print_intermediate, 0},
{"use-hyperplane-lsh", no_argument, &config.use_hyperplane, 1},
{"use-cross-polytope", no_argument, &config.use_hyperplane, 0},
{"random-init", no_argument, &config.random_init, 1},
{"pca-init", no_argument, &config.random_init, 0},
{"k-lo", required_argument, NULL, 'k'},
{"k-hi", required_argument, NULL, 'K'},
{"early-exaggeration", required_argument, NULL, 'x'},
{"late-exaggeration", required_argument, NULL, 'X'},
{"num-hash-tables", required_argument, NULL, 1},
{"num-hash-bits", required_argument, NULL, 2},
{"num-probes", required_argument, NULL, 3},
{"seed", required_argument, NULL, 's'},
{"max-iter", required_argument, NULL, 'i'},
{NULL, 0, NULL, 0}
};
char c;
while((c = getopt_long(argc, argv, "p:i:n:k:K:x:X:s:h", long_opts, NULL)) != -1) {
switch(c) {
case 0: /* flag */ break;
case 1: config.num_hash_tables = std::atoi(optarg); break;
case 2: config.num_hash_bits = std::atoi(optarg); break;
case 3: config.num_probes = std::atoi(optarg); break;
case 'p': config.perplexity = std::atoi(optarg); break;
case 'n': config.eta = std::atof(optarg); break;
case 'i': config.max_iter = std::atoll(optarg); break;
case 'k': config.kmeans_lo = std::atoi(optarg); break;
case 'K': config.kmeans_hi = std::atoi(optarg); break;
case 'x': config.early_exaggeration = std::atof(optarg); break;
case 'X': config.late_exaggeration = std::atof(optarg); break;
case 's': config.seed = std::atoll(optarg); break;
case 'h':
default: print_usage(argv[0]); std::exit(-1);
}
}
if(argc == optind) {
std::cerr << "no file given!\n";
print_usage(argv[0]);
std::exit(-1);
}
std::mt19937 gen{ config.seed };
std::vector<point_t> data = read_data(argv[optind]);
std::vector<int> labels;
normalize(data);
center(data);
size_t const n = data.size();
size_t const d = 2;
if(config.num_hash_bits == -1) { config.num_hash_bits = std::max(static_cast<size_t>(16), static_cast<size_t>(std::log2(n))); }
std::clog << "configuration:\n"
<< " perplexity: " << config.perplexity << '\n'
<< " eta: " << config.eta << '\n'
<< " early exaggeration: " << config.early_exaggeration << '\n'
<< " late exaggeration: " << config.late_exaggeration << '\n'
<< " iterations: " << config.max_iter << '\n'
<< " kmeans k range: [" << config.kmeans_lo << ", " << config.kmeans_hi << "]\n"
<< " initialization: " << (config.random_init ? "random" : "PCA") << "\n\n"
<< " number of hash tables: " << config.num_hash_tables << '\n'
<< " number of hash bits: " << config.num_hash_bits << '\n'
<< " number of probes (multiprobing): " << config.num_probes << '\n'
<< " LSH family: " << (config.use_hyperplane ? "hyperplane" : "cross-polytope") << "\n\n"
<< " computing objective: " << (config.compute_objective ? "on" : "off") << '\n'
<< " printing intermediate embeddings: " << (config.print_intermediate ? "on" : "off") << '\n'
<< " seed: " << config.seed << "\n\n";
Eigen::SparseMatrix<double> P_j_given_i = high_dimensional_affinities(data, config.perplexity, config.num_hash_tables, config.num_hash_bits, config.num_probes, config.use_hyperplane);
Eigen::SparseMatrix<double> P_ij = Eigen::SparseMatrix<double>(P_j_given_i.transpose()) + P_j_given_i;
P_ij /= P_ij.sum();
Eigen::MatrixXdr Y(n, d);
Eigen::MatrixXdr Y_(n, d);
if(config.random_init) {
initialize_gaussian(Y, 10e-4, gen);
} else {
initialize_PCA(Y, data);
}
Eigen::MatrixXdr iY = Eigen::MatrixXdr::Zero(n, d);
Eigen::MatrixXdr dY = Eigen::MatrixXdr::Zero(n, d);
Eigen::MatrixXdr gains = Eigen::MatrixXdr::Ones(n, d);
std::uniform_int_distribution<size_t> k_dist{ config.kmeans_lo, config.kmeans_hi };
if(config.compute_objective) { std::cout << "it,obj,normdY,procrustes\n"; }
for(size_t it = 0; it < config.max_iter; ++it) {
if(it % 100 == 0) { std::cerr << "it = " << it << '\n'; }
Eigen::MatrixXdr F_attr = Eigen::MatrixXdr::Zero(n, d);
double exaggeration = it < 0.25*config.max_iter ? config.early_exaggeration : 1; // artificially inflate P_ij value for first few iterations
exaggeration = it > 0.9*config.max_iter ? config.late_exaggeration : exaggeration;
for(int k = 0; k < P_ij.outerSize(); ++k) {
for(Eigen::SparseMatrix<double>::InnerIterator it{ P_ij, k }; it; ++it) {
int i = it.row(), j = it.col();
auto diff = Y.row(i) - Y.row(j);
F_attr.row(i) += exaggeration*it.value() * (1/(1 + diff.squaredNorm())) * diff;
}
}
// approximate F_rep by assigning cells using kmeans
size_t k = k_dist(gen);
auto [centroids, n_cell, point_assignments] = do_kmeans(Y, k, 10, gen, config.compute_objective);
assert((n_cell.array() > 0.0).all());
Eigen::MatrixXdr sq_dist_cell = compute_sq_dist_binomial(Y, centroids);
assert(!sq_dist_cell.hasNaN() && sq_dist_cell.allFinite());
Eigen::MatrixXdr q_icellZ_sq = (1/(sq_dist_cell.array() + 1).square()).matrix() * n_cell.asDiagonal();
double Z_est = ((1/(sq_dist_cell.array() + 1)).matrix() * n_cell.asDiagonal()).sum();
Eigen::MatrixXdr F_rep = Eigen::MatrixXdr::Zero(n, d); // NOTE: actually estimating F_repZ!
for(size_t i = 0; i < n; ++i) {
for(size_t j = 0; j < k; ++j) {
F_rep.row(i) += q_icellZ_sq(i, j) * (Y.row(i) - centroids.row(j));
}
}
F_rep /= Z_est;
dY = F_attr - F_rep;
for(size_t i = 0; i < n; ++i) {
for(size_t j = 0; j < d; ++j) {
gains(i, j) = std::max(mathematically_correct_sign(iY(i, j)) == mathematically_correct_sign(dY(i, j)) ? gains(i, j)*0.8 : gains(i, j)+0.2, 0.01);
}
}
Y_ = Y;
iY = momentum(it)*iY - config.eta*(gains.cwiseProduct(dY));
Y += iY;
Eigen::RowVectorXd Y_mean = Y.colwise().mean();
Y = Y.rowwise() - Y_mean;
if(config.compute_objective) { // warning: this is slow!
double kl = 0;
for(int k = 0; k < P_ij.outerSize(); ++k) {
for(Eigen::SparseMatrix<double>::InnerIterator it{ P_ij, k }; it; ++it) {
int i = it.row(), j = it.col();
double q_icell = std::sqrt(q_icellZ_sq(i, point_assignments[j]) / Z_est);
kl += it.value()*std::log2(it.value() / q_icell);
}
}
double procrustes_error = compute_procrustes(Y_, Y);
std::cout << it << "," << kl << "," << dY.norm() << "," << procrustes_error << '\n';
}
if(config.print_intermediate && it % 5 == 0) {
fs::create_directory("gif");
std::string outname = std::string{ "gif/" } + fs::path(argv[optind]).stem().string() + "_embedding_it_" + std::to_string(it) + ".csv";
print_csv(outname, Y, labels);
}
}
std::stringstream outname_ss;
outname_ss << "ktsne_" << fs::path(argv[optind]).stem().string() << "_embedding_eta_" << config.eta << "_p_" << config.perplexity << "_l_" << config.num_hash_tables << (config.use_hyperplane ? "_hyperplane" : "_crosspolytope") << "_k_" << config.kmeans_lo << "_K_" << config.kmeans_hi << (config.random_init ? "_random" : "_pca") << "_x_" << config.early_exaggeration << "_X_" << config.late_exaggeration << "_s_" << config.seed << ".csv";
print_csv(outname_ss.str(), Y, labels);
}