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 TSNETSNE(
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
| Parameter | Type | Default | Description |
|---|---|---|---|
n_components | int | 2 | Dimension of the embedded space (must be >= 1). |
perplexity | float | 30.0 | Effective number of local neighbors; must be strictly less than n_samples. |
early_exaggeration | float | 12.0 | Multiplier 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_iter | int | 1000 | Maximum number of optimization iterations. |
init | {"pca", "random"} | "pca" | Embedding initialization method. |
random_state | int or None | None | Random seed for reproducible initialization. |
device | str or None | None | Target compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type / Shape | Description |
|---|---|---|
embedding_ | ndarray of shape (n_samples, n_components) | Learned low-dimensional embedding coordinates. |
kl_divergence_ | float | Final Kullback-Leibler divergence after optimization. |
n_iter_ | int | Number of iterations actually run. |
learning_rate_ | float | Effective numeric learning rate used during optimization. |
n_features_in_ | int | Number of features seen during fit. |
device_ | str | Active 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).
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> TSNE | Fit X into the embedded space and populate embedding_. |
fit_transform | fit_transform(X, y=None) -> ndarray | Fit X into the embedded space and return embedding_. |
to | to(device) -> TSNE | Move execution target to a new hardware device. |
save | save(path) -> None | Save the fitted model and learned embedding_ to a binary .dit file. |
load | TSNE.load(path, device=None) -> TSNE | Load 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_)