Reproduce

Reproduction

The repository (github.com/heldernoid/hmeans) contains the implementation (scikit-learn interface), the baselines, the experiment plans, the logged results, and the scripts that generate the tables and figures of the report and of this site from those logs.

Installation and usage

pip install "git+https://github.com/heldernoid/hmeans.git"                      # NumPy backend only (CPU)
pip install "hmeans[torch] @ git+https://github.com/heldernoid/hmeans.git"     # + PyTorch backend: CPU, Apple MPS, CUDA
pip install -e ".[torch,test]"       # from a checkout; ./setup.sh builds the full experiment environment
import numpy as np
from hmeans import HMeans, EMeans

X = np.random.rand(200_000, 64).astype("float32")            # numpy array or torch tensor
km = HMeans(n_clusters=4096, n_passes=5, random_state=0).fit(X)
km.cluster_centers_.shape, km.labels_.shape, km.inertia_       # (4096, 64), (200000,), float
km.predict(X[:10]); km.transform(X[:10]); km.score(X)         # labels, distances, negative SSE
HMeans(n_clusters=4096, backend="numpy").fit(X)                # force the NumPy backend
HMeans(n_clusters=4096, backend="torch", device="cuda").fit(X)
EMeans(n_clusters=4096, n_passes=5).fit(X)                     # e-means, same interface

HMeans follows the scikit-learn estimator API: fit, predict, fit_predict, transform, score, get_params and set_params, and it works inside a Pipeline; scikit-learn itself is optional. n_passes is the total number of passes over the data (3 is a fast setting, 5 the default). Two backends implement the same algorithm: backend="numpy" needs only NumPy and runs on the CPU through BLAS; backend="torch" runs on CPU, Apple MPS or CUDA; "auto" uses torch when it is installed. ema_frac="auto" selects the split between EMA passes and exact Hartigan passes from the points per cluster (0.4 below 24, 0.8 below 64, 1.0 above). The package version comes from git tags through setuptools-scm, so a tag v0.2.0 installs as 0.2.0 and later commits as 0.2.1.devN+g<hash>. EMeans is the faithful e-means reimplementation with the same interface. pytest tests runs the test suite; the torch tests skip when torch is absent.

Repository layout

hmeans/backend.pydevice-agnostic exact assignment (chunked matmul + argmin), distance-computation counting, greedy k-means++
hmeans/emeans_ref.pye-means reimplementation: the constants, laws, schedule and revival of the shipped faiss recipe
hmeans/baselines.pyLloyd (random or k-means++ init), Sculley mini-batch
hmeans/estimator.pyHMeans and EMeans, the scikit-learn style estimators and backend selection
hmeans/core_numpy.pyHMeansNumpy: the NumPy implementation of the final recipe
hmeans/laws.pye-means constants, laws and schedule, the adaptive pass split (shared by both backends)
hmeans/hmeans.pyHMeansCore: the torch implementation with every ablation switch
experiments/run.pyone configuration, one JSON line in experiments/results/<plan>.jsonl (commit, config, exact train and held-out MSE, time, operation counts, loss curve)
experiments/suite.pythe experiment plans; resumable, never overwrites
experiments/analyze.pyregenerates experiments/tables/*.md and experiments/figures/*.png
experiments/export_site_data.pywrites site/src/data/results.json, the only data source of this site
site/this site (Astro 5, static output); public/assets/core.js is the browser port of the three methods
paper/hmeans.pdfthe technical note (download; tectonic hmeans.tex rebuilds it)
REPORT.md, NOTES.mdthe report, and the investigation log with every idea and measurement in order
LICENSEMIT, with the third-party notice for the adapted e-means playground code

Regenerating the results

About six hours on an M3 Ultra. Each plan appends to its own results file and skips runs it has already logged, so an interrupted plan resumes with the same command.

for p in main v3 scaling scaling_v3 ablation_v3 kmeanspp deep deep_v3; do python experiments/suite.py $p mps; done
python experiments/suite.py cpu cpu; python experiments/suite.py cpu_v3 cpu
python experiments/analyze.py                 # tables and figures for REPORT.md
python experiments/export_site_data.py        # results.json for this site

main holds the baselines and the v1 recipe, v2 and v3 the later recipes, ablation_v3 the ablations of the final recipe, scaling the SIFT k sweep, cpu the CPU timings and the official faiss cross-check, deep the 10M-row run (3.6 GB download, optional).

Building the site

cd site && npm install && npm run build && node scripts/relativize.mjs   # dist/ is a plain static site

The build output has no absolute paths, so dist/ serves from a GitHub Pages project site, a sub-folder or a local folder. Formulas are rendered at build time with KaTeX; the playground and the charts are plain scripts with no dependencies.

Environment notes