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: object

A generative density over featurized inputs with a calibrated out-of-distribution floor on log p(x).

The featurizer is any transform(list) -> matrix: HashedNGram for text, or HashedRecord for 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 alpha density quantile.

Parameters:
Return type:

DensityGate

log_density(texts)[source]

log p(x) of each input under the fitted density (higher = more typical of training data).

Parameters:

texts (Sequence[str])

Return type:

ndarray

is_ood(text)[source]

True when the input is atypical: log p(x) below the calibrated floor.

Parameters:

text (str)

Return type:

bool

ood_mask(texts)[source]
Parameters:

texts (Sequence[str])

Return type:

ndarray

to_spec()[source]
Return type:

dict[str, Any]

classmethod from_spec(spec)[source]
Parameters:

spec (dict[str, Any])

Return type:

DensityGate