Skip to Content
OchreML
Decomposition & Manifold

TSNE

t-Distributed Stochastic Neighbor Embedding for nonlinear manifold visualization with exact gradient descent.

TSNE (t-Distributed Stochastic Neighbor Embedding) performs nonlinear dimensionality reduction by converting pairwise Euclidean distances into conditional probabilities and minimizing the Kullback-Leibler divergence between high-dimensional affinities and a Student-t low-dimensional distribution. OchreML uses exact gradient descent with momentum, early exaggeration, and adaptive gains, with PCA or random initialization. (*≧ω≦*)

Backend Support

  • Backend primitives: N3 (pairwise Euclidean distance matrix), N2 (elementwise probability, gradient, and momentum updates).
  • Supported devices: "auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0". Falls back cleanly to multi-threaded Rayon CPU (f64) when an accelerator is unavailable.

Import & Signature

from ochreml.manifold import TSNE
# Or via top-level lazy import:
from ochreml import TSNE
TSNE(
    n_components: int = 2,
    perplexity: float = 30.0,
    early_exaggeration: float = 12.0,
    learning_rate: Union[str, float] = "auto",
    max_iter: int = 1000,
    init: str = "pca",
    random_state: Optional[int] = None,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_componentsint2Dimension of the embedded space (must be >= 1).
perplexityfloat30.0Effective number of local neighbors; must be strictly less than n_samples.
early_exaggerationfloat12.0Multiplier applied to joint probabilities during the first 250 iterations to form tight clusters.
learning_rate"auto" or float"auto"Gradient descent step size. When "auto", set to max(n_samples / early_exaggeration / 4.0, 50.0).
max_iterint1000Maximum number of optimization iterations.
init{"pca", "random"}"pca"Embedding initialization method.
random_stateint or NoneNoneRandom seed for reproducible initialization.
devicestr or NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeType / ShapeDescription
embedding_ndarray of shape (n_samples, n_components)Learned low-dimensional embedding coordinates.
kl_divergence_floatFinal Kullback-Leibler divergence after optimization.
n_iter_intNumber of iterations actually run.
learning_rate_floatEffective numeric learning rate used during optimization.
n_features_in_intNumber of features seen during fit.
device_strActive resolved compute device string.

Methods

TSNE is a non-parametric embedding estimator and therefore provides fit and fit_transform (rather than out-of-sample transform).

MethodSignatureDescription
fitfit(X, y=None) -> TSNEFit X into the embedded space and populate embedding_.
fit_transformfit_transform(X, y=None) -> ndarrayFit X into the embedded space and return embedding_.
toto(device) -> TSNEMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model and learned embedding_ to a binary .dit file.
loadTSNE.load(path, device=None) -> TSNELoad a saved model from a .dit file (also supported via ochreml.load).

Example

import numpy as np
import ochreml
from ochreml.manifold import TSNE

rng = np.random.default_rng(42)
X = np.vstack([
    rng.normal(loc=-3.0, scale=0.5, size=(40, 8)),
    rng.normal(loc=0.0, scale=0.5, size=(40, 8)),
    rng.normal(loc=3.0, scale=0.5, size=(40, 8)),
])

tsne = TSNE(
    n_components=2,
    perplexity=15.0,
    learning_rate="auto",
    max_iter=500,
    init="pca",
    random_state=42,
)
embedding = tsne.fit_transform(X)

print("Embedding shape:", embedding.shape)
print("Final KL divergence:", round(tsne.kl_divergence_, 4))
print("Effective learning rate:", tsne.learning_rate_)

tsne.save("tsne_model.dit")
loaded = ochreml.load("tsne_model.dit", device="cpu")
assert np.allclose(embedding, loaded.embedding_)

On this page