drnb notebooks
  • Notebooks
  • GitHub
  1. Articles
  2. Neighbor Recall: UMAP vs t-SNE
  • Experiments in Dimensionality Reduction
  • Articles
    • Do Hubs Hurt?
    • How Does Dimensionality Reduction Transform Distances?
    • Hubness
    • Heavy-tailed UMAP
    • Leopold: Lightweight Estimate of Preservation of Local Density
    • Measuring Media Bias
    • Normalized Compression Distance with 20 Newsgroups
    • PaCMAP: local scaling vs. k-nearest neighbors
    • PaCMAP Repeats
    • Neighbor Overlap and PCA
    • Neighbor Recall: UMAP vs t-SNE
    • Approximating Isomap with k-Nearest Neighbors and Stochastic Metric Multidimensional Scaling
    • Stochastic Metric Multidimensional Scaling
    • Renormalizing after TF-IDF + SVD
    • t-SNE Consistency with Random Initialization
    • UMAP with Annealed Neighbors. 1: Defaults
    • UMAP with Annealed Neighbors. 2: t-UMAP
    • UMAP with Annealed Neighbors. 3: PCA Initialization
    • Spectral Embedding
    • UMATO Evaluation
    • t-SNE Affinity: Gaussian vs. Uniform

On this page

  • Datasets
  • Evaluation Metrics
  • Create the Experiment
  • Methods to Compare
    • One Non-Default Setting
    • Recall
    • Recall at 50 and 150 neighbors
    • Global Preservation
  • Can We Improve UMAP’s Recall?
    • PCA initialization
    • Cauchy kernel
    • Increase Negative Sampling
  1. Articles
  2. Neighbor Recall: UMAP vs t-SNE

  • Show All Code
  • Hide All Code
Code
%load_ext lab_black

from drnb.experiment import Experiment

Dmitry Kobak noted in a tweet that t-SNE is superior to UMAP in recall of nearest neighbors. The specifics was about document embedding, but I had observed something similar across multiple datasets when documenting uwot. But I thought there was a bug in my nearest neighbor calculation code and so removed the values in late 2018, promising myself I would get around to working out if there was an error and fixing it. Better late than never here is an evaluation of nearest neigbor recall.

Datasets

I chose 20 datasets that vary in size, dimensionality and domain:

Code
datasets = [
    "iris",
    "penguins",
    "s1k",
    "olivetti",
    "frey",
    "coil20",
    "mnist",
    "fashion",
    "kuzushiji",
    "isoswiss",
    "scurvehole",
    "mammoth",
    "macosko2015",
    "tasic2018",
    "lamanno2020",
    "onet271",
    "avonet",
    "ng20",
    "iclr",
    "galaxy10sdss",
]
len(datasets)
20

Evaluation Metrics

I’m not really that interested in global evaluation but we may as well also look at random triplet evaluation (is the relative ordering of distances for three points preserved in the embedded space).

For recall, I will look at preservation of 15, 50 and 150 nearest neighbors.

Code
eval_metrics = ["rte", ("nnp", dict(n_neighbors=[15, 50, 150]))]

Create the Experiment

The Experiment class is a convenient way for me to evaluate and store results for different embedding methods on different datasets:

Code
recall_exp = Experiment(name="Recall", evaluations=eval_metrics)
Code
recall_exp.add_datasets(datasets)

Methods to Compare

Let’s start with t-SNE and UMAP with (mostly) their default parameters. For t-SNE this is OpenTSNE, so the initialization is via PCA rather than the random initialization used by many other packages (and used in the t-SNE papers). No attempt at this point will be made to equalize the number of iterations or the number of neighbors used internally. The document embedding work uses longer t-SNE optimization and anneals the early exaggeration step but Dmitry told me in an email that he doesn’t think it makes a massive different to recall so let’s not over-complicate things. Also, I will look at PCA as a baseline.

One Non-Default Setting

There is one way in which I will deviate from default settings. All the datasets below have had their exact nearest neighbors found using FAISS ahead of time. This saves time when running t-SNE and UMAP and also removes one way variations in recall quality could enter the assessment.

Code
recall_exp.add_method("pca")
Code
recall_exp.add_method("tsne")
Code
recall_exp.add_method("umap")

Off we go. t-SNE generates a lot of output, so the output of the cell below is hidden. Also, each dataset and method is run sequentially so this could take a while to complete.

Code
recall_exp.run()
[13:42:01] INFO     Running pca on iris                                                            experiment.py:58
           WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Loading faiss with AVX2 support.                                                   loader.py:54
           INFO     Could not load library with AVX2 support due to:                                   loader.py:58
                    ModuleNotFoundError("No module named 'faiss.swigfaiss_avx2'")                                  
           INFO     Loading faiss.                                                                     loader.py:64
           INFO     Successfully loaded faiss.                                                         loader.py:66
[13:42:03] INFO     Running pca on penguins                                                        experiment.py:58
           INFO     Running pca on s1k                                                             experiment.py:58
[13:42:04] INFO     Running pca on olivetti                                                        experiment.py:58
[13:42:06] INFO     Running pca on frey                                                            experiment.py:58
           INFO     Running pca on coil20                                                          experiment.py:58
[13:42:08] INFO     Running pca on mnist                                                           experiment.py:58
[13:42:21] INFO     Running pca on fashion                                                         experiment.py:58
[13:42:31] INFO     Running pca on kuzushiji                                                       experiment.py:58
[13:42:43] INFO     Running pca on isoswiss                                                        experiment.py:58
[13:42:45] INFO     Running pca on scurvehole                                                      experiment.py:58
[13:42:47] INFO     Running pca on mammoth                                                         experiment.py:58
[13:42:53] INFO     Running pca on macosko2015                                                     experiment.py:58
[13:43:02] INFO     Running pca on tasic2018                                                       experiment.py:58
[13:43:08] INFO     Running pca on lamanno2020                                                     experiment.py:58
[13:43:10] INFO     Running pca on onet271                                                         experiment.py:58
           INFO     Running pca on avonet                                                          experiment.py:58
[13:43:12] INFO     Running pca on ng20                                                            experiment.py:58
[13:43:16] INFO     Running pca on iclr                                                            experiment.py:58
[13:43:19] INFO     Running pca on galaxy10sdss                                                    experiment.py:58
[13:43:31] INFO     Running tsne on iris                                                           experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:32] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running tsne on penguins                                                       experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:33] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
           INFO     Running tsne on s1k                                                            experiment.py:58
[13:43:34] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:36] INFO     Running tsne on olivetti                                                       experiment.py:58
[13:43:37] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:38] INFO     Running tsne on frey                                                           experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:40] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:44] INFO     Running tsne on coil20                                                         experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:43:45] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:43:47] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:48] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:43:51] INFO     Running tsne on mnist                                                          experiment.py:58
[13:43:52] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:43:54] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:43:58] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:44:23] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:45:45] INFO     Running tsne on fashion                                                        experiment.py:58
[13:45:46] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:45:48] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:45:50] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:46:16] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:47:41] INFO     Running tsne on kuzushiji                                                      experiment.py:58
[13:47:42] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:47:45] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:47:46] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:48:12] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:49:37] INFO     Running tsne on isoswiss                                                       experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:49:38] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:49:45] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:50:32] INFO     Running tsne on scurvehole                                                     experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:50:45] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:51:11] INFO     Running tsne on mammoth                                                        experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:51:13] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:51:28] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:52:49] INFO     Running tsne on macosko2015                                                    experiment.py:58
[13:52:50] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:52:52] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:52:56] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:53:14] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:53:55] INFO     Running tsne on tasic2018                                                      experiment.py:58
[13:53:56] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:53:57] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:53:58] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:54:08] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:54:44] INFO     Running tsne on lamanno2020                                                    experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:54:45] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:54:49] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:54:57] INFO     Running tsne on onet271                                                        experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:54:58] INFO     Automatically determined negative gradient method `bh`                               tsne.py:53
[13:55:00] INFO     Running tsne on avonet                                                         experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
           INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
           INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:55:06] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:55:39] INFO     Running tsne on ng20                                                           experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:55:40] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:55:41] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:55:50] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:56:28] INFO     Running tsne on iclr                                                           experiment.py:58
           INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:56:29] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:56:30] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:56:37] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:57:03] INFO     Running tsne on galaxy10sdss                                                   experiment.py:58
[13:57:06] INFO     KNN index provided. Ignoring KNN-related parameters.                            affinity.py:186
[13:57:07] INFO     Precomputed affinities provided. Ignoring perplexity-related parameters.           tsne.py:1321
           INFO     Precomputed initialization provided. Ignoring initalization-related parameters.    tsne.py:1329
[13:57:14] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:57:24] INFO     Automatically determined negative gradient method `fft`                              tsne.py:53
[13:58:00] INFO     Running umap on iris                                                           experiment.py:58
[13:58:05] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
[13:58:06] INFO     Running umap on penguins                                                       experiment.py:58
[13:58:09] INFO     Running umap on s1k                                                            experiment.py:58
[13:58:12] INFO     Running umap on olivetti                                                       experiment.py:58
[13:58:15] INFO     Running umap on frey                                                           experiment.py:58
[13:58:18] INFO     Running umap on coil20                                                         experiment.py:58
[13:58:22] INFO     Running umap on mnist                                                          experiment.py:58
[13:58:52] INFO     Running umap on fashion                                                        experiment.py:58
[13:59:23] INFO     Running umap on kuzushiji                                                      experiment.py:58
[13:59:54] INFO     Running umap on isoswiss                                                       experiment.py:58
[14:00:05] INFO     Running umap on scurvehole                                                     experiment.py:58
[14:00:13] INFO     Running umap on mammoth                                                        experiment.py:58
[14:01:39] INFO     Running umap on macosko2015                                                    experiment.py:58
[14:02:02] INFO     Running umap on tasic2018                                                      experiment.py:58
/home/james/dev/drnb/venv/lib/python3.10/site-packages/sklearn/manifold/_spectral_embedding.py:274: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn(
[14:02:22] INFO     Running umap on lamanno2020                                                    experiment.py:58
[14:02:27] INFO     Running umap on onet271                                                        experiment.py:58
[14:02:30] INFO     Running umap on avonet                                                         experiment.py:58
[14:02:35] INFO     Running umap on ng20                                                           experiment.py:58
[14:02:45] INFO     Running umap on iclr                                                           experiment.py:58
[14:02:53] INFO     Running umap on galaxy10sdss                                                   experiment.py:58

Recall

Here are the nearest neighbor preservation values for 15 neighbors:

Code
recall_exp.to_df(metrics=["nnp-15"])
method pca tsne umap
metric nnp-15 nnp-15 nnp-15
iris 0.801333 0.827556 0.746222
penguins 0.52729 0.704678 0.649513
s1k 0.085067 0.414867 0.366333
olivetti 0.302333 0.501833 0.514333
frey 0.186022 0.584054 0.531433
coil20 0.371759 0.806343 0.752639
mnist 0.010113 0.340116 0.116833
fashion 0.017642 0.31651 0.128747
kuzushiji 0.00643 0.333426 0.14882
isoswiss 0.40196 0.79531 0.774383
scurvehole 0.17609 0.815374 0.767898
mammoth 0.39018 0.769836 0.643339
macosko2015 0.003346 0.024574 0.017684
tasic2018 0.015095 0.266837 0.161613
lamanno2020 0.05361 0.393229 0.311861
onet271 0.236502 0.497442 0.459183
avonet 0.149726 0.573422 0.430884
ng20 0.004199 0.240415 0.134989
iclr 0.007213 0.246945 0.119074
galaxy10sdss 0.033892 0.248308 0.095531

From eyeballing the table it looks like both t-SNE and UMAP do better than PCA in most cases and t-SNE certainly does seem superior to UMAP. Here are the mean recall values:

Code
recall_exp.to_df(metrics=["nnp-15"]).agg("mean")
method  metric
pca     nnp-15    0.188990
tsne    nnp-15    0.485054
umap    nnp-15    0.393566
dtype: float64

t-SNE does better than UMAP by nearly 10%. Looking at the biggest and smallest difference between t-SNE and UMAP:

Code
recall_exp.to_df(metrics=["nnp-15"], methods=["tsne", "umap"]).apply(
    lambda x: x.iloc[0] - x.iloc[1], axis=1
).agg(["idxmin", "min", "idxmax", "max"])
idxmin    olivetti
min        -0.0125
idxmax       mnist
max       0.223283
dtype: object

So for the Olivetti faces database, UMAP does very slightly better than t-SNE. t-SNE’s biggest improvement over UMAP is with the MNIST digits.

Recall at 50 and 150 neighbors

Let’s see if the trend is the same for a larger number of neighbors:

Code
recall_exp.to_df(metrics=["nnp-50"], methods=["pca", "tsne", "umap"]).agg("mean")
method  metric
pca     nnp-50    0.257930
tsne    nnp-50    0.491727
umap    nnp-50    0.442060
dtype: float64
Code
recall_exp.to_df(metrics=["nnp-150"], methods=["pca", "tsne", "umap"]).agg("mean")
method  metric 
pca     nnp-150    0.308494
tsne    nnp-150    0.484968
umap    nnp-150    0.457770
dtype: float64

Yes. UMAP underperforms compared to t-SNE, but does narrow the gap in performance.

Global Preservation

Although not something I want to focus on too much here, let’s see how the global preservation metrics fare:

Code
recall_exp.to_df(metrics=["rte-5"]).agg("mean")
method  metric
pca     rte-5     0.800013
tsne    rte-5     0.716403
umap    rte-5     0.695719
dtype: float64

Unsurprisingly, PCA is better than UMAP and t-SNE here. t-SNE does a slightly better job than UMAP for global preservation.

Can We Improve UMAP’s Recall?

From a contrastive perspective t-SNE and UMAP are very similar so we should be able to improve UMAP’s recall by making it more like t-SNE. A close reading of the linked paper suggests that the biggest difference is going to be the amount of negative sampling, but we’ll try some obvious things first to get them out of the way.

PCA initialization

OpenTSNE uses PCA initialization by default, UMAP uses a spectral initialization. The version of UMAP I am using doesn’t have a PCA initialization option, so I will add it manually as a step before UMAP:

Code
import drnb.embed.pipeline as pl

recall_exp.add_method(
    [
        pl.embedder(
            "pca",
        ),
        pl.embedder("umap"),
    ]
)

Internally UMAP does some scaling of the absolute values of the coordinates so I am not going to worry about manually scaling the PCA input to a small standard deviation as OpenTSNE does.

This embedding gets automatically named pca+umap which is a good enough identifier to distinguish it from the other methods.

Code
recall_exp.run()
[14:03:08] INFO     Running pca+umap on iris                                                       experiment.py:58
[14:03:11] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running pca+umap on penguins                                                   experiment.py:58
[14:03:14] INFO     Running pca+umap on s1k                                                        experiment.py:58
[14:03:17] INFO     Running pca+umap on olivetti                                                   experiment.py:58
[14:03:19] INFO     Running pca+umap on frey                                                       experiment.py:58
[14:03:23] INFO     Running pca+umap on coil20                                                     experiment.py:58
[14:03:27] INFO     Running pca+umap on mnist                                                      experiment.py:58
[14:03:50] INFO     Running pca+umap on fashion                                                    experiment.py:58
[14:04:17] INFO     Running pca+umap on kuzushiji                                                  experiment.py:58
[14:04:42] INFO     Running pca+umap on isoswiss                                                   experiment.py:58
[14:04:49] INFO     Running pca+umap on scurvehole                                                 experiment.py:58
[14:04:56] INFO     Running pca+umap on mammoth                                                    experiment.py:58
[14:05:15] INFO     Running pca+umap on macosko2015                                                experiment.py:58
[14:05:37] INFO     Running pca+umap on tasic2018                                                  experiment.py:58
[14:05:49] INFO     Running pca+umap on lamanno2020                                                experiment.py:58
[14:05:54] INFO     Running pca+umap on onet271                                                    experiment.py:58
[14:05:57] INFO     Running pca+umap on avonet                                                     experiment.py:58
[14:06:03] INFO     Running pca+umap on ng20                                                       experiment.py:58
[14:06:15] INFO     Running pca+umap on iclr                                                       experiment.py:58
[14:06:27] INFO     Running pca+umap on galaxy10sdss                                               experiment.py:58

Ok, how did we do:

Code
recall_exp.to_df(metrics=["nnp-15"], methods=["tsne", "umap", "pca+umap"]).agg("mean")
method    metric
tsne      nnp-15    0.485054
umap      nnp-15    0.393566
pca+umap  nnp-15    0.390994
dtype: float64

Basically no change. Still true at 50 neighbors?

Code
recall_exp.to_df(metrics=["nnp-50"], methods=["tsne", "umap", "pca+umap"]).agg("mean")
method    metric
tsne      nnp-50    0.491727
umap      nnp-50    0.442060
pca+umap  nnp-50    0.435625
dtype: float64

Yeah. Any effect on the global preservation at least?

Code
recall_exp.to_df(metrics=["rte-5"], methods=["tsne", "umap", "pca+umap"]).agg("mean")
method    metric
tsne      rte-5     0.716403
umap      rte-5     0.695719
pca+umap  rte-5     0.720174
dtype: float64

I am not going to do a statistical significance test on these results, but if you were prepared to believe that the difference between UMAP and t-SNE for global preservation was meaningful, then you might also be inclined to believe that if you initialize UMAP with PCA, you get slightly better global preservation than t-SNE. I am skeptical that those are practically significant though. At any rate, initialization is not the source of recall differences.

Cauchy kernel

Another obvious difference: t-SNE uses the Cauchy kernel for its output similarity kernel, whereas UMAP’s default is slightly different: it’s still heavier-tailed than a Gaussian, but it puts more emphasis on shorter distances than the Cauchy. However, you can get back the Cauchy kernel in UMAP by using a=1, b=1. So let’s do that. In uwot I call this mode of UMAP “t-UMAP”, so I will label it analogously here. I’ll also keep the PCA initialization.

Code
recall_exp.add_method(
    [
        pl.embedder(
            "pca",
        ),
        pl.embedder("umap", params=dict(a=1, b=1)),
    ],
    name="pca+tumap",
)
Code
recall_exp.run()
[14:06:52] INFO     Running pca+tumap on iris                                                      experiment.py:58
[14:06:57] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running pca+tumap on penguins                                                  experiment.py:58
[14:07:00] INFO     Running pca+tumap on s1k                                                       experiment.py:58
[14:07:04] INFO     Running pca+tumap on olivetti                                                  experiment.py:58
[14:07:08] INFO     Running pca+tumap on frey                                                      experiment.py:58
[14:07:13] INFO     Running pca+tumap on coil20                                                    experiment.py:58
[14:07:18] INFO     Running pca+tumap on mnist                                                     experiment.py:58
[14:07:44] INFO     Running pca+tumap on fashion                                                   experiment.py:58
[14:08:14] INFO     Running pca+tumap on kuzushiji                                                 experiment.py:58
[14:08:41] INFO     Running pca+tumap on isoswiss                                                  experiment.py:58
[14:08:52] INFO     Running pca+tumap on scurvehole                                                experiment.py:58
[14:09:00] INFO     Running pca+tumap on mammoth                                                   experiment.py:58
[14:09:16] INFO     Running pca+tumap on macosko2015                                               experiment.py:58
[14:09:49] INFO     Running pca+tumap on tasic2018                                                 experiment.py:58
[14:10:05] INFO     Running pca+tumap on lamanno2020                                               experiment.py:58
[14:10:11] INFO     Running pca+tumap on onet271                                                   experiment.py:58
[14:10:15] INFO     Running pca+tumap on avonet                                                    experiment.py:58
[14:10:21] INFO     Running pca+tumap on ng20                                                      experiment.py:58
[14:10:32] INFO     Running pca+tumap on iclr                                                      experiment.py:58
[14:10:41] INFO     Running pca+tumap on galaxy10sdss                                              experiment.py:58

Here are the results:

Code
recall_exp.to_df(
    metrics=["nnp-15"], methods=["tsne", "umap", "pca+umap", "pca+tumap"]
).agg("mean")
method     metric
tsne       nnp-15    0.485054
umap       nnp-15    0.393566
pca+umap   nnp-15    0.390994
pca+tumap  nnp-15    0.385984
dtype: float64

Wow, this actually managed to make things even worse. I’m impressed.

Increase Negative Sampling

Ok so far we haven’t been able to improve UMAP’s neighbor recall. My hopes rest with the negative_sample_rate parameter in UMAP, which increases the number of “negative” examples used to generate the repulsive part of the gradient. The default is 5, which means every item in the dataset is updated based on the repulsive interaction with 5 randomly selected other items. Let’s double this to 10. This is going to increase the run time of UMAP a bit.

Code
recall_exp.add_method(
    [
        pl.embedder(
            "pca",
        ),
        pl.embedder("umap", params=dict(a=1, b=1, negative_sample_rate=10)),
    ],
    name="pca+tumap-neg10",
)
Code
recall_exp.run()
[14:11:02] INFO     Running pca+tumap-neg10 on iris                                                experiment.py:58
[14:11:06] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running pca+tumap-neg10 on penguins                                            experiment.py:58
[14:11:09] INFO     Running pca+tumap-neg10 on s1k                                                 experiment.py:58
[14:11:13] INFO     Running pca+tumap-neg10 on olivetti                                            experiment.py:58
[14:11:16] INFO     Running pca+tumap-neg10 on frey                                                experiment.py:58
[14:11:20] INFO     Running pca+tumap-neg10 on coil20                                              experiment.py:58
[14:11:25] INFO     Running pca+tumap-neg10 on mnist                                               experiment.py:58
[14:12:01] INFO     Running pca+tumap-neg10 on fashion                                             experiment.py:58
[14:12:41] INFO     Running pca+tumap-neg10 on kuzushiji                                           experiment.py:58
[14:13:24] INFO     Running pca+tumap-neg10 on isoswiss                                            experiment.py:58
[14:13:36] INFO     Running pca+tumap-neg10 on scurvehole                                          experiment.py:58
[14:13:47] INFO     Running pca+tumap-neg10 on mammoth                                             experiment.py:58
[14:14:15] INFO     Running pca+tumap-neg10 on macosko2015                                         experiment.py:58
[14:15:12] INFO     Running pca+tumap-neg10 on tasic2018                                           experiment.py:58
[14:15:29] INFO     Running pca+tumap-neg10 on lamanno2020                                         experiment.py:58
[14:15:40] INFO     Running pca+tumap-neg10 on onet271                                             experiment.py:58
[14:15:45] INFO     Running pca+tumap-neg10 on avonet                                              experiment.py:58
[14:15:52] INFO     Running pca+tumap-neg10 on ng20                                                experiment.py:58
[14:16:05] INFO     Running pca+tumap-neg10 on iclr                                                experiment.py:58
[14:16:16] INFO     Running pca+tumap-neg10 on galaxy10sdss                                        experiment.py:58
Code
recall_exp.to_df(
    metrics=["nnp-15"],
    methods=["tsne", "umap", "pca+umap", "pca+tumap", "pca+tumap-neg10"],
).agg("mean")
method           metric
tsne             nnp-15    0.485054
umap             nnp-15    0.393566
pca+umap         nnp-15    0.390994
pca+tumap        nnp-15    0.385984
pca+tumap-neg10  nnp-15    0.406263
dtype: float64

At least we are going in the right direction. Not a lot better than vanilla UMAP, but it’s something. Let’s crank the negative_sample_rate up to 50 and see if we can make more progress:

Code
recall_exp.add_method(
    [
        pl.embedder(
            "pca",
        ),
        pl.embedder("umap", params=dict(a=1, b=1, negative_sample_rate=50)),
    ],
    name="pca+tumap-neg50",
)
Code
recall_exp.run()
[14:16:54] INFO     Running pca+tumap-neg50 on iris                                                experiment.py:58
[14:17:05] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running pca+tumap-neg50 on penguins                                            experiment.py:58
[14:17:10] INFO     Running pca+tumap-neg50 on s1k                                                 experiment.py:58
[14:17:17] INFO     Running pca+tumap-neg50 on olivetti                                            experiment.py:58
[14:17:26] INFO     Running pca+tumap-neg50 on frey                                                experiment.py:58
[14:17:36] INFO     Running pca+tumap-neg50 on coil20                                              experiment.py:58
[14:17:47] INFO     Running pca+tumap-neg50 on mnist                                               experiment.py:58
[14:19:24] INFO     Running pca+tumap-neg50 on fashion                                             experiment.py:58
[14:21:37] INFO     Running pca+tumap-neg50 on kuzushiji                                           experiment.py:58
[14:23:30] INFO     Running pca+tumap-neg50 on isoswiss                                            experiment.py:58
[14:24:37] INFO     Running pca+tumap-neg50 on scurvehole                                          experiment.py:58
[14:25:15] INFO     Running pca+tumap-neg50 on mammoth                                             experiment.py:58
[14:26:54] INFO     Running pca+tumap-neg50 on macosko2015                                         experiment.py:58
[14:28:32] INFO     Running pca+tumap-neg50 on tasic2018                                           experiment.py:58
[16:17:27] INFO     Running pca+tumap-neg50 on lamanno2020                                         experiment.py:58
[16:17:53] INFO     Running pca+tumap-neg50 on onet271                                             experiment.py:58
[16:18:01] INFO     Running pca+tumap-neg50 on avonet                                              experiment.py:58
[16:18:21] INFO     Running pca+tumap-neg50 on ng20                                                experiment.py:58
[16:19:07] INFO     Running pca+tumap-neg50 on iclr                                                experiment.py:58
[16:19:42] INFO     Running pca+tumap-neg50 on galaxy10sdss                                        experiment.py:58
Code
recall_exp.to_df(
    metrics=["nnp-15"],
    methods=[
        "tsne",
        "umap",
        "pca+umap",
        "pca+tumap",
        "pca+tumap-neg10",
        "pca+tumap-neg50",
    ],
).agg("mean")
method           metric
tsne             nnp-15    0.485054
umap             nnp-15    0.393566
pca+umap         nnp-15    0.390994
pca+tumap        nnp-15    0.385984
pca+tumap-neg10  nnp-15    0.406263
pca+tumap-neg50  nnp-15    0.443335
dtype: float64
Code
recall_exp.to_df(
    metrics=["nnp-50"],
    methods=[
        "tsne",
        "umap",
        "pca+umap",
        "pca+tumap",
        "pca+tumap-neg10",
        "pca+tumap-neg50",
    ],
).agg("mean")
method           metric
tsne             nnp-50    0.491727
umap             nnp-50    0.442060
pca+umap         nnp-50    0.435625
pca+tumap        nnp-50    0.433385
pca+tumap-neg10  nnp-50    0.444540
pca+tumap-neg50  nnp-50    0.461253
dtype: float64

Success! Well, the recall isn’t better than t-SNE, but increasing negative_sample_rate does seem to be the key to improving recall.

So is the royal road to recall to keep on improving negative_sample_rate? Maybe, but increasing negative_sample_rate by an order of magnitude means we are already in the same ballpark of runtime as just running t-SNE, which removes a major advantage of UMAP.

Visually, what is the effect of using a much larger number of negatives? Here is a comparison of the tsne, umap, and pca+tumap-neg50 embeddings:

Code
recall_exp.plot(methods=["tsne", "umap", "pca+tumap-neg50"])

Overall the pca+tumap-neg50 resemble the tsne results more, but I also see some clusters being broken up, so it’s not always a very appealing look. This is the sort of thing you would expect with t-SNE without early exaggeration so it looks like UMAP suffers from this too. The default spectral embedding is supposed to approximate the result of early exaggeration so perhaps spectral initialization is the way to go with UMAP with larger values of negative_sample_rate? Let’s give it a go.

Code
recall_exp.add_method(
    "umap", params=dict(a=1, b=1, negative_sample_rate=50), name="tumap-neg50"
)
recall_exp.run()
[16:23:16] INFO     Running tumap-neg50 on iris                                                    experiment.py:58
[16:23:20] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running tumap-neg50 on penguins                                                experiment.py:58
[16:23:26] INFO     Running tumap-neg50 on s1k                                                     experiment.py:58
[16:23:36] INFO     Running tumap-neg50 on olivetti                                                experiment.py:58
[16:23:41] INFO     Running tumap-neg50 on frey                                                    experiment.py:58
[16:23:55] INFO     Running tumap-neg50 on coil20                                                  experiment.py:58
[16:24:07] INFO     Running tumap-neg50 on mnist                                                   experiment.py:58
[16:26:57] INFO     Running tumap-neg50 on fashion                                                 experiment.py:58
[16:29:55] INFO     Running tumap-neg50 on kuzushiji                                               experiment.py:58
[16:32:37] INFO     Running tumap-neg50 on isoswiss                                                experiment.py:58
[16:33:22] INFO     Running tumap-neg50 on scurvehole                                              experiment.py:58
[16:34:11] INFO     Running tumap-neg50 on mammoth                                                 experiment.py:58
[16:36:52] INFO     Running tumap-neg50 on macosko2015                                             experiment.py:58
[16:38:51] INFO     Running tumap-neg50 on tasic2018                                               experiment.py:58
/home/james/dev/drnb/venv/lib/python3.10/site-packages/sklearn/manifold/_spectral_embedding.py:274: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn(
[16:39:48] INFO     Running tumap-neg50 on lamanno2020                                             experiment.py:58
[16:40:09] INFO     Running tumap-neg50 on onet271                                                 experiment.py:58
[16:40:17] INFO     Running tumap-neg50 on avonet                                                  experiment.py:58
[16:40:38] INFO     Running tumap-neg50 on ng20                                                    experiment.py:58
[16:41:26] INFO     Running tumap-neg50 on iclr                                                    experiment.py:58
[16:41:54] INFO     Running tumap-neg50 on galaxy10sdss                                            experiment.py:58
Code
recall_exp.to_df(
    metrics=["nnp-15"],
    methods=[
        "tsne",
        "umap",
        "pca+tumap-neg50",
        "tumap-neg50",
    ],
).agg("mean")
method           metric
tsne             nnp-15    0.485054
umap             nnp-15    0.393566
pca+tumap-neg50  nnp-15    0.443335
tumap-neg50      nnp-15    0.442990
dtype: float64
Code
recall_exp.plot(
    methods=["pca+tumap-neg50", "tumap-neg50"],
    datasets=["mnist", "fashion", "kuzushiji", "galaxy10sdss"],
)

Hmm, some clusters stay together better, but some are still affected just in a different way. Getting to diminishing returns here, but before I give up, maybe we need to “anneal” the increase in negative sampling rate? I’ll split the default 200 epochs that UMAP uses into 4 steps of 50 each, increasing the negative sampling rate at each stage. Smaller datasets actually get 500 epochs by default, so that may also affect their recall, but I’m not expecting miracles with the recall here and I am more interested to see what the visual effect is on the larger datasets anyway.

Code
recall_exp.add_method(
    [
        pl.embedder("umap", params=dict(a=1, b=1, negative_sample_rate=5, n_epochs=50)),
        pl.embedder(
            "umap", params=dict(a=1, b=1, negative_sample_rate=10, n_epochs=50)
        ),
        pl.embedder(
            "umap", params=dict(a=1, b=1, negative_sample_rate=20, n_epochs=50)
        ),
        pl.embedder(
            "umap", params=dict(a=1, b=1, negative_sample_rate=50, n_epochs=50)
        ),
    ],
    name="tumap-neg50-anneal",
)
Code
recall_exp.run()
[16:43:07] INFO     Running tumap-neg50-anneal on iris                                             experiment.py:58
[16:43:16] WARNING  151 nearest neighbors requested but only 150 items are available                  nbrpres.py:56
           INFO     Running tumap-neg50-anneal on penguins                                         experiment.py:58
[16:43:29] INFO     Running tumap-neg50-anneal on s1k                                              experiment.py:58
[16:43:44] INFO     Running tumap-neg50-anneal on olivetti                                         experiment.py:58
[16:43:57] INFO     Running tumap-neg50-anneal on frey                                             experiment.py:58
[16:44:12] INFO     Running tumap-neg50-anneal on coil20                                           experiment.py:58
[16:44:28] INFO     Running tumap-neg50-anneal on mnist                                            experiment.py:58
[16:45:55] INFO     Running tumap-neg50-anneal on fashion                                          experiment.py:58
[16:47:13] INFO     Running tumap-neg50-anneal on kuzushiji                                        experiment.py:58
[16:48:21] INFO     Running tumap-neg50-anneal on isoswiss                                         experiment.py:58
[16:48:47] INFO     Running tumap-neg50-anneal on scurvehole                                       experiment.py:58
[16:49:03] INFO     Running tumap-neg50-anneal on mammoth                                          experiment.py:58
[16:50:41] INFO     Running tumap-neg50-anneal on macosko2015                                      experiment.py:58
[16:51:35] INFO     Running tumap-neg50-anneal on tasic2018                                        experiment.py:58
/home/james/dev/drnb/venv/lib/python3.10/site-packages/sklearn/manifold/_spectral_embedding.py:274: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn(
[16:52:12] INFO     Running tumap-neg50-anneal on lamanno2020                                      experiment.py:58
[16:52:26] INFO     Running tumap-neg50-anneal on onet271                                          experiment.py:58
[16:52:36] INFO     Running tumap-neg50-anneal on avonet                                           experiment.py:58
[16:53:07] INFO     Running tumap-neg50-anneal on ng20                                             experiment.py:58
[16:53:47] INFO     Running tumap-neg50-anneal on iclr                                             experiment.py:58
[16:54:21] INFO     Running tumap-neg50-anneal on galaxy10sdss                                     experiment.py:58
Code
recall_exp.to_df(
    metrics=["nnp-15"],
    methods=[
        "tsne",
        "umap",
        "tumap-neg50",
        "tumap-neg50-anneal",
    ],
).agg("mean")
method              metric
tsne                nnp-15    0.485054
umap                nnp-15    0.393566
tumap-neg50         nnp-15    0.442990
tumap-neg50-anneal  nnp-15    0.396644
dtype: float64

Ok, well the results don’t seem great based on the mean recall: they are noticeably worse. What do the visualizations look like?

Code
recall_exp.plot(
    methods=["tumap-neg50", "tumap-neg50-anneal"],
    datasets=["mnist", "fashion", "kuzushiji", "galaxy10sdss"],
)

I think they look slightly better with annealing, but the recall values don’t seem to agree:

Code
recall_exp.to_df(
    metrics=["nnp-15"],
    methods=[
        "tsne",
        "umap",
        "tumap-neg50",
        "tumap-neg50-anneal",
    ],
)
method tsne umap tumap-neg50 tumap-neg50-anneal
metric nnp-15 nnp-15 nnp-15 nnp-15
iris 0.827556 0.746222 0.756444 0.729778
penguins 0.704678 0.649513 0.669006 0.619883
s1k 0.414867 0.366333 0.425 0.3802
olivetti 0.501833 0.514333 0.537 0.4785
frey 0.584054 0.531433 0.580085 0.542256
coil20 0.806343 0.752639 0.795926 0.743981
mnist 0.340116 0.116833 0.165105 0.139448
fashion 0.31651 0.128747 0.186104 0.156535
kuzushiji 0.333426 0.14882 0.204177 0.172146
isoswiss 0.79531 0.774383 0.794583 0.703517
scurvehole 0.815374 0.767898 0.804854 0.724769
mammoth 0.769836 0.643339 0.720311 0.606369
macosko2015 0.024574 0.017684 0.027321 0.021709
tasic2018 0.266837 0.161613 0.20457 0.177631
lamanno2020 0.393229 0.311861 0.366701 0.323654
onet271 0.497442 0.459183 0.497442 0.464528
avonet 0.573422 0.430884 0.528822 0.471529
ng20 0.240415 0.134989 0.219053 0.1766
iclr 0.246945 0.119074 0.20257 0.157032
galaxy10sdss 0.248308 0.095531 0.174723 0.142823

Results just seem lower across the board, not just for datasets where we spent fewer epochs optimizing than before. Are the recalls for 50 neighbors any different?

Code
recall_exp.to_df(
    metrics=["nnp-50"],
    methods=[
        "tsne",
        "umap",
        "tumap-neg50",
        "tumap-neg50-anneal",
    ],
)
method tsne umap tumap-neg50 tumap-neg50-anneal
metric nnp-50 nnp-50 nnp-50 nnp-50
iris 0.912 0.8796 0.823333 0.846267
penguins 0.789708 0.759415 0.748655 0.699883
s1k 0.45468 0.46666 0.46044 0.43526
olivetti 0.49165 0.5143 0.47095 0.4662
frey 0.566768 0.548712 0.548305 0.516234
coil20 0.722083 0.704569 0.695028 0.672097
mnist 0.325279 0.178148 0.22155 0.198403
fashion 0.334419 0.197999 0.254576 0.229204
kuzushiji 0.339807 0.218736 0.268043 0.24293
isoswiss 0.784139 0.787284 0.793898 0.69882
scurvehole 0.807434 0.77648 0.78892 0.703762
mammoth 0.769966 0.663365 0.687594 0.57821
macosko2015 0.042929 0.031705 0.038553 0.034635
tasic2018 0.385035 0.300099 0.333451 0.31388
lamanno2020 0.46934 0.4368 0.456566 0.429345
onet271 0.521123 0.526438 0.516174 0.496174
avonet 0.53945 0.467596 0.510967 0.485695
ng20 0.124909 0.097375 0.125673 0.112434
iclr 0.199575 0.146011 0.193087 0.16506
galaxy10sdss 0.254243 0.139906 0.220911 0.191357

A similar story. Oh well. Maybe there is a better annealing scheme, but I am not very motivated to find it at this point.