Download skill_eval.py from reneeice/zils-eval-tools: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/datasets/reneeice/zils-eval-tools/resolve/main/skill_eval.py
- Command line
-
hf download hf://datasets/reneeice/zils-eval-tools/skill_eval.py
-
curl -L -o skill_eval.py https://huggingface.co/datasets/reneeice/zils-eval-tools/resolve/main/skill_eval.py
2.98 kB
| #!/usr/bin/env python3 | |
| """Calibrated skill for one or more eval outputs, same metric as the official 49.59. | |
| Usage: skill_eval.py BANK.jsonl eval1.jsonl[:name] eval2.jsonl[:name] ... | |
| """ | |
| import json, sys | |
| from collections import defaultdict | |
| import numpy as np | |
| BANK = sys.argv[1] | |
| REPS, CALIB = 120, 0.20 | |
| def key_order(kind, keys): | |
| if kind == "score": return sorted(keys, key=lambda k: int(k)) | |
| if kind == "noul": return ["false", "true"] | |
| return sorted(keys) | |
| def metric(P, y, kind): | |
| if kind == "score": | |
| K = P.shape[1]; cp = np.cumsum(P, axis=1)[:, :-1]; cy = np.cumsum(np.eye(K)[y], axis=1)[:, :-1] | |
| return ((cp - cy) ** 2).sum(-1) / (K - 1) | |
| Y = np.zeros_like(P); Y[np.arange(len(y)), y] = 1.0 | |
| return ((P - Y) ** 2).sum(-1) | |
| def scale(z, T): | |
| z = z / T; z = z - z.max(1, keepdims=True); e = np.exp(z); return e / e.sum(1, keepdims=True) | |
| def fit(fams, calib): | |
| cache = [(np.log(np.clip(fams[k][0][i], 1e-30, None)), fams[k][1][i], k[1]) for k, i in calib.items() if len(i)] | |
| def loss(T): | |
| tot = n = 0 | |
| for lp, y, kind in cache: | |
| tot += metric(scale(lp, T), y, kind).sum(); n += len(y) | |
| return tot / n | |
| g = np.geomspace(0.05, 20, 160) | |
| return float(g[int(np.argmin([loss(t) for t in g]))]) | |
| def skill(fams, ev, T): | |
| cells = defaultdict(list) | |
| for k, i in ev.items(): | |
| if not len(i): continue | |
| P, y = fams[k]; kind = k[1] | |
| b = np.bincount(y, minlength=P.shape[1]).astype(float); b = (b + 1e-9) / (b.sum() + 1e-9 * P.shape[1]) | |
| base = metric(np.repeat(b[None, :], len(y), 0), y, kind).mean() | |
| if base <= 1e-12: continue | |
| Q = scale(np.log(np.clip(P[i], 1e-30, None)), T) | |
| cells[k[0]].append(1 - metric(Q, y[i], kind).mean() / base) | |
| return float(np.mean([np.mean(v) for v in cells.values()])) | |
| cases = {json.loads(l)["id"]: json.loads(l) for l in open(BANK) if l.strip()} | |
| for spec in sys.argv[2:]: | |
| path, _, name = spec.partition(":") | |
| buckets = defaultdict(list) | |
| for line in open(path): | |
| if not line.strip(): continue | |
| r = json.loads(line); c = cases[r["id"]] | |
| kind = r["type"]; keys = key_order(kind, list(r["probabilities"])) | |
| P = np.array([r["probabilities"][k] for k in keys], float); y = keys.index(str(r["label"])) | |
| buckets[(c["family"], kind, len(keys))].append((P, y)) | |
| fams = {k: (np.stack([a for a, _ in v]), np.array([b for _, b in v])) for k, v in buckets.items()} | |
| ks = list(fams); sizes = {k: len(fams[k][1]) for k in ks} | |
| rng = np.random.default_rng(5); vals = [] | |
| for _ in range(REPS): | |
| cal, ev = {}, {} | |
| for k in ks: | |
| m = sizes[k]; kk = max(1, int(round(m * CALIB))); perm = rng.permutation(m) | |
| cal[k] = np.sort(perm[:kk]); ev[k] = np.sort(perm[kk:]) | |
| vals.append(skill(fams, ev, fit(fams, cal))) | |
| a = np.array(vals) | |
| print(f"{name or path:24} skill={a.mean():.4f} sd={a.std(ddof=1):.4f} pts={100*a.mean():.2f}") | |