Lloyd is not the fastest algorithm for k-means for very long time. I am curious how e-means compares with more advanced algorithms such as BBD-tree based implementation. SMILE has a k-means implementation with both BBD-tree and llyod algorithm. BBD-tree approach is often 10x faster. Wonder if e-means is better than BBD-tree.
Maybe we combine the ideas of e-means and BBD-tree?
jlhawnyesterday at 9:44 PM
I wonder if this could be used anywhere as an alternative to Lloyd's algorithm. I have a hobby project which uses Lloyd's to "relax" an icosohedral equal area mesh on the surface of a sphere [1]. I'm going to try to swap it out with an adaptation of this and see if it's much better or if I can afford more iterations with this method.
Edit: tried it, and it’s definitely not better for this use case. e-means wins by needing fewer passes over a huge dataset, but relaxing a mesh on a sphere has no dataset: each Voronoi cell’s centroid can be computed directly from its geometry. Mini-batches just swap that for a noisy sampled estimate of the same thing. It has the same objective and the same optimum, so at best it matches Lloyd. In practice it was much slower and left the grid noisy, and with small sample budgets it even tangled the mesh.
I adapted the method to my IVF index (the code-daemon project) and achieved a 146x speedup on the GPU.
That's impressive!
leecarrahertoday at 2:43 PM
i would like to see comparisons to k-means++. k-means is rarely the bottleneck in most modern algorithms, instead it's the multiple runs or seeding of kmeans to determine a stable 'good' solution that takes the lion's share of processing time. If this avoids the km++ seeding process while achieving similarly accuracy and stability, then this is would be a great practical improvement.
gilfyoletoday at 11:29 AM
Thanks for this. I tried experimenting with e-means in retrieval engine im building, for that I had to port the CPU version from faiss to Rust
We use k-means to build the cells of our vector index, which today runs 5 Lloyd iterations on a sample of the data. So I ran a quick test on 500k LAION embeddings (768 dimensions, 4,096 cells) against our current code:
- Current setup of 5 iterations: about 4x faster, with slightly better recall at every probe count (for example 0.9518 vs 0.9473 at 32 probes) and fewer rows scanned per query
- Cells came out much more even. The biggest cell went from 6.6x the average size to 4.75x, and there were no empty cells (we had 7)
- With only 2 iterations it was about 10x faster and recall stayed close to what we have today
These numbers are from my mac. I'm rerunning on a Linux box with AVX-512 now. If it holds up, we'll try it in our index build
As far as I can tell this isn't particularly new, people have been doing minibatch k-means for a while, including with an EWA update. I assume the contribution here is that they tried to measure how much better it is and provided code? I'm not sure I agree with their claims though, because they didn't really measure the things that matter–sure, it's better than Lloyd's algorithm, but that's a very low bar.
If you are claiming that your algorithm is "highly parallelizable" and comes with "wall-clock speedups", then the actual metrics that matter here is not number of steps or even basic wall clock time as your size remains fixed, but how your algorithm scales as more data is present or parallelism is required. This algorithm splits up the minibatch across workers and has them compute local updates, then allreduces it. As the amount of data goes up, the comms become much more expensive (since you have to use a worse transport layer), which means that you're basically locked in a desperate attempt to do more local work before you send your partial update to everyone else. If you attempt this (not explored in the paper) you will find that the improvement in clustering by doing this extra work has diminishing returns–you only reduce the number of updates by a few steps (which is what drives your wall clock time). At scale you end up being heavily comms limited and not super parallel at all.
wood_spirityesterday at 8:01 PM
This looks awesome. Obvious in hindsight! The viz really explains it well.
Hope it gets added to scipy - it’s much more available in the envs many use than facebooks own faiss library.
shiandowyesterday at 11:05 PM
These kinds of algorithms tend to have equivalent descriptions as expectation maximisation or simulated annealing. I wonder if it's possible to understand their algorithm in that sense.
Though I suppose it may be as simple as expectation maximization with a friction term, since Sj = sj - a (sj - Sj).
Importantly they note that this update rule keeps the centroid within the convex hull, which is pretty important.
kingcauchytoday at 3:22 AM
Played around with changing our hierarchical balanced incremental clustering to this https://github.com/antflydb/antfly/pull/1607 bulk builds of existing indexes that need to be completely reindexed (like a shard split) were 2.5x faster, overall insertion times didn’t change that much because overall clustering incrementally as data is indexed is a small portion of the cpu time.
senderistatoday at 1:35 AM
The one remaining hyperparameter is of course k. I vaguely remember there was some rule of thumb for picking k like the "elbow of a curve", but what is the state of the art now for picking k?
Luke2642yesterday at 8:58 PM
This is a great example of why p=np is sometimes the wrong question. For this np hard problem approximate solutions are good enough, and if you get them 45x faster that's even better!
jdonaldsontoday at 4:43 AM
Here's one of my own efforts on density-aware locality-sensitive hashing for embeddings. It uses k-means at one stage, I'll give this approach a shot.
So this is a different clustering algorithm, of which there are many, maaaany?
That makes it... not very interesting, because it's not a drop-in replacement then, and potentially many other clustering methods would have worked in particular applications as well. Or does this one beat most of the others as well?
bashhq_ryesterday at 9:10 PM
so the epoch reduction is cool but the hyperparameter laws feel like they were fit post-hoc
detourdogtoday at 12:08 AM
Can someone explain in layman’s terms the contexts this is used?
hubertzhangtoday at 7:50 AM
what's the common use case for e-means/k-means nowadays?
robbienukestoday at 1:36 AM
What is most impressive about this excellent work is not just the staggering time savings but the robustness of the technique in its assignment of single centroids where legacy may create two due to artefacting or worse one centroid for two blobs. Their explanation that: mini-batches provide more noisy updates that are more likely to explore the optimization landscape and escape local minima makes perfect sense and just goes to show it is all not settled hash. Incredible work with real world benefits of real marginal utility.
coderenegadeyesterday at 9:57 PM
Very cool, saving this to play around with later.
arbor-grouptoday at 2:59 PM
[flagged]
novalyn_aitoday at 1:38 PM
[flagged]
yksaluntoday at 7:11 AM
[flagged]
xiaoliangliangtoday at 3:17 AM
I'd want to see final cluster quality, not just inertia, before treating "one-tenth the epochs" as a drop-in win. On badly imbalanced or overlapping clusters, Lloyd's often spends early iterations bouncing centroids around. If the interpolation mainly damps that motion, you can look converged on the objective while the partition is still poor. A plot of ARI or NMI versus epoch budget on messy data would make the claim a lot more convincing.
Thomashuettoday at 7:49 AM
Their example is terrible, there are clearly 3 clusters and trying to find 200 clusters doesn't make any sense.
They show their algorithm performs better on some degenerate case but what about when there are actually k clusters?
EDIT: actually I see that they have a section about "The k-means problem" and that's where I think they get it wrong. What they describe is not the problem itself but how to solve it. The real problem (for most people at least, they seem to have a different problem) is to find the clusters in a dataset where you already know that there are clusters and how many there are.