tsne.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, 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 
20 #include <cuml/common/logger.hpp>
21 
22 namespace raft {
23 class handle_t;
24 }
25 
26 namespace ML {
27 
29 
30 enum TSNE_INIT { RANDOM, PCA };
31 
32 struct TSNEParams {
33  // Number of output dimensions for embeddings Y.
34  int dim = 2;
35 
36  // Number of nearest neighbors used.
37  int n_neighbors = 1023;
38 
39  // Float between 0 and 1. Tradeoff for speed (0) vs accuracy (1).
40  // (Barnes-Hut only.)
41  float theta = 0.5f;
42 
43  // A tiny jitter to promote numerical stability. (Barnes-Hut only.)
44  float epssq = 0.0025;
45 
46  // How many nearest neighbors are used during construction of Pij.
47  float perplexity = 50.0f;
48 
49  // Number of iterations used to construct Pij.
51 
52  // The small tolerance used for Pij to ensure numerical stability.
53  float perplexity_tol = 1e-5;
54 
55  // How much pressure to apply to clusters to spread out
56  // during the exaggeration phase.
57  float early_exaggeration = 12.0f;
58 
59  // How much pressure to apply to clusters to
60  // spread out after the exaggeration phase. (FIT-SNE only)
61  float late_exaggeration = 1.0f;
62 
63  // How many iterations you want the early pressure to run for.
64  // If late exaggeration is used, it will be applied to all iterations
65  // that remain after this number of iterations.
66  int exaggeration_iter = 250;
67 
68  // Rounds up small gradient updates. (Barnes-Hut and Exact only.)
69  float min_gain = 0.01f;
70 
71  // The learning rate during exaggeration phase.
72  float pre_learning_rate = 200.0f;
73 
74  // The learning rate after exaggeration phase.
75  float post_learning_rate = 500.0f;
76 
77  // The maximum number of iterations TSNE should run for.
78  int max_iter = 1000;
79 
80  // The smallest gradient norm TSNE should terminate on.
81  // (Exact only; ignored for others.)
82  float min_grad_norm = 1e-7;
83 
84  // The momentum used during the exaggeration phase.
85  float pre_momentum = 0.5;
86 
87  // The momentum used after the exaggeration phase.
88  float post_momentum = 0.8;
89 
90  // Set this to -1 for pure random initializations or >= 0 for
91  // reproducible outputs. This sets random seed correctly, but there
92  // may still be some variance due to the parallel nature of this algorithm.
93  long long random_state = -1;
94 
95  // verbosity level for logging messages during execution
96  rapids_logger::level_enum verbosity = rapids_logger::level_enum::info;
97 
98  // Embedding initializer algorithm
100 
101  // When this is set to true, the distances from the knn graph will
102  // always be squared before computing conditional probabilities, even if
103  // the knn graph is passed in explicitly. This is to better match the
104  // behavior of Scikit-learn's T-SNE.
105  bool square_distances = true;
106 
107  // Distance metric to use.
109 
110  // Value of p for Minkowski distance
111  float p = 2.0;
112 
113  // Which implementation algorithm to use.
115 };
116 
138 void TSNE_fit(const raft::handle_t& handle,
139  float* X,
140  float* Y,
141  int n,
142  int p,
143  int64_t* knn_indices,
144  float* knn_dists,
146  float* kl_div = nullptr);
147 
172 void TSNE_fit_sparse(const raft::handle_t& handle,
173  int* indptr,
174  int* indices,
175  float* data,
176  float* Y,
177  int nnz,
178  int n,
179  int p,
180  int* knn_indices,
181  float* knn_dists,
183  float* kl_div = nullptr);
184 
185 } // namespace ML
Definition: params.hpp:34
DistanceType
Definition: distance_type.hpp:21
Definition: dbscan.hpp:29
void TSNE_fit(const raft::handle_t &handle, float *X, float *Y, int n, int p, int64_t *knn_indices, float *knn_dists, TSNEParams ¶ms, 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 ¶ms, 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:28
@ BARNES_HUT
Definition: tsne.h:28
@ FFT
Definition: tsne.h:28
@ EXACT
Definition: tsne.h:28
TSNE_INIT
Definition: tsne.h:30
@ RANDOM
Definition: tsne.h:30
@ PCA
Definition: tsne.h:30
Definition: dbscan.hpp:25
Definition: tsne.h:32
float perplexity
Definition: tsne.h:47
float pre_learning_rate
Definition: tsne.h:72
ML::distance::DistanceType metric
Definition: tsne.h:108
int perplexity_max_iter
Definition: tsne.h:50
bool square_distances
Definition: tsne.h:105
int exaggeration_iter
Definition: tsne.h:66
float min_grad_norm
Definition: tsne.h:82
float theta
Definition: tsne.h:41
float late_exaggeration
Definition: tsne.h:61
TSNE_ALGORITHM algorithm
Definition: tsne.h:114
long long random_state
Definition: tsne.h:93
float pre_momentum
Definition: tsne.h:85
int n_neighbors
Definition: tsne.h:37
float early_exaggeration
Definition: tsne.h:57
float post_momentum
Definition: tsne.h:88
int dim
Definition: tsne.h:34
float min_gain
Definition: tsne.h:69
float post_learning_rate
Definition: tsne.h:75
float epssq
Definition: tsne.h:44
TSNE_INIT init
Definition: tsne.h:99
float perplexity_tol
Definition: tsne.h:53
int max_iter
Definition: tsne.h:78
float p
Definition: tsne.h:111
rapids_logger::level_enum verbosity
Definition: tsne.h:96