Beemer Claude Opus 4.7 commited on
Commit ·
df55f26
1
Parent(s): 8552318
Coordinate-descent tuning sweep over the four retrieval knobs
Browse filesStandalone harness that runs canlex.eval repeatedly via subprocess
with different CANLEX_MN_WEIGHT / CANLEX_MN_CAP / CANLEX_REG_PENALTY /
CANLEX_BACKMATTER_PENALTY values, varying one at a time while holding
the others at their current best. Picks each knob's value by Hit@5
with MRR as a tiebreak; writes a streaming log to data/eval/sweep.log
and a compact JSON summary to data/eval/sweep.json.
py -m canlex.sweep
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- canlex/sweep.py +131 -0
canlex/sweep.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Coordinate-descent tuning sweep for the four retrieval knobs.
|
| 2 |
+
|
| 3 |
+
Runs the 141-question eval repeatedly, varying one knob at a time while holding
|
| 4 |
+
the others at their current best. Picks the value that maximises Hit@5 (with
|
| 5 |
+
MRR as the tiebreak) and continues to the next knob. One pass through all four
|
| 6 |
+
knobs is usually enough to settle.
|
| 7 |
+
|
| 8 |
+
The knobs are read from env vars at index-load time (see canlex/index.py), so
|
| 9 |
+
each combination is exercised in a fresh subprocess of canlex.eval. Outputs go
|
| 10 |
+
to data/eval/sweep.log and a compact JSON summary to data/eval/sweep.json.
|
| 11 |
+
|
| 12 |
+
py -m canlex.sweep
|
| 13 |
+
"""
|
| 14 |
+
import json
|
| 15 |
+
import os
|
| 16 |
+
import re
|
| 17 |
+
import subprocess
|
| 18 |
+
import sys
|
| 19 |
+
import time
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
|
| 22 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 23 |
+
LOG = ROOT / "data" / "eval" / "sweep.log"
|
| 24 |
+
SUMMARY = ROOT / "data" / "eval" / "sweep.json"
|
| 25 |
+
|
| 26 |
+
# (env var, list of candidate values, current default). The defaults match the
|
| 27 |
+
# literals in canlex/index.py; values bracket each on a roughly geometric grid.
|
| 28 |
+
KNOBS = [
|
| 29 |
+
("CANLEX_MN_WEIGHT", [0.0012, 0.0024, 0.005, 0.01], 0.0024),
|
| 30 |
+
("CANLEX_MN_CAP", [0.006, 0.012, 0.024, 0.05], 0.012),
|
| 31 |
+
("CANLEX_REG_PENALTY", [0.004, 0.008, 0.016, 0.032], 0.008),
|
| 32 |
+
("CANLEX_BACKMATTER_PENALTY",[0.004, 0.008, 0.016, 0.032], 0.008),
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
_METRIC_RE = re.compile(
|
| 37 |
+
r"Hit@1:\s*([\d.]+).*?Hit@3:\s*([\d.]+).*?Hit@5:\s*([\d.]+).*?"
|
| 38 |
+
r"Hit@10:\s*([\d.]+).*?MRR:\s*([\d.]+)", re.S)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _run_eval(env_overrides: dict[str, float]) -> dict[str, float]:
|
| 42 |
+
"""Run canlex.eval once with the given env overrides; return metrics dict."""
|
| 43 |
+
env = dict(os.environ)
|
| 44 |
+
for k, v in env_overrides.items():
|
| 45 |
+
env[k] = f"{v}"
|
| 46 |
+
proc = subprocess.run(
|
| 47 |
+
[sys.executable, "-u", "-m", "canlex.eval"],
|
| 48 |
+
capture_output=True, text=True, env=env, cwd=ROOT,
|
| 49 |
+
)
|
| 50 |
+
if proc.returncode != 0:
|
| 51 |
+
raise RuntimeError(f"eval failed (exit {proc.returncode}):\n"
|
| 52 |
+
f"{proc.stderr[-800:]}")
|
| 53 |
+
m = _METRIC_RE.search(proc.stdout)
|
| 54 |
+
if not m:
|
| 55 |
+
raise RuntimeError(f"could not parse eval output:\n{proc.stdout[-800:]}")
|
| 56 |
+
h1, h3, h5, h10, mrr = (float(x) for x in m.groups())
|
| 57 |
+
n_misses = proc.stdout.count("miss(es)") # 0 if we end up at 100
|
| 58 |
+
return {"hit1": h1, "hit3": h3, "hit5": h5, "hit10": h10, "mrr": mrr,
|
| 59 |
+
"stdout": proc.stdout}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _score(metrics: dict[str, float]) -> tuple[float, float]:
|
| 63 |
+
"""Order by Hit@5 then MRR (both higher is better)."""
|
| 64 |
+
return (metrics["hit5"], metrics["mrr"])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def main():
|
| 68 |
+
LOG.parent.mkdir(parents=True, exist_ok=True)
|
| 69 |
+
log = LOG.open("w", encoding="utf-8")
|
| 70 |
+
log.write(f"# CanLex tuning sweep -- {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
| 71 |
+
print(f"# CanLex tuning sweep -- writing log to {LOG}\n")
|
| 72 |
+
|
| 73 |
+
current = {name: default for name, _values, default in KNOBS}
|
| 74 |
+
all_runs: list[dict] = []
|
| 75 |
+
|
| 76 |
+
# Baseline at current defaults.
|
| 77 |
+
print("Baseline:", current)
|
| 78 |
+
log.write(f"\nBaseline: {current}\n")
|
| 79 |
+
baseline = _run_eval(current)
|
| 80 |
+
print(f" Hit@5={baseline['hit5']:.3f} MRR={baseline['mrr']:.3f}")
|
| 81 |
+
log.write(f" Hit@5={baseline['hit5']:.3f} MRR={baseline['mrr']:.3f}\n")
|
| 82 |
+
all_runs.append({"values": dict(current), "metrics":
|
| 83 |
+
{k: baseline[k] for k in ("hit1", "hit3", "hit5", "hit10", "mrr")}})
|
| 84 |
+
best_metrics = baseline
|
| 85 |
+
|
| 86 |
+
for name, values, _default in KNOBS:
|
| 87 |
+
print(f"\nSweeping {name} in {values} (others held at {current})...")
|
| 88 |
+
log.write(f"\nSweeping {name} in {values} (others held at {current})\n")
|
| 89 |
+
local_best = (current[name], _score(best_metrics), best_metrics)
|
| 90 |
+
for v in values:
|
| 91 |
+
if v == current[name]:
|
| 92 |
+
# Re-use the already-measured baseline at this knob value.
|
| 93 |
+
metrics = best_metrics
|
| 94 |
+
else:
|
| 95 |
+
run_values = dict(current, **{name: v})
|
| 96 |
+
metrics = _run_eval(run_values)
|
| 97 |
+
row = (f" {name}={v!r:<8s} -> Hit@1={metrics['hit1']:.3f} "
|
| 98 |
+
f"Hit@3={metrics['hit3']:.3f} Hit@5={metrics['hit5']:.3f} "
|
| 99 |
+
f"Hit@10={metrics['hit10']:.3f} MRR={metrics['mrr']:.3f}")
|
| 100 |
+
print(row); log.write(row + "\n"); log.flush()
|
| 101 |
+
all_runs.append({"values": dict(current, **{name: v}),
|
| 102 |
+
"metrics": {k: metrics[k] for k in
|
| 103 |
+
("hit1", "hit3", "hit5", "hit10", "mrr")}})
|
| 104 |
+
score = _score(metrics)
|
| 105 |
+
if score > local_best[1]:
|
| 106 |
+
local_best = (v, score, metrics)
|
| 107 |
+
if local_best[0] != current[name]:
|
| 108 |
+
print(f" ! {name}: {current[name]} -> {local_best[0]} "
|
| 109 |
+
f"(Hit@5 {best_metrics['hit5']:.3f} -> {local_best[2]['hit5']:.3f})")
|
| 110 |
+
log.write(f" ! {name}: {current[name]} -> {local_best[0]}\n")
|
| 111 |
+
current[name] = local_best[0]
|
| 112 |
+
best_metrics = local_best[2]
|
| 113 |
+
|
| 114 |
+
print(f"\nBest: {current}")
|
| 115 |
+
print(f" Hit@1={best_metrics['hit1']:.3f} Hit@3={best_metrics['hit3']:.3f} "
|
| 116 |
+
f"Hit@5={best_metrics['hit5']:.3f} Hit@10={best_metrics['hit10']:.3f} "
|
| 117 |
+
f"MRR={best_metrics['mrr']:.3f}")
|
| 118 |
+
log.write(f"\nBest: {current}\n Hit@1={best_metrics['hit1']:.3f} "
|
| 119 |
+
f"Hit@5={best_metrics['hit5']:.3f} MRR={best_metrics['mrr']:.3f}\n")
|
| 120 |
+
log.close()
|
| 121 |
+
SUMMARY.write_text(json.dumps({
|
| 122 |
+
"best": current,
|
| 123 |
+
"best_metrics": {k: best_metrics[k] for k in
|
| 124 |
+
("hit1", "hit3", "hit5", "hit10", "mrr")},
|
| 125 |
+
"runs": all_runs,
|
| 126 |
+
}, indent=2), encoding="utf-8")
|
| 127 |
+
print(f"\nLog: {LOG}\nSummary: {SUMMARY}")
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
main()
|