ML lifecycle — ধাপে ধাপে
এই পাঠে যা শিখবেন
- ML lifecycle-এর ৭টি ধাপ — কী, কেন, কখন
- প্রতিটি ধাপে সাধারণ MLOps tooling
- Lifecycle কোথায় সবচেয়ে বেশি ভাঙে — feedback loops, drift, skew
- Iterative নাকি waterfall — ML-এর জন্য কোনটি প্রযোজ্য
১ · Lifecycle কেন আলাদা?
Software lifecycle (SDLC) সাধারণত — requirement → design → code → test → deploy। ML lifecycle-এ অতিরিক্ত একটি dimension — data। Data বদলায়, label বদলায়, behavior বদলায়। তাই ML lifecycle শুধু একটি linear pipeline না, একটি continuous loop।
১) Problem framing → ২) Data collection → ৩) Preprocessing & feature engineering → ৪) Model training → ৫) Evaluation → ৬) Deployment → ৭) Monitoring। Monitoring থেকে শেখা insight ১ ও ২-এ ফিরে যায়।
২ · ধাপ ১ — Problem framing
"ML করব" দিয়ে শুরু করা ভুল। প্রথম প্রশ্ন: "এটা কি আদৌ ML problem?"
- Business problem → ML task: "Customer churn কমাও" → "৩০ দিনের মধ্যে ছেড়ে যাবে কিনা — binary classification।"
- Success metric: ML metric (AUC) এর সাথে business metric (retained revenue) align করতে হবে।
- Constraint: latency budget, interpretability requirement, regulatory limit।
৩ · ধাপ ২ — Data collection
Andrew Ng-এর বিখ্যাত কথা: "Data is the new code." Bangladeshi context-এ এটা আরও সত্য — labeled data কম, quality variable, language coverage অসম্পূর্ণ।
- Source: internal logs, user behavior, third-party API, public dataset, manual labeling।
- Quality: missing value, duplicate, label noise, sampling bias।
- Versioning: dataset কোন version-এ — git-style track (Lesson 10 — DVC)।
- Privacy: PII (NID, mobile, address) — anonymize বা hash।
৪ · ধাপ ৩ — Preprocessing ও feature engineering
Raw data সরাসরি model-এ যায় না। কিছু সাধারণ transformation:
- Missing value imputation (mean, median, model-based)।
- Encoding categorical (one-hot, target encoding, embedding)।
- Scaling/normalization (StandardScaler, MinMax)।
- Text — tokenization, lemmatization, embedding।
- Time-series — lag features, rolling stats।
৫ · ধাপ ৪ ও ৫ — Train ও evaluate
এই ধাপ data scientist-এর "favorite" — কিন্তু lifecycle-এর মাত্র ২০%। Modeling-এ MLOps-এর গুরুত্বপূর্ণ practices:
- Experiment tracking: hyperparameter, metric, artifact log (MLflow — Lesson 8)।
- Reproducibility: seed fix, environment lock, data version pin।
- Cross-validation: single train/test split যথেষ্ট নয়।
- Multiple metrics: accuracy, precision, recall, F1, AUC — context-অনুযায়ী।
- Fairness check: subgroup performance — gender, region, age-এর across।
৬ · ধাপ ৬ — Deployment
Deployment pattern কয়েক ধরনের (বিস্তারিত Lesson 15-এ):
- Batch: রাতে একবার সব prediction generate করে DB-তে রাখা।
- Online API: REST/gRPC endpoint — real-time।
- Streaming: Kafka থেকে event-driven।
- Edge: mobile app বা IoT device-এ on-device inference।
৭ · ধাপ ৭ — Monitoring
Production-এ মডেলের জীবন শুরু — শেষ নয়। Monitor করতে হয় ৪ স্তরে:
- Infrastructure: CPU, memory, latency, error rate।
- Data quality: input schema, missing value, range।
- Model performance: accuracy (যদি ground truth পাওয়া যায়), prediction distribution।
- Business KPI: conversion, fraud rate, retention।
৮ · একটি minimal lifecycle script
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
import joblib
# ১. Data load (versioned by DVC in real setup)
df = pd.read_csv("data/v1/customers.csv")
# ২. Preprocess
X = df.drop(columns=["churned"])
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# ৩. Train
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
# ৪. Evaluate
auc = roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])
print(f"AUC: {auc:.3f}")
# ৫. Save (registry input)
joblib.dump(model, "models/churn-v1.0.0.joblib")
print("✓ Model saved → models/churn-v1.0.0.joblib")
ভাবনার প্রশ্ন
প্র ০১ ML lifecycle-এর কোন ধাপে সবচেয়ে বেশি সময় লাগে এবং কোথায় সবচেয়ে বেশি ভাঙে? দুটো কি একই ধাপ — নাকি ভিন্ন?
এই দুটো প্রশ্নের উত্তর প্রায়ই অনুমানের বিপরীত — যা MLOps-এর জন্য একটি গুরুত্বপূর্ণ insight।
সবচেয়ে বেশি সময় — ধাপ ২ ও ৩ (Data + preprocessing):
- একাধিক industry survey (CrowdFlower 2017, Anaconda 2020, Forbes 2023) — Data scientist-রা ৬০-৮০% সময় data-তে কাটান।
- Data collection (১০-১৫%), cleaning (২৫-৩০%), feature engineering (২৫-৩০%) — modeling মাত্র ১৫-২০%।
- Bangladeshi context-এ আরও বেশি — কারণ labeled Bangla dataset কম, manual labeling ব্যয়বহুল।
সবচেয়ে বেশি ভাঙে — ৪→৬ transition (training → deployment):
- Notebook-এ training accuracy ৯২%, production-এ ৭৮% — এই gap কুখ্যাত।
- কারণ — feature engineering pandas-এ done, production Java/Go/Scala-তে rewrite হয়।
- সামান্য pandas-Java ভিন্নতা (NaN handling, time zone, integer overflow) — কোটি টাকার impact।
দ্বিতীয় frequent ভাঙন — ৬→৭ transition (deploy → monitor):
- মডেল production-এ গেল — কিন্তু কেউ accuracy track করে না (ground truth lag)।
- Drift হলে — silent failure।
- সমাধান — Lesson 24-29-এ বিস্তারিত।
তৃতীয় ভাঙন — ৭→২ feedback loop:
- Monitoring দেখাল drift, কিন্তু retraining trigger হয় না — কারণ pipeline manual।
- সমাধান — automated retraining (Kubeflow, Airflow)।
মূল উপলব্ধি: MLOps সময়ের অসামঞ্জস্যকে স্বীকার করে — "modeling সহজ, data + ops কঠিন।" তাই MLOps platform বেশিরভাগই data + deployment automation-এ — modeling-এ সবচেয়ে কম invest।
প্র ০২ "Iterative" বনাম "waterfall" — ML lifecycle এই দুটোর মধ্যে কোনটা? কেন?
ML lifecycle "iterative" — কিন্তু software-এর agile iteration থেকে ভিন্ন। কারণ — ML iteration শুধু code-এর না, data-rও।
Waterfall ML কেন ভাঙে:
- "আগে সব data collect করি, তারপর model train" — সাধারণত ফলাফল: data যথেষ্ট না, কিন্তু আবার collect করা ব্যয়বহুল।
- "আগে full system বানাই, তারপর deploy" — production-এ realize করা যায় feature engineering ভিন্ন infrastructure দরকার।
ML-এ iteration-এর তিন স্তর:
- Hyperparameter iteration: minutes — same data, different config।
- Data iteration: days — new features, new sources, more labels।
- Concept iteration: weeks/months — problem reframing, label redefining।
"CRISP-DM" — একটি classic iterative ML process:
- Business understanding → Data understanding → Data prep → Modeling → Evaluation → Deployment।
- প্রতিটি phase থেকে আগের phase-এ ফিরে যাওয়ার allow।
- 1996-এ proposed, এখনো relevant।
"MLOps + agile" practical recipe:
- ২-সপ্তাহের sprint, কিন্তু MVP-তে production deployment include — না হলে real feedback আসে না।
- "Demo day" — model ranking improve হলেই enough; deployment success criteria আলাদা।
- Retrospective-এ data quality issue আলাদা track।
মূল উপলব্ধি: ML iterative — কিন্তু iteration cost asymmetric। Hyperparam tweak সস্তা, label redefining ব্যয়বহুল। ভাল MLOps platform এই asymmetry-কে accommodate করে — সস্তা iteration auto, ব্যয়বহুল iteration explicit decision-এ।
প্র ০৩ "Feedback loop" থেকে শেখা data — যেখানে model-এর prediction পরবর্তী data influence করে — এটা কী সমস্যা তৈরি করে?
এটি ML system-এর একটি subtle ও বিপজ্জনক সমস্যা — Sculley et al. (২০১৫) "Hidden Feedback Loops" নামে চিহ্নিত করেছেন।
সমস্যাটা কী:
- Model একটি prediction দেয় (e.g., "এই product user-কে recommend").
- User শুধু recommended product দেখে — অন্য option কম দেখে।
- User click data → পরের training set → model আরও confident একই recommendation-এ।
- "Self-fulfilling prophecy" — model নিজের bias reinforce করে।
উদাহরণ:
- News feed: Facebook algorithm clickbait promote করে → user click → model আরও clickbait দেখায় → polarization।
- Job recommendation: "Engineer = male" historical bias → model female-কে engineering job কম দেখায় → female click কম → bias আরও শক্ত।
- Loan approval: particular demographic-কে reject → তাদের data কম → model আরও uncertain → reject more।
- Bangladesh context: credit scoring model — rural users কম data → reject more → financial inclusion-এর বিপরীত।
সমাধান কৌশল:
- Exploration: ১০-২০% random recommendation — diverse data collect।
- Counterfactual logging: যা দেখানো হয়নি, সেটার counterfactual prediction-ও log।
- Inverse propensity weighting: training-এ rare event বেশি weight।
- Independent test: A/B test-এ random group রাখা — bias measure।
- Multi-armed bandit: exploit-explore balance built-in।
Detection:
- Prediction distribution drift over time।
- Subgroup performance gap বাড়ছে কিনা।
- "Long tail" — rare class-এর accuracy কমছে কিনা।
মূল উপলব্ধি: ML systems-এ data এবং model একে অপরকে shape করে — এটি static system নয়। ভাল MLOps practice এই dynamic-কে স্বীকার করে — exploration, fairness audit, counterfactual analysis built-in। নাহলে model "শিখে" এমন কিছু যা কেউ চায়নি।
প্র ০৪ "Training-serving skew" lifecycle-এর কোন junction-এ ঘটে? এটি detect ও prevent করার কী কী উপায়?
Training-serving skew = training-এর time-এ feature যেভাবে compute হয়, serving-এর time-এ ভিন্নভাবে compute হওয়া। এটি ML production-এর সবচেয়ে কুখ্যাত bug — কারণ silent ও hard to detect।
Junction: ধাপ ৩ (preprocess) ও ধাপ ৬ (deploy)-এর মাঝে। Code rewrite, environment ভিন্নতা, data freshness gap — সব এখানে।
সাধারণ skew sources:
- Code rewrite: training python (pandas), serving Java (DataFrame library)। Behavior একই হওয়া উচিত — কিন্তু edge case ভিন্ন।
- Time travel: training-এ "user-এর গত ৩০ দিনের avg purchase" — কিন্তু serving-এ live aggregate। লুক-এহেড bias।
- Schema drift: upstream data team column rename — training pipeline ঠিক, serving feature missing।
- Default value: training-এ missing → median impute। Serving-এ missing → null → model ভুল।
- Library version: training scikit-learn 1.2, serving 1.3 — minor difference accumulate।
Detection:
- Training-serving feature comparison: এক batch training data → serving system-এ পাঠিয়ে output match।
- Statistical comparison: training feature distribution vs production feature distribution (KS, PSI — Lesson 25)।
- Shadow deployment: নতুন model production traffic-এ silent run, prediction compare।
- Canary metric: production accuracy vs training accuracy — gap > threshold = alert।
Prevention:
- Feature Store (Lesson 12): single transformation code, training ও serving উভয়ের জন্য।
- Single language pipeline: training ও serving একই Python (FastAPI) — rewrite avoid।
- Pickled preprocessing: sklearn Pipeline serialize — feature engineering সহ deploy।
- Schema validation: Great Expectations / Pandera training ও serving উভয় time-এ।
- Reproducible env: Docker image training-এ build, serving-এও same image।
Real example:
- Uber early days — Michelangelo paper-এ feature consistency-কে centerpiece করেছিল।
- Netflix — production feature pipeline same as training, mandated।
মূল উপলব্ধি: Skew lifecycle-এর সবচেয়ে subtle ও damaging bug। MLOps-এর প্রায় অর্ধেক tooling — Feature Store, Model Registry, schema validation — এই bug prevent করতে design করা। যে engineer skew বুঝে handle করতে পারে — সে production ML-এ valuable।
অনুশীলন
-
লিখুন: Pathao-এর "ride pricing" model-এর জন্য ৭-ধাপের ML lifecycle map করুন। প্রতিটি ধাপে input, output, ও ১টি challenge উল্লেখ করুন।
- Problem: demand-supply pricing। Challenge: surge fairness।
- Data: historical rides, weather, traffic, holiday। Challenge: rare event (Eid)।
- Preprocess: lag feature, geo bucket। Challenge: geo data noise।
- Train: gradient boosting / DNN। Challenge: overfit on holidays।
- Evaluate: MAE, MAPE, business metric (acceptance rate)। Challenge: ML metric ↔ business metric mismatch।
- Deploy: low-latency API। Challenge: feature freshness।
- Monitor: drift, complaint rate। Challenge: ground truth slow।
-
চিন্তা: আপনি data scientist। লিড বললেন: "model accuracy ৮৫%, deploy কর।" — আপনি কোন ৩টি প্রশ্ন আগে জিজ্ঞেস করবেন?
- "Test set production data-র representative? Validation strategy কী?"
- "Latency budget কত? প্রতিদিন কত request? Infrastructure কী?"
- "Ground truth কখন পাব? Monitoring + retraining plan কী?"
-
হিসাব: একটি ML team-এর ৫ data scientist বছরে গড়ে ৪টি model production-এ পাঠায়। প্রতিটি model-এ data prep + feature engineering ৫০% সময়। যদি একটি Feature Store invest করায় এই সময় ২৫%-এ কমে — annual capacity gain কত (full-time-equivalent-এ)?
৫ engineer × ৫০% time = ২.৫ FTE-equivalent feature work। কমিয়ে ২৫% = ১.২৫ FTE। Saved = ১.২৫ FTE — অর্থাৎ এক person-year, প্রায় ১৫-২০ লাখ BDT salary equivalent। এর সাথে production stability gain (skew prevention) — উপরি।
আরও পড়ুন
- পাঠ ৩ · MLOps maturity levels পরবর্তী পাঠ Google-এর ০/১/২ maturity scale — কোথায় আছেন, কোথায় যেতে হবে।
- পাঠ ১ · MLOps কী, কেন আগের পাঠ Lifecycle বুঝতে অসুবিধা হলে — আগের পাঠে ফিরে যান।
- পাঠ ১৩ · Pipeline orchestration সম্পর্কিত Lifecycle-কে automated pipeline-এ পরিণত করার tools।
- সব AI Courses ABCL TECH