Sub topic 9
The KL divergence objective
This chapter assumes you know how t-SNE builds high-dimensional affinities p_ij (with perplexity) and how the Student-t kernel gives low-dimensional affinities q_ij. Here we cover how t-SNE scores a layout and why that score shapes the plots.
What is being compared
t-SNE has two probability tables over pairs of points:
p_ij: how strongly points i and j are neighbours in the original space.q_ij: how strongly they are neighbours in the 2-D map, computed with the Student-t kernel.
Each table sums to 1 over all pairs. t-SNE moves the map points until q looks like p.
The objective
The mismatch is measured with the Kullback-Leibler (KL) divergence:
KL(P || Q) = sum over i != j of p_ij * log( p_ij / q_ij )KL is zero only when q_ij = p_ij for every pair. It is not symmetric, and that asymmetry matters.
Why the asymmetry shapes the plot
Look at each pair's cost term:
- If
p_ijis large andq_ijis small, thenlog(p_ij / q_ij)is large. Two true neighbours placed far apart are penalised heavily. - If
p_ijis tiny andq_ijis large, the term is multiplied by a tinyp_ij. Two distant points placed close together are barely penalised.
So t-SNE preserves local structure well. It does not protect global structure. Distances between clusters and cluster sizes can change without raising the cost much, which is why you should not read them as meaningful.
Watching the objective
scikit-learn exposes the final KL value after fitting as kl_divergence_. The code below fits t-SNE for several perplexities and prints it.
from sklearn.datasets import load_digits
from sklearn.manifold import TSNE
X, y = load_digits(return_X_y=True)
for perp in [5, 30, 100]:
tsne = TSNE(perplexity=perp, init="pca", learning_rate="auto",
random_state=0)
tsne.fit_transform(X)
print(f"perplexity={perp:>3} KL = {tsne.kl_divergence_:.3f}")Optimising the cost
The cost is minimised by gradient descent. Each map point feels a pull toward the points it should be near and a push away from the others. The pull is strong where p_ij exceeds q_ij, and the push is strong where q_ij exceeds p_ij. The cost is non-convex, so different seeds or initialisations can reach different local minima. Initialisation, early exaggeration and learning rate, covered next, exist to manage that.
Exercise
Suppose two points are true neighbours (pij is large) but your map places them far apart (qij is small). Separately, two points are far apart in the original space (p_ij is tiny) but your map places them close together. Which error does the KL objective penalise more, and what does this imply for how much you should trust distances between clusters?
Show answer
The first error (neighbours placed far apart) is penalised much more, because log(pij / qij) is large and pij is large. The second error is multiplied by a tiny pij, so its cost is small. t-SNE therefore protects local neighbourhoods but not global layout, so distances between clusters in a t-SNE plot should not be trusted.