Trainer¶
mllabs.Trainer
¶
Runs cross-validation training on a subset of Pipeline nodes.
No Project dependency, and nothing to inject but a cache and a
resolver: like :class:~mllabs.Experimenter, a Trainer owns its own store — here a
:class:~mllabs._trainer_store.TrainerStore, built from path — and
everything needed to reopen it lives in that directory. Project
supplies the path and records the name in its index.
Constructing is creating: it splits the data and writes fresh state, so
pointing it at an existing directory starts that Trainer over rather than
resuming it. :meth:load_trainer is how an existing one comes back, on
exactly the splits it was trained with.
Adopt a Pipeline with :meth:set_pipeline (never a constructor argument),
and pass the Predictors to :meth:train. There is no separate "select"
step: what you train is what you asked to train, and the Pipeline nodes
those Predictors read are pulled in with them.
Two artifact stores, not one. Pipeline nodes keep the Trainer's own base
path; Predictors get their own directory underneath it. They are the same
:class:~mllabs._store.NodeStore class — a Predictor's fitted model and
per-split history are stored exactly like a node's — but separating them
means a Predictor is told apart structurally rather than by which history
table happened to record it, and it keeps the two __node_hist.db files
from colliding. __predictors/ also holds the
:class:~mllabs.PredictorStore with the definitions themselves, so a
reloaded Trainer comes back with its selection intact.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Trainer name. |
pipeline |
Pipeline
|
The adopted Pipeline, kept as this Trainer's own
|
predictors |
list[Predictor]
|
Everything :meth: |
selected_nodes |
list[str]
|
Pipeline nodes those Predictors read, topologically ordered. Derived, not stored. |
node_store |
NodeStore
|
Artifacts + history for Pipeline nodes. |
predictor_store |
NodeStore
|
Artifacts + history for Predictors. |
predictor_defs |
PredictorStore
|
The Predictor definitions themselves. |
train_folds |
list[TrainFold]
|
Per-split data flows and artifact stores. |
pipeline_version
property
¶
The adopted Pipeline's version — always a frozen one, or None if unset.
Read off :attr:pipeline rather than kept beside it, so there is no
second copy to fall out of step with it.
predictors
property
¶
Every Predictor this Trainer has been asked to train.
Read from predictor_defs rather than held as state: :meth:train
registers what it is given, so this is what has actually been trained
(or attempted) here, and it survives a reload for free.
selected_nodes
property
¶
Pipeline nodes the registered Predictors read, topologically ordered.
get_node_error(node_name)
¶
Return the error dict for a node or Predictor in error state, or None.
Predictors are covered as well as Pipeline nodes: each store records
its own history, so a failed Predictor leaves an 'error' row in
predictor_store the same way a failed node does in node_store.
get_status(node_name)
¶
Return the disk status of a node or Predictor across all splits.
Returns 'built', None (init, or errored — NodeStore only
knows whether obj.pkl exists; see :meth:get_node_error for the
error detail), or 'inconsistent' if splits differ.
load_trainer(path, data, aug_data=None, cache=None, resolver=None)
staticmethod
¶
Reopen the Trainer rooted at path.
Everything comes out of that directory — meta and splits from its
__trainer.db, the Pipeline from its pipeline.pkl, the
Predictors from __predictors/. The splits come back as the stored
indices rather than being recomputed, so the folds are exactly the
ones that were trained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The Trainer's base directory. |
required | |
data
|
Dataset to attach. |
required | |
aug_data
|
optional
|
External data appended to train splits. A
live dataset, or an |
None
|
cache
|
DataCache
|
Shared LRU cache. |
None
|
resolver
|
Resolver
|
Resolves a stored spec's params
into live objects at Processor-construction time (see
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Trainer |
The reopened Trainer. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If path holds no saved Trainer. |
predictor_specs()
¶
{name: ProcessorSpec} for the registered Predictors.
predictor_status(name)
¶
Where name stands: init / trained / retired /
error, or None if it is not registered here.
predictor_statuses()
¶
{name: status} for every registered Predictor.
process(data, v=None)
¶
Apply trained processors to new data, yielding one result per split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Input dataset. |
required | |
v
|
Output column filter applied to Predictor outputs. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
DataFrame |
Concatenated Predictor outputs for each split. |
purge_retired()
¶
Remove every retired Predictor. Returns the names removed.
remove_predictor(name)
¶
Erase name from this Trainer entirely — definition, artifacts, history.
Retiring deliberately leaves all three standing: the definition is the
inscription on the tombstone, and the history says what ran before it.
They accumulate, so clearing them is a separate act with its own call
rather than a side effect of the switch that ended the Predictor —
the shape Project.remove_trial has, for the same reason.
Works on any status, not only retired ones.
reset_nodes(nodes)
¶
Drop nodes' artifacts so the next :meth:train rebuilds them.
This is the initialising sense of the word, and it is deliberately
not the retiring one. Asking for a rebuild leaves the Pipeline
definition untouched, so a Predictor caught by the cascade will be fed
exactly the inputs it had before and would train to the same model —
there is nothing to end, and it goes back to init.
A version switch is the other case, and takes the other path: there
the inputs really did change, the same model cannot be produced again,
and the Predictor is retired rather than reset (:meth:set_pipeline).
A Predictor already retired stays that way — a rebuild request is not
a resurrection.
retiring_predictors(pipeline)
¶
Which Predictors adopting pipeline would retire, before adopting.
The question has to be asked beforehand, because afterwards there is
nothing left to ask: the artifacts are gone and the only record of why
is the status this call decides. Same shape as
Experimenter.stale_nodes, and for the same reason — this is the
code :meth:set_pipeline acts on, not a second opinion about it.
A Predictor is retired when the change reaches a node it reads, which means the inputs that produced its artifact no longer exist under the adopted definition. One that reads only nodes defined identically in both versions keeps its artifact and is restamped onto the new version instead: training it there would produce the same model, so claiming the new version is true.
Nothing is retired twice, and adopting onto an empty Pipeline retires nothing — there is no previous definition to have diverged from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Pipeline
|
The version being considered. |
required |
Returns:
| Type | Description |
|---|---|
|
list[str]: Predictor names, sorted. |
save()
¶
Persist meta and splits. Artifacts and Predictors save themselves.
set_pipeline(pipeline)
¶
Adopt an already-loaded Pipeline. It must be a published version.
Takes the Pipeline object directly rather than a version number —
this class has no way to load one by version itself (see the class
docstring); Project.add_trainer() resolves that before calling
this. self.pipeline_version is read straight off pipeline (its
.version), never tracked separately.
A draft is refused, and only a draft. What a Trainer produces is what gets deployed, so it has to be able to say what it was trained against, and a draft carries no number to say it with. Which published version is not this call's business: an older one answers that question as well as the newest, and training a Predictor evaluated against an older definition is exactly what adopting that version is for.
The Pipeline is written to this Trainer's own pipeline.pkl, which
:meth:load reads back — reopening needs only this directory, never
a Project.
Which nodes are actually selected depends on the Predictors — pass
them to :meth:train.
Diffs the two Pipelines (:meth:Pipeline.diff_from) and resets the
node artifacts the change invalidated via :meth:reset_nodes, which
also cascades into any selected Predictor that reads a reset node —
unlike Experimenter.set_pipeline, a Trainer has no notion of a
past execution to preserve, so a Predictor trained against a
since-changed node is simply stale.
The Predictors already registered here move onto the adopted version
with it (see _restamp_predictors): adopting is what says which
definition this Trainer trains against, and stranding them behind would
leave their artifacts deleted and unrebuildable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Pipeline
|
Already-built, already-published Pipeline. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pipeline is a draft. |
stale_nodes(pipeline)
¶
Pipeline nodes adopting pipeline would reset, sorted.
The node half of what a version switch costs;
:meth:retiring_predictors is the other. They are different kinds of
loss and are worth asking for separately — a node comes back on the
next :meth:train, a retired Predictor does not come back at all.
Same traversal :meth:set_pipeline acts on, so this is a preview
rather than a second opinion about it. The counterpart to
Experimenter.stale_nodes, which a Trainer had no equivalent of.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Pipeline
|
The version being considered. |
required |
Returns:
| Type | Description |
|---|---|
|
list[str]: Node names, sorted. |
to_inferencer(v=None)
¶
Export trained processors to a standalone :class:~mllabs.Inferencer.
All selected nodes must be in built state. The Inferencer is stamped
with :meth:_trainer_spec so the deployed pickle can say where it came
from without holding a Trainer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Output column filter passed to the Inferencer. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Inferencer |
Independent inferencer ready for deployment. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If any selected node is not built. |
train(predictors=None, n_jobs=1, gpu_id_list=None, logger=None)
¶
Train predictors and the Pipeline nodes they read, across all splits.
Nodes go first (topological order), then the Predictors. Only what is
not already built on disk is run, so calling this again after adding a
Predictor trains just that one — nothing already trained is discarded.
Node staleness is settled when a Pipeline is adopted
(:meth:set_pipeline), which does reset what it invalidates.
The Predictors are registered in predictor_defs, which is what
:attr:predictors, :meth:process and :meth:to_inferencer read, and
what a reopened Trainer comes back with. Registering is an upsert: a
Predictor trained by an earlier call keeps its definition and its
artifacts.
Each one has to be defined against the version this Trainer adopted. One promoted from a Trial carries the version its candidate was evaluated on, so training it elsewhere would ship something that was never measured; adopt that version instead, since every published version is adoptable. One authored here with no version gets this Trainer's — the version it is being defined against is the one in front of it, and this Trainer knows that without a registry to ask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictors
|
list[Predictor]
|
What to train. A
:class: |
None
|
n_jobs
|
int
|
Number of parallel workers. Default 1 (sequential). |
1
|
gpu_id_list
|
list
|
GPU IDs for GPU-enabled nodes. |
None
|
logger
|
Logger instance. Default: shared |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If predictors holds a :class: |
ValueError
|
If a Predictor is defined against a different Pipeline version than the adopted one. |