UMAP for t‑SNE users
What changes between t‑SNE and UMAP? I start with the similarities each method calculates and work through to the code that moves the points.
I wrote and maintain uwot, an implementation of UMAP in R and C++. If you’ve made a UMAP plot of single-cell data with Seurat, you’ve probably used it.
I coauthored the second version of the UMAP paper with Leland McInnes and John Healy. I was trying to work out how UMAP differed from t‑SNE, so I read the Python code and asked Leland about the bits I couldn’t follow.
What changes between t‑SNE and UMAP? I start with the similarities each method calculates and work through to the code that moves the points.
I worked out an analytical version of Lee and co-workers’ estimate of local dimension. It comes almost for free with the perplexity calculations t-SNE already does. Practically speaking it was doomed, but I made a lot of charts and now I’m going to show them to you.
Several different-looking updates ought to give the same answer. Checking that took a bit of working out.
I cut six novels into passages of ten lines each and looked for passages that use similar words. If I put them on a UMAP plot, do the same passages still end up next to each other?
Each dot is one of 7,344 passages. Word neighbors use Jensen–Shannon distance between TF-IDF profiles. Map neighbors use straight-line distance in this UMAP run. Download the R script.
I gave UMAP two groups of 5,000 points. In 50 dimensions the blue group has ten
times the standard deviation of the orange one. How much of that difference makes
it onto the page? Try changing the density scale in LEOPOLD, my approximation to
densMAP in uwot.
I put 12,000 points on a plane and calculated the push each one feels in t‑SNE. FFT does this on a grid. A mutual tree groups the points and works out how pairs of groups push each other. What happens if I keep the points and give them more room?
As the points move farther apart, FFT needs a larger grid. For each force calculation,
smallvis uses FFT if the padded grid has at most 14 cells per point and at
most four million cells in total. Otherwise it uses the mutual tree. It checks again
on the next calculation, so it can switch back if the layout shrinks. The two timing curves can cross at a different scale.
I scaled the same four groups along both axes in steps of 0.1×. Each time shown is the median of seven single-threaded calculations after warm-up, with the two methods run in shuffled order. I used a grid interval of 1 for FFT and θ = 0.35 for the mutual tree. I checked which result the hybrid actually returned at every step.
I ran this on an Intel Core i7-8750H under Ubuntu on WSL, using R 4.5.2, smallvis 0.0.2.9001 and zufft 0.1.0. Download the R script.
Here I held a smaller set of 4,200 points still so I could check every pair. Pick a point to compare the approximate push with the exact one.
| Calculation | Time | Force error |
|---|
Time is the median of seven runs. Force error compares all the arrows with their exact values.
I generated four groups of points with a fixed seed. The exact calculation visits every pair. Barnes–Hut groups distant points in a tree. FFT interpolation collects their contributions on a grid. All three use the same positions and the same normalized t‑SNE repulsion.
The reference is the Barnes–Hut tree with θ set to zero, checked against a direct sum. Timings cover one force calculation after warm-up, with the FFT workspace reused and the methods run in shuffled order. Relative force error is the length of the error vector divided by the length of the exact vector, after joining all the points’ arrows end to end.
I ran this on an Intel Core i7-8750H under Ubuntu on WSL, using R 4.5.2, smallvis 0.0.2.9001 and zufft 0.1.0. Download the R script.
hnswlib to R. Both can supply neighbors to uwot.I changed one constraint in Local Tangent Space Alignment. The results were surprisingly interesting in many cases (note that I didn’t say “better”).
Can the Codex skills I use on my projects get better with experience? I can’t afford a big suite of evals, so in codex-workflows I’m borrowing an idea from agile development. Alongside skills for jobs like editing a vignette, there are skills for running retrospectives. Agents look back at a task, decide what’s worth changing in the skills they used, and check on later tasks whether it helped.