t-SNE

t-SNE

Draw a map where similar items end up close together.

Press Play to watch the map form, or change the perplexity. Pictures that look alike end up close together.

  • bar across
  • bar down
  • slash
  • backslash
  • a near picture the map lost

KL(P ∥ Q)=∑i≠jpijlog⁡pijqij\mathrm{KL}(P \,\|\, Q) = \sum_{i \ne j} p_{ij} \log \frac{p_{ij}}{q_{ij}}

Step t = 1000: KL(P ‖ Q) = 0.035

Picture 1 (bar across): with perplexity u = 8, its neighbourhood is σ = 17.8 wide. Its 5 nearest in pixels: 32, 25, 38, 40, 19; 4 of them are still among its nearest on the map.

Neighbours kept 86%
of each picture's 5 nearest in pixels

The 40 pictures, with distances from picture 1.
1bar across40.00.0
2backslash554.57.7
3bar across550.75.2
4backslash452.57.0
5backslash456.08.6
6bar across551.15.7
7bar down456.612.9
8bar down556.013.3
9slash355.311.9
10backslash458.26.9
11bar down560.714.1
12slash457.09.7
13backslash454.67.6
14slash351.311.4
15slash352.611.1
16bar down458.114.2
17slash456.19.9
18bar down560.113.7
19bar across549.44.9
20slash456.78.2
21bar down552.612.9
22bar across550.96.6
23slash459.410.5
24backslash459.38.0
25bar across426.31.1
26bar down560.615.3
27bar across550.96.3
28slash454.69.4
29bar down557.814.1
30bar down565.715.8
31backslash453.38.3
32bar across419.40.6
33backslash458.46.9
34backslash551.26.8
35backslash454.86.6
36bar down561.413.8
37slash351.811.7
38bar across426.51.1
39slash458.110.6
40bar across548.96.0

Try it in Python

The same 40 pictures, starting map and perplexity as the Lab, in Python. By hand with NumPy takes the Lab's steps one by one; scikit-learn's TSNE runs by its own rules, so its last digits can differ.

What you are looking at

Each square on the map is a tiny picture, 8 by 8 pixels. To the computer, a picture is just 64 numbers: how dark each pixel is. There are four kinds of shape (a bar across, a bar down, a slash and a backslash), drawn at slightly different places, with a few smudges.

64 numbers cannot be drawn on a flat page. t-SNE draws a map instead: it places the pictures so that pictures with similar pixels end up close together. It is never told the kinds; the coloured frames are only there for you to check its work. The pictures are made up, and Refresh Data shows another set.

What each symbol means

xᵢ
Picture i as its 64 pixel numbers.
yᵢ
Where picture i sits on the map.
pᵢⱼ
How close pictures i and j are in pixels, as a probability.
qᵢⱼ
How close they are on the map, measured the same way.
σᵢ
The width of picture i's neighbourhood in pixels.
u
The perplexity: roughly how many neighbours each picture pays attention to.
t
How many steps the map has moved so far.

How t-SNE draws its map

  1. Measure closeness in the pictures. For each picture, put a bell curve over the pixel distances to all the others. Near pictures get a high probability, far ones almost none. Each curve's width σᵢ is chosen so that it covers about u neighbours.
    pj∣i=e−∥xi−xj∥2/2σi2∑k≠ie−∥xi−xk∥2/2σi2,pij=pj∣i+pi∣j2np_{j \mid i} = \frac{e^{-\lVert x_i - x_j \rVert^2 / 2\sigma_i^2}}{\sum_{k \ne i} e^{-\lVert x_i - x_k \rVert^2 / 2\sigma_i^2}}, \qquad p_{ij} = \frac{p_{j \mid i} + p_{i \mid j}}{2n}
  2. Measure closeness on the map in the same way, but with a curve that has longer tails. That gives far-apart groups room to spread out instead of piling up in the middle.
    qij=(1+∥yi−yj∥2)−1∑k≠l(1+∥yk−yl∥2)−1q_{ij} = \frac{(1 + \lVert y_i - y_j \rVert^2)^{-1}}{\sum_{k \ne l} (1 + \lVert y_k - y_l \rVert^2)^{-1}}
  3. Move the map a little at a time to make the two match. The mismatch is measured by the KL cost; each step moves every picture downhill on it. Pairs that should be close pull together; pairs that should be apart push away.
    KL(P ∥ Q)=∑i≠jpijlog⁡pijqij\mathrm{KL}(P \,\|\, Q) = \sum_{i \ne j} p_{ij} \log \frac{p_{ij}}{q_{ij}}

The map starts as a tiny random cloud. For the first 250 steps the pulls are made 12 times stronger, so groups form early and can then settle into place.

Perplexity

With a small perplexity, each picture cares only about its very nearest few, so the map shows small, tight clumps, sometimes splitting a kind into pieces. With a larger perplexity, each picture looks further, and the map shows the bigger groups more clearly. Typical values are between 5 and 50; it can never be more than the number of other pictures.

How to read the map

Closeness on the map is meaningful: near pictures are similar. But sizes and gaps are not. A tight group is not necessarily more alike than a loose one, and the distance between two groups says little about how different they are. The map also has no up or down: run it again and the groups may come out turned or mirrored.

The score Neighbours kept checks the map honestly: for each picture, how many of its 5 nearest pictures in pixels are also among its 5 nearest on the map.

The model in short

  1. Measure how close every pair is in the data.
  2. Start with a tiny random map.
  3. Move the map step by step until closeness on the map matches closeness in the data.
  4. Read who is near whom, not the sizes or the gaps.