← All AI/ML articles

t-SNE, visualised.

Turn 10-dimensional data into a 2D map by keeping neighbours close. Watch the points slide into place, then learn which parts of the picture you can trust and which you cannot.

1. Measure who is near whom2. Place points in 2D3. Nudge until neighbours agree
Each dot is one 10-D sample, coloured by its group (the algorithm never sees the colours). Hover a dot to see its 5 strongest high-dimensional neighbours.
Iteration0
KL cost0
Phase-
Behind the scenes · 1

Who counts as a neighbour?

For each point i, t-SNE places a Gaussian bump over the high-dimensional space and turns distances into probabilities: pj|i is how likely i would pick j as a neighbour. The bump width σi is tuned per point so that every point has the same perplexity, roughly “how many neighbours it effectively has”. Dense areas get narrow bumps, sparse areas wide ones.


Probabilities pj|i from the selected point (hover a dot above to change it), largest first, coloured by the neighbour’s group.
Behind the scenes · 2

The map uses a heavy-tailed kernel

In the 2D map, similarity between two points uses a Student-t curve, 1 / (1 + d²), instead of a Gaussian. Its tail falls off slowly, so moderately distant points can sit farther apart on the map than they were in high dimensions without paying a big penalty. This is what lets clusters pull apart and leaves room in 2D.


Cyan = Student-t, 1/(1+d²). Pink = Gaussian, exp(−d²). Far from zero the t curve stays much higher.
Behind the scenes · 3

Nudge until the two maps agree

t-SNE measures the mismatch between the high-dimensional probabilities P and the 2D probabilities Q with the KL divergence, then moves every point a little along the gradient to reduce it. Each point is pulled towards its true neighbours and pushed away from the rest. During early exaggeration the P values are multiplied so clusters can form freely; after that the real P is used.


KL cost (against the true P) over iterations. It usually falls and flattens, though it is not guaranteed to reach the best possible map.
for each iteration: qij from the 2D points (t-kernel) gradi = 4 Σj (pij − qij) · (yi − yj) / (1 + ‖yi − yj‖²) update y with momentum and per-coordinate gains
Behind the scenes · 4

What not to read from the picture

t-SNE keeps local neighbourhoods. Sizes of clusters and gaps between clusters on the map need not match the real data. The table compares each group’s average spread around its centre in the original 10-D space with its spread on the map, both as a ratio to the first group.


If the two ratio columns differ a lot, the map is not showing the groups’ true relative spread. Try “3 clusters, unequal spread” and different perplexities.

The mathematics

The mathematics behind t-SNE

Numbers below update live from the actual run.

A. High-dimensional affinities and perplexity

pj|i = exp(−‖xi−xj‖² / 2σi²) / Σk≠i exp(−‖xi−xk‖² / 2σi²)
Perp(Pi) = 2H(Pi)   ,   H(Pi) = −Σj pj|i log2 pj|i

B. Symmetrise

pij = (pj|i + pi|j) / 2n

C. Low-dimensional affinities

qij = (1 + ‖yi−yj‖²)−1 / Σk≠l (1 + ‖yk−yl‖²)−1

D. Cost and gradient

C = KL(P ‖ Q) = Σi≠j pij log(pij / qij)
∂C/∂yi = 4 Σj (pij − qij)(yi − yj)(1 + ‖yi−yj‖²)−1
Practical notes
  • Perplexity is commonly tried in the range of about 5 to 50 and should be smaller than the number of points. The best value depends on the data.
  • Results depend on the random start. Run several seeds before trusting a feature of the map.
  • This page uses the exact O(n²) method. Large datasets use approximations such as Barnes–Hut.
  • Standard t-SNE has no mapping for new points; you would have to rerun it or use another method.
Experiment

Break it on purpose

Tiny perplexity

Set perplexity to 2. Each point only cares about a couple of neighbours, so the map can fragment into small pieces.

Huge perplexity

Set perplexity to 50. Each point now looks at a wide neighbourhood, and the fine local structure can blur.

No clusters at all

Load one featureless blob. Look at what the map does anyway, then restart from a new random start.

Check yourself

Three quick questions

Key takeaways

  • Idea: convert distances to neighbour probabilities in high dimensions, then arrange 2D points so their neighbour probabilities match (minimising KL divergence).
  • Perplexity sets how wide a neighbourhood each point considers. It changes the picture, so try several values.
  • The heavy-tailed kernel lets clusters separate on the map.
  • Read it locally: cluster sizes and the distances between clusters may not be meaningful, and the map can look structured even when the data is not.
  • Stability: results depend on the random start, learning rate and iterations, so check several runs.
  • Use: great for exploring structure by eye; less suited to measuring distances or to transforming new data.
← Back to all articles