Datasets:
Tasks:
Reinforcement Learning
Formats:
parquet
Size:
100M - 1B
Tags:
offline-reinforcement-learning
off-policy-evaluation
contextual-bandit
content-moderation
benchmark
License:
Download evaluate.py from thwalsh/moderators-playbook-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 6.41 kB
-
https://huggingface.co/datasets/thwalsh/moderators-playbook-benchmark/resolve/main/evaluate.py
- Command line
-
hf download hf://datasets/thwalsh/moderators-playbook-benchmark/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/thwalsh/moderators-playbook-benchmark/resolve/main/evaluate.py
6.41 kB
| #!/usr/bin/env python | |
| """One command that scores the two tasks of the Moderator's Playbook Benchmark. | |
| This is the entry point a consumer runs to reproduce the benchmark's headline. It wraps the grading | |
| scripts and changes no logic. | |
| python evaluate.py --root . # Task 1, in the release directory: the core tier's trace | |
| python evaluate.py --root . --reps 200 # the published number of repetitions, the default | |
| python evaluate.py --root . --holed 5 # Task 1's positivity variant: 5 settings per context never logged | |
| python evaluate.py --root . --task 2 # Task 2 on the core: violation rate, value and hypervolume | |
| TASK 1 IS A LOGGED BANDIT DRAWN FROM THE FULL GRID (deviation D16). In every repetition some seeds fit | |
| the outcome model, the greedy arm and the tilted logger; on the others every context logs ONE arm drawn | |
| from the logger, with its propensity recorded exactly. The truth is the target's full-information value | |
| on those same interactions, computed because every arm was run on every interaction, so an estimator's | |
| error is its off-policy error and nothing else. Handing every estimator every arm instead makes IPS, | |
| SNIPS and DR one number by algebra, which is why that grading is not the task. | |
| THE GRADED COMPOSITE IS `r_util + r_safe + |r_fid|` (deviation D17): the cohort gap's size, because the | |
| steering label sets its sign. Every objective is also graded alone, with an all-objectives sum beside | |
| them. `r_sys` is a deterministic count of forward passes that the core holds constant across its arms, | |
| so it carries no information about the action and is never part of a graded reward. | |
| READ THE INTERVALS. Every error is a mean over repetitions with a bootstrap interval for that mean, and | |
| every comparison between two estimators is a paired difference with its own interval. | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| HERE = Path(__file__).resolve().parent | |
| # The grading script ships in code/ beside this file, copied from the source tree at every release | |
| # refresh; the fallback is the path in the source tree, so a consumer working inside the repository | |
| # needs no copy. | |
| LOCAL = HERE / "code" / "ds4_ope_bandit.py" | |
| FALLBACK = Path("/sciclone/home/thwalsh/hypernets/scripts/ds4_ope_bandit.py") | |
| LOCAL2 = HERE / "code" / "ds4_task2_core.py" | |
| FALLBACK2 = Path("/sciclone/home/thwalsh/hypernets/scripts/ds4_task2_core.py") | |
| def main(): | |
| ap = argparse.ArgumentParser( | |
| description="Score off-policy estimators on Task 1 of the Moderator's Playbook Benchmark.") | |
| ap.add_argument("--root", required=True, | |
| help="the corpus root, the directory holding core/ and its trace.parquet; in the " | |
| "release that is the release directory itself") | |
| ap.add_argument("--reps", type=int, default=200, | |
| help="repetitions; each one redraws the fold and the logged actions") | |
| ap.add_argument("--out", default="", | |
| help="where to write the JSON (default: core/ope_bandit.json, core/ope_positivity.json " | |
| "or core/task2_core.json under --root)") | |
| ap.add_argument("--task", type=int, choices=(1, 2), default=1, | |
| help="1, off-policy evaluation (the default); 2, constrained selection on the core") | |
| ap.add_argument("--holed", type=int, default=0, | |
| help="Task 1's positivity variant: this many settings per context the logger never plays") | |
| a = ap.parse_args() | |
| if a.task == 2: | |
| g2 = LOCAL2 if LOCAL2.exists() else FALLBACK2 | |
| if not g2.exists(): | |
| sys.exit(f"[evaluate] cannot find the Task 2 script; looked at {LOCAL2} and {FALLBACK2}") | |
| print("[evaluate] running the Task 2 script's validation suite first") | |
| if subprocess.call([sys.executable, str(g2), "--selftest"]) != 0: | |
| sys.exit("[evaluate] the Task 2 script failed its own validation suite; no number is printed.") | |
| out = a.out or os.path.join(a.root, "core", "task2_core.json") | |
| rc = subprocess.call([sys.executable, str(g2), "--root", a.root, "--out", out]) | |
| if rc != 0 or not os.path.isfile(out): | |
| sys.exit(f"[evaluate] Task 2 exited {rc} or wrote no {out}; read its output above.") | |
| print(f"\n[evaluate] wrote {out}: violation rate, value and hypervolume for four baselines, each " | |
| f"with its interval") | |
| return | |
| grader = LOCAL if LOCAL.exists() else FALLBACK | |
| if not grader.exists(): | |
| sys.exit(f"[evaluate] cannot find the grading script; looked at {LOCAL} and {FALLBACK}") | |
| trace = os.path.join(a.root, "core", "trace.parquet") | |
| if not os.path.isfile(trace): | |
| sys.exit(f"[evaluate] {trace} does not exist. --root is the CORPUS root, the directory that " | |
| f"holds the tiers, not a tier and not a parquet file.") | |
| # Run the grader's own validation suite first. It carries planted-pass and planted-fail fixtures, | |
| # and a program that has not demonstrated it can fail is not evidence. | |
| print("[evaluate] running the grader's validation suite first") | |
| if subprocess.call([sys.executable, str(grader), "--selftest"]) != 0: | |
| sys.exit("[evaluate] the grader failed its own validation suite. Nothing downstream of it is " | |
| "trustworthy, and no number is printed.") | |
| out = a.out or os.path.join(a.root, "core", "ope_positivity.json" if a.holed else "ope_bandit.json") | |
| rc = subprocess.call([sys.executable, str(grader), "--root", a.root, "--reps", str(a.reps), | |
| "--out", out] + (["--holed", str(a.holed)] if a.holed else [])) | |
| if rc != 0: | |
| sys.exit(f"[evaluate] the grader exited {rc}. Read its output above; it refuses rather than " | |
| f"returning a plausible number.") | |
| if not os.path.isfile(out): | |
| sys.exit(f"[evaluate] the grader exited 0 and wrote no {out}. Treat that as a failure: an exit " | |
| f"code is not evidence, the artifact is.") | |
| with open(out) as f: | |
| d = json.load(f) | |
| print(f"\n[evaluate] wrote {out}: {d.get('reps')} repetitions under the loggers " | |
| f"{', '.join(sorted(d.get('loggers', {})))}") | |
| print("[evaluate] the truth is each target's value on the same logged interactions; read every " | |
| "error with its interval.") | |
| if __name__ == "__main__": | |
| main() | |