Thuta Learning
Deep Learning with PyTorch
AdvancedAIintermediate

Model Evaluation နှင့် Metrics များ

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Model Evaluation နှင့် Metrics များ concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ PyTorch code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Accuracy ဆိုတာ model တစ်ခုရဲ့ prediction တွေထဲက မှန်တဲ့ အချိုးပါ။ လူတိုင်းအရင်ဆုံး ကြည့်လေ့ရှိတဲ့ metric ဖြစ်ပေမယ့် class imbalance ရှိတဲ့ data မှာတော့ အန္တရာယ်ရှိပါတယ်။ Email ရဲ့ 90% ဟာ spam မဟုတ်တဲ့ dataset တစ်ခုမှာ ဘာမှ မသင်ယူထားတဲ့ model တစ်ခုက 'spam မဟုတ်ဘူး' ဆိုပြီး အမြဲတမ်း predict လုပ်ရုံနဲ့တောင် accuracy 90% ရနိုင်ပါတယ်။ ဒါကြောင့် accuracy တစ်ခုတည်းနဲ့ model က တကယ်အသုံးဝင်လား ဆိုတာ ခွဲခြားလို့ မရပါဘူး။ Confusion matrix — true positive, false positive, true negative, false negative အရေအတွက်တွေ — ဟာ ပိုကောင်းတဲ့ metric တွေအားလုံးရဲ့ အခြေခံ ဖြစ်ပါတယ်။ Precision (positive လို့ ခန့်မှန်းလိုက်တဲ့ အားလုံးထဲက ဘယ်နှစ်ခုက အမှန်ပါလဲ) နဲ့ recall (တကယ် positive ဖြစ်တဲ့ အားလုံးထဲက ဘယ်နှစ်ခုကို model က မှန်အောင် ဖမ်းမိလဲ) တို့ဟာ ကွဲပြားတဲ့ အမှားအမျိုးအစားနှစ်ခုကို ဖော်ပြပြီး အချင်းချင်း trade-off ဖြစ်ကြပါတယ် — model ကို ပိုပြီး positive လို့ flag ခိုင်းလိုက်ရင် recall တက်ပေမယ့် precision ကျလေ့ရှိပါတယ်။ ဘယ်ဟာက ပိုအရေးကြီးလဲဆိုတာ အမှားတစ်ခုချင်းစီရဲ့ ကုန်ကျစရိတ်ပေါ် မူတည်ပါတယ်။ Real email ကို spam ထင်ပြီး ဖျောက်ထားတဲ့ spam filter ဟာ spam အချို့ကို ဖြတ်သန်းခွင့်ပြုမိတာထက် ဆိုးပါတယ်၊ ဒါကြောင့် precision ကို ဦးစားပေးသင့်ပါတယ်။ ဒါပေမယ့် cancer screening model တစ်ခုမှာတော့ တကယ်ရှိတဲ့ case ကို လွတ်သွားခြင်းဟာ ကျန်းမာတဲ့ လူနာကို follow-up ခေါ်မိတာထက် ပိုဆိုးလွန်းလို့ recall ကို ဦးစားပေးသင့်ပါတယ်။ Precision နဲ့ recall နှစ်ခုစလုံးရဲ့ harmonic mean ဖြစ်တဲ့ F1 score ကတော့ ဒီနှစ်ခုကို နံပါတ်တစ်ခုတည်းအဖြစ် ပေါင်းစပ်ပေးပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Tutorial Platform မှာ lesson discussion တွေထဲက ရိုင်းစိုင်းတဲ့ comment တွေကို ဖမ်းပြီး moderator ကို alert ပေးမယ့် sentiment classifier တစ်ခု ရှိတယ်လို့ စဉ်းစားကြည့်ပါ။ comment အများစုက neutral (သို့) positive ဖြစ်တာကြောင့် ရိုင်းစိုင်းတဲ့ comment class ဟာ သဘာဝအရ ရှားပါတယ် — ဒါဟာ accuracy က လိမ်ညာနိုင်တဲ့ imbalanced setting အတိအကျပါပဲ။ Accuracy ကိုပဲ ကြည့်မယ်ဆိုရင် ဘာမှ flag မလုပ်တဲ့ classifier ဟာ အလုပ်လုပ်နေသလို ထင်ရနိုင်ပါတယ်။ တကယ်အရေးကြီးတာက recall (ရိုင်းစိုင်းတဲ့ comment တွေကို ဖမ်းမိပြီး review ရောက်အောင် လုပ်နိုင်ခြင်း) နဲ့ precision (moderator ကို false alarm များစွာ မပေးမိခြင်း) ကြားက ဟန်ချက်ညီမှုပါ။ Labeled comment held-out set ပေါ်မှာ confusion matrix ကနေတွက်ထားတဲ့ precision, recall, F1 ကို accuracy နဲ့အတူ report လုပ်တာမှသာ team အနေနဲ့ classifier က deployment လုပ်ဖို့ အဆင်သင့်ဖြစ်မဖြစ် သိနိုင်မှာပါ။

အတူတူ စမ်းရေးကြည့်မယ်

python
import torch

# 100 samples: 90 "not spam" (0), 10 "spam" (1) -- realistic class imbalance
actual = torch.cat([torch.zeros(90, dtype=torch.long), torch.ones(10, dtype=torch.long)])
predicted = torch.zeros(100, dtype=torch.long)

# Model wrongly flags 2 legitimate emails as spam (false positives)
predicted[3] = 1
predicted[7] = 1

# Model correctly catches only 3 of the 10 real spam emails (7 false negatives)
predicted[90] = 1
predicted[91] = 1
predicted[92] = 1

# Confusion matrix counts, computed with tensor comparisons
TP = ((predicted == 1) & (actual == 1)).sum().item()
TN = ((predicted == 0) & (actual == 0)).sum().item()
FP = ((predicted == 1) & (actual == 0)).sum().item()
FN = ((predicted == 0) & (actual == 1)).sum().item()

accuracy = (TP + TN) / actual.numel()
precision = TP / (TP + FP)
recall = TP / (TP + FN)
f1 = 2 * precision * recall / (precision + recall)

print(f"Confusion matrix -> TP: {TP}, FP: {FP}, FN: {FN}, TN: {TN}")
print(f"Accuracy:  {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall:    {recall:.4f}")
print(f"F1 Score:  {f1:.4f}")
You should see
Confusion matrix -> TP: 3, FP: 2, FN: 7, TN: 88
Accuracy:  0.9100
Precision: 0.6000
Recall:    0.3000
F1 Score:  0.4000
Accuracy 91% ရှိပေမယ့် model ဟာ တကယ့် spam ရဲ့ 30% ကိုပဲ ဖမ်းမိတာ (recall) ဖြစ်ပြီး accuracy ဖုံးကွယ်ထားတဲ့ အားနည်းချက်ကို precision နဲ့ recall က ဖော်ထုတ်ပေးပါတယ်။

၅ မိနစ် စမ်းကြည့်

Code ကို ပြင်ပြီး model ဟာ positive 10 ခုထဲက 3 ခုအစား 8 ခု ဖမ်းမိအောင် လုပ်ကြည့်ပါ (false positive 2 ခုကိုတော့ မပြောင်းထားပါနဲ့)၊ metric လေးခုလုံးကို ပြန်တွက်ပြီး accuracy ဟာ သိပ်မပြောင်းလဲဘဲ recall ကတော့ ဘာကြောင့် သိသိသာသာ တက်လာလဲဆိုတာ comment နဲ့ ရှင်းပြပါ။

သတိလေးတစ်ချက်

Imbalanced dataset ပေါ်မှာ accuracy တစ်ခုတည်းကို report လုပ်ပြီး model အလုပ်လုပ်တယ်လို့ ကောက်ချက်ချမိတာ — တကယ်တော့ majority class ကိုပဲ အမြဲ predict လုပ်နေတာ ဖြစ်နိုင်ပါတယ်။

Precision (သို့) recall ရဲ့ denominator (TP+FP သို့ TP+FN) က zero ဖြစ်နေချိန် စားခြင်း — positive ကို လုံးဝ predict မလုပ်တဲ့ model တစ်ခုအတွက် precision ဟာ undefined ဖြစ်ပြီး 0 အဖြစ် အလိုအလျောက် မမှတ်သင့်ပါဘူး။

Wikipedia — Precision and recallDeep Learning

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Imbalanced dataset ပေါ်မှာ accuracy တစ်ခုတည်းကို report လုပ်ပြီး model အလုပ်လုပ်တယ်လို့ ကောက်ချက်ချမိတာ — တကယ်တော့ majority class ကိုပဲ အမြဲ predict လုပ်နေတာ ဖြစ်နိုင်ပါတယ်။
  • Precision (သို့) recall ရဲ့ denominator (TP+FP သို့ TP+FN) က zero ဖြစ်နေချိန် စားခြင်း — positive ကို လုံးဝ predict မလုပ်တဲ့ model တစ်ခုအတွက် precision ဟာ undefined ဖြစ်ပြီး 0 အဖြစ် အလိုအလျောက် မမှတ်သင့်ပါဘူး။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Code ကို ပြင်ပြီး model ဟာ positive 10 ခုထဲက 3 ခုအစား 8 ခု ဖမ်းမိအောင် လုပ်ကြည့်ပါ (false positive 2 ခုကိုတော့ မပြောင်းထားပါနဲ့)၊ metric လေးခုလုံးကို ပြန်တွက်ပြီး accuracy ဟာ သိပ်မပြောင်းလဲဘဲ recall ကတော့ ဘာကြောင့် သိသိသာသာ တက်လာလဲဆိုတာ comment နဲ့ ရှင်းပြပါ။

You'll know it worked when: Confusion matrix -> TP: 3, FP: 2, FN: 7, TN: 88 Accuracy: 0.9100 Precision: 0.6000 Recall: 0.3000 F1 Score: 0.4000 Accuracy 91% ရှိပေမယ့် model ဟာ တကယ့် spam ရဲ့ 30% ကိုပဲ ဖမ်းမိတာ (recall) ဖြစ်ပြီး accuracy ဖုံးကွယ်ထားတဲ့ အားနည်းချက်ကို precision နဲ့ recall က ဖော်ထုတ်ပေးပါတယ်။

Model Evaluation နှင့် Metrics များ | Thuta Learning