mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Add optimization study O02: does choosing fewer, better examples preserve targeting quality at lower cost? - Arms at matched budgets over fit rows only: uniform stratified sampling (exact inclusion probabilities), lexical max-min diversity on hashed n-grams (no text kept), and representation arms (k-means representatives, k-center coreset, boundary hard negatives fitted on a disjoint half). - Quality is held-out: a difference-of-means direction fitted on the selection is scored on select rows or a fixed group-disjoint holdout no arm sees. In-sample AUC is reported as optimistic and never used. - Cost counts forward passes and activation bytes (representation arms pay for embedding the pool unless amortization is declared); host timings are reported but excluded from the verdict and digest. - Coverage limits (every set, rare sets) flag arms that drop cases. - The verdict is "no improvement" unless a representation arm beats both baselines beyond seed spread at equal or lower cost without a coverage flag. Locked test is refused; reports are reproducible. - `obliteratus experiments select-examples` and a Python API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
257 lines
10 KiB
Python
257 lines
10 KiB
Python
"""Example selection arms for the O02 study: stratified, lexical and representation-based.
|
|
|
|
Every arm picks pool positions for a per-class budget (``{True: positives,
|
|
False: negatives}``) from a ``Pool`` of permitted development rows only.
|
|
Randomness comes from an explicit ``torch.Generator``; ties break by pool
|
|
position, so a fixed seed gives identical picks.
|
|
|
|
* ``uniform_stratified`` (baseline): per class, strata are set labels;
|
|
allocation is proportional (largest remainder, at least one row per stratum
|
|
when the budget allows). Within a stratum it samples distinct independence
|
|
groups uniformly, then one row per group, and returns each row's exact
|
|
inclusion probability.
|
|
* ``lexical_maxmin`` (baseline): greedy max-min (farthest point) on Jaccard
|
|
distance between hashed word 1-2 gram sets derived from in-memory text.
|
|
* ``kmeans_representatives``, ``kcenter_coreset`` and
|
|
``boundary_hard_negatives`` (representation): row nearest each seeded
|
|
k-means++/Lloyd centroid; greedy k-center on activations; and diverse
|
|
negatives with the smallest margin to a difference-of-means boundary fitted
|
|
on a group-disjoint fold that the arm never selects from (positives by
|
|
k-center on the same candidate fold).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import math
|
|
import re
|
|
from dataclasses import dataclass
|
|
|
|
import torch
|
|
|
|
from obliteratus.analysis.labeled_selection import group_folds
|
|
from obliteratus.concepts.extraction import pooled_control_mean
|
|
|
|
NOT_PROBABILITY = "not a probability sample"
|
|
|
|
|
|
class ArmUnavailable(ValueError):
|
|
"""The arm cannot produce the requested budget (reported, never silently filled)."""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Pool:
|
|
"""Permitted development rows: activations, class, set label, group, lexical features."""
|
|
|
|
values: torch.Tensor
|
|
positive: torch.Tensor
|
|
sets: tuple
|
|
groups: tuple
|
|
lexical: torch.Tensor | None = None
|
|
|
|
def __len__(self):
|
|
return len(self.sets)
|
|
|
|
def members(self, positive):
|
|
return [i for i in range(len(self)) if bool(self.positive[i]) == positive]
|
|
|
|
|
|
def generator(*parts):
|
|
"""A CPU generator seeded from the given parts (stable across processes)."""
|
|
seed = int(hashlib.sha256(":".join(map(str, parts)).encode()).hexdigest()[:15], 16)
|
|
return torch.Generator().manual_seed(seed)
|
|
|
|
|
|
def allocate(sizes, total):
|
|
"""Largest-remainder proportional allocation, capped by size, >= 1 each when possible."""
|
|
labels = sorted(sizes)
|
|
if total > sum(sizes.values()) or total < 0:
|
|
raise ArmUnavailable(f"Budget {total} exceeds the {sum(sizes.values())} available rows")
|
|
floor = 1 if total >= len(labels) else 0
|
|
n = sum(sizes.values())
|
|
quota = {label: total * sizes[label] / n for label in labels}
|
|
alloc = {label: min(sizes[label], max(floor, math.floor(quota[label]))) for label in labels}
|
|
while sum(alloc.values()) < total:
|
|
open_ = [label for label in labels if alloc[label] < sizes[label]]
|
|
alloc[max(open_, key=lambda lab: (quota[lab] - alloc[lab], -labels.index(lab)))] += 1
|
|
while sum(alloc.values()) > total:
|
|
open_ = [label for label in labels if alloc[label] > floor]
|
|
alloc[min(open_, key=lambda lab: (quota[lab] - alloc[lab], labels.index(lab)))] -= 1
|
|
return alloc
|
|
|
|
|
|
def lexical_features(rows, ngrams=(1, 2)):
|
|
"""``{(path, record_id): frozenset(hash)}`` of hashed lowercase word n-grams.
|
|
|
|
Only 64-bit hashes are kept; the text itself is never stored or returned.
|
|
"""
|
|
out = {}
|
|
for row in rows:
|
|
words = re.findall(r"\w+", row["text"].lower())
|
|
grams = {f"{n}:{' '.join(words[i:i + n])}" for n in ngrams
|
|
for i in range(len(words) - n + 1)}
|
|
out[(row["path"], row["record_id"])] = frozenset(
|
|
int.from_bytes(hashlib.blake2b(g.encode(), digest_size=8).digest(), "big")
|
|
for g in grams)
|
|
return out
|
|
|
|
|
|
def feature_matrix(features):
|
|
"""Binary ``(n, V)`` float64 matrix from per-row hash sets (vocabulary sorted)."""
|
|
vocabulary = {h: j for j, h in enumerate(sorted(set().union(*features)))}
|
|
matrix = torch.zeros(len(features), max(len(vocabulary), 1), dtype=torch.float64)
|
|
for i, row in enumerate(features):
|
|
for h in row:
|
|
matrix[i, vocabulary[h]] = 1.0
|
|
return matrix
|
|
|
|
|
|
def jaccard_distances(matrix):
|
|
inter = matrix @ matrix.T
|
|
sizes = matrix.sum(1)
|
|
union = sizes[:, None] + sizes[None, :] - inter
|
|
return torch.where(union > 0, 1 - inter / union.clamp_min(1), torch.zeros_like(union))
|
|
|
|
|
|
def farthest_point(distances, k, start):
|
|
"""Greedy max-min traversal; ties go to the lowest index."""
|
|
chosen = [start]
|
|
nearest = distances[start].clone()
|
|
while len(chosen) < k:
|
|
nearest[chosen] = -1.0
|
|
nxt = int(torch.argmax(nearest))
|
|
chosen.append(nxt)
|
|
nearest = torch.minimum(nearest, distances[nxt])
|
|
return chosen
|
|
|
|
|
|
def _check(members, k):
|
|
if k > len(members):
|
|
raise ArmUnavailable(f"Budget {k} exceeds the {len(members)} candidate rows of a class")
|
|
|
|
|
|
def uniform_stratified(pool, allocation, gen, seed=0):
|
|
picked, probability = [], []
|
|
for positive, k in allocation.items():
|
|
members = pool.members(positive)
|
|
_check(members, k)
|
|
strata = {}
|
|
for i in members:
|
|
strata.setdefault(pool.sets[i], []).append(i)
|
|
per = allocate({s: len(rows) for s, rows in strata.items()}, k)
|
|
for label in sorted(strata):
|
|
rows, n_h = strata[label], per[label]
|
|
groups = {}
|
|
for i in rows:
|
|
groups.setdefault(pool.groups[i], []).append(i)
|
|
names = sorted(groups)
|
|
if n_h <= len(names):
|
|
for name in [names[j] for j in torch.randperm(len(names), generator=gen)[:n_h]]:
|
|
members_g = groups[name]
|
|
picked.append(members_g[int(torch.randint(len(members_g), (1,),
|
|
generator=gen))])
|
|
probability.append(n_h / len(names) / len(members_g))
|
|
else:
|
|
picked += [rows[j] for j in torch.randperm(len(rows), generator=gen)[:n_h]]
|
|
probability += [n_h / len(rows)] * n_h
|
|
return picked, probability
|
|
|
|
|
|
def lexical_maxmin(pool, allocation, gen, seed=0):
|
|
if pool.lexical is None:
|
|
raise ArmUnavailable("No lexical features (plan-time text) were supplied")
|
|
picked = []
|
|
for positive, k in allocation.items():
|
|
members = pool.members(positive)
|
|
_check(members, k)
|
|
distances = jaccard_distances(pool.lexical[members])
|
|
start = int(torch.randint(len(members), (1,), generator=gen))
|
|
picked += [members[j] for j in farthest_point(distances, k, start)]
|
|
return picked, None
|
|
|
|
|
|
def kcenter_coreset(pool, allocation, gen, seed=0, candidates=None):
|
|
picked = []
|
|
for positive, k in allocation.items():
|
|
members = [i for i in pool.members(positive) if candidates is None or i in candidates]
|
|
_check(members, k)
|
|
x = pool.values[members]
|
|
start = int(torch.randint(len(members), (1,), generator=gen))
|
|
picked += [members[j] for j in farthest_point(torch.cdist(x, x), k, start)]
|
|
return picked, None
|
|
|
|
|
|
def _kmeans(x, k, gen, iterations=50):
|
|
first = int(torch.randint(len(x), (1,), generator=gen))
|
|
centers = [x[first]]
|
|
for _ in range(1, k):
|
|
d2 = torch.cdist(x, torch.stack(centers)).min(1).values ** 2
|
|
if float(d2.sum()) <= 0:
|
|
index = int(torch.randint(len(x), (1,), generator=gen))
|
|
else:
|
|
index = int(torch.multinomial(d2 / d2.sum(), 1, generator=gen))
|
|
centers.append(x[index])
|
|
centers, assign = torch.stack(centers), None
|
|
for _ in range(iterations):
|
|
new = torch.cdist(x, centers).argmin(1)
|
|
if assign is not None and torch.equal(new, assign):
|
|
break
|
|
assign = new
|
|
for j in range(k):
|
|
if bool((assign == j).any()):
|
|
centers[j] = x[assign == j].mean(0)
|
|
return centers
|
|
|
|
|
|
def kmeans_representatives(pool, allocation, gen, seed=0):
|
|
picked = []
|
|
for positive, k in allocation.items():
|
|
members = pool.members(positive)
|
|
_check(members, k)
|
|
x = pool.values[members]
|
|
distances = torch.cdist(_kmeans(x, k, gen), x)
|
|
chosen = []
|
|
for row in distances:
|
|
for j in row.argsort(stable=True).tolist():
|
|
if j not in chosen:
|
|
chosen.append(j)
|
|
break
|
|
picked += [members[j] for j in chosen]
|
|
return picked, None
|
|
|
|
|
|
def boundary_hard_negatives(pool, allocation, gen, seed=0, oversample=3):
|
|
labels = [("positive" if bool(p) else "negative") for p in pool.positive]
|
|
fold = group_folds(pool.groups, 2, seed, labels)
|
|
fit = [i for i in range(len(pool)) if fold[i] == 0]
|
|
candidates = set(range(len(pool))) - set(fit)
|
|
pos = [i for i in fit if bool(pool.positive[i])]
|
|
negs = {}
|
|
for i in fit:
|
|
if not bool(pool.positive[i]):
|
|
negs.setdefault(pool.sets[i], []).append(i)
|
|
if not pos or not negs:
|
|
raise ArmUnavailable("The boundary-fit fold lacks one class")
|
|
positive_mean = pool.values[pos].mean(0)
|
|
control_mean = pooled_control_mean({s: pool.values[r] for s, r in negs.items()})
|
|
direction = positive_mean - control_mean
|
|
threshold = float((positive_mean + control_mean) @ direction) / 2
|
|
picked, _ = kcenter_coreset(pool, {True: allocation[True]}, gen, candidates=candidates)
|
|
negatives = sorted((i for i in candidates if not bool(pool.positive[i])),
|
|
key=lambda i: (abs(float(pool.values[i] @ direction) - threshold), i))
|
|
k = allocation[False]
|
|
_check(negatives, k)
|
|
near = negatives[:min(len(negatives), oversample * k)]
|
|
x = pool.values[near]
|
|
picked += [near[j] for j in farthest_point(torch.cdist(x, x), k, 0)]
|
|
return picked, None, {"labels_consulted": len(fit)}
|
|
|
|
|
|
ARMS = {
|
|
"uniform_stratified": ("baseline-uniform", uniform_stratified),
|
|
"lexical_maxmin": ("baseline-lexical", lexical_maxmin),
|
|
"kmeans_representatives": ("representation", kmeans_representatives),
|
|
"kcenter_coreset": ("representation", kcenter_coreset),
|
|
"boundary_hard_negatives": ("representation", boundary_hard_negatives),
|
|
}
|