drnb notebooks
  • Notebooks
  • GitHub
  1. Articles
  2. t-SNE Affinity: Gaussian vs. Uniform
  • 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
  1. Articles
  2. t-SNE Affinity: Gaussian vs. Uniform

Gaussian vs Uniform Affinities in t-SNE

  • Show All Code
  • Hide All Code

Usually affinities in the high-dimensional space in t-SNE are calculated using a Gaussian function: the further point \(j\) gets from point \(i\) the lower the affinity between \(i\) and \(j\). This ties into the calibration process in t-SNE where the parameter of the Gaussian has to be selected such that the perplexity of the probability distribution meets a certain value. The process goes: pick a target perplexity, then calculate the the \(k\)-nearest neighbor graph where \(k\) is set to be three times the target perplexity. For example, if you want a perplexity of 30, you use 90 nearest neighbors. To maintain sparsity, outside the \(k\)-nearest neighbors, the affinity is set to 0.

However, sometimes a simplification is used: pick \(k\)-nearest neighbors and set the affinity to 1 for those neighbors, and then 0 for everything else. So no perplexity, no Gaussian. This means the value of \(k\) can be much smaller than three times the value of the perplexity which provides a nice speed up.

The first paper to do this as far as I am aware was Yang and co-workers. Later, Böhm and co-workers explicitly compared Gaussian with what they called “binary affinities” for MNIST using both t-SNE and UMAP. They do not see much change in results in either case.

The question is what value of \(k\) to use for the k-nearest neigbors. The Böhm paper notes a rough equivalence between a perplexity of 30 and \(k\) set to 15.

Below we’ll try this for a few datasets, comparing perplexity of 30, with \(k\) = 15 (as recommended by Böhm and co-workers), \(k\) = 30 (in Laurens van der Maatens’ t-SNE FAQ he describes perplexity as “comparable with the number of nearest neighbors k that is employed in many manifold learners”) and \(k\) = 90 (the full 3 x perplexity value).

Code
import drnb.embed.pipeline as pl
from drnb.experiment import Experiment
Code
perplexity = 30
n_neighbors = 3 * perplexity

experiment = Experiment(
    name="tsne-affinity",
    evaluations=["rte", "rpc", ("nnp", dict(n_neighbors=[15, 50, 150]))],
)
Code
experiment.add_method(
    pl.embedder(
        "tsne",
        params={
            "perplexity": perplexity,
            "n_neighbors": n_neighbors,
            "random_state": 42,
            "n_jobs": 1,
        },
    ),
    name=f"tsne-p{perplexity}",
)

experiment.add_method(
    pl.embedder(
        "tsne",
        params={
            "affinity": "uniform",
            "n_neighbors": n_neighbors,
            "random_state": 42,
            "n_jobs": 1,
        },
    ),
    name=f"tsne-uniform-{n_neighbors}",
)

experiment.add_method(
    pl.embedder(
        "tsne",
        params={
            "affinity": "uniform",
            "n_neighbors": perplexity,
            "random_state": 42,
            "n_jobs": 1,
        },
    ),
    name=f"tsne-uniform-{perplexity}",
)

experiment.add_method(
    pl.embedder(
        "tsne",
        params={
            "affinity": "uniform",
            "n_neighbors": perplexity // 2,
            "random_state": 42,
            "n_jobs": 1,
        },
    ),
    name=f"tsne-uniform-{perplexity // 2}",
)
Code
datasets = [
    "s1k",
    "mammoth",
    "scurvehole",
    "isoswiss",
    "synthetic-hierarchical",
    "spheres",
    "coil20",
    "mnist",
    "fashion",
    "ng20pacmap",
    "macosko2015pca50",
]

experiment.add_datasets(datasets)
Code
experiment.run()
           INFO     Experiment tsne-affinity writing to                              __init__.py:169
                    /home/james/rdev/datasets/experiments/tsne-affinity                             
[14:02:49] INFO     Running tsne-p30 on s1k                                          __init__.py:235
[14:02:50] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:02:55] INFO     Running tsne-p30 on mammoth                                      __init__.py:235
[14:02:58] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:05:26] INFO     Running tsne-p30 on scurvehole                                   __init__.py:235
[14:05:27] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:06:11] INFO     Running tsne-p30 on isoswiss                                     __init__.py:235
[14:06:13] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:07:38] INFO     Running tsne-p30 on synthetic-hierarchical                       __init__.py:235
[14:07:41] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:09:28] INFO     Running tsne-p30 on spheres                                      __init__.py:235
[14:09:29] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:09:51] INFO     Running tsne-p30 on coil20                                       __init__.py:235
[14:09:54] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:10:01] INFO     Running tsne-p30 on mnist                                        __init__.py:235
[14:10:07] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:12:50] INFO     Running tsne-p30 on fashion                                      __init__.py:235
[14:12:57] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:15:37] INFO     Running tsne-p30 on ng20pacmap                                   __init__.py:235
[14:15:38] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:16:50] INFO     Running tsne-p30 on macosko2015pca50                             __init__.py:235
[14:16:51] WARNING  Failed to load precomputed neighbors: This file contains pickled external.py:365
                    (object) data. If you trust the file you can load it unsafely                   
                    using the `allow_pickle=` keyword argument or `pickle.load()`.                  
[14:17:11] WARNING  Running openTSNE.TSNE with params={'perplexity': 30,             external.py:365
                    'random_state': 42, 'n_jobs': 1}                                                
[14:18:47] INFO     Running tsne-uniform-90 on s1k                                   __init__.py:235
[14:18:48] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:18:52] INFO     Running tsne-uniform-90 on mammoth                               __init__.py:235
[14:18:54] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:21:06] INFO     Running tsne-uniform-90 on scurvehole                            __init__.py:235
[14:21:07] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:21:55] INFO     Running tsne-uniform-90 on isoswiss                              __init__.py:235
[14:21:56] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:22:57] INFO     Running tsne-uniform-90 on synthetic-hierarchical                __init__.py:235
[14:22:58] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
[14:22:59] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:24:44] INFO     Running tsne-uniform-90 on spheres                               __init__.py:235
[14:24:45] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:25:05] INFO     Running tsne-uniform-90 on coil20                                __init__.py:235
[14:25:07] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:25:13] INFO     Running tsne-uniform-90 on mnist                                 __init__.py:235
[14:25:16] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
[14:25:17] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:27:39] INFO     Running tsne-uniform-90 on fashion                               __init__.py:235
[14:27:43] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:30:11] INFO     Running tsne-uniform-90 on ng20pacmap                            __init__.py:235
[14:30:12] WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:30:54] INFO     Running tsne-uniform-90 on macosko2015pca50                      __init__.py:235
           WARNING  Failed to load precomputed neighbors: This file contains pickled external.py:365
                    (object) data. If you trust the file you can load it unsafely                   
                    using the `allow_pickle=` keyword argument or `pickle.load()`.                  
           WARNING  Calculating uniform affinities with n_neighbors = 90             external.py:365
[14:31:14] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:32:29] INFO     Running tsne-uniform-30 on s1k                                   __init__.py:235
[14:32:30] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:32:33] INFO     Running tsne-uniform-30 on mammoth                               __init__.py:235
[14:32:34] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
[14:32:35] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:34:33] INFO     Running tsne-uniform-30 on scurvehole                            __init__.py:235
[14:34:34] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:35:16] INFO     Running tsne-uniform-30 on isoswiss                              __init__.py:235
[14:35:17] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
[14:35:18] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:36:31] INFO     Running tsne-uniform-30 on synthetic-hierarchical                __init__.py:235
[14:36:32] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
[14:36:33] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:37:57] INFO     Running tsne-uniform-30 on spheres                               __init__.py:235
[14:37:58] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:38:13] INFO     Running tsne-uniform-30 on coil20                                __init__.py:235
[14:38:15] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:38:20] INFO     Running tsne-uniform-30 on mnist                                 __init__.py:235
[14:38:23] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
[14:38:24] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:40:16] INFO     Running tsne-uniform-30 on fashion                               __init__.py:235
[14:40:20] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:42:20] INFO     Running tsne-uniform-30 on ng20pacmap                            __init__.py:235
[14:42:21] WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:43:12] INFO     Running tsne-uniform-30 on macosko2015pca50                      __init__.py:235
[14:43:13] WARNING  Failed to load precomputed neighbors: This file contains pickled external.py:365
                    (object) data. If you trust the file you can load it unsafely                   
                    using the `allow_pickle=` keyword argument or `pickle.load()`.                  
           WARNING  Calculating uniform affinities with n_neighbors = 30             external.py:365
[14:43:22] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:44:22] INFO     Running tsne-uniform-15 on s1k                                   __init__.py:235
[14:44:23] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:44:27] INFO     Running tsne-uniform-15 on mammoth                               __init__.py:235
[14:44:28] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:46:29] INFO     Running tsne-uniform-15 on scurvehole                            __init__.py:235
[14:46:30] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:47:11] INFO     Running tsne-uniform-15 on isoswiss                              __init__.py:235
[14:47:12] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:48:38] INFO     Running tsne-uniform-15 on synthetic-hierarchical                __init__.py:235
[14:48:40] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:49:57] INFO     Running tsne-uniform-15 on spheres                               __init__.py:235
[14:49:58] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:50:14] INFO     Running tsne-uniform-15 on coil20                                __init__.py:235
[14:50:16] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:50:21] INFO     Running tsne-uniform-15 on mnist                                 __init__.py:235
[14:50:24] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:52:19] INFO     Running tsne-uniform-15 on fashion                               __init__.py:235
[14:52:22] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:54:14] INFO     Running tsne-uniform-15 on ng20pacmap                            __init__.py:235
[14:54:15] WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
           WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
[14:55:13] INFO     Running tsne-uniform-15 on macosko2015pca50                      __init__.py:235
[14:55:14] WARNING  Failed to load precomputed neighbors: This file contains pickled external.py:365
                    (object) data. If you trust the file you can load it unsafely                   
                    using the `allow_pickle=` keyword argument or `pickle.load()`.                  
           WARNING  Calculating uniform affinities with n_neighbors = 15             external.py:365
[14:55:20] WARNING  Running openTSNE.TSNE with params={'random_state': 42, 'n_jobs': external.py:365
                    1}                                                                              
Code
experiment.plot()

The uniform-90 result looks nothing like the perplexity-30 results, which is not really a surprise as most of the 90 neighbors have negligible weight on them in the perplexity-30 setting. Although it does seem like a much higher perplexity would do scurvehole and mammoth some good. Meanwhile, the uniform-15 and uniform-30 results are a lot more similar. The uniform-15 results do seem to be a bit closer to the perplexity-30 results than uniform-30.

So there you have it. For t-SNE the smooth Gaussian distribution has no particular advantage over just using \(k\) = 15, with the latter requiring one sixth of the neighbors to be calculated and stored.

Also, this should make us skeptical of any attempts to mint new dimensionality reduction methods by focusing on different functional forms for the input affinities or rescaling pair-wise contributions or different symmetrization schemes: the weights needs to change substantially across a large number of neighbors to see a big difference.

Code
experiment.to_df(metrics=["nnp-15", "nnp-150"])
method tsne-p30 tsne-uniform-90 tsne-uniform-30 tsne-uniform-15
metric nnp-15 nnp-150 nnp-15 nnp-150 nnp-15 nnp-150 nnp-15 nnp-150
s1k 0.419933 0.481027 0.282467 0.54414 0.3878 0.514833 0.445133 0.48068
mammoth 0.77596 0.720178 0.631201 0.770992 0.724823 0.703005 0.780305 0.672176
scurvehole 0.819239 0.825025 0.811846 0.882232 0.791597 0.810075 0.798969 0.792066
isoswiss 0.79776 0.796456 0.81447 0.867869 0.774873 0.779322 0.783993 0.756205
synthetic-hierarchical 0.122467 0.427861 0.063558 0.479221 0.086619 0.456156 0.12456 0.432779
spheres 0.098567 0.226511 0.031527 0.248921 0.04794 0.241964 0.09304 0.224887
coil20 0.807361 0.539352 0.5425 0.676407 0.761019 0.521481 0.812731 0.509171
mnist 0.348847 0.334197 0.177685 0.360993 0.301099 0.356854 0.37043 0.340956
fashion 0.325383 0.365611 0.187772 0.395965 0.280806 0.386477 0.344018 0.369233
ng20pacmap 0.380173 0.315698 0.250288 0.357155 0.351159 0.345024 0.408108 0.326394
macosko2015pca50 0.228002 0.240894 0.101936 0.257514 0.172551 0.259523 0.223421 0.249517