Experiment Logging using MLflow

MLflow के जरिए parameters, metrics, artifacts और models को track करना सीखें — लोकल और remote tracking server सेटअप, autologging, Model Registry और best practices सहित।

🧪 Experiment Logging using MLflow

Machine Learning projects में सबसे बड़ी चुनौती है experiments को consistently track करना। कौन सा model किस data पर चला, किन hyperparameters से सबसे अच्छा score आया, और कौन से artifacts generate हुए — इन सबका clear record MLflow देता है। MLflow एक open-source platform है जो tracking, projects, models और model registry जैसे components प्रदान करता है, ताकि research से production तक सब कुछ reproducible और auditable रहे।

🔧 MLflow Installation

# isolated env recommend
python -m venv .venv && source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install --upgrade pip
pip install mlflow scikit-learn pandas numpy
    

🧱 MLflow Components (Quick Overview)

  • MLflow Tracking: Parameters, Metrics, Artifacts और Models log करने के लिए API और UI।
  • MLflow Projects: Reproducible runs के लिए standardized project structure।
  • MLflow Models: Model packaging format जो कई flavors को support करता है।
  • Model Registry: Staging → Production lifecycle management, versioning और approvals।

🚀 Local Tracking Server & UI

सबसे पहले local tracking को try करें और फिर जरूरत पड़ने पर remote server पर जाएँ।

# लोकल UI launch (default: http://127.0.0.1:5000)
mlflow ui --backend-store-uri sqlite:///mlruns.db --default-artifact-root ./mlruns
    

backend-store-uri में runs का metadata store होता है और artifact-root में files (plots, models) सेव होते हैं। SQLite dev के लिए पर्याप्त है; prod में PostgreSQL या MySQL बेहतर होता है।

📝 Manual Logging (Params, Metrics, Artifacts)

import mlflow, time
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

X, y = load_iris(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)

mlflow.set_tracking_uri("http://127.0.0.1:5000")  # remote हो तो वही URI दें
mlflow.set_experiment("iris-rf-experiment")

with mlflow.start_run(run_name="rf-baseline"):
    n_estimators = 200
    max_depth = 5

    mlflow.log_param("n_estimators", n_estimators)
    mlflow.log_param("max_depth", max_depth)

    model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
    model.fit(Xtr, ytr)
    preds = model.predict(Xte)
    acc = accuracy_score(yte, preds)

    mlflow.log_metric("accuracy", acc)
    mlflow.sklearn.log_model(model, artifact_path="model")  # model artifact save
    # किसी भी file को artifact के रूप में log कर सकते हैं
    with open("notes.txt", "w") as f:
        f.write(f"Run at {time.time()} with acc={acc}")
    mlflow.log_artifact("notes.txt", artifact_path="notes")
    print("Logged run with accuracy:", acc)
    

⚡ Autologging (Super Quick)

Autologging libraries जैसे scikit-learn, Keras, XGBoost के साथ parameters, metrics और model artifacts अपने आप log कर देता है।

import mlflow
import mlflow.sklearn
mlflow.set_experiment("autolog-demo")
mlflow.sklearn.autolog()

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)

with mlflow.start_run(run_name="logreg-auto"):
    clf = LogisticRegression(max_iter=200)
    clf.fit(Xtr, ytr)   # training के साथ logs auto capture हो जाते हैं
    # predict/evaluate भी autolog में track होता है जहां supported हो
    

📦 Artifacts: Plots, Data Snapshots, Feature Importance

MLflow artifacts में आप confusion matrix, ROC curves, feature importance, config files, और data sample dumps store कर सकते हैं ताकि बाद में audit या debug कर सकें।

🏷️ Experiments, Runs और Tags

बेहतर organization के लिए experiments को meaningful नाम दें और runs पर tags लगाएँ जैसे dataset_version, git_sha, owner, branch।

with mlflow.start_run(run_name="rf-v2") as run:
    mlflow.set_tag("dataset_version", "v1.3")
    mlflow.set_tag("git_sha", "a1b2c3d")
    mlflow.log_param("feature_set", "fset_v5")
    # ...
    

🗃️ Model Registry: Staging → Production

Registry से model versions manage करें, approvals लें और stage transitions track करें। CI/CD pipelines में promote/demote करना भी आसान हो जाता है।

import mlflow
result = mlflow.register_model(
    model_uri="runs:/<run_id>/model",
    name="customer-churn-classifier"
)
# UI से या API से stage = Staging/Production सेट कर सकते हैं
    

🌐 Remote Tracking Server + Artifact Store

Team collaboration के लिए remote server उपयोग करें। Metadata के लिए PostgreSQL और artifacts के लिए S3 जैसे object storage use करें।

# सर्वर मशीन पर (example)
export MLFLOW_TRACKING_URI=http://0.0.0.0:5000
mlflow server 
  --host 0.0.0.0 --port 5000 
  --backend-store-uri postgresql+psycopg2://user:pass@db-host:5432/mlflowdb 
  --default-artifact-root s3://mlflow-artifacts-bucket
    

क्लाइंट साइड पर mlflow.set_tracking_uri("http://server:5000") सेट करें। S3 credentials के लिए env vars configure करें।

🧭 Manual Logging vs Autologging

Manual Logging Autologging
Complete control, custom objects log कर सकते हैं Setup आसान, common params/metrics auto
Code verbose हो सकता है Limited to supported libraries
Complex pipelines के लिए बेहतर Quick baselines के लिए ideal

🔒 Best Practices for Tracking

  • हर run के साथ git commit, dataset hash और feature set id tag करें।
  • Configs को YAML में रखें और उन्हें artifact के रूप में log करें।
  • Sensitive data artifacts में न डालें; PII को redact करें।
  • Experiment naming convention तय करें: project_env_usecase
  • CI में smoke runs रखें ताकि logging break न हो।

🧯 Common Errors & Fixes

  • UI खाली दिख रही है: सही experiment select करें; backend-store-uri वही है क्या जिसे server use कर रहा है।
  • Artifacts upload fail: S3 credentials और bucket policy verify करें; path में spaces से बचें।
  • Mixed runs: Team में सभी लोग same tracking URI और experiment नाम use करें।
  • Version drift: mlflow client और server versions compatible रखें।

📈 From Tracking to Production

जब best run चुना जाए, उसी model को registry से Staging में promote करें, integration tests पास होने पर Production में ले जाएँ, और inference service में load करें। Monitoring stack (Prometheus, Grafana) के साथ feedback loop बनाकर retraining triggers सेट करें — यही MLOps का core है।

निष्कर्ष: MLflow experiment tracking एक developer friendly और enterprise-ready तरीका देता है जिससे आपकी research traceable बनती है, deployment decisions data-driven होते हैं और पूरी ML pipeline reproducible रहती है।