#!/usr/bin/env python3 """Score Decide Zero's published answers yourself: python3 score.py (standard library only). Each CSV beside this file holds one row per answer the served model gave, asked through its API: noul_*.csv P(true) for a yes/no question (reviews: "Is this review positive?", asked with and without true/false criteria; AG News: "Is this news article about ?") score_*.csv the answered level of five, the probability of each level and the expected position examples_*.csv BANKING77 answers with 1 / 3 / 5 examples per option sent in the request Accuracy is the share of answers on the right side of 0.5 (noul) or at the right level (score, choice). Calibration error (ECE) uses 10 equal-width bins of the model's confidence. """ import csv import glob import json import os HERE = os.path.dirname(os.path.abspath(__file__)) def ece(ok, conf): bins = [[] for _ in range(10)] for o, c in zip(ok, conf): bins[min(int(c * 10), 9)].append((o, c)) n = len(ok) return sum(abs(sum(o for o, _ in b) / len(b) - sum(c for _, c in b) / len(b)) * len(b) / n for b in bins if b) def noul(rows, truth, col): p = [float(r[col]) for r in rows] ok = [(v >= 0.5) == t for v, t in zip(p, truth)] return {"accuracy": round(100 * sum(ok) / len(ok), 2), "ece": round(ece(ok, [max(v, 1 - v) for v in p]), 3)} out = {} for path in sorted(glob.glob(os.path.join(HERE, "*.csv"))): name = os.path.basename(path)[:-4] rows = list(csv.DictReader(open(path, newline="", encoding="utf-8"))) if name in ("noul_sst2", "noul_imdb"): t = [r["positive"] == "1" for r in rows] out[name] = {"n": len(rows), "with_criteria": noul(rows, t, "p_true_with_criteria"), "instructions_only": noul(rows, t, "p_true_instructions_only")} elif name == "noul_agnews": out[name] = {"n": len(rows), **noul(rows, [r["true"] == "1" for r in rows], "p_true")} elif name.startswith("score_"): y = [int(r["level"]) for r in rows] a = [int(r["answered_level"]) for r in rows] e = [float(r["expected_position"]) for r in rows] conf = [max(json.loads(r["probabilities"])) for r in rows] ok = [u == v for u, v in zip(a, y)] out[name] = {"n": len(rows), "accuracy": round(100 * sum(ok) / len(ok), 2), "within_one": round(100 * sum(abs(u - v) <= 1 for u, v in zip(a, y)) / len(y), 2), "mae_expected_position": round(sum(abs(u - v) for u, v in zip(e, y)) / len(y), 3), "ece": round(ece(ok, conf), 3)} elif name.startswith("examples_"): ok = [r["answer"] == r["label"] for r in rows] out[name] = {"n": len(rows), "accuracy": round(100 * sum(ok) / len(ok), 2), "ece": round(ece(ok, [float(r["confidence"]) for r in rows]), 3)} print(json.dumps(out, indent=1))