Manuscript Psy03-C@1 · Psy03-C-v1.tex · listing · d3380b90
Listing: Listing
Unexecuted regression specifications · lines 2546–2644
def ideal_blend_accuracy(alpha):
centers = [(-3., -3.), (3., 3.), (-3., 3.), (3., -3.)]
groups = defaultdict(Counter)
for label, visual in enumerate(centers):
for distractor in centers:
observation = tuple(
(1.0 - alpha) * visual[j] + alpha * distractor[j]
for j in range(2)
)
groups[observation][label] += 1
return sum(max(counts.values()) for counts in groups.values()) / 16
def proposed_tests():
# These tests are specified, not reported as executed.
net = CheckedGRUNetwork(hidden_size=3, seed=7)
gen = generator(9, "test_cell")
u = torch.randn((4, 2), dtype=torch.float64, generator=gen)
h = torch.randn((4, 3), dtype=torch.float64, generator=gen)
# Verify the actual reset-gated hidden affine convention.
gi = nn.functional.linear(
u, net.gru_cell.weight_ih, net.gru_cell.bias_ih
)
gh = nn.functional.linear(
h, net.gru_cell.weight_hh, net.gru_cell.bias_hh
)
ir, iz, inn = gi.chunk(3, dim=1)
hr, hz, hn = gh.chunk(3, dim=1)
r = torch.sigmoid(ir + hr)
update = torch.sigmoid(iz + hz)
candidate = torch.tanh(inn + r * hn)
manual = (1.0 - update) * candidate + update * h
torch.testing.assert_close(
manual, net.gru_cell(u, h), rtol=1e-12, atol=1e-12
)
x = torch.zeros((5, 20, 2), dtype=torch.float64)
_, states = net(
x, mode="ordinary", tau=1e6,
streams=noise_streams(11), return_states=True
)
assert float(states.min()) >= 0.0
assert float(states.max()) <= 1.0
net.activation = "tanh"
_, states = net(
x, mode="biased", sigma=1e6, beta=10.0,
streams=noise_streams(12), return_states=True
)
assert float(states.abs().max()) <= 1.0
# Biased ReLU has no unit-cube invariant, even without Gaussian noise.
net.activation, net.a, net.p = "relu", 1.0, 0.0
_, states = net(
x, mode="biased", beta=10.0, return_states=True
)
assert float(states[:, 0, :].min()) >= 9.0
before = phase_snapshot(net, "before")
deleted = net.delete_fraction(0.0, permanent=True)
assert all(value == 0 for value in deleted.values())
for key, value in before["state_dict"].items():
assert torch.equal(net.state_dict()[key], value)
net.delete_fraction(0.5, permanent=True)
saved = phase_snapshot(net, "post_scar")
restored = restore_phase(saved)
for tag, _ in restored.weight_items():
mask = getattr(restored, "M_" + tag)
scar = getattr(restored, "S_" + tag)
assert not bool((mask & scar).any())
# Repeated calls reset h_0 and replay named evaluation streams.
labels = torch.zeros(5, dtype=torch.long)
first = evaluate_explicit(
restored, x, labels, seed=20, mode="ordinary", tau=0.5
)
second = evaluate_explicit(
restored, x, labels, seed=20, mode="ordinary", tau=0.5
)
assert torch.equal(first["correctness"], second["correctness"])
for alpha, expected in (
(0.0, 1.0), (0.25, 1.0), (0.5, 9.0/16.0),
(0.75, 1.0), (1.0, 0.25)
):
assert ideal_blend_accuracy(alpha) == expected
row = {"candidate_id": "c0", "budget": {"d_w": 0.02}}
assert checked_match([row], 0.01, 0.001) is None
assert checked_match([row], 0.02, 0.001) is row
# Complete deletion preserves permanent-mask admissibility.
restored.delete_fraction(1.0, permanent=True)
for tag, parameter in restored.weight_items():
assert int(getattr(restored, "M_" + tag).sum()) == 0
assert int(torch.count_nonzero(parameter)) == 0