tsne.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cuml/common/logger.hpp>
20 
21 #include <cuvs/distance/distance.hpp>
22 
23 namespace raft {
24 class handle_t;
25 }
26 
27 namespace ML {
28 
30 
31 enum TSNE_INIT { RANDOM, PCA };
32 
33 struct TSNEParams {
34  // Number of output dimensions for embeddings Y.
35  int dim = 2;
36 
37  // Number of nearest neighbors used.
38  int n_neighbors = 1023;
39 
40  // Float between 0 and 1. Tradeoff for speed (0) vs accuracy (1).
41  // (Barnes-Hut only.)
42  float theta = 0.5f;
43 
44  // A tiny jitter to promote numerical stability. (Barnes-Hut only.)
45  float epssq = 0.0025;
46 
47  // How many nearest neighbors are used during construction of Pij.
48  float perplexity = 50.0f;
49 
50  // Number of iterations used to construct Pij.
52 
53  // The small tolerance used for Pij to ensure numerical stability.
54  float perplexity_tol = 1e-5;
55 
56  // How much pressure to apply to clusters to spread out
57  // during the exaggeration phase.
58  float early_exaggeration = 12.0f;
59 
60  // How much pressure to apply to clusters to
61  // spread out after the exaggeration phase. (FIT-SNE only)
62  float late_exaggeration = 1.0f;
63 
64  // How many iterations you want the early pressure to run for.
65  // If late exaggeration is used, it will be applied to all iterations
66  // that remain after this number of iterations.
67  int exaggeration_iter = 250;
68 
69  // Rounds up small gradient updates. (Barnes-Hut and Exact only.)
70  float min_gain = 0.01f;
71 
72  // The learning rate during exaggeration phase.
73  float pre_learning_rate = 200.0f;
74 
75  // The learning rate after exaggeration phase.
76  float post_learning_rate = 500.0f;
77 
78  // The maximum number of iterations TSNE should run for.
79  int max_iter = 1000;
80 
81  // The smallest gradient norm TSNE should terminate on.
82  // (Exact only; ignored for others.)
83  float min_grad_norm = 1e-7;
84 
85  // The momentum used during the exaggeration phase.
86  float pre_momentum = 0.5;
87 
88  // The momentum used after the exaggeration phase.
89  float post_momentum = 0.8;
90 
91  // Set this to -1 for pure random initializations or >= 0 for
92  // reproducible outputs. This sets random seed correctly, but there
93  // may still be some variance due to the parallel nature of this algorithm.
94  long long random_state = -1;
95 
96  // verbosity level for logging messages during execution
98 
99  // Embedding initializer algorithm
101 
102  // When this is set to true, the distances from the knn graph will
103  // always be squared before computing conditional probabilities, even if
104  // the knn graph is passed in explicitly. This is to better match the
105  // behavior of Scikit-learn's T-SNE.
106  bool square_distances = true;
107 
108  // Distance metric to use.
109  cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2SqrtExpanded;
110 
111  // Value of p for Minkowski distance
112  float p = 2.0;
113 
114  // Which implementation algorithm to use.
116 };
117 
139 void TSNE_fit(const raft::handle_t& handle,
140  float* X,
141  float* Y,
142  int n,
143  int p,
144  int64_t* knn_indices,
145  float* knn_dists,
147  float* kl_div = nullptr);
148 
173 void TSNE_fit_sparse(const raft::handle_t& handle,
174  int* indptr,
175  int* indices,
176  float* data,
177  float* Y,
178  int nnz,
179  int n,
180  int p,
181  int* knn_indices,
182  float* knn_dists,
184  float* kl_div = nullptr);
185 
186 } // namespace ML
Definition: params.hpp:34
#define CUML_LEVEL_INFO
Definition: log_levels.hpp:28
Definition: dbscan.hpp:30
void TSNE_fit(const raft::handle_t &handle, float *X, float *Y, int n, int p, int64_t *knn_indices, float *knn_dists, TSNEParams &params, float *kl_div=nullptr)
Dimensionality reduction via TSNE using Barnes-Hut, Fourier Interpolation, or naive methods....
void TSNE_fit_sparse(const raft::handle_t &handle, int *indptr, int *indices, float *data, float *Y, int nnz, int n, int p, int *knn_indices, float *knn_dists, TSNEParams &params, float *kl_div=nullptr)
Dimensionality reduction via TSNE using either Barnes Hut O(NlogN) or brute force O(N^2).
TSNE_ALGORITHM
Definition: tsne.h:29
@ BARNES_HUT
Definition: tsne.h:29
@ FFT
Definition: tsne.h:29
@ EXACT
Definition: tsne.h:29
TSNE_INIT
Definition: tsne.h:31
@ RANDOM
Definition: tsne.h:31
@ PCA
Definition: tsne.h:31
Definition: dbscan.hpp:26
Definition: tsne.h:33
float perplexity
Definition: tsne.h:48
float pre_learning_rate
Definition: tsne.h:73
int perplexity_max_iter
Definition: tsne.h:51
bool square_distances
Definition: tsne.h:106
int exaggeration_iter
Definition: tsne.h:67
int verbosity
Definition: tsne.h:97
float min_grad_norm
Definition: tsne.h:83
cuvs::distance::DistanceType metric
Definition: tsne.h:109
float theta
Definition: tsne.h:42
float late_exaggeration
Definition: tsne.h:62
TSNE_ALGORITHM algorithm
Definition: tsne.h:115
long long random_state
Definition: tsne.h:94
float pre_momentum
Definition: tsne.h:86
int n_neighbors
Definition: tsne.h:38
float early_exaggeration
Definition: tsne.h:58
float post_momentum
Definition: tsne.h:89
int dim
Definition: tsne.h:35
float min_gain
Definition: tsne.h:70
float post_learning_rate
Definition: tsne.h:76
float epssq
Definition: tsne.h:45
TSNE_INIT init
Definition: tsne.h:100
float perplexity_tol
Definition: tsne.h:54
int max_iter
Definition: tsne.h:79
float p
Definition: tsne.h:112