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.
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.
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.
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).
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.
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.
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.
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.
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
Balanced e-means is described in Appendix A of the e-means paper (arXiv:2611.09501):