EugeneBoondock's picture
Sponge online learners and the compression-scored search that found them
b999890 verified
Raw History Blame Contribute Delete
2.4 kB
"""Scoring maths. Trusted: this only ever runs in the search process, never beside a candidate.
The learner must predict each byte before it is shown that byte, so the total cost in
bits is a direct measure of how well it generalises (prequential coding). The size of
the learner's own source is added, so memorising data inside the code does not pay.
"""
from __future__ import annotations
import math
import zlib
import numpy as np
ALPHABET = 256
FLOOR = 1e-4 # every byte keeps at least FLOOR/256 probability, so cost is finite
class CandidateError(Exception):
"""The candidate broke the contract."""
def byte_cost_bits(prediction, actual: int) -> float:
"""Bits needed to encode `actual` under the candidate's prediction."""
probs = np.asarray(prediction, dtype=np.float64)
if probs.shape != (ALPHABET,):
raise CandidateError(f"predict() returned shape {probs.shape}, expected (256,)")
total = float(probs.sum())
if not math.isfinite(total) or total <= 0.0 or float(probs.min()) < 0.0:
raise CandidateError("predict() returned negative, non-finite or all-zero values")
share = float(probs[actual]) / total
return -math.log2(share * (1.0 - FLOOR) + FLOOR / ALPHABET)
def curriculum_parts(byte_bits, curriculum: dict) -> dict[str, float]:
"""Split a curriculum stream's per-byte costs into the parts that make up the score.
Each kind of text appears twice. Parts, all in bits per byte:
<kind> the whole first helping of that kind
adaptation the opening bytes of every first helping (how fast it picks up a new kind)
retention every second helping (what it kept after the other kinds came in between)
"""
opening = int(curriculum["adapt_bytes"])
parts: dict[str, float] = {}
openings, returns = [], []
for segment in curriculum["segments"]:
costs = byte_bits[segment["start"]:segment["end"]]
if segment["pass"] == 1:
parts[segment["kind"]] = float(costs.mean())
openings.append(float(costs[:opening].mean()))
else:
returns.append(float(costs.mean()))
if openings:
parts["adaptation"] = sum(openings) / len(openings)
if returns:
parts["retention"] = sum(returns) / len(returns)
return parts
def code_cost_bits(source: str) -> int:
return 8 * len(zlib.compress(source.encode("utf-8"), 9))