mixle.task.model module¶
Callable task-model wrapper for serialized local models.
The artifact contract (mixle.task.artifact) makes a model durable.
TaskModel makes it directly usable by pairing a fitted model with an I/O
adapter that converts raw application inputs into model features and converts
model outputs into application results. The adapter is serialized in the
artifact manifest, so TaskModel.load(path) reconstructs the full
raw_input -> result callable in a fresh process.
Adapters self-describe and rebuild through a registry
(register_adapter / IOAdapter.from_spec). The built-in
TextClassifierIO supports the distillation path with a dependency-free
hashed character n-gram featurizer, a small classifier, and a stored label map.
- 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.The featurizer is deterministic and dependency-free. It serializes as three scalar settings and rebuilds without a fitted vocabulary or external tokenizer. Counts are L2-normalized per row.
- transform(texts)[source]
Return L2-normalized hashed n-gram feature rows for
texts.
- class HashedRecord(dim=256, seed=0)[source]
Bases:
objectMap a heterogeneous record to a fixed-width hashed feature vector.
Each tuple position or dictionary key owns a hashed namespace. Categorical, string, and boolean values contribute an indicator feature; numeric values contribute a bounded value feature and a presence feature. The transform is stateless and deterministic, so it serializes as two scalar settings and rebuilds without a fitted encoder or vocabulary.
- transform(records)[source]
Return L2-normalized hashed feature rows for heterogeneous records.
- to_spec()[source]
Return the serializable record-featurizer configuration.
- 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.
- class RecordClassifierIO(featurizer, labels)[source]
Bases:
_ClassifierIOrecord -> label: hashed-record features into a small classifier (tuples/dicts of mixed fields).
- 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.- 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]
Predict labels for raw inputs by maximizing the per-label joint score.
- predict(model, raw_input)[source]
Predict the label for one raw input.
- to_spec()[source]
Return the serializable structured-classifier adapter specification.
- 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:
- batch(raw_inputs)[source]
Run the wrapped model on a batch of raw inputs through its adapter.
- save(path)[source]
Persist as a task artifact: the model payload plus the adapter’s
iospec and metadata.