← LOGBOOK

I borrowed a speed trick from k-means. It made EM faster too, with nothing to tune.

7 min readmachine learningclusteringpre-registered

The short version

  • Computers often need to sort things into groups. One classic way is called k-means.
  • Last week, Meta's research lab made k-means about 10× faster (the paper). They call the new version e-means.
  • k-means has a cousin called EM. It does the same job, but it is more careful about points that sit between groups.
  • I copied Meta's trick into EM. I changed nothing in their settings.
  • It worked on all 15 tests. On big datasets it was 3 to 15 times faster. On small ones, it was not faster.

First: what are we even doing?

Imagine the Sorting Hat from Harry Potter. A new student sits down. The hat looks at them and picks a house.

k-means is a strict Sorting Hat. Every student goes into exactly one house. No maybes. Even a student who is half brave and half clever gets one answer.

EM is a more honest Sorting Hat. It says things like: "62% Ravenclaw, 35% Gryffindor, 3% Hufflepuff." That is more useful when a student sits on the border between two houses.

Try it. Move your cursor over the picture below. The white ring is a new student.

The Sorting Hat, two waysMove your cursor (or tap) anywhere. The white ring is a new student.

K-MEANS: HARD SORT

Hufflepuff

One house, no doubts. Even right on the border.

GAUSSIAN MIXTURE: SOFT SORT

Gryffindor0%
Ravenclaw6%
Hufflepuff92%
Slytherin2%

Every student gets a share of every house. Faded dots are the hard calls.

In real work, the "students" are data points. They might be football players' movement patterns, cells under a microscope, or sounds in a recording. The "houses" are groups the computer finds by itself.

The slow part

Both methods learn the houses by repeating two steps:

  1. Sort every student, using the current guess of what each house looks like.
  2. Update each house's description, based on who got sorted into it.

The old way does step 1 for every single student before it is allowed to do step 2. With a million students, that is a million checks before one tiny improvement.

Think of an election. The old way is like holding a full national vote every time you want to know how people feel. It is accurate. It is also very slow.

Meta's trick: the exit poll

TV news does not wait for every vote to be counted. It runs an exit poll: ask a small group, update the forecast, ask another small group, update again.

e-means does this in three moves:

  1. Small batches. Look at a few thousand points, update, move on.
  2. Fading memory. New batches count more than old ones. It is like your Spotify "On Repeat" playlist: recent listens push old favourites down.
  3. A recipe instead of guesswork. Exit polls have settings: how big each batch is, and how much to trust it. Normally people fiddle with these until things look right, like adjusting an old TV antenna. Meta measured the best settings once, at many sizes, and wrote them down as a formula. You type in the size of your problem, and the formula tells you the settings. It is like a cake recipe that tells you the oven time for any size of cake.

There is one more detail. Sometimes a group ends up with nobody in it. I call these ghosts. e-means fixes them with an "Endgame" move: like the end of Avengers: Endgame, the vanished come back. It takes the most crowded group and splits it in two, so the ghost gets a fresh start.

My question

Does the same recipe work for EM? Nobody had checked.

Here is the race. Both sides see the same data and start from the same guess. Both get the same amount of work per tick. The left side waits to ask everyone before each update. The right side uses the exit poll. Press play.

The race: ask everyone vs exit pollSame data, same starting guess, same amount of work per tick. Press play. Dashed rings are ghosts: groups that ended up with nobody.
work done 0%
Ask everyoneregular EM
fit score
0.07
updates
0
ghosts
0
Exit polle-EM
fit score
0.07
updates
0
ghosts
0
Fit score (higher is better) as work goes onAsk everyoneExit poll
Press play to draw the race

The recipe picked these for you: batch = 185 points, starting step = 0.86. Nothing was tuned.

Watch three things:

  • The exit poll's fit score climbs first. It makes many small updates while the other side is still counting.
  • The ghosts. Turn "Endgame revival" off, press "New data" a few times, and you will see groups die and stay dead.
  • The settings at the bottom. Nobody chose them. The recipe did.

How I tested it

I wrote down the rules before running anything. If the results came out badly, I could not quietly change the test afterwards. Scientists call this pre-registering.

  • What I compared: normal EM against EM with Meta's trick, which I call e-EM.
  • The settings: Meta's recipe, copied exactly. If I had needed to tweak it, the test would count as a fail.
  • The data: five public datasets, from 63,000 to about 1 million points, each split into 64, 256 and 1,024 groups. That makes 15 tests.
  • To pass, e-EM had to:
    • catch up with normal EM within 5 passes over the data;
    • end up at least as good;
    • leave fewer ghosts.

What happened

All three rules passed, on 15 out of 15 tests.

The real results, all 15 testsPick a size and what to show. Hover a row for the raw numbers.
Groups (k)Show
same speed
SIFT1M900k points
8.3×
GloVe-1001.07M points
4.4×
Covertype523k points
3.2×
Fashion-MNIST63k points
1.0×
MNIST63k points
1.0×

Big datasets: 3–15× faster. The small ones (63k points): about the same speed. That's the honest limit.

In plain words:

  • It caught up fast. Normal EM got 25 passes over the data. e-EM matched that score in 1 to 4 passes.
  • It ended slightly better, not just equal: about half a percent better on the typical test, even when normal EM got a smart head start.
  • It left almost no ghosts. In the biggest tests, normal EM left up to 53 empty groups. e-EM usually left zero.
  • It was 3 to 15 times faster on big data, between 500,000 and 1 million points.

The part I find most interesting: Meta made their recipe for k-means. It worked for EM without changing a single number. That suggests the recipe captures something general about learning from small batches, not something special about k-means.

Then I tried to break it

A result you only got once is a rumour. So I wrote down three more tests, again before running them, and ran them.

The follow-upsThree new tests. Pick one.
Run 1 (original)
15/15 caught up
15/15 ended as good
Run 2
15/15 caught up
14/15 ended as good
Run 3
14/15 caught up
13/15 ended as good

Each square is one test. Red squares are misses. Every miss was on the same dataset (Covertype, the big groups), and each one missed by less than half a percent.

1. Same test, three runs. Think of Groundhog Day: Phil lives the same day again and again, but small things change each time. I reran all 15 tests twice more, each time from a different random start.

  • It held. On every run, e-EM caught up within 5 passes on at least 14 of 15 tests, and ended at least as good on at least 13.
  • Every miss was on the same dataset, Covertype, with the biggest group counts. The misses were tiny: less than half a percent.

2. The time-based version. Normal EM treats every data point as separate. But a lot of real data comes in order: a heartbeat, a run, a football match. Think of Netflix. What you watch next depends on what you just watched. The time-aware cousin of EM is called a hidden Markov model. It learns the groups and the order they tend to come in: walking, then stairs, then sitting.

  • I tested it on two public datasets from phone and body sensors. One has everyday moves like walking and climbing stairs. The other has sports like rowing, cycling and basketball.
  • It worked, again with Meta's numbers unchanged. It reached the old method's score 4 to 7 times faster (about 3 seconds instead of about 18), and usually ended higher.
  • 2 of its 18 runs never quite caught up. That's on the chart too.

3. The fuller version. The first test used groups shaped like ellipses that line up with the axes. The fuller version lets each group tilt and stretch any way it likes.

  • On smooth, rounded data, like image features, word meanings and handwritten digits, it worked.
  • It broke in two places:
    • Covertype. Many of its columns are just yes or no, so some groups end up squashed flat, like a sheet of paper. Measuring how a flat sheet tilts is like measuring a sheet of paper's thickness with a ruler made for books. The fading memory gets the shape wrong.
    • Clothes photos with 64 groups. It ended clearly worse, on all three runs.

So now I know where the line is. The recipe travels well, to new runs and even to data that moves through time. It does not survive groups that are squashed flat. That's the next thing to fix.

What it doesn't show

  • No speed-up on small data. At 63,000 points it was about as fast as normal EM. The recipe already uses big batches there, so there is little to gain.
  • The fuller version has a real weak spot, described above. I am not claiming it works there.
  • The speed numbers are from one graphics card at a time. They leave out the time spent loading data.

Why I care

A lot of real analysis in medicine and sports still runs on these old, reliable methods. The slow part is often not the maths. It is a person fiddling with settings until the numbers stop jumping around. Tools that come with their settings already worked out, and that say clearly where they stop working, let people spend their time on patients and players instead.

Next: a fix for the squashed-flat groups, and using the time-based version on things I care about: heartbeats from athletes, and players moving on a pitch.