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
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.
| 1 | bar across | 4 | 0.0 | 0.0 |
| 2 | backslash | 5 | 54.5 | 7.7 |
| 3 | bar across | 5 | 50.7 | 5.2 |
| 4 | backslash | 4 | 52.5 | 7.0 |
| 5 | backslash | 4 | 56.0 | 8.6 |
| 6 | bar across | 5 | 51.1 | 5.7 |
| 7 | bar down | 4 | 56.6 | 12.9 |
| 8 | bar down | 5 | 56.0 | 13.3 |
| 9 | slash | 3 | 55.3 | 11.9 |
| 10 | backslash | 4 | 58.2 | 6.9 |
| 11 | bar down | 5 | 60.7 | 14.1 |
| 12 | slash | 4 | 57.0 | 9.7 |
| 13 | backslash | 4 | 54.6 | 7.6 |
| 14 | slash | 3 | 51.3 | 11.4 |
| 15 | slash | 3 | 52.6 | 11.1 |
| 16 | bar down | 4 | 58.1 | 14.2 |
| 17 | slash | 4 | 56.1 | 9.9 |
| 18 | bar down | 5 | 60.1 | 13.7 |
| 19 | bar across | 5 | 49.4 | 4.9 |
| 20 | slash | 4 | 56.7 | 8.2 |
| 21 | bar down | 5 | 52.6 | 12.9 |
| 22 | bar across | 5 | 50.9 | 6.6 |
| 23 | slash | 4 | 59.4 | 10.5 |
| 24 | backslash | 4 | 59.3 | 8.0 |
| 25 | bar across | 4 | 26.3 | 1.1 |
| 26 | bar down | 5 | 60.6 | 15.3 |
| 27 | bar across | 5 | 50.9 | 6.3 |
| 28 | slash | 4 | 54.6 | 9.4 |
| 29 | bar down | 5 | 57.8 | 14.1 |
| 30 | bar down | 5 | 65.7 | 15.8 |
| 31 | backslash | 4 | 53.3 | 8.3 |
| 32 | bar across | 4 | 19.4 | 0.6 |
| 33 | backslash | 4 | 58.4 | 6.9 |
| 34 | backslash | 5 | 51.2 | 6.8 |
| 35 | backslash | 4 | 54.8 | 6.6 |
| 36 | bar down | 5 | 61.4 | 13.8 |
| 37 | slash | 3 | 51.8 | 11.7 |
| 38 | bar across | 4 | 26.5 | 1.1 |
| 39 | slash | 4 | 58.1 | 10.6 |
| 40 | bar across | 5 | 48.9 | 6.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
- 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.
- 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.
- 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.
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
- Measure how close every pair is in the data.
- Start with a tiny random map.
- Move the map step by step until closeness on the map matches closeness in the data.
- Read who is near whom, not the sizes or the gaps.