Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import importlib.util
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
from scipy.optimize import check_grad
|
|
|
|
SRC = Path(__file__).resolve().parents[1] / "simulation/semantic-learning"
|
|
sys.path.insert(0, str(SRC))
|
|
spec = importlib.util.spec_from_file_location("nonlinear_probe", SRC / "nonlinear_head_probe.py")
|
|
probe = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(probe)
|
|
sys.path.remove(str(SRC))
|
|
|
|
|
|
def test_analytic_gradient_matches_finite_differences():
|
|
rng = np.random.default_rng(7)
|
|
x = rng.normal(size=(8, 3))
|
|
y = np.array([0, 1] * 4)
|
|
weight = np.array([0.03, 0.07, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2])
|
|
hidden = 4
|
|
theta = rng.normal(scale=0.2, size=3 * hidden + 2 * hidden + 1)
|
|
|
|
def loss(t):
|
|
return probe.objective(t, x, y, weight, hidden, 0.01)[0]
|
|
|
|
def grad(t):
|
|
return probe.objective(t, x, y, weight, hidden, 0.01)[1]
|
|
|
|
assert check_grad(loss, grad, theta) < 1e-6
|
|
|
|
|
|
def test_non_linear_head_can_learn_xor_and_rejects_control_data():
|
|
x = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]], dtype=float)
|
|
y = np.array([0, 1, 1, 0])
|
|
units = [dict(x=x, y=y, split="train", group="synthetic")]
|
|
m = probe.fit_mlp(units, 4, 7, 0.001, 500)
|
|
assert m["converged"]
|
|
assert np.array_equal(probe.mlp_logits(x, m) > 0, y.astype(bool))
|
|
m2 = probe.fit_mlp(units, 4, 7, 0.001, 500)
|
|
assert m["w1"] == m2["w1"] and m["w2"] == m2["w2"]
|
|
with pytest.raises(ValueError, match="training units"):
|
|
probe.fit_mlp([dict(units[0], split="regression")], 4, 7, 0.001, 500)
|
|
|
|
|
|
def test_all_seeds_and_each_street_must_pass_without_winner_selection():
|
|
group = dict(
|
|
linear=dict(recall=1.0, fpr=0.2), a=dict(recall=1.0, fpr=0.1), b=dict(recall=1.0, fpr=0.1)
|
|
)
|
|
audit = dict(
|
|
linear=dict(recall=1.0, fpr=0.4), a=dict(recall=1.0, fpr=0.2), b=dict(recall=1.0, fpr=0.2)
|
|
)
|
|
hist = {"road": dict(recall=1.0, fpr=0.05)}
|
|
out = probe.qualify({"road": group}, audit, ["a", "b"], dict(a=True, b=True), True, hist)
|
|
assert (
|
|
out["capacity_test_passed"]
|
|
and not out["historical_repair_all_seeds"]
|
|
and out["selected_model"] is None
|
|
)
|
|
bad = dict(group, b=dict(recall=0.99, fpr=0.01))
|
|
assert not probe.qualify({"road": bad}, audit, ["a", "b"], dict(a=True, b=True), True, hist)[
|
|
"capacity_test_passed"
|
|
]
|
|
assert not probe.qualify({"road": group}, audit, ["a", "b"], dict(a=True, b=False), True, hist)[
|
|
"capacity_test_passed"
|
|
]
|
|
assert not probe.qualify({"road": group}, audit, ["a", "b"], dict(a=True, b=True), False, hist)[
|
|
"capacity_test_passed"
|
|
]
|
|
assert not probe.qualify(
|
|
{"road": group},
|
|
dict(audit, b=audit["linear"]),
|
|
["a", "b"],
|
|
dict(a=True, b=True),
|
|
True,
|
|
hist,
|
|
)["capacity_test_passed"]
|