Sub topic 10
Automating the Lifecycle with Pipelines
This chapter assumes you know basic Python, Git, supervised model training, and how a web service is deployed. You will learn how to turn separate machine learning tasks into a repeatable pipeline.
Why pipelines matter
A machine learning pipeline is an ordered set of automated steps. Each step consumes inputs and produces outputs called artifacts. An artifact might be a validated dataset, a feature table, a trained model, or an evaluation report.
Without a pipeline, a team may run commands manually:
- Download data.
- Clean the data.
- Train a model.
- Evaluate it.
- Deploy it.
Manual steps are easy to forget or run in the wrong order. A pipeline records the dependencies and runs the same process consistently. An orchestration system is a tool that schedules these steps, tracks their state, retries failures, and stores logs.
A lifecycle pipeline
A typical training pipeline might look like this:
The arrows represent dependencies. Feature building cannot begin until validation succeeds. Registration should happen only when evaluation passes the release criteria.
A small Python program can express the same idea:
from pathlib import Path
import json
ARTIFACTS = Path("artifacts")
ARTIFACTS.mkdir(exist_ok=True)
def validate_data():
data = {"rows": 1000, "missing_rate": 0.01}
if data["rows"] < 100 or data["missing_rate"] > 0.10:
raise ValueError("Data validation failed")
(ARTIFACTS / "validated.json").write_text(json.dumps(data))
return data
def train_model(data):
model = {"algorithm": "baseline", "rows_used": data["rows"]}
(ARTIFACTS / "model.json").write_text(json.dumps(model))
return model
def evaluate_model(model):
accuracy = 0.94
report = {"accuracy": accuracy, "model": model}
(ARTIFACTS / "evaluation.json").write_text(json.dumps(report))
if accuracy < 0.90:
raise ValueError("Model did not meet the release threshold")
return report
def run_pipeline():
data = validate_data()
model = train_model(data)
report = evaluate_model(model)
print("Ready for registration:", report["accuracy"])
if __name__ == "__main__":
run_pipeline()A production orchestrator would add scheduling, distributed execution, artifact storage, retries, access control, and notifications. The task functions should still be small and independently testable.
Design choices and common errors
| Practice | Why it matters |
|---|---|
| Store outputs as versioned artifacts | A later run can be inspected or reproduced |
| Make steps idempotent | Retrying a step does not corrupt its output |
| Validate at boundaries | Bad data fails before expensive training |
| Record parameters and code versions | Results can be explained later |
Idempotent means that running a step twice produces the same intended result rather than duplicating or damaging data. Avoid hidden inputs such as an unrecorded local file or the current time. Pass inputs explicitly and record the dataset, code commit, environment, and model parameters.
Exercise
Evaluation passes, but model registration fails. Should the pipeline retrain the model automatically?
Show answer
No. Retry the registration step if the failure is temporary. Retraining is appropriate only when the training or evaluation inputs changed, or when a defined retraining policy requires it.