Lesson 06 � Advanced

MLOPS: ML
OPERATIONS.

MLOps se ML pipeline automate hoti hai � data se model tak sab automated. DevOps ka ML version. Experiments track karo, models version karo, aur production mein reliable deploy karo.

? 22 min✓ Intermediate✓ Prerequisite: Cloud Deployment

WHY: MLOps kyun zaroori hai?

Jab aap ML model train karte ho, tab sab kuch manually hota hai � data load, model train, evaluate, deploy. But production mein ye sab repeat hota hai. Naya data aata hai, model retrain hota hai, versions badalte hain. Agar manually karo toh errors aayenge aur time lagega. MLOps ye sab automate kar deta hai � jaise DevOps ne web deployment automate kiya, waise MLOps ne ML deployment automate kiya.

EXPERIMENT TRACKING

MLflow jaise tools se har experiment log karo � parameters, metrics, artifacts. Kaunsa model best tha, ye easily milega.

MODEL REGISTRY

Model versions ka central repository. Production-ready, staging, archived � sab categorized. Version control for models.

CI/CD

Continuous Integration aur Continuous Deployment. Code push karo, automatically test ho, model train ho, aur deploy ho jaye.

PIPELINE

ML workflow ka automation. Data preprocessing se lekar model serving tak � ek defined pipeline jo reliable aur repeatable hai.

Concepts ka deep dive

Experiment Tracking � MLflow

MLflow ek open-source platform hai jo ML experiments ko track karta hai. Har training run ke parameters, metrics, aur model artifacts log karte ho. Baad mein compare karte ho ki kaunsa configuration best result diya. Ye manual Excel tracking ka replacement hai.

Model Registry � Version Control for Models

Model Registry ek centralized place hai jahan models ka version management hota hai. Har model ko ek naam milta hai, stages assign hote hain � "Staging", "Production", "Archived". Team members ko pata hota hai ki kaunsa model kab deploy hua.

CI/CD for ML

Traditional CI/CD mein code test hota hai aur deploy hota hai. ML mein extra steps hain � data validation, model training, evaluation metrics check. Agar model accuracy threshold se neeche gayi toh deploy nahi hona chahiye. Ye sab automated hota hai.

ML Pipeline

Pipeline ek sequence of steps hai jo ML workflow ko define karta hai. Data ingestion ✓ Preprocessing ✓ Training ✓ Evaluation ✓ Deployment. Agar ek step mein kuch badla toh pipeline automatically baaki steps bhi run karta hai.

Code: MLflow se experiment track karo

Ab ek real example dekhte hain � MLflow use karke experiment tracking. Ye code directly copy karke run kar sakte ho.

python
# MLflow experiment tracking
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Create data
X, y = make_classification(n_samples=200, n_features=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# Log experiment
mlflow.set_experiment("ai-engineering-demo")

with mlflow.start_run():
 model = RandomForestClassifier(n_estimators=100)
 model.fit(X_train, y_train)
 accuracy = model.score(X_test, y_test)
 
 mlflow.log_param("n_estimators", 100)
 mlflow.log_metric("accuracy", accuracy)
 mlflow.sklearn.log_model(model, "model")
 
 print(f"Accuracy: {accuracy:.2%}")
 print("Experiment logged to MLflow!")
Code ka breakdown: mlflow.set_experiment() experiment ka naam set karta hai. mlflow.start_run() se ek new run start hota hai. log_param() parameters log karta hai, log_metric() metrics. log_model() pura model save karta hai MLflow format mein.

How it works � step by step

Model Registry � production ke liye

Model track karna sirf beginning hai. Production mein aapko model ko stage karna padta hai � pehle testing mein, phir production mein. Model Registry ye workflow manage karta hai.

python
# Model Registry setup
from mlflow import MlflowClient

client = MlflowClient()

# Register model
model_name = "ai-predictor"
model_uri = "runs://model"

client.create_registered_model(model_name)
client.create_model_version(
 name=model_name,
 source=model_uri,
 description="RandomForest classifier for demo"
)

# Move to staging
client.transition_model_version_stage(
 name=model_name,
 version=1,
 stage="Staging"
)

# Move to production
client.transition_model_version_stage(
 name=model_name,
 version=1,
 stage="Production"
)

print(f"Model {model_name} v1 moved to Production!")

Pipeline automation

Manual steps se kaam nahi chalta. Ek ML pipeline define karo jo automatically data load kare, preprocess kare, train kare, evaluate kare, aur deploy kare.

python
# Simple ML Pipeline with steps
import mlflow
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification

# Create pipeline
pipeline = Pipeline([
 ('scaler', StandardScaler()),
 ('classifier', RandomForestClassifier(n_estimators=100))
])

# Generate data
X, y = make_classification(n_samples=300, n_features=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# Train and track
with mlflow.start_run(run_name="pipeline-run"):
 pipeline.fit(X_train, y_train)
 score = pipeline.score(X_test, y_test)
 
 mlflow.log_metric("accuracy", score)
 mlflow.sklearn.log_model(pipeline, "pipeline_model")
 
 print(f"Pipeline accuracy: {score:.2%}")
MLflow playgroundExperiment tracking try karo
Run Python dabayein

Exercise: Test your knowledge

Quick check

MLOps kya hai? Ek line mein batao.

Sochho: DevOps ka ML version � kya automate hota hai ML mein?

MLOps Best Practices

MLOps complete?

Ab Monitoring par chalo � production mein model ki health kaise track karein seekho.