Swann Bessa
e-means
Meta FAIR École nationale des ponts et chaussées

Balanced e-means: clusters of equal size, at the speed of e-means

Balanced e-means is e-means with one extra number per cluster: a bias that pushes points away from clusters that are too full. The clusters come out the same size, the loss stays as low as k-means', and training costs about as much as plain e-means.

Try it in your browser

One run of Balanced e-means on 4,000 points in 2-D, with 5 clusters. One dense blob holds 45% of the points, so at the start one cluster is three times too big (orange bar). Each bias b moves every batch: it goes down for a cluster that is too full and up for one that is too empty, and the borders between the clusters move with it. By the end all five bars sit on the equal share.

Why equal sizes

Many uses of clustering need clusters of the same size. In an inverted-file search index (IVF), a query scans a few clusters; if some clusters are huge, those queries are slow, and the search time becomes unpredictable. The same need shows up when data is split across machines, and in self-supervised learning, where every cluster should be used equally.

Plain k-means does not care about sizes: dense regions get big clusters. Existing balanced methods fix that, but they assign points one at a time, which makes them slow at scale.

One bias per cluster

Balanced e-means gives each cluster \(j\) a bias \(b_j\). A point \(x\) goes to the cluster with the lowest biased cost:

\[ \|x - c_j\|^2 - b_j \]

A positive bias makes a cluster more attractive; a negative one acts as a penalty. After each mini-batch of \(B\) points, cluster \(j\) has received \(n_j\) of them, against an equal share of \(\bar c = B/k\). Its bias then moves toward that share:

\[ b_j \;\leftarrow\; b_j + u_{\mathrm{eff}}\,\frac{\bar c - n_j}{\bar c} \]

A cluster that got too many points loses some at the next batch; one that got too few gains some. The step \(u_{\mathrm{eff}}\) follows the scale of the squared distances, so a single default (\(u = 0.02\)) works on every dataset we tried. In optimization terms, the biases are the Lagrange multipliers of the constraint "all clusters have the same size", and this update is gradient ascent on them.

Two clusters: without bias the border is halfway; with biases it moves toward the overfull cluster
What a bias does to the border. (a) Without bias, the border between clusters A and B is halfway between their centres, and point x goes to the closer one, A, which is already too full. (b) A's bias drops to −0.6 and B's rises to +0.3. The border slides toward A, and x and the points in the hatched strip now go to B (biased cost 1.14 instead of 1.60).

Training has two phases. First, centroids and biases learn together, as in e-means (15 epochs by default). Then the centroids are frozen and only the biases keep moving, to polish the balance (10 epochs).

Four snapshots of a Balanced e-means run with the cluster sizes under each
The same toy run, four moments. Top: the cells each cluster owns under the biased cost. Bottom: cluster sizes against the equal share \(\bar c\) (dashed): orange is too full, green too empty, grey within 2.5%. The imbalance (\(\mathrm{IF}-1\), 0 when all sizes are equal) falls from 1.19 at the start to \(10^{-4}\) after polishing.

Try it

The Balanced e-means playground runs the method in your browser, next to Lloyd's k-means, on 2-D shapes: blob grids, checkerboards, moons, a spiral. Each panel shows the cells, the cluster sizes and the loss along training. On every shape in the menu, Balanced e-means ends both more balanced and with a lower loss than Lloyd on most seeds: Lloyd often parks two centroids on one blob and leaves another blob shared, and forcing equal sizes pulls the centroids out of that trap.

The Balanced e-means playground on a fine checkerboard
The playground on a fine checkerboard of 32 cells, 32 clusters. Balanced e-means (middle) gives every cell its own centroid; Lloyd (left) does not. Loss 32% lower, imbalance 1.018 against 1.143, and the largest cluster is 1.19 times the mean size against 1.86 for Lloyd. Open the playground →

Results

Balance at almost no cost in loss. At exact balance, the imbalance (\(\mathrm{IF}-1\)) is between \(5\times10^{-5}\) and \(6\times10^{-4}\), and the training loss is between 1.5% below and 0.9% above unbalanced Lloyd's, on DEEP, SIFT, SPACEV, SimSearchNet++ and DINO10B, from 1M to 1B vectors. Allowing a range of sizes instead of equal ones traces a trade-off curve that lies below those of the balanced baselines.

Sorted cluster sizes and loss against imbalance on SIFT
SIFT, 10M vectors, 65,536 clusters. Left: cluster sizes, sorted, divided by the mean, for different allowed size ranges, from exactly equal (dark) to loose (light), against Lloyd (dotted). Right: training loss relative to Lloyd against imbalance; lower left is better. The square is exact balance.

A better search index. For nearest-neighbour search we keep the balanced centroids but drop the biases when filling the index: each vector goes to its plain nearest centroid. We call this index BIVF. Across 30 settings (5 datasets, 1M to 1B vectors), BIVF has a higher Recall@10 than Lloyd every time, by +0.2 to +3.1 points, and it cuts the spread of the work per query by 67% to 95%. The search code itself does not change.

Spread of vectors scanned per query, Lloyd vs BIVF
Spread of the number of vectors scanned per query (standard deviation over mean), against the fraction of the database scanned, at 100M and 1B vectors. BIVF (blue) is 3 to 20 times steadier than Lloyd (red), so query time is predictable.

As fast as e-means. The only change to the assignment is one number added per cluster, and a whole mini-batch is still assigned at once. An epoch costs at most 10% more than an e-means epoch from 10M vectors on. On one H100 GPU, the full 25 epochs take under a second for 1M vectors and 16,384 clusters, 6 to 7 seconds for 10M and 65,536, and about 3.5 minutes for 100M and 262,144. The balanced baselines assign points one by one and could not be run at 1B.

Code

Balanced e-means is being merged into faiss, next to e-means (faiss/contrib/e_means). It runs on CPU or GPU, with PyTorch tensors (BalancedEmeans) or NumPy arrays (NumpyBalancedEmeans):

from faiss.contrib.e_means import BalancedEmeans

em = BalancedEmeans(d, k)               # 15 epochs + 10 of polishing
centroids, labels = em.train(x)
_, labels = em.assign(x)                # equal sizes: biases kept
_, labels = em.assign(x, biased=False)  # for a BIVF index

Citing this work

Balanced e-means is described in Appendix A of the e-means paper (arXiv:2611.09501):

@misc{bessa2026emeansoutperformingkmeansin, title={e-means: outperforming k-means (in one-tenth the epochs)}, author={Swann Bessa and Pierre-Emmanuel Mazaré and Gergely Szilvasy and Hugo Touvron and David Picard and Matthijs Douze and Hervé Jégou}, year={2026}, eprint={2611.09501}, archivePrefix={arXiv}, primaryClass={cs.LG}, url={https://arxiv.org/abs/2611.09501}, }