mixle.task.density module¶
DensityGate – a real p(x) over inputs, so the model can escalate on “I’ve never seen this”.
Conformal sets calibrate which label is plausible, but a softmax over a ReLU net has no p(x) – it cannot
tell a typical input from a wildly novel one, and will hand back a confident singleton for both. That residual
is exactly what a describable random process fixes: fit a generative density over the input features (a
diagonal-Gaussian mixture by EM – mixle’s home turf), and an input whose log p(x) falls below a calibrated
floor is out-of-distribution -> escalate, regardless of how confident the classifier looks.
Pair it with mixle.task.calibrate.CalibratedTaskModel (which accepts a density_gate=): the cascade
then escalates when the conformal set is ambiguous or the input is atypical – the union of “unsure which
label” and “unlike anything I trained on”. The density is a fitted mixle distribution, so it serializes into the
artifact and reloads identically.
- class DensityGate(featurizer, density=None, log_threshold=None)[source]
Bases:
objectA generative density over featurized inputs with a calibrated out-of-distribution floor on
log p(x).The featurizer is any
transform(list) -> matrix:HashedNGramfor text, orHashedRecordfor dict/tuple records (so record models get the same OOD protection).- Parameters:
featurizer (Any)
density (Any)
log_threshold (float | None)
- fit(texts, *, n_components=4, alpha=0.02, max_its=60, min_covar=1e-3, seed=0)[source]
Fit a diagonal-Gaussian mixture to the features and set the OOD floor at the
alphadensity quantile.
- log_density(texts)[source]
log p(x)of each input under the fitted density (higher = more typical of training data).
- is_ood(text)[source]
True when the input is atypical:
log p(x)below the calibrated floor.