↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Keeping neighbours together

t-SNE and UMAP draw high-dimensional data as a 2-D map. Neither tries to preserve distances, which is impossible in two dimensions; both try to keep each point's near neighbours near. The result is a map whose clusters are meaningful and whose distances between clusters mostly are not.

The page maps scikit-learn's digits: 1,797 handwritten digits of 8 × 8 pixels, each a point in 64 dimensions. The maps never see the labels; the colours are added afterwards.

Neighbours as probabilities

t-SNE turns distances into probabilities. Around each point it puts a Gaussian and asks how likely would be to pick each other point as its neighbour:

Each is found by bisection so that the distribution has the same perplexity, , as if the point had about that many equally likely neighbours (30 here). A digit in a dense region gets a narrow Gaussian and an isolated one a wide one; the panel for the picked digit shows its and its ten most likely neighbours by . The two directions are averaged, .

On the map, the similarity of two points is a Student-t with one degree of freedom, . Its heavy tail is the point of the method: moderately distant pairs can be placed far apart in 2-D without much cost, which leaves room between clusters.

Springs between points

t-SNE minimizes by gradient descent. The gradient has a simple shape,

a spring between every pair, pulling with and pushing with . scikit-learn's optimizer adds momentum and per-coordinate gains that grow while a coordinate's gradient keeps its sign and shrink when it flips. The page records scikit-learn's own run, every iteration's embedding read from inside its optimizer; replaying the momentum-and-gains update from the recorded gradients reproduces all 1,000 embeddings bit for bit. KL falls from 3.98 at the start to 0.680, and the Neighbours with the same digit chart, the share of each point's 10 nearest map neighbours with the same label, rises to 98.2 %.

Early exaggeration

For the first 250 iterations scikit-learn multiplies by 12. Attraction then outweighs repulsion, so points gather into compact clumps while the whole map stays small, and only when returns to its real size does repulsion spread them out; the KL chart drops sharply right after the switch. The idea is that clusters should find each other before they have room to separate.

On these digits it turns out not to matter. The same run without exaggeration, started from the same PCA layout, already has 98.2 % same-digit neighbours at iteration 25 and ends with a KL of 0.678, a trustworthiness of 0.9928 and 98.4 % same-digit neighbours, against 0.680, 0.9923 and 98.2 % with it. Exaggeration was introduced for random starts; with a PCA start, the large-scale arrangement is already reasonable, which Kobak and Linderman argue is what matters.

Perplexity changes what counts as near

Perplexity sets how many neighbours each point tries to keep. At 5 the median is 0.33 and clusters break into fragments; at 300 it is 0.83, every point attends to a sixth of the data and the clusters crowd together. The four final maps keep 97.7, 98.2, 97.5 and 96.0 % same-digit neighbours at perplexity 5, 30, 100 and 300. Their KL values (0.88 down to 0.44) cannot be compared, because each perplexity defines a different .

UMAP

UMAP builds a weighted graph of each point's 15 nearest neighbours instead of a full : an edge's weight is , where is the distance to the nearest neighbour, so every point is connected to at least one other with weight 1, and makes the weights sum to . The two directions are merged as fuzzy sets, , which leaves 34,236 nonzero weights here. The layout scores a pair at map distance by , with = 1.58 and = 0.895 fitted to the min_dist setting of 0.1, and moves points by stochastic gradient descent: along edges they attract, against randomly drawn other points they repel.

It starts from a spectral layout of the graph, with 72 % same-digit neighbours after the first epoch. The first epochs take large steps and scramble it, down to 36 %, before the step size decays and the clusters settle, at 98.2 % after 500 epochs with a trustworthiness of 0.988. The UMAP settings maps change one thing each: 5 or 50 neighbours keep 98.0 and 97.4 % same-digit neighbours, a min_dist of 0 or 0.5 keeps 98.0 and 97.2 %.

SNE was introduced by Hinton and Roweis; van der Maaten and Hinton added the Student-t and the symmetric , and Barnes–Hut approximations later made it scale. McInnes, Healy and Melville derived UMAP from fuzzy topology. The Embeddings tab in Retrieval maps sentence embeddings the same way.

Papers

  1. Visualizing Data using t-SNE Laurens van der Maaten, Geoffrey Hinton · Journal of Machine Learning Research 2008 t-SNE: symmetric P, the Student-t on the map, the gradient as springs, early exaggeration and the gains optimizer.
  2. Stochastic Neighbor Embedding Geoffrey Hinton, Sam Roweis · NeurIPS 2002 Neighbour probabilities from Gaussians with a per-point width set by perplexity, the step t-SNE keeps.
  3. UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction Leland McInnes, John Healy, James Melville · arXiv 2018 UMAP's fuzzy neighbour graph, the a, b curve fitted to min_dist and the negative-sampling layout recorded here.
  4. Initialization is critical for preserving global data structure in both t-SNE and UMAP Dmitry Kobak, George C. Linderman · Nature Biotechnology 2021 With a PCA or spectral start both methods keep the large-scale layout, which is why early exaggeration matters little here.
  5. Automated optimized parameters for T-distributed stochastic neighbor embedding improve visualization and analysis of large datasets Anna C. Belkina et al. · Nature Communications 2019 Learning rate proportional to n divided by the exaggeration, the rule behind scikit-learn's learning_rate='auto'.
  6. How to Use t-SNE Effectively Martin Wattenberg, Fernanda Viégas, Ian Johnson · Distill 2016 Interactive examples of what perplexity and iteration count change, and why cluster sizes and distances mislead.

See also