qdtsne
A quick and dirty t-SNE C++ library
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef QDTSNE_UTILS_HPP
2#define QDTSNE_UTILS_HPP
3
10#include <random>
11#include <cmath>
12#include <vector>
13
14#include "aarand/aarand.hpp"
15#include "knncolle/knncolle.hpp"
16
17#ifndef QDTSNE_CUSTOM_PARALLEL
18#include "subpar/subpar.hpp"
19#endif
20
21namespace qdtsne {
22
34template<typename Index_, typename Float_>
36
44inline int perplexity_to_k(double perplexity) {
45 return std::ceil(perplexity * 3);
46}
47
61template<int num_dim_, typename Float_ = double>
62void initialize_random(Float_* Y, size_t num_points, int seed = 42) {
63 std::mt19937_64 rng(seed);
64
65 size_t total = num_points * num_dim_;
66 bool odd = total % 2;
67 if (odd) {
68 --total;
69 }
70
71 // Box-Muller gives us two random values at a time.
72 for (size_t i = 0; i < total; i += 2) {
73 auto paired = aarand::standard_normal<Float_>(rng);
74 Y[i] = paired.first;
75 Y[i + 1] = paired.second;
76 }
77
78 if (odd) {
79 // Adding the poor extra for odd total lengths.
80 auto paired = aarand::standard_normal<Float_>(rng);
81 Y[total] = paired.first;
82 }
83
84 return;
85}
86
98template<int num_dim_, typename Float_ = double>
99std::vector<Float_> initialize_random(size_t num_points, int seed = 42) {
100 std::vector<Float_> Y(num_dim_ * num_points);
102 return Y;
103}
104
117template<typename Task_, class Run_>
119#ifndef QDTSNE_CUSTOM_PARALLEL
120 // Don't make this nothrow_ = true, there's too many allocations and the
121 // derived methods for the nearest neighbors search could do anything...
122 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
123#else
125#endif
126}
127
128}
129
130#endif
Status of the t-SNE iterations.
Definition Status.hpp:64
std::vector< std::vector< std::pair< Index_, Float_ > > > NeighborList
Quick and dirty t-SNE.
int perplexity_to_k(double perplexity)
Definition utils.hpp:44
knncolle::NeighborList< Index_, Float_ > NeighborList
Lists of neighbors for each observation.
Definition utils.hpp:35
void initialize_random(Float_ *Y, size_t num_points, int seed=42)
Definition utils.hpp:62
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition utils.hpp:118