Loss function কী, কেন দরকার
এই পাঠে যা শিখবেন
- Loss function-এর ভূমিকা — কেন training এর কেন্দ্র
- MSE, MAE — regression-এর জন্য
- BCE, Cross-Entropy — classification-এর জন্য
- Loss function choose করার criteria
- PyTorch-এ built-in loss function ব্যবহার
১ · Loss Function কী
একটি neural network-এর forward pass একটি prediction $\hat{y}$ দেয়। আসল target $y$। দু'টোর মধ্যে পার্থক্য — এক single number-এ summarize — সেটাই loss।
$$L = \mathcal{L}(\hat{y}, y)$$
- $L$ ছোট = prediction ভাল।
- $L$ বড় = prediction খারাপ।
- $L = 0$ = perfect prediction।
- Training-এর লক্ষ্য — $L$ কমানো (gradient descent-এ)।
১) Loss: single example-এ error।
২) Cost: পুরো dataset-এ গড় (batch loss)।
৩) Objective: training-এ যা minimize করা হচ্ছে (cost + regularization)।
ব্যবহারিক-এ — interchangeably ব্যবহৃত হয়।
২ · Mean Squared Error (MSE) — Regression
$$\text{MSE} = \frac{1}{N} \sum_{i=1}^{N} (\hat{y}_i - y_i)^2$$
- Range: $[0, \infty)$।
- Properties: smooth, differentiable, convex (linear regression-এ)।
- Square: বড় error-কে disproportionately punish।
- Use case: house price prediction, temperature forecast, etc।
সমস্যা:
- Outlier-এর প্রতি sensitive — একটি bad sample পুরো training distort করতে পারে।
- Gradient large error-এ বড় — instability।
৩ · Mean Absolute Error (MAE)
$$\text{MAE} = \frac{1}{N} \sum_{i=1}^{N} |\hat{y}_i - y_i|$$
- Outlier-এর প্রতি robust।
- Gradient — constant magnitude (sign-only)।
- $y = \hat{y}$-এ non-differentiable (subgradient)।
- Slow convergence-এর সম্ভাবনা।
৪ · Huber Loss — MSE + MAE-এর মাঝামাঝি
$$L_\delta(\hat{y}, y) = \begin{cases} \frac{1}{2}(\hat{y} - y)^2 & |\hat{y} - y| \leq \delta \\ \delta(|\hat{y} - y| - \frac{1}{2}\delta) & \text{otherwise} \end{cases}$$
- ছোট error-এ MSE-এর মতো — smooth, fast।
- বড় error-এ MAE-এর মতো — robust।
- $\delta$ — hyperparameter।
- Robust regression-এর favorite।
৫ · Binary Cross-Entropy (BCE) — Binary Classification
$$\text{BCE} = -\frac{1}{N} \sum_{i=1}^{N} \left[ y_i \log(\hat{y}_i) + (1 - y_i) \log(1 - \hat{y}_i) \right]$$
- $\hat{y}_i \in (0, 1)$ — sigmoid output।
- $y_i \in \{0, 1\}$ — true label।
- Information theory: probability distribution-এর মধ্যে cross-entropy।
- Confident wrong answer — heavy penalty (log goes to infinity)।
উদাহরণ:
- $y = 1, \hat{y} = 0.99$: $L = -\log(0.99) \approx 0.01$ — ভাল।
- $y = 1, \hat{y} = 0.5$: $L = -\log(0.5) \approx 0.69$ — uncertain।
- $y = 1, \hat{y} = 0.01$: $L = -\log(0.01) \approx 4.6$ — খারাপ।
৬ · Cross-Entropy Loss — Multi-class Classification
$$\text{CE} = -\sum_{c=1}^{C} y_c \log(\hat{y}_c)$$
- $y$ — one-hot vector ($K$ class-এ একটি ১, বাকি ০)।
- $\hat{y}$ — softmax output (probability distribution)।
- সরলীকৃত: শুধু true class-এর predicted probability-র negative log।
- $\text{CE} = -\log(\hat{y}_{\text{true class}})$।
৭ · PyTorch-এ Loss Functions
import torch
import torch.nn as nn
# Regression
y_true = torch.tensor([3.0, 5.0, 7.0])
y_pred = torch.tensor([2.5, 5.2, 7.8])
print("MSE:", nn.MSELoss()(y_pred, y_true).item())
print("MAE:", nn.L1Loss()(y_pred, y_true).item())
print("Huber:", nn.HuberLoss(delta=1.0)(y_pred, y_true).item())
# Binary classification (use BCEWithLogitsLoss for stability)
logits = torch.tensor([2.0, -1.0, 0.5])
targets = torch.tensor([1.0, 0.0, 1.0])
print("\nBCE:", nn.BCEWithLogitsLoss()(logits, targets).item())
# Multi-class classification
logits = torch.tensor([[2.0, 0.5, -1.0],
[0.1, 1.5, 0.3]])
targets = torch.tensor([0, 1]) # class indices, NOT one-hot
print("CE:", nn.CrossEntropyLoss()(logits, targets).item())
৮ · Loss Function Choose কীভাবে
| Task | Output | Loss |
|---|---|---|
| House price (regression) | scalar | MSE বা Huber |
| Spam detection (binary) | probability | BCEWithLogitsLoss |
| MNIST digit (multi-class) | 10 logits | CrossEntropyLoss |
| Image tagging (multi-label) | K logits | multiple BCE |
| Imbalanced object detection | scores | Focal Loss |
| Speech recognition | sequence | CTC Loss |
| Generative model | distribution | KL Divergence / NLL |
| Embedding learning | vectors | Triplet / Contrastive |
৯ · Loss-এর সৌন্দর্য — Maximum Likelihood
MSE ও Cross-Entropy — দু'টোই Maximum Likelihood Estimation (MLE)-এর special case।
- MSE: $y \sim \mathcal{N}(\hat{y}, \sigma^2)$ ধরলে — log-likelihood maximize করা = MSE minimize।
- Cross-Entropy: categorical distribution-এ MLE।
এই unifying perspective probabilistic ML-এর foundation।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ Cross-Entropy কেন classification-এর default? MSE classification-এ কেন কাজ করে না (বা খারাপ কাজ করে)?
DL-এর সবচেয়ে important "কেন" — যা proof তেমন তবু intuition গভীর।
MSE-এর সমস্যা classification-এ:
- Saturation: Sigmoid + MSE — output close to 0 বা 1-এ gradient নিভে যায়।
- Slow learning when wrong: Confident wrong prediction-এ — gradient ছোট, network ধীরে correct হয়।
- Non-convex (with sigmoid): multiple local minima।
Cross-Entropy যা সমাধান করে:
- Strong gradient on confident wrong: $-\log(p)$ where $p \to 0$ — gradient very large।
- Convex (with softmax): single global minimum।
- Probabilistic interpretation: probability distribution-এর মধ্যে comparison।
- Maximum likelihood: classical statistics-এর সাথে align।
গাণিতিকভাবে why:
- Sigmoid output $p = \sigma(z)$।
- MSE: $\frac{\partial L}{\partial z} = (p - y) \cdot p(1-p)$ — tiny when $p$ saturates।
- BCE: $\frac{\partial L}{\partial z} = p - y$ — clean, never saturates।
Information theory perspective:
- Cross-Entropy $H(y, \hat{y})$ — distribution similarity।
- Minimizing CE = matching predicted to true distribution।
- Shannon-এর coding theory direct connection।
কখন MSE classification-এ ব্যবহার:
- Soft labels: targets continuous (0.7, 0.2, 0.1 instead of one-hot)।
- Knowledge distillation: teacher logits target — MSE loss নিয়মিত।
- Regression-as-classification: sometimes মিশ্রণ।
- সাধারণত — cross-entropy preferred।
Empirical evidence:
- ImageNet-এ — CE-তে ৭৫%+ accuracy, MSE-তে ৭০% (similar architecture)।
- Convergence speed — CE ২-৩x faster।
- প্রতিটি SOTA classifier — CE-based।
Recent counterexample:
- Hui & Belkin ২০২১ — "Evaluation of Neural Architectures Trained with Square Loss vs Cross-Entropy in Classification Tasks" — MSE-ও সমান কাজ করে কিছু settings-এ।
- Convention বাঁচায় — কিন্তু absolute requirement না।
মূল উপলব্ধি: MSE possible কিন্তু CE preferred। Gradient property, probabilistic interpretation, empirical performance — তিনটোই CE-এর পক্ষে। Modern DL-এর foundational choice।
প্র ০২ Class imbalance (যেমন ৯৯% sample healthy, ১% disease) — সাধারণ loss function-এ কী সমস্যা হয়? Focal Loss, weighted CE — কীভাবে সমাধান দেয়?
Real-world ML-এর সবচেয়ে common challenge — যা academic dataset-এ rarely দেখা যায়।
Imbalance-এর প্রভাব:
- ৯৯% healthy → model শুধু "healthy" predict করলেই ৯৯% accuracy।
- Loss minimum-এ পৌঁছায় — কিন্তু minority class miss।
- Disease detection useless।
- Medical, fraud, anomaly — সব এই problem-এ ভোগে।
সমাধান (১) — Resampling:
- Oversampling minority — duplicate করুন।
- Undersampling majority — drop করুন।
- SMOTE — synthetic minority generation।
- সমস্যা: overfitting risk, information loss।
সমাধান (২) — Weighted Loss:
- $L = -w_1 \cdot y \log(\hat{y}) - w_0 (1-y) \log(1-\hat{y})$।
- $w_1 = \frac{N_0}{N_1}$ — inverse frequency।
- Minority class-এর gradient amplified।
- PyTorch:
nn.CrossEntropyLoss(weight=class_weights)।
সমাধান (৩) — Focal Loss:
$$\text{FL}(p_t) = -(1 - p_t)^\gamma \log(p_t)$$
- Lin et al. ২০১৭ — RetinaNet-এর জন্য।
- $(1 - p_t)^\gamma$ — easy example down-weighted, hard example focused।
- $\gamma = 2$ — common choice।
- Object detection-এ background imbalance handle।
Mechanics:
- $p_t = 0.9$ (easy positive): loss factor $(0.1)^2 = 0.01$ — minimal।
- $p_t = 0.5$ (uncertain): loss factor $(0.5)^2 = 0.25$ — full attention।
- Effect — gradient hard examples-এ concentrated।
Other approaches:
- Threshold tuning: default 0.5 না — minority recall-এর জন্য lower।
- Two-stage training: first balanced subset, then full data।
- Cost-sensitive learning: domain-specific weights।
- One-class classification: anomaly detection paradigm।
Metrics matter equally:
- Accuracy — useless on imbalanced।
- Precision, Recall, F1 — minority-aware।
- ROC-AUC, PR-AUC — threshold-independent।
- Cost-aware metrics — business value।
Bangladesh examples:
- bKash fraud: ০.১% transactions fraudulent। Focal/weighted loss।
- TB detection (X-ray): ~৫% positive rate। Class weighting।
- Loan default: ১০-২০% default। Slight imbalance, weighted CE।
- Anomaly detection (network security): ০.০১% anomaly। Specialized methods।
Empirical guideline:
- ৩:১ পর্যন্ত — সাধারণ loss + weighted।
- ১০:১+ → Focal Loss preferred।
- ১০০:১+ → resampling + Focal + threshold tuning।
- ১০০০:১+ → anomaly detection paradigm।
মূল উপলব্ধি: Imbalance handling — production ML-এর core skill। Loss function tweaking — first weapon। কিন্তু metric, threshold, business context — সবই matter। Single technique sufficient rarely।
প্র ০৩ Loss curve plot করার অভ্যাস ML practitioner-দের কেন গুরুত্বপূর্ণ? Healthy vs unhealthy loss curve — কীভাবে চেনা যায়?
Practical ML debugging-এর সবচেয়ে fundamental skill।
কেন plot:
- Training-এর "যাত্রা" দেখা।
- Hyperparameter সঠিক কিনা।
- Convergence-এর evidence।
- Overfitting/underfitting detect।
- Bug-এর প্রথম signal।
সাধারণত plot করা:
- Training loss vs epoch।
- Validation loss vs epoch।
- Learning rate (যদি schedule)।
- Gradient norm।
- Accuracy/metric।
Healthy curve characteristics:
- Train loss steady decline।
- Val loss decline (slightly behind train)।
- Gap small (< 10-20%)।
- Both plateau eventually।
- No sudden spikes।
Unhealthy patterns (১) — Overfitting:
- Train loss continue declining।
- Val loss starts increasing।
- Gap widens।
- Solution: regularization, dropout, early stopping, more data।
Unhealthy patterns (২) — Underfitting:
- Both losses high।
- Plateau early।
- Gap small (both bad)।
- Solution: bigger model, more features, longer training।
Unhealthy patterns (৩) — Diverging:
- Loss exploding (NaN, Inf)।
- Gradient explosion।
- Solution: lower LR, gradient clipping, proper init।
Unhealthy patterns (৪) — Stuck:
- Loss flat from epoch 1।
- No learning at all।
- Causes: bug in code, wrong loss, dead ReLU, wrong target encoding।
Unhealthy patterns (৫) — Oscillating:
- Wild fluctuations।
- No clear trend।
- Solution: lower LR, larger batch, smoother optimizer।
Unhealthy patterns (৬) — Spiky:
- Mostly declining, but periodic spikes।
- Causes: bad batch, NaN gradient, learning rate too high।
- Solution: gradient clipping, robust optimizer।
Tools:
- TensorBoard: PyTorch built-in।
- Weights & Biases: hosted, collaborative।
- MLflow: open-source, self-hosted।
- Matplotlib: simple custom plots।
Best practices:
- Log per-batch (smooth chart)।
- Log per-epoch (high-level view)।
- Smoothing (EMA) for noise reduction।
- Y-axis log scale for wide range।
- Multiple runs comparison।
"First epoch" sanity check:
- Random init-এ initial loss expected: $-\log(1/C)$ for $C$-class CE।
- 10-class: ~2.30।
- Initial loss off — bug in setup।
মূল উপলব্ধি: Loss curve = ML model-এর "vital signs"। Doctor যেমন pulse, BP দেখে — ML practitioner loss curve দেখে। Plot না করা = blind training। Bug-এর ৮০% loss curve থেকে identify।
প্র ০৪ "Loss" আর "metric" দু'টো কেন আলাদা? Training loss low কিন্তু production metric খারাপ — এটা কেন ঘটে এবং কী করতে হবে?
ML-এর সবচেয়ে gripping practical issue — যা academic paper-এ underrepresented।
Loss vs Metric — পার্থক্য:
- Loss: training-এ optimize করা — differentiable, mathematical।
- Metric: business/user-এর কাছে যা matter — accuracy, F1, latency।
- Different optimization targets।
উদাহরণ — disconnect:
- Spam classifier — Loss: BCE, Metric: F1, precision।
- Recommendation — Loss: MSE on rating, Metric: click-through rate।
- Search ranking — Loss: pairwise, Metric: NDCG।
- Object detection — Loss: focal + L1, Metric: mAP।
কেন direct metric optimize না:
- Non-differentiable: accuracy, F1 — discrete, gradient নাই।
- Compute cost: NDCG-এর সব ranking compute expensive।
- Sparse: ছোট batch-এ noisy estimate।
Surrogate loss-এর role:
- BCE → accuracy-এর smooth proxy।
- MSE → ranking error proxy।
- Triplet loss → retrieval accuracy।
- "Surrogate" — true objective-এর approximation।
Mismatch examples:
- Imbalanced binary: low BCE possible — but precision/recall poor।
- Calibration: low CE — but confidence overestimated।
- Distribution shift: training loss low, production accuracy poor।
- User experience: high accuracy কিন্তু ৫ second latency = bad UX।
Solutions (১) — Better surrogate:
- Soft F1 loss — F1-এর smooth approximation।
- LambdaRank — ranking-এর gradient-based।
- Focal loss — imbalance-aware।
Solutions (২) — Multi-objective:
- Loss = $L_{\text{primary}} + \lambda L_{\text{secondary}}$।
- Accuracy + calibration + fairness।
- Pareto frontier exploration।
Solutions (৩) — Threshold tuning:
- Same model, different decision threshold — different metric।
- Validation set-এ optimal threshold বের করুন।
- Calibration techniques।
Solutions (৪) — Reinforcement Learning:
- Direct metric optimization (REINFORCE)।
- RLHF — human preference reward।
- Policy gradient — non-differentiable metric possible।
Production reality check:
- Always validation-এ business metric track।
- A/B test in production — true judgment।
- User feedback loop।
- Online metric monitoring।
Goodhart's Law:
- "When a measure becomes a target, it ceases to be a good measure."
- Loss-কে blindly optimize → metric exploitation।
- Continuous metric review essential।
Bangladesh case study:
- Pathao ride-matching — model loss low, but cancellation rate high। User experience metric mismatch।
- Daraz recommendation — engagement low despite low MSE। Click-through optimization needed।
- Bangla translation — BLEU high, native speaker quality poor। Subjective metric mismatch।
মূল উপলব্ধি: Loss = math, Metric = business। প্রতিটি ML project-এ — দু'টো-ই carefully define করতে হয়। Training loss-এ blind faith = production failure। ML engineering — gap bridge করা।
অনুশীলন
-
Compute: $y = 1, \hat{y} = 0.7$ — BCE কত?
$L = -(1 \cdot \log(0.7) + 0 \cdot \log(0.3)) = -\log(0.7) \approx 0.357$।
-
Choose: প্রতিটি task-এর জন্য সঠিক loss:
- (ক) ছবিতে কয়টি বস্তু (count)
- (খ) Email subject categorize (5 category)
- (গ) Click prediction (yes/no)
- (ঘ) Stock price tomorrow (with rare crashes)
- (ক) Poisson regression বা MSE
- (খ) CrossEntropy
- (গ) BCEWithLogits
- (ঘ) Huber (robust to crashes)
-
Debug: Training loss = 0.001, validation loss = 2.5। কী সমস্যা, কী করবেন?
Severe overfitting — model train data মুখস্থ করেছে।
Solutions:
- Regularization (L2 weight decay, dropout) যোগ করুন।
- Smaller model।
- Early stopping — best validation point-এ stop।
- Data augmentation।
- More training data যদি possible।
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ০৮ · PyTorch পরিচিতি — tensor পরবর্তী পাঠ DL-এর সব tensor-এ — base API।
- পাঠ ০৬ · Forward pass আগের পাঠ যা loss-এর input দেয়।
- পাঠ ০৯ · Backpropagation পরের module Loss থেকে gradient — training-এর engine।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।