File size: 1,731 Bytes
d14d984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""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__)