<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dimension Reduction on Jiang Yi(姜祎)'s Homepage</title><link>https://jiangyigithub.github.io/ai.github.io/tags/dimension-reduction/</link><description>Recent content in Dimension Reduction on Jiang Yi(姜祎)'s Homepage</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 08 Apr 2026 21:44:44 +0800</lastBuildDate><atom:link href="https://jiangyigithub.github.io/ai.github.io/tags/dimension-reduction/index.xml" rel="self" type="application/rss+xml"/><item><title>Notes on t-SNE</title><link>https://jiangyigithub.github.io/ai.github.io/p/notes-on-t-sne/</link><pubDate>Thu, 02 May 2024 13:13:12 +0800</pubDate><guid>https://jiangyigithub.github.io/ai.github.io/p/notes-on-t-sne/</guid><description>&lt;p&gt;This post introduces how to understand t-SNE.&lt;/p&gt;
&lt;h1 id="introduction"&gt;&lt;a href="#introduction" class="header-anchor"&gt;&lt;/a&gt;Introduction
&lt;/h1&gt;&lt;p&gt;t-SNE an dimension reduction algorithm, which projects high-dimensional data into low-dimensional space. Thus the algorithm can be used to visualize the data distribution.&lt;/p&gt;
&lt;p&gt;To understand how t-SNE works, we first review the SNE algorithms, then we introduce the t-SNE algorithm.&lt;/p&gt;
&lt;h1 id="sne"&gt;&lt;a href="#sne" class="header-anchor"&gt;&lt;/a&gt;SNE
&lt;/h1&gt;&lt;h2 id="method"&gt;&lt;a href="#method" class="header-anchor"&gt;&lt;/a&gt;Method
&lt;/h2&gt;&lt;p&gt;Stochastic Neighbor Embedding, or SNE, is the previous version of t-SNE.&lt;/p&gt;
&lt;p&gt;The basic idea behind SNE is that: &lt;em&gt;data points that are close in high-dimensional space should be close in lower-dimensional space too&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Formally speaking, given a data set $X\in\mathbb{R}^{D\times N}$ consisting of $N$ data points, with each data point lies in $D$ dimensional space. Our goal is to reduce the data points into $d&lt;&lt; D$ dimensional space $Y\in\mathbb{R}^{d\times N}$, that is, we seek to find a map $f:\mathbb{R}^{D\times N}\to \mathbb{R}^{d\times N}$ such that $f(X)=Y$. Usually, $d=2$ or $d=3$ for visualization use.&lt;/p&gt;
&lt;p&gt;SNE measures &amp;ldquo;close&amp;rdquo; in a probabilistic way. The similarity is represented by converting Euclidean distance between data points to condition probabilities:&lt;/p&gt;
$$ p_{j\mid i} = \frac{\exp\left(-\Vert\bm{x}_i-\bm{x}_j\Vert^2/(2\sigma_i^2)\right)}{\sum_{k\neq i}\exp\left(-\Vert\bm{x}_i-\bm{x}_k\Vert^2/(2\sigma_i^2)\right)} $$&lt;p&gt;the above equation can be interpreted as &lt;em&gt;the probability of point $\bm{x}_j$ being a neighbor of point $\bm{i}$ is proportional to the distance between them&lt;/em&gt;. $\sigma_i$ is the variance of the Gaussian distribution that is centered on data point $\bm{x}_i$. We introduce the method for determining $\sigma_i$ later.&lt;/p&gt;
&lt;p&gt;Similarly, we can construct a probability distribution $q$ based on $Y$.&lt;/p&gt;
$$ q_{j\mid i} = \frac{\exp\left(-\Vert\bm{x}_i-\bm{x}_j\Vert^2\right)}{\sum_{k\neq i}\exp\left(-\Vert\bm{x}_i-\bm{x}_k\Vert^2\right)} $$&lt;p&gt;where we set the variance as $1/\sqrt{2}$ following the original paper.&lt;/p&gt;
&lt;p&gt;$p_{i\mid i}$ and $q_{i\mid i}$ are set $0$ since we are only interested in modeling pairwise similarities.&lt;/p&gt;
&lt;p&gt;Now we want $q_{j\mid i}$ are as close as $p_{j\mid i}$, that is, we want two distributions are as close as to each other. This can be measured by &lt;strong&gt;Kullback- Leibler divergence&lt;/strong&gt;, which is written as:&lt;/p&gt;
$$ C(P, Q) = \sum_{i=1}^N\mathrm{KL}(P_i\Vert Q_i)=\sum_{i=1}^N\sum_{j=1}^N p_{j\mid i}\log \frac{p_{i\mid j}}{q_{i\mid j}} $$&lt;p&gt;where $P_i=[p_{1\mid i},\dots,p_{N\mid i}]\in\mathbb{R}^N$ and $Q_i=[q_{1\mid i},\dots,q_{N\mid i}]\in\mathbb{R}^N$.&lt;/p&gt;
&lt;h2 id="choosing"&gt;&lt;a href="#choosing" class="header-anchor"&gt;&lt;/a&gt;Choosing $\sigma$
&lt;/h2&gt;&lt;p&gt;Now we introduce how to choose $\sigma$. Note that $\sigma$ determines the distribution of data points, larger $\sigma$ indicates sparser distribution of data points. The original paper uses &lt;em&gt;perplexity&lt;/em&gt; to measure such sparsity. It is defined as&lt;/p&gt;
$$ \mathrm{Perp}(P_i) = 2^{H(P_i)} $$&lt;p&gt;where $H(P_i)$ is the &lt;em&gt;Shannon entropy&lt;/em&gt; of $P_i$ measured in bits:&lt;/p&gt;
$$ H(P_i) = -\sum_{i=1}^N p_{j\mid i}\log p_{j\mid i} $$&lt;p&gt;The perplexity can be interpreted as a smooth measure of the effective number of neighbors. The performance of SNE is fairly robust to changes in the perplexity, and typical values are between 5 and 50.&lt;/p&gt;
&lt;p&gt;Notice that $p_{j\mid i}$, by setting different value on $\mathrm{Perp}(P_i)$, we can obtain different $\sigma_i$ via binary search.&lt;/p&gt;
&lt;h2 id="optimization"&gt;&lt;a href="#optimization" class="header-anchor"&gt;&lt;/a&gt;Optimization
&lt;/h2&gt;&lt;p&gt;Our goal now becomes minimizing $C(p, q)$ over variables $\bm{y}_1,\dots,\bm{y}_N\in\mathbb{R}^d$, given $p$ and hyperparameter $\sigma_i, i=1,\dots,N$. This can be done via gradient descent methods. The gradient is now given by&lt;/p&gt;
$$ \frac{d C}{d\bm{y}_i}=2\sum_{j=1}^N\left(p_{j\mid i} - q_{j\mid i} + p_{i\mid j}- q_{i\mid j} \right)(\bm{y}_i-\bm{y}_j)\in\mathbb{R}^d, \ i=1,\dots,N $$&lt;p&gt;We can write it in matrix form and add a momentum term:&lt;/p&gt;
$$ Y^{t+1} = Y^t + \beta\frac{dC}{dY} + \alpha_t\left(Y^{t-1}-Y^{t-2}\right) $$&lt;p&gt;where $\beta$ is the step size and $\alpha_t$ is momentum parameter,&lt;/p&gt;
$$ Y^t = [\bm{y}_1^t,\dots, \bm{y}_N^t]\in\mathbb{R}^{d\times N} ,\ \frac{dC}{dY} = \left[\frac{d C}{d\bm{y}_1},\dots,\frac{d C}{d\bm{y}_N}\right]\in\mathbb{R}^{d\times N} $$&lt;h1 id="t-sne"&gt;&lt;a href="#t-sne" class="header-anchor"&gt;&lt;/a&gt;t-SNE
&lt;/h1&gt;&lt;h2 id="symmetric-sne"&gt;&lt;a href="#symmetric-sne" class="header-anchor"&gt;&lt;/a&gt;Symmetric SNE
&lt;/h2&gt;&lt;p&gt;The first difference between t-SNE and SNE is the probability, t-SNE uses symmetric version of SNE to simplify computations.&lt;/p&gt;
&lt;p&gt;Different from SNE, symmetric SNE uses a joint probability instead of a condition probability:&lt;/p&gt;
$$ C(P, Q) = \sum_{i=1}^N\mathrm{KL}(P\Vert Q)=\sum_{i=1}^N\sum_{j=1}^N p_{ij}\log \frac{p_{ij}}{q_{ij}} $$&lt;p&gt;where $p_{ij}$ and $q_{ij}$ are defined as&lt;/p&gt;
$$ p_{ij} = \frac{\exp\left(-\Vert\bm{x}_i-\bm{x}_j\Vert^2/(2\sigma^2)\right)}{\sum_{k\neq r}\exp\left(-\Vert\bm{x}_r-\bm{x}_k\Vert^2/(2\sigma^2)\right)},q_{ij} = \frac{\exp\left(-\Vert\bm{x}_i-\bm{x}_j\Vert^2\right)}{\sum_{k\neq r}\exp\left(-\Vert\bm{x}_r-\bm{x}_k\Vert^2\right)} $$&lt;p&gt;the problem if joint probability $p_{ij}$ is that if there is an outlier $\bm{x}_i$, then $p_{ij}$ will be extremely small for all $j$. This problem can be solved by defining $p_{ij}$ from the conditional probability $p_{i\mid j}$ and $p_{j\mid i}$&lt;/p&gt;
$$ p_{ij} = \frac{p_{j\mid i}+p_{i\mid j}}{2N} $$&lt;p&gt;This ensures that&lt;/p&gt;
$$ \sum_{j=1}^N p_{ij} &gt; \frac{1}{2N} $$&lt;p&gt;for all $\bm{x}_i$, in result, each data point makes a significant contribution to the cost function.&lt;/p&gt;
&lt;p&gt;In this case, the gradient of the cost function is now given by&lt;/p&gt;
$$ \frac{d C}{d\bm{y}_i}=4\sum_{j=1}^N\left(p_{ij} - q_{ij}\right)(\bm{y}_i-\bm{y}_j)\in\mathbb{R}^d, \ i=1,\dots,N $$&lt;h2 id="t-sne-1"&gt;&lt;a href="#t-sne-1" class="header-anchor"&gt;&lt;/a&gt;t-SNE
&lt;/h2&gt;&lt;p&gt;Experiments show that symmetric SNE seems to produce maps that are just as good as asymmetric SNE, and sometimes even a little better.&lt;/p&gt;
&lt;p&gt;However, there is a problem with SNE, that is, the &lt;em&gt;crowding problem&lt;/em&gt;, which manifests as a tendency for points in the low-dimensional space to be clustered too closely together, particularly in high-density regions of the data.&lt;/p&gt;
&lt;p&gt;The causes of the crowding problems are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Data points in high dimensional space tend to far from each other, which makes the distance information less useful.&lt;/li&gt;
&lt;li&gt;SNE aims to preserve the local structure of the data points, but it can struggle with non-linear relationships. The projected data points will be closed to each other due to this reason.&lt;/li&gt;
&lt;li&gt;The optimization algorithm used by SNE can get stuck in local minimum.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To alleviate the crowding problem, t-SNE is introduced in the following way:
&lt;em&gt;In the high-dimensional space, we convert distances into probabilities using a Gaussian distribution. In the low-dimensional map, we can use a probability distribution that has much heavier tails than a Gaussian to convert distances into probabilities.&lt;/em&gt;
This allows a moderate distance in the high-dimensional space to be faithfully modeled by a much larger distance in the map and, as a result, it eliminates the unwanted attractive forces between map points that represent moderately dissimilar data points.&lt;/p&gt;
&lt;p&gt;t-SNE uses student t-distribution in low-dimensional map:&lt;/p&gt;
$$ q_{ij} = \frac{\left(1+\Vert\bm{y}_i-\bm{y}_j\Vert^2\right)^{-1}}{\sum_{k\neq r}\left(1+\Vert\bm{y}_i-\bm{y}_j\Vert^2\right)^{-1}} $$&lt;p&gt;A Student t-distribution with a single degree of freedom is used, because it has the particularly nice property that $\left(1+\Vert\bm{y}_i-\bm{y}_j\Vert^2\right)^{-1}$ approaches an inverse square law for large pairwise distances $\Vert\bm{y}_i-\bm{y}_j\Vert$ in the low-dimensional map.&lt;/p&gt;
&lt;p&gt;Compared to Gaussian distribution, t-distribution is heavily tailed。&lt;/p&gt;
&lt;p&gt;A computationally convenient property of t-SNE is that it is much faster to evaluate the density of a point under a Student t-distribution than under a Gaussian because it does not involve an exponential, even though the Student t-distribution is equivalent to an infinite mixture of Gaussians with different variances.&lt;/p&gt;
&lt;p&gt;The gradient of t-SNE is given by&lt;/p&gt;
$$ \frac{d C}{d\bm{y}_i}=4\sum_{j=1}^N\left(p_{ij} - q_{ij}\right)(\bm{y}_i-\bm{y}_j)\left(1+\Vert\bm{y}_i-\bm{y}_j\Vert^2\right)^{-1}\in\mathbb{R}^d, \ i=1,\dots,N $$&lt;p&gt;The advantages of t-SNE gradients over SNE are given by:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The t-SNE gradient strongly repels dissimilar data points that are modeled by a small pairwise distance in the low-dimensional representation.&lt;/li&gt;
&lt;li&gt;Second, although t-SNE introduces strong repulsions between dissimilar data points that are modeled by small pairwise distances, these repulsions do not go to infinity.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The algorithm is given as follows:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://jiangyigithub.github.io/ai.github.io/p/notes-on-t-sne/t-SNE-algorithm.png"
width="1910"
height="1096"
loading="lazy"
alt="algorithm"
class="gallery-image"
data-flex-grow="174"
data-flex-basis="418px"
&gt;&lt;/p&gt;
&lt;h2 id="optimization-1"&gt;&lt;a href="#optimization-1" class="header-anchor"&gt;&lt;/a&gt;Optimization
&lt;/h2&gt;&lt;p&gt;There are some optimizations that can be used to improve performance of t-SNE:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Early compression, which is used to force the map points to stay close together at the start of the optimization&lt;/li&gt;
&lt;li&gt;Early exaggeration, which is used to multiply all of the pi j’s by, for example, 4, in the initial stages of the optimization&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id="implementation"&gt;&lt;a href="#implementation" class="header-anchor"&gt;&lt;/a&gt;Implementation
&lt;/h1&gt;&lt;h1 id="reference"&gt;&lt;a href="#reference" class="header-anchor"&gt;&lt;/a&gt;Reference
&lt;/h1&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="http://jmlr.org/papers/v9/vandermaaten08a.html" target="_blank" rel="noopener"
&gt;Visualizing Data using t-SNE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>