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