mixle.task.model module¶
TaskModel – a fitted small model wrapped as a plain callable: task(raw_input) -> result.
The artifact contract (mixle.task.artifact) makes a model durable; this makes it usable. A task model
pairs a fitted model (a torch module or a mixle distribution) with an I/O adapter that turns raw input
(a string, a record) into the model’s input and the model’s output into a result (a label, a number). The
adapter is serialized into the manifest’s io block, so TaskModel.load(path) reconstructs the whole
raw -> result function in a fresh process – the point of the package: a regular program loads a small
local model and just calls it.
Adapters self-describe and self-rebuild through a registry (register_adapter / IOAdapter.from_spec). The
built-in TextClassifierIO is the workhorse for the distillation path: a dependency-free hashed
character n-gram featurizer feeds a small classifier whose argmax indexes a stored label list – the shape of a
“scrape this field” / “classify this line” model you distill from a big teacher and run locally.
- class HashedNGram(n=3, dim=256, seed=0)[source]
Bases:
objectMap a string to a fixed-width float vector by hashing its character n-grams into
dimbuckets.Deterministic and dependency-free (stdlib
hashlib), so it serializes as three numbers and rebuilds identically anywhere – no fitted vocabulary, no external tokenizer. Counts are L2-normalized per row.
- class HashedRecord(dim=256, seed=0)[source]
Bases:
objectMap a heterogeneous record (tuple or dict) to a fixed vector by the hashing trick – tabular tasks.
Each field is hashed by
key: a categorical/string/bool value contributes a 1 athash("key=value"); a numeric value contributes a boundedtanh(value)athash("num:key")(and its presence at a second bucket). Stateless and deterministic – no fitted encoder or vocabulary – so it serializes as two numbers and rebuilds identically. The shape of a “classify this record / route this ticket / flag this transaction” model.
- register_adapter(kind, from_spec)[source]
Register an adapter’s
from_specfactory underkindso a savedioblock can rebuild it.
- adapter_from_spec(spec)[source]
Rebuild an adapter from its
iospec (thekindfield selects the factory).
- class TextClassifierIO(featurizer, labels)[source]
Bases:
_ClassifierIOstr -> label: hashed character n-gram features into a small classifier.- kind = 'text_classifier'
- class RecordClassifierIO(featurizer, labels)[source]
Bases:
_ClassifierIOrecord -> label: hashed-record features into a small classifier (tuples/dicts of mixed fields).- kind = 'record_classifier'
- class StructuredClassifierIO(field_keys, label_index, labels)[source]
Bases:
objectrecord -> labelthrough a structured probabilistic model instead of a neural net.The model is a fitted joint over
(field_1, ..., field_m, label)– aDependencyTreeDistribution(or mixture) discovered bymixle.inference.structure.learn_structure(). Classification is the generative ruleargmax_label P(features, label): score each candidate label and pick the best. Becausesoftmax_label log P(features, label) = P(label | features)exactly (the feature evidence is a shared constant across labels),proba_batch()returns the true posterior – not a softmax over arbitrary logits – so conformal calibration (mixle.task.calibrate) and the density gate operate on a real probability.The student is interpretable (
model.edges()shows the discovered dependencies), kilobytes on disk, and round-trips through the json artifact path. It assumes a fixed schema: every record exposes the same fields (field_keysfor dicts, positional for tuples) – the variable set a Bayesian network is defined over.- kind = 'structured_classifier'
- logits_batch(model, raw_inputs)[source]
Per-label log-joint
log P(features, label)as an(m, K)score matrix (the classifier logits).
- proba_batch(model, raw_inputs)[source]
The exact posterior
P(label | features)– softmax of the per-label log-joints (shared evidence cancels).
- predict_batch(model, raw_inputs)[source]
- class TaskModel(model, adapter, *, builder=None, config=None, payload='torch', task='', meta=None)[source]
Bases:
objectA fitted small model plus its I/O adapter, callable as
task(raw) -> resultand saveable to a directory.- Parameters:
- save(path)[source]
Persist as a task artifact: the model payload plus the adapter’s
iospec and metadata.