Skip to contents

tsne() computes or reuses a KNN graph, then runs the package-native two-phase t-SNE optimizer. The default optimizer is CPU-native. Explicit backend = "metal" and backend = "cuda" requests use the matching package-native GPU optimizer when compiled and fail clearly if the requested backend is unavailable. The implementation follows published sparse-affinity and interpolation-based t-SNE methods and provides independent CPU, Metal, and CUDA kernels.

Usage

tsne(
  data,
  perplexity = NULL,
  n_components = 2L,
  init_data = NULL,
  Y_init = NULL,
  standardize = FALSE,
  pca_dims = NULL,
  metric = c("euclidean", "cosine", "correlation"),
  nn = NULL,
  seed = 4L,
  backend = NULL,
  keep_knn = FALSE,
  verbose = FALSE,
  n.cores = NULL,
  learning_rate = "auto",
  early_exaggeration_iter = NULL,
  early_exaggeration = "auto",
  n_iter = NULL,
  exaggeration = NULL,
  initial_momentum = 0.8,
  final_momentum = 0.8,
  max_step_norm = "auto",
  negative_gradient_method = "auto",
  record_costs = FALSE,
  auto_config = TRUE,
  landmarks = FALSE,
  transform_k = NULL,
  transform_perplexity = 5,
  transform_iter = 250L,
  transform_early_exaggeration_iter = 0L,
  transform_n_negatives = NULL,
  initialization = c("median", "weighted", "random"),
  massive = "off",
  output = NULL,
  chunk_rows = NULL,
  storage = NULL,
  memory_limit = "available",
  devices = NULL,
  landmark_method = "reservoir",
  checkpoint = FALSE,
  resume = FALSE,
  affinity_k = NULL,
  target_recall = NULL,
  ...
)

Arguments

data

Numeric matrix/data frame, a file-backed massive_matrix(), a list containing KNN indices and distances, or a saved affinity graph. A file-backed matrix without landmarks triggers full-data graph preparation in the experimental one-call route.

perplexity

t-SNE perplexity. If NULL, uses the largest safe value up to 30 that is available for the input. Compact affinity support uses ceiling(perplexity) non-self neighbors.

n_components

Output dimensionality, 2 or 3. FFT repulsion is used on every backend.

init_data

Optional original high-dimensional data matrix used only to compute PCA initialization. It is not used for neighbor search or optimization.

Y_init

Optional explicit initial layout. In full-graph massive mode this must be a 2D or 3D file-backed .f32 matrix. Use pca(data, tsne_init = TRUE)$tsne_init to precompute and reuse a PCA initialization.

standardize

Center and scale columns before KNN. Defaults to FALSE so one-call results match a KNN object computed from the supplied matrix.

pca_dims

Optional PCA dimension before KNN.

metric

KNN distance metric for one-call matrix input: "euclidean", "cosine", or "correlation".

nn

Optional precomputed KNN output for resident matrix input. In EXPERIMENTAL massive = "landmark" mode, a previous massive landmark embedding reuses its saved selection and query-to-landmark KNN. Its source, reference, neighbor count, and backend must match.

seed

Random seed.

backend

Execution backend: "cpu", "cuda", or "metal". CPU KNN uses faissR Flat exact search below 5,000 rows, FAISS HNSW for larger Euclidean searches, and faissR HNSW for other supported metrics. For Metal and CUDA, faissR selects native Flat or IVF-Flat as described in precompute_knn(). Resident CUDA fits can request multiple devices. Unsupported GPU requests raise an error. One-call file-backed Metal t-SNE prepares neighbors with faissR's native Metal search. The pipeline records that stage. Metal bounds graph uploads, but keeps coordinates and optimizer state resident and reserves space for the FFT workspace. It rejects fits outside the device working-set budget; it is not fully streamed.

keep_knn

If TRUE, retain KNN matrices or file-backed KNN files in the returned pipeline. One-call file-backed fits release KNN files after graph construction when this is FALSE.

verbose

Print optimizer progress.

n.cores

Number of CPU cores used by CPU PCA preprocessing, KNN, and t-SNE optimization. With NULL, PCA uses options(n.cores) (one by default), while KNN and t-SNE use their stage defaults. Native GPU stages ignore this argument.

learning_rate

Positive number or "auto". With "auto", the native optimizer uses n / exaggeration separately for each phase.

early_exaggeration_iter

Number of early-exaggeration iterations.

early_exaggeration

Early-exaggeration multiplier, or "auto" for the default t-SNE rule.

n_iter

Number of normal optimization iterations after early exaggeration.

exaggeration

Normal-phase exaggeration. NULL means 1.

initial_momentum

Momentum during early exaggeration.

final_momentum

Momentum during normal optimization.

max_step_norm

Maximum per-point update norm. "auto" uses the same validated limit on CPU, Metal, and CUDA. Use NULL or NA to disable clipping.

negative_gradient_method

"auto" or "fft"; both use FFT. Other methods fail explicitly. GPU FFT requires compiled native symbols.

record_costs

If TRUE, compute diagnostic KL/cost traces. CUDA and three-dimensional Metal fits reject this option explicitly.

auto_config

If TRUE, choose missing t-SNE settings with a native C++ opt-SNE-inspired policy. The policy uses n / early_exaggeration for "auto" learning rate, chooses missing iteration limits, and enables KLD-based early stopping on small CPU runs where the monitor is not prohibitively expensive. Explicit user-supplied values are respected.

landmarks

FALSE (the default) embeds all observations. A fraction in (0, 1), a positive landmark count, or explicit row indices enables landmark embedding followed by fixed-reference transformation of the remaining rows. File-backed landmark mode currently accepts a count only; fractions and explicit row indices require resident input.

transform_k

Number of landmark neighbors used to place non-landmark observations. Used only when landmarking is enabled.

transform_perplexity

Perplexity used to transform non-landmark rows.

transform_iter

Number of normal transform iterations. Use zero for projection without transform refinement.

transform_early_exaggeration_iter

Number of transform early-exaggeration iterations.

transform_n_negatives

Number of sampled reference negatives used by large fixed-reference transforms.

initialization

Initial placement for transformed rows: "median", "weighted", or "random".

massive

"off" (default), experimental "landmark", or "out_of_core_graph" for a prepared t-SNE affinity graph. When omitted, a prepared file-backed graph selects full-graph mode; a file-backed matrix selects a full fit unless landmarks are supplied.

output

New .f32 path for experimental file-backed coordinates. A one-call full fit also writes KNN, affinity, and PCA initialization files beside this path and reports their stage times.

chunk_rows

Maximum experimental query rows per batch.

storage

"auto", "resident", or "file_backed". When omitted, uses options(storage = ...). Explicit "resident" on a massive_matrix() file descriptor loads float32 input after a RAM and memory_limit check; the limit does not bound the subsequent fit.

memory_limit

Conservative experimental RAM budget. When omitted for file-backed input, uses options(memory_limit = ...), or detected available RAM.

devices

Optional CUDA device indices. In resident full-data mode, one-call matrix KNN currently uses faissR on GPU 0 and returns host neighbors. Multiple-device one-call fits use an experimental faissR FAISS GPU replica KNN route with host results; its scaling needs audit. The experimental file-backed full-graph route also accepts multiple CUDA devices. Resident landmark fits coordinate the reference optimizer across devices and shard the fixed-reference query transform across them. Query KNN is computed on one GPU and materialized in host RAM before its device shards; the scientific calculation uses no SSD files.

landmark_method

Sampling method for experimental landmark mode: "reservoir" (default) or "random" for fast random-access storage. Auto mode uses ordinary t-SNE when its conservative memory estimate fits; otherwise it reports and runs approximate landmark t-SNE. The landmark route requires a file-backed massive_matrix() and a new .f32 output. Full-graph mode streams affinities into the CPU, Metal, or CUDA FFT optimizer.

checkpoint

Save file-backed workflow stages. Resident t-SNE fits cannot checkpoint or resume; interruption restarts the fit. Full-graph file-backed t-SNE also saves coordinates, momentum updates, and adaptive gains every 100 iterations and at the final iteration. One-call full fits use a source-identified stage record and restartable global graph sorting, which needs more SSD space than the default bucketed sort.

resume

Continue a matching interrupted file-backed workflow. Changed input, backend, or fit controls fail without recomputation. One-call recovery requires a committed stage or native checkpoint; an incomplete stage with no valid checkpoint fails closed.

affinity_k

Optional number of non-self neighbors for t-SNE affinities. Defaults to ceiling(perplexity); use ceiling(3 * perplexity) for conventional-width support. This setting also controls the reference fit in resident landmark mode and KNN preparation for a file-backed one-call full fit. It is unavailable when a prepared affinity graph is supplied.

target_recall

Requested KNN tuning tier: 0.9, 0.95, or 0.99. NULL uses options(fastEmbedR.target_recall = ...), then 0.99. Non-default tiers require resident or full file-backed input without landmarks or supplied KNN. An exact KNN route remains exact.

...

Additional low-level parameters passed to tsne_knn().

Value

Resident fits return a fastEmbedR_embedding; landmark fits also contain model for project_landmark_model(). File-backed full fits return a massive_matrix() descriptor with output and stage metadata. File-backed landmark projection may return a fastEmbedR_massive_projection. Use massive_read_rows() to inspect bounded output rows without loading the full result.

Details

The t-SNE API exposes the principal scientifically consequential optimizer controls: perplexity, initialization, iteration counts, early and normal exaggeration, learning rate, momentum, and clipping. FFT repulsion is fixed. Setting auto_config = FALSE disables automatic iteration and stopping choices; explicit values always override automatic values. The matrix-input function deliberately does not expose a nearest-neighbor index type or tuning parameters: it uses the backend router with the requested recall tier. Supply an externally generated KNN object to tsne_knn() when search policy must be controlled independently. The resolved settings are in fit$parameters. Affinities default to compact support with k = ceiling(perplexity) non-self neighbors. Landmark mode embeds the selected reference rows with the same t-SNE implementation and transforms all remaining rows while keeping the reference coordinates fixed. Resident precomputed KNN input and explicit init_data or Y_init cannot be combined with landmarking.

Examples

fit <- tsne(
    as.matrix(iris[, 1:4]),
    perplexity = 5,
    early_exaggeration_iter = 5, n_iter = 10, seed = 1
)
plot(fit, labels = iris$Species)