Next-Federation / inference.py
Lamapi's picture
Publish Next-Federation model
759bd70 verified
Raw History Blame Contribute Delete
1.78 kB
"""Next Federation: load the released global model (FedProx) or rerun the federated simulation.
Usage:
python inference.py --selftest
from inference import load, predict
"""
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 fed_core as fc # noqa: E402
from nlrt import forward, 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. Returns class probabilities (N, 10) from the global model."""
z = forward(spec["nets"]["global"], np.asarray(pixels, dtype=float) / 16.0)
z = z - z.max(axis=1, keepdims=True)
e = np.exp(z)
return e / e.sum(axis=1, keepdims=True)
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())
# rerun a tiny federated simulation and compare with stored output
X = np.asarray(t["sim"]["pixels"], dtype=float) / 16.0
y = np.asarray(t["sim"]["y"])
parts = fc.partition(y.tolist(), 4, 0.5, 2)
gt = list(range(len(y)))
r = fc.run(X, y, parts, X[gt], y[gt], "fedprox", rounds=2, seed=2, **spec["meta"]["hyper"])
sim_diff = float(np.abs(np.asarray(r["curve"]) - np.asarray(t["sim"]["expected_curve"])).max())
ok = diff < 1e-6 and sim_diff < 1e-9
print(json.dumps({"passed": ok, "max_abs_diff": diff, "sim_curve_diff": sim_diff, "n_samples": len(t["pixels"])}))
return ok
if __name__ == "__main__":
if "--selftest" in sys.argv:
sys.exit(0 if _selftest() else 1)
print(__doc__)