Datasets:
drop select_tokens.py, superseded by choose_fire_set.py
Browse files- selection/select_tokens.py +0 -96
selection/select_tokens.py
DELETED
|
@@ -1,96 +0,0 @@
|
|
| 1 |
-
"""How the fire set for each program is chosen.
|
| 2 |
-
|
| 3 |
-
This is the whole of the "set logic" -- no LLM, no gradient, no search over
|
| 4 |
-
programs. Two functions:
|
| 5 |
-
|
| 6 |
-
token_optimum the EXACT best F1 achievable by any rule of the form
|
| 7 |
-
`token in T`, over all subsets T. Greedy by precision is
|
| 8 |
-
provably optimal here, so it runs in one sorted pass.
|
| 9 |
-
|
| 10 |
-
best_k_tokens the best set of AT MOST k tokens, by greedy marginal F1.
|
| 11 |
-
Under a size budget the objective changes and the optimum
|
| 12 |
-
above is no longer a valid starting point -- see the note
|
| 13 |
-
in the docstring. This is what actually produces the
|
| 14 |
-
50-token fire sets in programs/.
|
| 15 |
-
|
| 16 |
-
Both take `rows`: one dict per distinct token the feature ever fired on, with
|
| 17 |
-
token the token string
|
| 18 |
-
fires how many times the feature fired on it (in the train corpus)
|
| 19 |
-
appears how many times the token occurred at all (in the train corpus)
|
| 20 |
-
and `n_positives`, the feature's total firing count. `appears - fires` is the
|
| 21 |
-
token's false positives, which is why the corpus occurrence count is needed
|
| 22 |
-
and not just the firing count.
|
| 23 |
-
|
| 24 |
-
Extracted verbatim from lib/sae_synthesis.py in the source repo.
|
| 25 |
-
"""
|
| 26 |
-
from typing import List, Optional, Sequence, Tuple
|
| 27 |
-
|
| 28 |
-
import numpy as np
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def token_optimum(rows: List[dict], n_positives: int) -> Tuple[float, List[str]]:
|
| 33 |
-
"""Exact best F1 over all rules of the form `token in T`, T a set of strings.
|
| 34 |
-
|
| 35 |
-
NAMING. This was called `token_optimum`, which misleads in both
|
| 36 |
-
directions. It IS an upper bound, but only WITHIN the class of token-only
|
| 37 |
-
rules. For programs in general it is a LOWER bound — a program that reads
|
| 38 |
-
context can and does exceed it (one feature: token-optimum 0.048, a
|
| 39 |
-
context-reading program 0.674). Say "token optimum" or "best token-only
|
| 40 |
-
rule", never "the ceiling".
|
| 41 |
-
|
| 42 |
-
Greedy by precision is optimal: adding t to a set scoring f helps iff
|
| 43 |
-
prec(t) > f/2, and precision decreases along the sorted order, so the best
|
| 44 |
-
set is a prefix.
|
| 45 |
-
"""
|
| 46 |
-
if not rows or n_positives == 0:
|
| 47 |
-
return 0.0, []
|
| 48 |
-
order = sorted(rows, key=lambda r: -r["p_fire"])
|
| 49 |
-
tp = fp = 0
|
| 50 |
-
best_f1, best_k = 0.0, 0
|
| 51 |
-
for k, r in enumerate(order, 1):
|
| 52 |
-
tp += r["fires"]
|
| 53 |
-
fp += r["appears"] - r["fires"]
|
| 54 |
-
f1 = 2 * tp / (2 * tp + fp + (n_positives - tp)) if tp else 0.0
|
| 55 |
-
if f1 > best_f1:
|
| 56 |
-
best_f1, best_k = f1, k
|
| 57 |
-
return best_f1, [r["token"] for r in order[:best_k]]
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def best_k_tokens(rows: List[dict], n_positives: int, k: int) -> List[str]:
|
| 61 |
-
"""Best <= k token rule, by greedy marginal F1 gain.
|
| 62 |
-
|
| 63 |
-
Truncating the unconstrained optimum is NOT the answer. Greedy-by-precision
|
| 64 |
-
is optimal without a budget, but its prefix is dominated by rare tokens that
|
| 65 |
-
fire once or twice at precision 1.0 and carry almost no recall. Measured:
|
| 66 |
-
cutting 29,255 tokens to the top 4,000 by precision scored WORSE than an
|
| 67 |
-
arbitrary alphabetical 4,000 (median 0.103 vs 0.135 over 813 programs),
|
| 68 |
-
because the arbitrary slice at least sampled high-recall tokens.
|
| 69 |
-
|
| 70 |
-
Under a cardinality budget the objective changes, so solve that objective:
|
| 71 |
-
repeatedly add whichever token most increases F1. O(k * n) and exact-greedy
|
| 72 |
-
rather than a slice of someone else's optimum.
|
| 73 |
-
"""
|
| 74 |
-
if not rows or n_positives <= 0 or k <= 0:
|
| 75 |
-
return []
|
| 76 |
-
fires = np.array([r["fires"] for r in rows], dtype=np.float64)
|
| 77 |
-
appears = np.array([r["appears"] for r in rows], dtype=np.float64)
|
| 78 |
-
toks = [r["token"] for r in rows]
|
| 79 |
-
chosen: List[int] = []
|
| 80 |
-
avail = np.ones(len(rows), dtype=bool)
|
| 81 |
-
tp = fp = 0.0
|
| 82 |
-
best_f1 = 0.0
|
| 83 |
-
for _ in range(min(k, len(rows))):
|
| 84 |
-
ntp = tp + fires
|
| 85 |
-
nfp = fp + (appears - fires)
|
| 86 |
-
f1 = np.where(ntp > 0, 2 * ntp / (2 * ntp + nfp + (n_positives - ntp)), 0.0)
|
| 87 |
-
f1[~avail] = -1.0
|
| 88 |
-
j = int(np.argmax(f1))
|
| 89 |
-
if f1[j] <= best_f1:
|
| 90 |
-
break # no single addition helps any more
|
| 91 |
-
best_f1 = float(f1[j])
|
| 92 |
-
tp, fp = float(ntp[j]), float(nfp[j])
|
| 93 |
-
avail[j] = False
|
| 94 |
-
chosen.append(j)
|
| 95 |
-
return [toks[i] for i in chosen]
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|