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