Chapter 06 of 12

Model Registry & Versioning

Version everything - models, data, code, and configs. Manage ML models like production artifacts.

What is a Model Registry?

A Model Registry is a centralized catalog that stores, versions, and manages ML model lifecycles. Think of it as Docker Registry + deployment pipeline for ML models.

Development
→
Staging
→
Production
→
Archived

MLflow Model Registry

Register a Model

import mlflow

# Register after training
with mlflow.start_run():
    mlflow.sklearn.log_model(model, "model",
        registered_model_name="churn_predictor")

Promote Through Stages

from mlflow import MlflowClient
client = MlflowClient()

# Promote to Staging
client.transition_model_version_stage(
    name="churn_predictor", version=3, stage="Staging")

# After validation, promote to Production
client.transition_model_version_stage(
    name="churn_predictor", version=3, stage="Production")

# Load production model
model = mlflow.pyfunc.load_model("models:/churn_predictor/Production")

Automated Validation Gate

def validate_for_production(model_name, version):
    client = MlflowClient()
    mv = client.get_model_version(model_name, version)
    run = client.get_run(mv.run_id)
    
    accuracy = run.data.metrics.get("accuracy", 0)
    f1 = run.data.metrics.get("f1", 0)
    
    checks = [
        ("accuracy >= 0.85", accuracy >= 0.85),
        ("f1 >= 0.80", f1 >= 0.80),
    ]
    
    # Compare with current production
    prod = client.get_latest_versions(model_name, ["Production"])
    if prod:
        prod_f1 = client.get_run(prod[0].run_id).data.metrics.get("f1", 0)
        checks.append(("better than prod", f1 > prod_f1))
    
    all_pass = all(p for _, p in checks)
    for name, passed in checks:
        print(f"  {'PASS' if passed else 'FAIL'}: {name}")
    
    if all_pass:
        client.transition_model_version_stage(
            model_name, version, "Production")
    return all_pass

Data Versioning with DVC

# Initialize DVC
git init && dvc init

# Track large dataset
dvc add data/customer_data.parquet
git add data/customer_data.parquet.dvc
git commit -m "Add training data v1"

# Push to remote storage
dvc remote add -d myremote s3://bucket/dvc-store
dvc push

# Switch to previous data version
git checkout v1.0
dvc checkout

What to Version

WhatToolYour Familiarity
CodeGitYou know this!
DataDVC / LakeFSGit-like for data
ModelsMLflow / W&BNew skill
PipelinesZenML configNew skill
EnvironmentDockerYou know this!