Add learned dominance selector ablations
Browse files
workspace/scripts/eval_learned_dominance_selector.py
CHANGED
|
@@ -21,7 +21,7 @@ from cil.metrics import macro_micro_summary # noqa: E402
|
|
| 21 |
from scripts.eval_dominance_selector import _DominanceScorer, _chart_map, _rows # noqa: E402
|
| 22 |
|
| 23 |
|
| 24 |
-
|
| 25 |
"bias",
|
| 26 |
"candidate_score",
|
| 27 |
"candidate_score_minus_base_score",
|
|
@@ -33,6 +33,7 @@ FEATURE_NAMES = [
|
|
| 33 |
"tangent_linf_norm",
|
| 34 |
"num_candidates",
|
| 35 |
]
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def main(argv: list[str] | None = None) -> int:
|
|
@@ -53,6 +54,21 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 53 |
parser.add_argument("--out-dir", type=Path, default=Path("runs/ctt_learned_dominance_val_to_test"))
|
| 54 |
parser.add_argument("--k", type=int, default=8)
|
| 55 |
parser.add_argument("--ridge-lambdas", default="0,0.01,0.1,1,10,100")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
parser.add_argument("--bootstrap-samples", type=int, default=1000)
|
| 57 |
args = parser.parse_args(argv)
|
| 58 |
|
|
@@ -77,12 +93,16 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 77 |
calibration_charts,
|
| 78 |
scorer=scorer,
|
| 79 |
k=args.k,
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
eval_dataset = _candidate_dataset(
|
| 82 |
eval_rows,
|
| 83 |
eval_charts,
|
| 84 |
scorer=scorer,
|
| 85 |
k=args.k,
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
best = _fit_select_ridge(calibration_dataset, lambdas=lambdas)
|
| 88 |
eval_cases = _evaluate_dataset(eval_dataset, best["weights"], best["mean"], best["std"], tau=best["tau"])
|
|
@@ -116,7 +136,9 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 116 |
"report_type": "learned_dominance_selector_eval",
|
| 117 |
"schema_version": 1,
|
| 118 |
"k": args.k,
|
| 119 |
-
"
|
|
|
|
|
|
|
| 120 |
"ridge_lambdas": lambdas,
|
| 121 |
"selected_lambda": best["lambda"],
|
| 122 |
"tau": best["tau"],
|
|
@@ -181,6 +203,8 @@ def _candidate_dataset(
|
|
| 181 |
*,
|
| 182 |
scorer: _DominanceScorer,
|
| 183 |
k: int,
|
|
|
|
|
|
|
| 184 |
) -> dict[str, Any]:
|
| 185 |
samples: list[dict[str, Any]] = []
|
| 186 |
by_row: dict[int, list[int]] = defaultdict(list)
|
|
@@ -203,34 +227,34 @@ def _candidate_dataset(
|
|
| 203 |
tangents[candidate_index] if candidate_index < len(tangents) else [],
|
| 204 |
dtype=float,
|
| 205 |
)
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
_source_rank(candidate_types[candidate_index] if candidate_index < len(candidate_types) else ""),
|
| 217 |
-
tangent_rms,
|
| 218 |
-
tangent_linf,
|
| 219 |
-
float(len(scores)),
|
| 220 |
-
],
|
| 221 |
-
dtype=float,
|
| 222 |
)
|
| 223 |
sample_index = len(samples)
|
| 224 |
by_row[row_index].append(sample_index)
|
| 225 |
base_utility = float(row["base_utility"])
|
| 226 |
base_success = float(bool(row.get("base_success", False)))
|
| 227 |
hidden = [float(value) for value in row.get("hidden_chart_utilities", [])]
|
|
|
|
| 228 |
samples.append(
|
| 229 |
{
|
| 230 |
"row_index": row_index,
|
| 231 |
"candidate_index": candidate_index,
|
| 232 |
"feature": feature,
|
| 233 |
-
"target_margin":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
"candidate_utility": utilities[candidate_index],
|
| 235 |
"candidate_success": successes[candidate_index],
|
| 236 |
"base_utility": base_utility,
|
|
@@ -249,6 +273,74 @@ def _candidate_dataset(
|
|
| 249 |
return {"samples": samples, "by_row": dict(by_row), "num_rows": len(rows)}
|
| 250 |
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
def _fit_select_ridge(dataset: dict[str, Any], *, lambdas: list[float]) -> dict[str, Any]:
|
| 253 |
samples = dataset["samples"]
|
| 254 |
if not samples:
|
|
@@ -460,6 +552,8 @@ def _report(metrics: dict[str, Any]) -> str:
|
|
| 460 |
f"Eval rows: `{metrics['num_eval_rows']}`",
|
| 461 |
f"Selected ridge lambda: `{metrics['selected_lambda']}`",
|
| 462 |
f"Tau: `{metrics['tau']:.6f}`",
|
|
|
|
|
|
|
| 463 |
"",
|
| 464 |
"The ridge calibrator and threshold are fit on calibration measured rows only. Eval outcomes are used only for reporting.",
|
| 465 |
"",
|
|
|
|
| 21 |
from scripts.eval_dominance_selector import _DominanceScorer, _chart_map, _rows # noqa: E402
|
| 22 |
|
| 23 |
|
| 24 |
+
BASIC_FEATURE_NAMES = [
|
| 25 |
"bias",
|
| 26 |
"candidate_score",
|
| 27 |
"candidate_score_minus_base_score",
|
|
|
|
| 33 |
"tangent_linf_norm",
|
| 34 |
"num_candidates",
|
| 35 |
]
|
| 36 |
+
FEATURE_NAMES = BASIC_FEATURE_NAMES
|
| 37 |
|
| 38 |
|
| 39 |
def main(argv: list[str] | None = None) -> int:
|
|
|
|
| 54 |
parser.add_argument("--out-dir", type=Path, default=Path("runs/ctt_learned_dominance_val_to_test"))
|
| 55 |
parser.add_argument("--k", type=int, default=8)
|
| 56 |
parser.add_argument("--ridge-lambdas", default="0,0.01,0.1,1,10,100")
|
| 57 |
+
parser.add_argument(
|
| 58 |
+
"--feature-set",
|
| 59 |
+
choices=("basic", "tangent"),
|
| 60 |
+
default="basic",
|
| 61 |
+
help="Deployment-visible feature family for candidate-level dominance fitting.",
|
| 62 |
+
)
|
| 63 |
+
parser.add_argument(
|
| 64 |
+
"--target",
|
| 65 |
+
choices=("utility_margin", "success", "success_weighted_margin"),
|
| 66 |
+
default="utility_margin",
|
| 67 |
+
help=(
|
| 68 |
+
"Calibration target. success_weighted_margin fits utility margin plus "
|
| 69 |
+
"candidate success to prioritize the lexicographic success/progress utility."
|
| 70 |
+
),
|
| 71 |
+
)
|
| 72 |
parser.add_argument("--bootstrap-samples", type=int, default=1000)
|
| 73 |
args = parser.parse_args(argv)
|
| 74 |
|
|
|
|
| 93 |
calibration_charts,
|
| 94 |
scorer=scorer,
|
| 95 |
k=args.k,
|
| 96 |
+
feature_set=args.feature_set,
|
| 97 |
+
target=args.target,
|
| 98 |
)
|
| 99 |
eval_dataset = _candidate_dataset(
|
| 100 |
eval_rows,
|
| 101 |
eval_charts,
|
| 102 |
scorer=scorer,
|
| 103 |
k=args.k,
|
| 104 |
+
feature_set=args.feature_set,
|
| 105 |
+
target=args.target,
|
| 106 |
)
|
| 107 |
best = _fit_select_ridge(calibration_dataset, lambdas=lambdas)
|
| 108 |
eval_cases = _evaluate_dataset(eval_dataset, best["weights"], best["mean"], best["std"], tau=best["tau"])
|
|
|
|
| 136 |
"report_type": "learned_dominance_selector_eval",
|
| 137 |
"schema_version": 1,
|
| 138 |
"k": args.k,
|
| 139 |
+
"feature_set": args.feature_set,
|
| 140 |
+
"target": args.target,
|
| 141 |
+
"feature_names": _feature_names(args.feature_set),
|
| 142 |
"ridge_lambdas": lambdas,
|
| 143 |
"selected_lambda": best["lambda"],
|
| 144 |
"tau": best["tau"],
|
|
|
|
| 203 |
*,
|
| 204 |
scorer: _DominanceScorer,
|
| 205 |
k: int,
|
| 206 |
+
feature_set: str = "basic",
|
| 207 |
+
target: str = "utility_margin",
|
| 208 |
) -> dict[str, Any]:
|
| 209 |
samples: list[dict[str, Any]] = []
|
| 210 |
by_row: dict[int, list[int]] = defaultdict(list)
|
|
|
|
| 227 |
tangents[candidate_index] if candidate_index < len(tangents) else [],
|
| 228 |
dtype=float,
|
| 229 |
)
|
| 230 |
+
feature = _candidate_feature(
|
| 231 |
+
score=score,
|
| 232 |
+
base_score=base_score,
|
| 233 |
+
score_mean=score_mean,
|
| 234 |
+
score_std=score_std,
|
| 235 |
+
candidate_index=candidate_index,
|
| 236 |
+
candidate_type=candidate_types[candidate_index] if candidate_index < len(candidate_types) else "",
|
| 237 |
+
tangent=tangent,
|
| 238 |
+
num_candidates=len(scores),
|
| 239 |
+
feature_set=feature_set,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
)
|
| 241 |
sample_index = len(samples)
|
| 242 |
by_row[row_index].append(sample_index)
|
| 243 |
base_utility = float(row["base_utility"])
|
| 244 |
base_success = float(bool(row.get("base_success", False)))
|
| 245 |
hidden = [float(value) for value in row.get("hidden_chart_utilities", [])]
|
| 246 |
+
target_margin = utilities[candidate_index] - base_utility
|
| 247 |
samples.append(
|
| 248 |
{
|
| 249 |
"row_index": row_index,
|
| 250 |
"candidate_index": candidate_index,
|
| 251 |
"feature": feature,
|
| 252 |
+
"target_margin": _target_value(
|
| 253 |
+
target,
|
| 254 |
+
utility_margin=target_margin,
|
| 255 |
+
candidate_success=successes[candidate_index],
|
| 256 |
+
),
|
| 257 |
+
"measured_utility_margin": target_margin,
|
| 258 |
"candidate_utility": utilities[candidate_index],
|
| 259 |
"candidate_success": successes[candidate_index],
|
| 260 |
"base_utility": base_utility,
|
|
|
|
| 273 |
return {"samples": samples, "by_row": dict(by_row), "num_rows": len(rows)}
|
| 274 |
|
| 275 |
|
| 276 |
+
def _feature_names(feature_set: str) -> list[str]:
|
| 277 |
+
if feature_set == "basic":
|
| 278 |
+
return list(BASIC_FEATURE_NAMES)
|
| 279 |
+
if feature_set == "tangent":
|
| 280 |
+
return [
|
| 281 |
+
*BASIC_FEATURE_NAMES,
|
| 282 |
+
*[f"tangent_{index:02d}" for index in range(21)],
|
| 283 |
+
*[f"abs_tangent_{index:02d}" for index in range(21)],
|
| 284 |
+
]
|
| 285 |
+
raise ValueError(f"unknown feature_set: {feature_set}")
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
def _candidate_feature(
|
| 289 |
+
*,
|
| 290 |
+
score: float,
|
| 291 |
+
base_score: float,
|
| 292 |
+
score_mean: float,
|
| 293 |
+
score_std: float,
|
| 294 |
+
candidate_index: int,
|
| 295 |
+
candidate_type: Any,
|
| 296 |
+
tangent: np.ndarray,
|
| 297 |
+
num_candidates: int,
|
| 298 |
+
feature_set: str,
|
| 299 |
+
) -> np.ndarray:
|
| 300 |
+
tangent = np.asarray(tangent, dtype=float).reshape(-1)
|
| 301 |
+
if tangent.size < 21:
|
| 302 |
+
tangent = np.pad(tangent, (0, 21 - tangent.size))
|
| 303 |
+
elif tangent.size > 21:
|
| 304 |
+
tangent = tangent[:21]
|
| 305 |
+
tangent_rms = float(np.linalg.norm(tangent) / math.sqrt(max(1, tangent.size)))
|
| 306 |
+
tangent_linf = float(np.max(np.abs(tangent))) if tangent.size else 0.0
|
| 307 |
+
basic = np.asarray(
|
| 308 |
+
[
|
| 309 |
+
1.0,
|
| 310 |
+
float(score),
|
| 311 |
+
float(score) - float(base_score),
|
| 312 |
+
(float(score) - float(score_mean)) / float(score_std),
|
| 313 |
+
float(candidate_index),
|
| 314 |
+
float(candidate_index == 0),
|
| 315 |
+
_source_rank(candidate_type),
|
| 316 |
+
tangent_rms,
|
| 317 |
+
tangent_linf,
|
| 318 |
+
float(num_candidates),
|
| 319 |
+
],
|
| 320 |
+
dtype=float,
|
| 321 |
+
)
|
| 322 |
+
if feature_set == "basic":
|
| 323 |
+
return basic
|
| 324 |
+
if feature_set == "tangent":
|
| 325 |
+
return np.concatenate([basic, tangent.astype(float), np.abs(tangent).astype(float)])
|
| 326 |
+
raise ValueError(f"unknown feature_set: {feature_set}")
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def _target_value(
|
| 330 |
+
target: str,
|
| 331 |
+
*,
|
| 332 |
+
utility_margin: float,
|
| 333 |
+
candidate_success: float,
|
| 334 |
+
) -> float:
|
| 335 |
+
if target == "utility_margin":
|
| 336 |
+
return float(utility_margin)
|
| 337 |
+
if target == "success":
|
| 338 |
+
return float(candidate_success)
|
| 339 |
+
if target == "success_weighted_margin":
|
| 340 |
+
return float(utility_margin) + float(candidate_success)
|
| 341 |
+
raise ValueError(f"unknown target: {target}")
|
| 342 |
+
|
| 343 |
+
|
| 344 |
def _fit_select_ridge(dataset: dict[str, Any], *, lambdas: list[float]) -> dict[str, Any]:
|
| 345 |
samples = dataset["samples"]
|
| 346 |
if not samples:
|
|
|
|
| 552 |
f"Eval rows: `{metrics['num_eval_rows']}`",
|
| 553 |
f"Selected ridge lambda: `{metrics['selected_lambda']}`",
|
| 554 |
f"Tau: `{metrics['tau']:.6f}`",
|
| 555 |
+
f"Feature set: `{metrics['feature_set']}`",
|
| 556 |
+
f"Target: `{metrics['target']}`",
|
| 557 |
"",
|
| 558 |
"The ridge calibrator and threshold are fit on calibration measured rows only. Eval outcomes are used only for reporting.",
|
| 559 |
"",
|