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.
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.
MLflow jaise tools se har experiment log karo � parameters, metrics, artifacts. Kaunsa model best tha, ye easily milega.
Model versions ka central repository. Production-ready, staging, archived � sab categorized. Version control for models.
Continuous Integration aur Continuous Deployment. Code push karo, automatically test ho, model train ho, aur deploy ho jaye.
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.
# 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!")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
- Step 1:
make_classification()synthetic data generate karta hai � 200 samples, 5 features. - Step 2:
mlflow.set_experiment()ek experiment name define karta hai. Agar experiment nahi hai toh create ho jaata hai. - Step 3:
with mlflow.start_run()context manager ek tracking run start karta hai. Sab logging is block mein hota hai. - Step 4: Model train hota hai, accuracy calculate hoti hai, aur MLflow mein log ho jaata hai � params, metrics, aur model artifact.
- Step 5: MLflow UI mein jaake sab experiments compare kar sakte ho �
mlflow uicommand se server start hota hai.
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.
# 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.
# 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%}")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
- Har experiment track karo: MLflow ya similar tool use karo � kabhi mat socho ki "yaad rahega".
- Model versioning zaroori hai: Production model ka naam aur version hamesha documented rakho.
- Automate testing: Model accuracy threshold set karo � usse neeche deploy mat karo.
- Pipeline define karo: Manual steps ki jagah automated pipeline � repeatable aur reliable.
- Monitoring mat bhulo: Deploy ke baad model ki performance track karte raho � drift detect karo.
Ab Monitoring par chalo � production mein model ki health kaise track karein seekho.