drnb notebooks
  • Notebooks
  • GitHub
  1. Articles
  2. Heavy-tailed UMAP
  • 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

  • Adding a precision
  • Heavy-tailed Neg-UMAP
  1. Articles
  2. Heavy-tailed UMAP

Heavy-tailed UMAP

  • Show All Code
  • Hide All Code
Code
import drnb.embed.pipeline as pl

Kobak and co-workers define a heavy-tailed kernel for t-SNE as:

\[ w_{ij} = 1/\left(1 + \frac{1}{\alpha} d_{ij}^2\right)^\alpha \]

\(\alpha = 1\) gives the usual t-SNE Cauchy kernel. \(\alpha < 1\) gives a progressively heavier tail which can reveal finer structure. \(\alpha > 1\) gives a less heavy tail approaching the Gaussian as \(\alpha \to \infty\).

The htumap embedder combines this kernel with UMAP (hijacking the a parameter to use to specify the heavy-tail parameter \(\alpha\)).

For deriving the gradient, here are some derivatives:

\[ \frac{\partial{d_{ij}^2}}{\partial{\mathbf{y_i}}} = 2 \left(\mathbf{y_i} - \mathbf{y_j}\right) \]

\[ \frac{\partial{w_{ij}}}{\partial{d_{ij}^2}} = -w_{ij}^{\left(\alpha + 1 \right) / \alpha} \]

The effective cost function for the attractive part of UMAP is:

\[ C_{\mathrm{UMAP}}^+ = -\log w_{ij} \]

\[ \frac{\partial{C_{\mathrm{UMAP}}^+}}{\partial{w_{ij}}} = -\frac{1}{w_{ij}} \]

giving the gradient:

\[ \frac{\partial{C_{\mathrm{UMAP}}^+}}{\partial{\mathbf{y_i}}} = 2 w_{ij}^{1/\alpha}\left(\mathbf{y_i} - \mathbf{y_j}\right) = \frac{2 \alpha}{\alpha + d_{ij}^2}\left(\mathbf{y_i} - \mathbf{y_j}\right) \]

For the repulsive part of the cost:

\[ C_{\mathrm{UMAP}}^- = \log \frac{1}{1-w_{ij}} = -\log \left(1 - w_{ij}\right) \]

\[ \frac{\partial{C_{\mathrm{UMAP}}^-}}{\partial{w_{ij}}} = \frac{1}{1-w_{ij}} \]

giving the gradient:

\[ \frac{\partial{C_{\mathrm{UMAP}}^-}}{\partial{\mathbf{y_i}}} = \frac{-2w_{ij}^{\left(\alpha + 1 \right) / \alpha}}{1 - w_{ij}}\left(\mathbf{y_i} - \mathbf{y_j}\right) \]

Adding a precision

In the same way a Gaussian can have a bandwidth/precision parameter to control its width, you can do the same for the heavy-tailed kernel:

\[ w_{ij} = 1/\left(1 + \frac{\beta}{\alpha} d_{ij}^2\right)^\alpha \\ \frac{\partial{w_{ij}}}{\partial{d_{ij}^2}} = -\beta w_{ij}^{\left(\alpha + 1 \right) / \alpha} \]

The gradients then become:

\[ \frac{\partial{C_{\mathrm{UMAP}}^+}}{\partial{\mathbf{y_i}}} = 2 \beta w_{ij}^{1/\alpha}\left(\mathbf{y_i} - \mathbf{y_j}\right) \\ \frac{\partial{C_{\mathrm{UMAP}}^-}}{\partial{\mathbf{y_i}}} = \frac{-2 \beta w_{ij}^{\left(\alpha + 1 \right) / \alpha}}{1 - w_{ij}}\left(\mathbf{y_i} - \mathbf{y_j}\right) \]

This doesn’t have any practical value unless you allow the precision to vary per point, e.g. to reflect the original density of the data.

Heavy-tailed Neg-UMAP

The (potentially) more numerically stable version of UMAP with a heavy-tailed kernel would look like:

\[ C_{\mathrm{negUMAP}}^+ = \log \left( \frac{w_{ij}}{\gamma + w_{ij}} \right) \\ \frac{\partial{C_{\mathrm{negUMAP}}^+}}{\partial{w_{ij}}} = -\frac{\gamma}{w_{ij} \left(\gamma + w_{ij}\right)} \\ C_{\mathrm{negUMAP}}^- = \log \left( \frac{1}{\gamma + w_{ij}} \right) \\ \frac{\partial{C_{\mathrm{negUMAP}}^-}}{\partial{w_{ij}}} = \frac{1}{\gamma + w_{ij}} \]

leading to these gradients:

\[ \frac{\partial{C_{\mathrm{negUMAP}}^+}}{\partial{\mathbf{y_i}}} = \frac{2 \gamma \beta w_{ij}^{1/\alpha}}{\gamma + w_{ij}} \left(\mathbf{y_i} - \mathbf{y_j}\right)\\ \frac{\partial{C_{\mathrm{negUMAP}}^-}}{\partial{\mathbf{y_i}}} = \frac{-2 \beta w_{ij}^{\left(\alpha + 1 \right) / \alpha}}{\gamma + w_{ij}}\left(\mathbf{y_i} - \mathbf{y_j}\right) \]

a=1 is \(\alpha=1\) and should give the standard Cauchy results with UMAP (i.e. t-UMAP):

Code
pl.standard_eval(method="htumap", params=dict(a=1), dataset="mnist")

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.6167228571428572), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=0.35601097345352173, info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.1082085714285714), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.17255028571428574), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.26018066666666667), info={'metric': 'euclidean', 'n_neighbors': 150})]

Here’s a result with a heavier tail:

Code
pl.standard_eval(method="htumap", params=dict(a=0.5), dataset="mnist")

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.6080485714285715), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=0.32316476106643677, info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.12454095238095238), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.18760000000000002), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.2710571428571429), info={'metric': 'euclidean', 'n_neighbors': 150})]

Note that some of the 8 cluster has got stuck with the 5 cluster (and some of the 5 is stuck with the three). Initializing with the UMAP result should help:

Code
pl.standard_eval(
    [
        pl.embedder("htumap", params=dict(a=1)),
        pl.embedder("htumap", params=dict(a=0.5)),
    ],
    dataset="mnist",
)

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.6047342857142857), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=0.3182254433631897, info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.12452285714285712), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.18834914285714288), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.27159133333333335), info={'metric': 'euclidean', 'n_neighbors': 150})]

Still some 5s stuck with the 3s, but the 8s are now better.

Setting a=1000 should give an SNE-like result:

Code
pl.standard_eval(method="htumap", params=dict(a=1000), dataset="mnist")

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.6356142857142858), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=0.407348096370697, info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.05922857142857143), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.10870457142857144), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.18225085714285713), info={'metric': 'euclidean', 'n_neighbors': 150})]