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#include <cstddef>
14
15#include "aarand/aarand.hpp"
16#include "knncolle/knncolle.hpp"
17
18#ifndef QDTSNE_CUSTOM_PARALLEL
19#include "subpar/subpar.hpp"
20#endif
21
22namespace qdtsne {
23
35template<typename Index_, typename Float_>
37
45inline int perplexity_to_k(double perplexity) {
46 return std::ceil(perplexity * 3);
47}
48
62template<std::size_t num_dim_, typename Float_ = double>
63void initialize_random(Float_* Y, std::size_t num_points, int seed = 42) {
64 std::mt19937_64 rng(seed);
65
66 std::size_t total = num_points * num_dim_; // already size_t's, so no need to cast to avoid overflow.
67 bool odd = total % 2;
68 if (odd) {
69 --total;
70 }
71
72 // Box-Muller gives us two random values at a time.
73 for (std::size_t i = 0; i < total; i += 2) {
74 auto paired = aarand::standard_normal<Float_>(rng);
75 Y[i] = paired.first;
76 Y[i + 1] = paired.second;
77 }
78
79 if (odd) {
80 // Adding the poor extra for odd total lengths.
81 auto paired = aarand::standard_normal<Float_>(rng);
82 Y[total] = paired.first;
83 }
84
85 return;
86}
87
99template<std::size_t num_dim_, typename Float_ = double>
100std::vector<Float_> initialize_random(std::size_t num_points, int seed = 42) {
101 std::vector<Float_> Y(num_points * num_dim_); // already size_t's, so no need to cast to avoid overflow.
102 initialize_random<num_dim_>(Y.data(), num_points, seed);
103 return Y;
104}
105
118template<typename Task_, class Run_>
119void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range) {
120#ifndef QDTSNE_CUSTOM_PARALLEL
121 // Don't make this nothrow_ = true, there's too many allocations and the
122 // derived methods for the nearest neighbors search could do anything...
123 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
124#else
125 QDTSNE_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
126#endif
127}
128
129}
130
131#endif
std::vector< std::vector< std::pair< Index_, Distance_ > > > NeighborList
Quick and dirty t-SNE.
knncolle::NeighborList< Index_, Float_ > NeighborList
Lists of neighbors for each observation.
Definition utils.hpp:36
void initialize_random(Float_ *Y, std::size_t num_points, int seed=42)
Definition utils.hpp:63
int perplexity_to_k(double perplexity)
Definition utils.hpp:45
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition utils.hpp:119