PyTorch and t-SNE
PyTorch 2.14.1 does not provide a maintained high-level t-SNE estimator. A Gaussian kernel with one global bandwidth and a Gaussian output kernel is not t-SNE: genuine t-SNE matches per-point perplexities, symmetrizes probabilities, uses a heavy-tailed Student output distribution, and applies stabilization details. Use PyTorch to produce feature vectors and a maintained implementation for the visualization.
1. Prepare deterministic tensor features
import matplotlib.pyplot as plt
import torch
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
torch.manual_seed(42)
digits = load_digits()
X = digits.data[:300] / 16.0
y = digits.target[:300]
X_tensor = torch.tensor(X, dtype=torch.float64)
centered = X_tensor - X_tensor.mean(dim=0, keepdim=True)
assert centered.shape == (300, 64)
2. Reduce cost, then fit t-SNE
The CPU conversion is explicit. PCA is fitted to this visualization sample and reduces dense input dimensions before t-SNE.
X_pca = PCA(n_components=30, random_state=42).fit_transform(centered.numpy())
embedding = TSNE(
n_components=2,
perplexity=30,
init="pca",
learning_rate="auto",
max_iter=350,
random_state=42,
).fit_transform(X_pca)
assert embedding.shape == (300, 2)
3. Visualize known labels
scatter = plt.scatter(embedding[:, 0], embedding[:, 1], c=y, cmap="tab10", s=16)
plt.colorbar(scatter, label="Digit label")
plt.title("t-SNE view of scikit-learn digits features")
plt.close()
These are 8×8 images from scikit-learn's digits dataset, not MNIST. Labels color the visualization but were not used by t-SNE. Apparent groups do not measure classifier accuracy or establish clusters.