Sub topic 11
Barnes-Hut and FFT approximations for large data
This chapter assumes you know the t-SNE objective and its gradient from the earlier chapters. You should also be comfortable with scikit-learn's fit_transform and with big-O notation.
Why exact t-SNE is slow
The t-SNE gradient has two parts. Attractive forces pull neighbouring points together. Repulsive forces push every pair of points apart. Computing the repulsion exactly means evaluating all N² pairs at every iteration, and the exact method also stores a dense N×N matrix. This becomes impractical beyond roughly 10,000 points.
Both approximations below keep the same objective. They only change how the gradient is computed.
Step one: sparse attractive forces
Most pairwise affinities are almost zero. Both approximations compute affinities only for each point's nearest neighbours, about 3 × perplexity of them, and treat the rest as zero. This makes the attractive part cost O(N) per iteration. The repulsive part is still the expensive one.
Barnes-Hut: group distant points
Barnes-Hut stores the 2-D embedding in a quadtree, a tree that repeatedly splits space into four cells. When a cell is far enough from a point, the algorithm replaces all the points in it with one centre of mass. A cell is "far enough" if its width divided by its distance from the point is below a threshold called angle (θ).
- Smaller
anglegives more accuracy and more computation. Atangle=0the result is exact. - Larger
anglegives faster but coarser repulsion. In scikit-learn the default is 0.5. - Cost drops to O(N log N) per iteration.
import time
from sklearn.datasets import load_digits
from sklearn.manifold import TSNE
X, y = load_digits(return_X_y=True)
for method, kwargs in [("exact", {}), ("barnes_hut", {"angle": 0.5})]:
start = time.perf_counter()
Z = TSNE(method=method, init="pca", learning_rate="auto",
random_state=0, **kwargs).fit_transform(X)
print(f"{method}: {time.perf_counter() - start:.1f}s")On 1,797 points the gap is already visible. It widens quickly as N grows. Scikit-learn's Barnes-Hut method supports at most three output dimensions.
FFT-accelerated interpolation
FIt-SNE goes further. It places a regular grid over the embedding and spreads each point's contribution onto the grid nodes by interpolation. The repulsive force is a convolution of the grid values with the t-kernel, and the fast Fourier transform computes that convolution quickly. The result is then interpolated back to the points. The cost per iteration is roughly O(N), which makes millions of points feasible.
from openTSNE import TSNE # pip install openTSNE
embedding = TSNE(negative_gradient_method="fft", n_jobs=4,
random_state=0).fit(X)openTSNE chooses FFT for large inputs by default. It also provides transform for placing new points, which scikit-learn's TSNE lacks.
Choosing a method
| Method | Typical size | Main knob |
|---|---|---|
| Exact | up to a few thousand points | none |
| Barnes-Hut | up to about 100,000 points | `angle` |
| FFT | hundreds of thousands and more | grid settings |
Exercise
Barnes-Hut t-SNE runs too slowly on your data. You raise angle from 0.5 to 0.8. What do you gain and what do you risk?
Show answer
You gain speed, because more distant cells are summarised by a single centre of mass. You risk less accurate repulsive forces, which can distort fine local structure. Compare trustworthiness at both settings.