Thuta Learning
Deep Learning with PyTorch
BasicAIintermediate

Autograd နှင့် Gradients

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

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

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

Neural network train လုပ်တယ်ဆိုတာ အခြေခံအားဖြင့် optimization loop တစ်ခုပါ—parameter တစ်ခုချင်းစီအတွက် loss ကို နည်းနည်းလျှော့ဖို့ ဘယ်လိုပြောင်းရမလဲဆိုတာ ထပ်ခါထပ်ခါ မေးနေတာပါ။ ဒီမေးခွန်းကို derivative တစ်ခုက အတိအကျ ဖြေပေးနိုင်တယ်—parameter တစ်ခုနဲ့ ပတ်သက်တဲ့ loss ရဲ့ gradient က အမြန်ဆုံး တက်လာမယ့် direction နဲ့ steepness ကို ပြောပြတယ်၊ ဒါကြောင့် parameter ကို ဆန့်ကျင်ဘက် direction ဘက်ကို အနည်းငယ် ရွှေ့လိုက်ရင် loss လျော့ကျသွားတယ်။ Architecture အသစ်တိုင်းအတွက် ဒီ derivative တွေကို လက်ဖြင့် တွက်ရမယ်ဆိုရင် လက်တွေ့မဖြစ်နိုင်ဘူး—parameter သန်းချီပါတဲ့၊ layer ဆယ်ချီစုစည်းထားတဲ့ network တစ်ခုအတွက် chain rule ကို ထပ်ခါထပ်ခါ သုံးရမယ်၊ algebra အမှားတစ်ခုတည်းနဲ့တောင် training ချက်ချင်း ပျက်စီးသွားနိုင်တယ်။ PyTorch ရဲ့ autograd ကတော့ code run နေစဉ် `requires_grad=True` ပါတဲ့ tensor တွေအပေါ် လုပ်ဆောင်တဲ့ operation တိုင်းကို computational graph ထဲ record လုပ်ထားခြင်းဖြင့် ဒီပြဿနာကို ဖြေရှင်းပေးတယ်။ Scalar တန်ဖိုးတစ်ခု (loss လိုမျိုး) ပေါ်မှာ `.backward()` ခေါ်လိုက်တာက ဒီ graph ကို နောက်ပြန်လျှောက်သွားပြီး၊ record လုပ်ထားတဲ့ step တိုင်းမှာ chain rule ကို auto-apply လုပ်ကာ tensor တစ်ခုစီရဲ့ `.grad` attribute ထဲကို ရလဒ်ကို စုပေါင်းသိမ်းဆည်းပေးတယ်။ ဒါကြောင့် ဘယ် architecture မျိုးမဆို gradient ကို လူ ကိုယ်တိုင် derive လုပ်စရာမလိုဘဲ train လုပ်နိုင်ခြင်း ဖြစ်ပါတယ်။

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

Tutorial Platform က feedback comment တွေအပေါ် sentiment classifier တစ်ခု train လုပ်တဲ့အခါ 'model က ဒီ comment ရဲ့ sentiment ကို မှားခန့်ခဲ့တယ်' ဆိုတဲ့ အချက်ကို network ထဲက weight တိုင်းအတွက် တိကျတဲ့ update တစ်ခုအဖြစ် ပြောင်းပေးတာက autograd ပါပဲ—ဒါမရှိဘူးဆိုရင် engineer တွေက architecture ပြောင်းလဲမှု တိုင်းရဲ့ layer တိုင်းအတွက် gradient ကို လက်ဖြင့် derive လုပ်ရမှာမို့ experiment လုပ်ဖို့ မဖြစ်နိုင်လောက်အောင် နှေးကွေးသွားမှာပါ။ ဒီ course ထဲက model တိုင်း—layer တစ်ခုတည်းပါတဲ့ network ကနေ Projects chapter ထဲက mini transformer အထိ—ခုနက မြင်တွေ့တော့မယ့် mechanism အတိအကျကိုပဲ အားထားနေတာပါ—tensor တွေကို gradient လိုအပ်တယ်လို့ mark လုပ်တယ်၊ တွက်ချက်တယ်၊ `.backward()` ခေါ်တယ်၊ `.grad` ကို ဖတ်တယ်ပေါ့။

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

python
import torch

x = torch.tensor(2.0, requires_grad=True)
y = x**2 + 3*x  # y = x^2 + 3x

y.backward()  # computes dy/dx and stores it in x.grad

print("x =", x.item())
print("y =", y.item())
print("x.grad =", x.grad.item())  # dy/dx = 2x + 3, so at x=2: 2*2 + 3 = 7
You should see
x = 2.0၊ y = 10.0 (2^2 + 3*2 = 10 ဖြစ်လို့)၊ ပြီးတော့ x.grad = 7.0 ကို print ထုတ်ပြီး၊ လက်ဖြင့်တွက်ထားတဲ့ derivative dy/dx = 2x + 3 ကို x = 2 မှာ အစားထိုးတွက်ထားတဲ့ တန်ဖိုးနဲ့ ကိုက်ညီပါတယ်။

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

Expression ကို y = x**3 - 2*x အဖြစ် ပြောင်းပြီး x = 1.0 မှာ .backward() ကို run ကြည့်ပါ၊ x.grad ရလဒ်ကို လက်ဖြင့်တွက်ထားတဲ့ derivative dy/dx = 3x^2 - 2 နဲ့ ကိုက်ညီမကိုက်ညီ စစ်ဆေးပါ။

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

Graph တစ်ခုတည်းပေါ် .backward() ကို retain_graph=True မပါဘဲ ထပ်ခေါ်တာ—error တက်တယ်။ Default အနေနဲ့ PyTorch က memory ချွေတာဖို့ backward pass တစ်ခုပြီးရင် graph ကို ဖျက်လိုက်တယ်၊ training loop အများစုက step တိုင်းမှာ graph အသစ် build လုပ်နေရင်းမို့ပါ။

Leaf tensor တစ်ခုမှာ requires_grad=True ထည့်ဖို့ မေ့တာ (ဒါမှမဟုတ် detach ဖြစ်သွားစေတဲ့ plain Python operation နဲ့ wrap လုပ်မိတာ)—.grad က None အနေနဲ့ တိတ်တိတ်ဆိတ်ဆိတ် ကျန်ခဲ့တတ်တယ်။ Error မတက်ဘဲ gradient မျှော်လင့်နေရာမှာ None ကို တွေ့ရလို့ ရှုပ်ထွေးစေတတ်တယ်။

PyTorch Docs — AutogradDeep Learning

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

  • Graph တစ်ခုတည်းပေါ် .backward() ကို retain_graph=True မပါဘဲ ထပ်ခေါ်တာ—error တက်တယ်။ Default အနေနဲ့ PyTorch က memory ချွေတာဖို့ backward pass တစ်ခုပြီးရင် graph ကို ဖျက်လိုက်တယ်၊ training loop အများစုက step တိုင်းမှာ graph အသစ် build လုပ်နေရင်းမို့ပါ။
  • Leaf tensor တစ်ခုမှာ requires_grad=True ထည့်ဖို့ မေ့တာ (ဒါမှမဟုတ် detach ဖြစ်သွားစေတဲ့ plain Python operation နဲ့ wrap လုပ်မိတာ)—.grad က None အနေနဲ့ တိတ်တိတ်ဆိတ်ဆိတ် ကျန်ခဲ့တတ်တယ်။ Error မတက်ဘဲ gradient မျှော်လင့်နေရာမှာ None ကို တွေ့ရလို့ ရှုပ်ထွေးစေတတ်တယ်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

Expression ကို y = x**3 - 2*x အဖြစ် ပြောင်းပြီး x = 1.0 မှာ .backward() ကို run ကြည့်ပါ၊ x.grad ရလဒ်ကို လက်ဖြင့်တွက်ထားတဲ့ derivative dy/dx = 3x^2 - 2 နဲ့ ကိုက်ညီမကိုက်ညီ စစ်ဆေးပါ။

You'll know it worked when: x = 2.0၊ y = 10.0 (2^2 + 3*2 = 10 ဖြစ်လို့)၊ ပြီးတော့ x.grad = 7.0 ကို print ထုတ်ပြီး၊ လက်ဖြင့်တွက်ထားတဲ့ derivative dy/dx = 2x + 3 ကို x = 2 မှာ အစားထိုးတွက်ထားတဲ့ တန်ဖိုးနဲ့ ကိုက်ညီပါတယ်။

Autograd နှင့် Gradients | Thuta Learning