Dimensionality Reduction
t-Distributed Stochastic Neighbor Embedding (t-SNE)
2023-12-23
Table below is the summary of the comparison among key dimensional reduction algorithms.
| PCA | LLE/MDS | t-SNE | |
| | Linear | Nonlinear | Nonlinear |
| Mapping | Low dimensional representation to maximize the distribution of the samples. | Preserve the Euclidean distance between samples at the lower dimension. | Preserve the probability of the distance between samples at the lower dimension. |
The t-SNE is a major method for dimensionality reduction in especially very high dimensions. Its mapping aims to preserve the distribution of distances between samples in high dimensions in low dimensions as well. To achieve the goal, it uses the minimization of the difference (i.e., KL divergence) between the locally clustered Gaussian distribution in high dimensions and the Student’s t-distribution in low dimensions as the objective function.
Since it is very challenge to have good distribution estimation due to lack of enough sample size at a high-dimensional space (aka the curve of dimensionality), the dispersion is calculated from the predefined number of neighboring samples around each of the relevant samples. Here the number is set as a hyperparameter called Perplexity.
In other words, assuming that the distance||xi-xj||2 between each sample (xi) and the neighboring sample (xj) follows a normal distribution with variance σi2, the probability distribution of the distance is as follows.
\(P_{j|i}=\dfrac{\exp -\dfrac{\parallel x_i-x_j\parallel ^2}{2\sigma _i^2}}{\sum _{k\ne i}^{\ }\exp -\dfrac{\parallel x_i-x_k\parallel ^2}{2\sigma _i^2}}\)
Here, the variance must be iteratively calculated using the Bisection method to achieve the sum of Shannon Entropy for the sample, H(Pi), equal to the log2(Perplexity) value.
\(H(P_i)=-\sum _j^{\ }P_{j|i}\log _2(P_{j|i})\)
\(\text{Perplexity}(P_i)=2^{H(P_i)}\) 또는\(\log _2\text{Perplexity}(P_i)=H(P_i)\)
The plot below shows the relationship between the (1/2/variance) satisfying the given Perplexity and the sqrt(1/2/variance) where the variance is directly calculated from the distance between samples of the next perplexity count. It shows the statistical relevance of the Perplexity to the distribution.

Once the variances have been computed for each sample, we calculate the KL divergence from the distributions pij in the high-dimensional space (x) and the distributions qij in the low-dimensional space (y). We then use the gradient descent method using the derivative of the KL divergence to update the values of y.
\(p_{ij}=\dfrac{p_{i|j}+p_{j|i}}{2N},\text{ set }p_{ii}=0\)
\(q_{ij}=\dfrac{(1+\parallel y_i-y_j\parallel ^2)^{-1}}{\sum _k^{\ }\sum _{l\ne k}^{\ }(1+\parallel y_k-y_l\parallel ^2)^{-1}},\text{ set }q_{ii}=0\)
In the above Pij definition, pi|j represents the mean for each sample, so it was divided by N to ensure that the sum of probabilities for the entire sample is 1.
Using the definition of the KL divergence, the KL becomes
\(C=KL(P\parallel Q)=\sum _{i\ne j}^{\ }p_{ij}\log (p_{ij}/q_{ij})\)
It's derivative to yi becomes, (Refer Journal of Machine Learning Research 9 (2008) 2579-2605 for the proof)
\(\delta C/\delta y_i=4\sum _j^{\ }(p_{ij}-q_{ij})(y_i-y_j)(1+\parallel y_i-y_j\parallel ^2)^{-1}\)
By introducing d_ij,
\(d_{ij}\equiv (1+\parallel y_i-y_j\parallel ^2)^{-1}\) and \(\sum_j d_{ij} = d_i\)
The derivative to yi becomes
\(\delta C/\delta y_i=4y_i(p_i-q_i)d_i-4\left[(p_{ij}-q_{ij})d_{ij}\right]^Ty_j\)
where pi, qi, di, and y are the vectors of size n. There is one derivative per each dimension of y.
The y is updated using the learning rate η and the momentum α as follows.
\(y(t)=y(t-1)+\Delta y(t)\)
\(y(t)=\alpha(t)\Delta y(t-1)-\eta (t) \delta C/\delta y(t)\)
where the learning rate, η is assigned per dimension independently, and depending on the sign notation, it adjusts the scale as follows:
\(sign\left(\Delta y(t-1)\right)=\ sign\left(\delta C/\delta y(t)\right)\ \rightarrow \ \eta(t)=\eta(t-1)*0.85\)
\(sign\left(\Delta y(t-1)\right)\ne sign\left(\delta C/\delta y(t)\right)\ \rightarrow \ \eta(t)=\eta(t-1)+0.15\)
The moment α(t) is fixed at 0.5.
The steps to run t-SNE in Excel.
Step 0. Data preparation - We use the Iris dataset whose n=150, dimension = 4.
Step 1.1 Calculate the distance matrix ||xi-xj||2
Dij = SUMXMY2(Xi,Xj)
Use INDEX function to select Xi & Xj

Step 1.2 Assign an initial value to Vi from Vi=1/(2σi2), which will be updated later, to calculate the probability Pj|i between samples, and normalized for their sum to be 1.
Step 1.3. Calculate Shannon's Entropy H(Pi) and Perplexity.
\(H(P_i)=\text{SUM}(-P_{j|i}\text{LOG}(P_{j|i},2))\)
\(Perplexity=\text{POWER}(2,H(P_i))\)

Step 1.4. We need to find V for each sample such that it's Perplexity = Target, T.
Calculate the local Perplexity sensitivity at Vt-1, dP/dV and d2P/dV2, and use the following equation to update the Vt for Perplexity = T.
\(P_t=P_{t-1}+dP=P_{t-1}+\dfrac{dP}{dV}(V_t-V_{t-1})+\dfrac{d^2P}{dV^2}\dfrac{(V_t-V_{t-1})^2}{2}\)P
(a) Find the "Perplexity" number of the closest neighbors using the distance matrix, dij.
Depending on the Excel version, it can be done by using the Filter function or repeated using the SMALL function.
(b) Calculate the Variance of the neighbor's Dij and set initial Vi = sqrt (1/2/Variance) which is just an empirical guess.
(c) Compute the Compute Perplexity, P for the Vi
\(P_{j|i}=\dfrac{\exp -D_{ij}V_i }{\sum _{k\ne i}^{\ }\exp -D_{ik}V_i}\)
Here, the Vi must be iteratively calculated using the Bisection method to achieve the sum of Shannon Entropy for the sample, H(Pi), equal to the log2(Perplexity) value.
\(H(P_i)=-\sum _j^{\ }P_{j|i}\log _2(P_{j|i})\)
\(\text{Perplexity}(P_i)=2^{H(P_i)}\) or \(\log _2\text{Perplexity}(P_i)=H(P_i)\)
(d) Compute the P+ for Vi+1
\(P^+=\dfrac{\exp -D_{ij}(V_i+1) }{\sum _{k\ne i}^{\ }\exp -D_{ik}(V_i+1)}\)
(e) Compute the P- for Vi-1
\(P^-=\dfrac{\exp -D_{ij}(V_i-1) }{\sum _{k\ne i}^{\ }\exp -D_{ik}(V_i-1)}\)
(f) Compute the dV as follows:
\(dP/dV=(P^+-P^-)/2\equiv \alpha\)
\(d^2P/dV^2=(P^++P^--2P)\equiv \beta\)
\(V_i(t)=V_i(t-1)+dV\)
\(dV=-\alpha-\sqrt{\alpha^2-2\beta(P-T)}/\beta\)
(g) Repeat (c)~(f) 10 times. Take Pj|i for the final Vi.
Step 2. Calculate Pij
\(P_{ij}=(P_{j|i}+\dfrac{\text{TRANSPOSE}(P_{j|i}))}{2N}\)
Step 3. Initialize the lower dimensional space, Y1 and Y2 using RAND()
Step 4. Calculate qij
First calculate the d_ij \(d_{ij}=(1+\parallel y_i-y_j\parallel ^2)^{-1}\) and
\(q_{ij} = \dfrac {d_{ij}}{\bar{\bar {d_{ij}}}} \) Here, note that qij is normalized by the grand average such that sum of qij = 1 (vs. pj|i was normalized for each sample)
Step 5.1 Calculate zij = (pij-qij)dij and yi(pi-qi)di
For each sample i, calculate (pi-qi)di = SUM((pij-qij)dij), and
y1i(pi-qi)di and y2i(pi-qi)di using the two lower dimensional vectors.
Step 5.2 Calculate \(\sum_j \left[(p_{ij}-q_{ij})d_{ij}\right]y_j\) and δC/δyi
For each i, (pij-qij)dijyj = MMULT((pij-qij)dij, yj)
δC/δyi = 4*(yi(pi-qi)di-(pij-qij)dijyj)

Step 6. For each dimension of Y, set the learning rate initially to 1 and Δy = 0.

Step 7. Update the learning rate and the Δy with the moment.
η(t) = if(sign(Δy(t-1))=sign(dC/dy(t), η(t-1)*0.85, η(t-1)+0.15)
Δy(t) = α(t)*Δy(t-1) - 500*(max(0.01,η(t))*dC/dy(t) where MAX function employed to ensure the minimum learning rate > 0.01 .
Step 8. Update Y
y(t)= y(t-1) + Δy(t).
Repeat Step 4~8 1000 times using the Excel's Iterative Calculation option.
Following is an example of the results using Perplexity=20. Even though Iris is a simple dataset, but t-SNE demonstrates a decent lower dimensional representation.
