Sub topic 3
Versioning data, code, and models for reproducibility
This chapter assumes you know the train, validate, test, deploy workflow, basic Python, and Git. It covers how a retraining pipeline records its inputs, decides whether a new model is better, and rolls out changes safely.
Versioning data, code, and models
Each retrain should record three things: the data snapshot, the code commit, and the parameters. Store them with the model so you can trace any production model back to its inputs.
import hashlib
import json
def fingerprint(path, code_commit, params):
with open(path, "rb") as f:
data_hash = hashlib.sha256(f.read()).hexdigest()
record = {"data_sha256": data_hash, "code_commit": code_commit, "params": params}
return json.dumps(record, sort_keys=True)The evaluation gate: champion versus challenger
The champion is the live model. The challenger is the newly trained model. Compare both on the same held-out data, and promote the challenger only if it clearly wins.
def should_promote(challenger_auc, champion_auc, data_ok, min_gain=0.002):
if not data_ok:
return False, "data_validation_failed"
if challenger_auc - champion_auc < min_gain:
return False, "gain_below_threshold"
return True, "promote"
print(should_promote(0.912, 0.905, True))Registering and promoting models
Register each challenger that passes the gate in a model registry, with a version number, its metrics, and its lineage record. Promotion moves a pointer, such as a champion alias, to the new version. Keep the previous version so you can roll back.
Safe rollout: shadow, canary, and rollback
Shadow deployment runs the challenger on live traffic without serving its answers. A canary sends a small share of real requests, such as 5%, to the challenger. If metrics regress, point the alias back to the previous version.
| Stage | Users affected | Purpose |
|---|---|---|
| Shadow | None | Compare predictions safely |
| Canary | Small share | Check live metrics |
| Full | All | Complete promotion |
Orchestrating the pipeline
An orchestrator runs the stages in order: validate data, train, evaluate, register, and roll out. Each stage writes its artifacts, and a failed stage stops the run.
Guardrails: cooldowns, budgets, and human approval
- Cooldown: wait a fixed period, such as two days, between retrains to prevent loops.
- Budget: cap compute spending per run and per month.
- Human approval: require a person to approve promotion to production.
- Data checks: reject batches that fail validation before training.
Testing and observing the pipeline itself
Test each stage with fixed sample data. Confirm that the gate blocks a known-bad model. Monitor run duration, failure rate, and the time since the last successful promotion.
A maturity path from manual to continuous training
Start with a person running retrains by hand. Next, run them on a schedule with an approval step. Finally, trigger retraining on drift and promote automatically only when the gate and guardrails pass.
Exercise
Why keep the previous champion version in the registry?
Show answer
So you can roll back by repointing the champion alias if the new model fails in production.