Download inference.py from thelamapi/Next-Ask: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/thelamapi/Next-Ask/resolve/main/inference.py
- Command line
-
hf download hf://thelamapi/Next-Ask/inference.py
-
curl -L -o inference.py https://huggingface.co/thelamapi/Next-Ask/resolve/main/inference.py
1.73 kB
| """Next Ask inference: predict digits and choose the next examples to label. | |
| Usage: | |
| python inference.py --selftest | |
| from inference import load, predict, next_queries | |
| """ | |
| import json | |
| import os | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, HERE) | |
| import numpy as np # noqa: E402 | |
| import ask_core as ak # noqa: E402 | |
| from nlrt import load_model # noqa: E402 | |
| def load(path=os.path.join(HERE, "model.json")): | |
| return load_model(path) | |
| def predict(spec, pixels): | |
| """pixels: (N, 64) ints 0..16 (8x8 digits). Returns class probabilities (N, 10).""" | |
| return ak.proba(spec["tensors"]["W"], ak.features(pixels)) | |
| def next_queries(spec, pool_pixels, labeled, labels, strategy="hybrid", batch=5, seed=0): | |
| """Retrain on the labeled pool items and return the next `batch` pool indices to label.""" | |
| X = ak.features(pool_pixels) | |
| W = ak.train(X[labeled], np.asarray(labels), **spec["meta"]["hyper"]) | |
| return ak.select(strategy, W, X, labeled, batch, seed) | |
| def _selftest(): | |
| spec = load() | |
| with open(os.path.join(HERE, "selftest.json"), encoding="utf-8") as f: | |
| t = json.load(f) | |
| P = predict(spec, t["pixels"]) | |
| diff = float(np.abs(P - np.asarray(t["expected_proba"])).max()) | |
| picks = next_queries(spec, t["pool_pixels"], t["labeled"], t["labels"], t["strategy"], t["batch"], t["seed"]) | |
| ok = diff < 1e-6 and picks == t["expected_picks"] | |
| print(json.dumps({"passed": bool(ok), "max_abs_diff": diff, "picks": picks, | |
| "expected_picks": t["expected_picks"], "n_samples": len(t["pixels"])})) | |
| return ok | |
| if __name__ == "__main__": | |
| if "--selftest" in sys.argv: | |
| sys.exit(0 if _selftest() else 1) | |
| print(__doc__) | |