Sub topic 10
Optimising t-SNE: initialisation, early exaggeration and learning rate
This chapter assumes you know gradient descent, the t-SNE objective (KL divergence between high- and low-dimensional affinities), and scikit-learn's fit/transform interface. Here you will see how the optimiser turns that objective into a layout, and which three settings most affect the result.
What the optimiser does
t-SNE minimises the KL divergence by gradient descent with momentum. The variables are the 2-D coordinates of every point. Each step pulls points toward neighbours they are attracted to and pushes all points apart. The objective is non-convex, so the starting layout and the step size both change where the run ends up.
Initialisation
Random initialisation draws small Gaussian coordinates. It works, but different seeds can place clusters in different relative positions, and distant clusters may never find their correct arrangement.
PCA initialisation starts from the first two principal components, rescaled to a small spread. It is deterministic for a given dataset and tends to preserve the global arrangement of clusters, because PCA already captures the largest-variance directions. In scikit-learn, init="pca" is the default from version 1.2 onward. Prefer it unless you want to measure seed sensitivity on purpose.
Early exaggeration
During the first phase (250 iterations by default), the high-dimensional affinities are multiplied by an exaggeration factor (12 by default in scikit-learn). Attraction then dominates repulsion, so points in the same neighbourhood collapse into tight groups that can move through the map as units. This lets clusters rearrange before they are locked in. After the phase ends, the factor returns to 1 and the groups expand.
If you see clusters that are fragmented into several pieces, a larger exaggeration or more early iterations sometimes helps. Treat that as a diagnostic, not a fix to apply by default.
Learning rate
The learning rate sets the step size. Both extremes fail visibly:
| Learning rate | Typical symptom |
|---|---|
| Too low | Points stay in a dense, compressed ball with few visible clusters |
| Too high | Points spread into an even ring or "ball" with little structure |
A good heuristic is roughly n_samples / early_exaggeration / 4. Setting learning_rate="auto" applies this rule, so it is the sensible default.
Running it
The code below fits t-SNE with these three settings written out. It also reads the final KL divergence from kl_divergence_, which lets you compare runs. Lower is better only when the runs use the same perplexity, because the objective changes with it.
from sklearn.datasets import load_digits
from sklearn.manifold import TSNE
X, y = load_digits(return_X_y=True)
for seed in [0, 1, 2]:
tsne = TSNE(
n_components=2,
perplexity=30,
init="pca",
early_exaggeration=12,
learning_rate="auto",
random_state=seed,
)
Z = tsne.fit_transform(X)
print(f"seed={seed} final KL={tsne.kl_divergence_:.3f}")Exercise
Your t-SNE plot shows every point packed into one dense ball with no visible clusters. Which setting do you suspect first, and what do you change?
Show answer
Suspect the learning rate being too low. Set learningrate="auto" (about nsamples / early_exaggeration / 4) or raise it, then check that the iterations are enough for the layout to converge.
Exercise
Why does PCA initialisation make runs more reproducible and often better at global layout than random initialisation?
Show answer
PCA gives a fixed starting layout that follows the directions of largest variance, so cluster positions start in a sensible arrangement and do not depend on a random draw. Random initialisation can start with clusters in an arbitrary arrangement that the optimiser may never fix.