Sub topic 9
Versioning Models and Using a Registry
Assume you know basic Python, Git, command-line use, supervised model training, and how a web service is deployed. This chapter focuses on the operational step between evaluating a model and releasing it.
Why a model registry is needed
A trained model is more than one file. Its behavior depends on the training data, source code, feature definitions, dependencies, parameters, and evaluation results. A model registry is a shared system that stores model artifacts with this context and gives each registered model version a stable identity.
Without a registry, teams often pass files through shared folders or container images. They may not know which file is serving, which data produced it, or whether a newer model passed the required checks.
A registry usually records:
- The model artifact and its format.
- The source run, code revision, and data reference.
- Parameters, metrics, and evaluation reports.
- The model version number.
- Approval status, such as candidate or production.
- An audit trail of who changed its status and when.
A model version should be immutable. If the artifact changes, create a new version rather than replacing the old file. This makes rollback possible.
Versions, aliases, and promotion
A registry separates a model's identity from its deployment location. For example, fraud-detector can have versions 12 and 13. An alias such as champion can point to version 12 while version 13 is tested. If version 13 passes, move the alias to version 13. The serving system loads the model through the alias instead of hard-coding a file path.
| Registry concept | Meaning |
|---|---|
| Registered model | Named model family, such as `fraud-detector` |
| Version | Immutable snapshot of one trained artifact |
| Alias | Movable label, such as `candidate` or `champion` |
| Lineage | Links from the model to its run, code, data, and checks |
Registering a model with MLflow
MLflow is an experiment and model-tracking tool. This example trains a classifier, logs it, registers it, and assigns the candidate alias. The SQLite backend gives the local registry a persistent database.
import mlflow
import mlflow.sklearn
from mlflow.models import infer_signature
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("iris-classification")
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=42
)
model_name = "iris-classifier"
with mlflow.start_run() as run:
model = LogisticRegression(max_iter=200).fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
mlflow.log_param("max_iter", 200)
mlflow.log_metric("accuracy", accuracy)
mlflow.sklearn.log_model(
model,
"model",
signature=infer_signature(X_train, model.predict(X_train)),
)
model_uri = f"runs:/{run.info.run_id}/model"
registered = mlflow.register_model(model_uri, model_name)
client = mlflow.MlflowClient()
client.set_registered_model_alias(
model_name, "candidate", registered.version
)
print(f"Registered version {registered.version}; accuracy={accuracy:.3f}")A registry does not decide whether a model is safe to release. Your evaluation and release policy must do that. Store the evidence in the registry, then automate promotion only after checks for quality, fairness, security, latency, and compatibility pass.
Exercise
Why should a serving system load the champion alias instead of a hard-coded model file?
Show answer
The alias can move to a tested model version without changing serving code, and it can move back for a fast rollback.