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