Sub topic 2
Tracking experiments, parameters and metrics
This chapter assumes you can train and evaluate a scikit-learn classifier, use Git and the command line, and have a rough idea of how a web service is deployed. It covers six parts of the MLOps lifecycle in order: experiment tracking, release criteria, the model registry, pipelines, CI/CD checks, and deployment.
Tracking experiments
An experiment tracker is a tool that records each training run: its parameters, its metrics, and the random seed used. Without one, you cannot reliably compare runs or reproduce a result. The example below uses MLflow, an open-source tracker.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
SEED = 42
C = 1.0
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=SEED
)
with mlflow.start_run():
model = LogisticRegression(C=C, max_iter=200).fit(X_train, y_train)
accuracy = accuracy_score(y_test, model.predict(X_test))
mlflow.log_param("C", C)
mlflow.log_param("seed", SEED)
mlflow.log_metric("accuracy", accuracy)Evaluating models and setting release criteria
A release criterion is a rule that decides whether a candidate model may replace the current one. A good rule combines an absolute floor with a comparison against production. The function below applies both checks.
def should_promote(candidate_accuracy, production_accuracy, min_gain=0.0, floor=0.90):
"""Release gate: the candidate must clear an absolute floor and not be worse than production."""
if candidate_accuracy < floor:
return False
return candidate_accuracy >= production_accuracy + min_gain
print(should_promote(0.94, 0.92)) # True
print(should_promote(0.89, 0.85)) # False: below the absolute floor
print(should_promote(0.93, 0.95)) # False: worse than productionVersioning models and using a registry
A model registry is a catalog where each trained model gets a numbered version, a stage such as "staging" or "production", and a link to the run that produced it. The registry lets a team answer which model is live and where it came from.
| Stage | Meaning |
|---|---|
| None | Registered but not yet reviewed |
| Staging | Passed evaluation and awaiting checks |
| Production | Serving live traffic |
| Archived | Retired, kept for audit |
Automating the lifecycle with pipelines
A pipeline is a series of steps (load data, train, evaluate, register) that an orchestrator runs in a fixed order. Automation makes each run repeatable and removes manual copying of files between steps.
CI/CD for machine learning: code, data and model checks
Continuous integration (CI) runs automated checks on every change. Continuous delivery (CD) moves a checked change toward production. For machine learning, the checks cover three things:
- Code: unit tests and linting pass.
- Data: the input schema and value ranges match what the model expects.
- Model: the release criteria above pass.
Deploying and serving models
Deployment is how predictions reach users. Three common patterns are:
- Batch scoring: the model runs on a schedule over a stored dataset and writes results to a table.
- Online serving: the model runs behind an API and answers each request as it arrives.
- Staged rollout: a new version receives a small share of traffic first, and the share grows if its metrics hold.
Exercise
A candidate model has accuracy 0.91 and production has accuracy 0.93. Should should_promote approve it with the default settings?
Show answer
No. The candidate clears the 0.90 floor, but 0.91 is less than 0.93 plus a minimum gain of 0.0, so the function returns False.