Lesson 7 of 9
Pipeline Versioning, Experiment Tracking & Metadata Management with MLflow
In enterprise machine learning, reproducibility is a legal and operational mandate. Machine learning pipelines must be tracked alongside their training code git commit hash, environment lockfile, training dataset checksum, hyperparameters, metrics, and serialized model artifacts. MLflow provides the industry-standard framework for experiment tracking, run comparison, model registry governance, and lineage management.
Production MLflow Experiment Tracking Architecture
import numpy as np
import pandas as pd
import tempfile
import os
# Simulated Production MLflow Experiment Tracking Workflow
class MockMLflowTracker:
def __init__(self, experiment_name: str):
self.experiment_name = experiment_name
self.runs = []
def log_run(self, run_name: str, params: dict, metrics: dict, artifacts: list):
run_record = {
'run_name': run_name,
'git_commit': 'a1b2c3d4e5f6',
'params': params,
'metrics': metrics,
'artifacts': artifacts
}
self.runs.append(run_record)
print(f"[MLflow] Logged Run '{run_name}' to experiment '{self.experiment_name}'")
print(f" Params : {params}")
print(f" Metrics: {metrics}")
# Simulate tracking multiple pipeline experimentation runs
tracker = MockMLflowTracker(experiment_name="churn-prediction-v2")
# Run 1: Baseline Logistic Regression
tracker.log_run(
run_name="baseline_logistic_regression",
params={"model": "LogisticRegression", "C": 1.0, "penalty": "l2"},
metrics={"test_roc_auc": 0.8234, "test_pr_auc": 0.5412, "latency_ms": 1.2},
artifacts=["pipeline_v1.joblib", "preprocessor.json"]
)
# Run 2: Tuned HistGradientBoosting
tracker.log_run(
run_name="tuned_gradient_boosting",
params={"model": "HistGradientBoosting", "learning_rate": 0.05, "max_leaf_nodes": 31},
metrics={"test_roc_auc": 0.8942, "test_pr_auc": 0.7125, "latency_ms": 8.4},
artifacts=["pipeline_v2.joblib", "shap_summary.png", "model_card.md"]
)
print("\n=== MLFLOW MODEL REGISTRY GOVERNANCE ===")
print("Stage Transition: 'tuned_gradient_boosting' promoted to [STAGING] for shadow deployment testing.")Pro Tip: Log the Git Hash and Data Checksum in MLflow
Always log `mlflow.set_tag('git_commit', repo.head.commit.hexsha)` and an MD5 checksum of your training data Parquet file. If a production model begins misbehaving 6 months later, you can instantly check out the exact historical code and dataset to reproduce the issue.
MlPipelineVersioningTracking Visual Representation
Hands-On Challenge: Build an MLflow Run Logger
Write a Python context manager `track_experiment(run_name)` that automatically records execution time, CPU memory usage, and Scikit-Learn pipeline parameters to disk in a structured JSON ledger.
Quick Quiz & Key Concept Checks
Common Pitfall: Logging Giant Datasets Directly to MLflow
Do not log 50GB training CSVs as MLflow run artifacts; it will saturate the tracking server's network bandwidth and disk storage. Log a metadata pointer and checksum (`s3://lake/data_v2.parquet`, `md5: 8f3a...`) instead.