Download benchmarks/proxy.py from Nabidnur/PISCES: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/Nabidnur/PISCES/resolve/main/benchmarks/proxy.py
- Command line
-
hf download hf://Nabidnur/PISCES/benchmarks/proxy.py
-
curl -L -o proxy.py https://huggingface.co/Nabidnur/PISCES/resolve/main/benchmarks/proxy.py
2.15 kB
| """Quick proxy: DEEP prefix, small k. Usage: python proxy.py DATA_DIR n k T seeds...""" | |
| import sys, time, json | |
| import numpy as np, faiss | |
| sys.path.insert(0, __file__.rsplit("/benchmarks", 1)[0]) | |
| from faiss.contrib.e_means import NumpyEmeans | |
| from aries.pemeans import PEmeans | |
| faiss.omp_set_num_threads(4) | |
| data, n, k, T = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), float(sys.argv[4]) | |
| seeds = [int(s) for s in sys.argv[5:]] | |
| x = np.ascontiguousarray(np.load(f"{data}/train_1M.npy", mmap_mode="r")[:n]) | |
| test = np.load(f"{data}/test.npy") | |
| def mse(c): | |
| D, I = faiss.knn(test, c, 1) | |
| diff = test.astype(np.float64) - c[I[:, 0]].astype(np.float64) | |
| return (diff * diff).sum() / len(test) | |
| V = {"official": lambda s: NumpyEmeans(x.shape[1], k, nepochs=T, seed=s, batch_law="throughput"), | |
| "PE(faiss,usage)=identity": lambda s: PEmeans(x.shape[1], k, nepochs=T, seed=s, batch_law="throughput", split="faiss", gain="usage"), | |
| "PE(principal,usage)": lambda s: PEmeans(x.shape[1], k, nepochs=T, seed=s, batch_law="throughput", split="principal", gain="usage"), | |
| "PE(faiss,greedy)": lambda s: PEmeans(x.shape[1], k, nepochs=T, seed=s, batch_law="throughput", split="faiss", gain="greedy"), | |
| "PE(principal,greedy)": lambda s: PEmeans(x.shape[1], k, nepochs=T, seed=s, batch_law="throughput", split="principal", gain="greedy"), | |
| "PE(principal,sample)": lambda s: PEmeans(x.shape[1], k, nepochs=T, seed=s, batch_law="throughput", split="principal", gain="sample")} | |
| res = {} | |
| for name, mk in V.items(): | |
| vals, secs = [], [] | |
| for s in seeds: | |
| init = x[np.random.RandomState(s).permutation(n)[:k]].copy() | |
| t = time.time(); km = mk(s); c, _ = km.train(x, init_centroids=init); secs.append(time.time() - t) | |
| vals.append(mse(c)) | |
| res[name] = vals | |
| print(f"{name:28s} test MSE {np.mean(vals):.5f} ± {np.std(vals):.5f} ({np.mean(secs):.1f}s) per-seed {[round(v,5) for v in vals]}", flush=True) | |
| base = np.array(res["official"]) | |
| for name, v in res.items(): | |
| d = 100 * (np.array(v) - base) / base | |
| print(f"{name:28s} paired change vs official: {d.mean():+.3f}% (wins {int((d<0).sum())}/{len(d)})") | |