Thuta Learning
Deep Learning with PyTorch
BasicAIintermediate

Tensors နှင့် PyTorch အခြေခံများ

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

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

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

Tensor ဆိုတာ PyTorch ရဲ့ အခြေခံ data structure ဖြစ်ပြီး၊ multi-dimensional array တစ်ခုပါ—scalar (dimension 0, number တစ်ခုတည်း) ကနေ vector (dimension 1, number list) ၊ matrix (dimension 2, grid) ၊ ပြီးတော့ dimension ပိုများတဲ့ level အထိ generalize လုပ်ထားတာပါ။ RGB image 8 ခု၊ တစ်ခုချင်းစီ 28x28 pixel ပါတဲ့ batch တစ်ခုက shape (8, 3, 28, 28) ရှိတဲ့ 4D tensor သဘာဝကျကျ ဖြစ်လာတယ်—batch size, color channel, height, width ပေါ့။ ကြည့်ရင် tensor က NumPy array နဲ့ တူတယ်၊ PyTorch ကလည်း creation, indexing အတွက် NumPy ရဲ့ API ကို တမင်တကာ တူအောင်ဒီဇိုင်းလုပ်ထားတယ်၊ ဒါပေမဲ့ NumPy array မှာမရှိတဲ့ အရာနှစ်ခုကို tensor ထဲ ထပ်ထည့်ထားတယ်—operation တွေရဲ့ history ကို track လုပ်ထားနိုင်လို့ gradient တွေကို auto-calculate လုပ်နိုင်တယ် (နောက် lesson မှာ လေ့လာမယ်)၊ ပြီးတော့ `.to('cuda')` နဲ့ GPU ပေါ်ကို ရွှေ့နိုင်လို့ operation တွေက CPU core တစ်ခုချင်းစီ အစီအစဉ်တကျ run မယ့်အစား massively parallel matrix math အနေနဲ့ run နိုင်တယ်။ Tensor တိုင်းမှာ shape (dimension) တစ်ခုနဲ့ dtype (element ရဲ့ numeric type၊ neural network computation တွေမှာ float32 အများဆုံးသုံး) တစ်ခုစီ ရှိတယ်—ဒီနှစ်ခုမှာ mismatch ဖြစ်တာက network တည်ဆောက်တဲ့အခါ အဖြစ်များဆုံး runtime error ရင်းမြစ်ပါပဲ။

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

ဒီ course တည်ဆောက်သွားမယ့် feature တိုင်း—feedback sentiment classification, learned search ranking, lesson recommendation—တွေက platform data (feedback text, click log, lesson embedding) တွေကို tensor အဖြစ် ပြောင်းတာကနေ စတင်ရတယ်၊ ဘာကြောင့်လဲဆိုတော့ PyTorch model တွေနဲ့ GPU တွေ compute လုပ်နိုင်တဲ့ form တစ်ခုတည်းက ဒါပါပဲ။ Learner feedback comment 64 ခုပါတဲ့ batch တစ်ခုကို number အဖြစ် ပြောင်းလိုက်ရင် shape (64, 200) ရှိတဲ့ tensor ဖြစ်လာနိုင်တယ်—comment 64 ခု၊ တစ်ခုချင်းစီကို number 200 နဲ့ ကိုယ်စားပြုထားတာပါ။ Shape, dtype, tensor math အခြေခံတွေကို အခုကတည်းက ကျွမ်းကျင်ထားမှသာ နောက် lesson တိုင်းရဲ့ code ကို ရှင်းရှင်းလင်းလင်း နားလည်နိုင်မှာပါ။

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

python
import torch

# Create tensors
a = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
b = torch.randn(2, 2)

print("a shape:", a.shape, "dtype:", a.dtype)
print("b shape:", b.shape)

# Element-wise operation
c = a + b
print("a + b =\n", c)

# Matrix multiplication
d = a @ b
print("a @ b =\n", d)

# A batch of 8 RGB images, 28x28 pixels -> 4D tensor
batch = torch.zeros(8, 3, 28, 28)
print("batch shape:", batch.shape)
You should see
Tensor a ရဲ့ shape (torch.Size([2, 2])) နဲ့ dtype (torch.float32)၊ element-wise addition a + b ရဲ့ ရလဒ်၊ matrix multiplication a @ b ရဲ့ ရလဒ်၊ ပြီးတော့ 4D batch tensor ရဲ့ shape (torch.Size([8, 3, 28, 28])) တို့ကို print ထုတ်ပေးပါတယ်။

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

torch.randn သုံးပြီး shape (5, 4, 3) ရှိတဲ့ 3D tensor တစ်ခုကို random value တွေနဲ့ create လုပ်ပါ၊ shape နဲ့ dtype ကို print လုပ်ပါ၊ ပြီးရင် .view() ဒါမှမဟုတ် .reshape() သုံးပြီး shape (5, 12) အဖြစ် ပြောင်းပြီး shape အသစ်ကို print လုပ်ပါ။

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

Tensor dtype တွေ ရောနှောသုံးတာ (ဥပမာ float32 tensor ကို int64 tensor နဲ့ ပေါင်းတာ) — operation အများစုမှာ RuntimeError တက်နိုင်တယ်၊ source မတူတဲ့ tensor တွေ ပေါင်းသုံးတိုင်း .dtype ကို အမြဲစစ်ပါ။

Matrix multiplication လုပ်ချင်လို့ `*` သုံးမိတာ—`*` က element-wise ဖြစ်ပြီး shape တူ (ဒါမှမဟုတ် broadcast ဖြစ်) ဖို့ လိုအပ်တယ်၊ `@`/`torch.matmul` ကတော့ matrix multiplication အစစ်အတွက် shape rule မတူဘူး၊ ဒီနှစ်ခု ရောထွေးမိရင် crash ဖြစ်တာ ဒါမှမဟုတ် အဖြေမှားနေတာ ဖြစ်တတ်တယ်။

PyTorch Docs — TensorsDeep Learning

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

  • Tensor dtype တွေ ရောနှောသုံးတာ (ဥပမာ float32 tensor ကို int64 tensor နဲ့ ပေါင်းတာ) — operation အများစုမှာ RuntimeError တက်နိုင်တယ်၊ source မတူတဲ့ tensor တွေ ပေါင်းသုံးတိုင်း .dtype ကို အမြဲစစ်ပါ။
  • Matrix multiplication လုပ်ချင်လို့ `*` သုံးမိတာ—`*` က element-wise ဖြစ်ပြီး shape တူ (ဒါမှမဟုတ် broadcast ဖြစ်) ဖို့ လိုအပ်တယ်၊ `@`/`torch.matmul` ကတော့ matrix multiplication အစစ်အတွက် shape rule မတူဘူး၊ ဒီနှစ်ခု ရောထွေးမိရင် crash ဖြစ်တာ ဒါမှမဟုတ် အဖြေမှားနေတာ ဖြစ်တတ်တယ်။
  • နမူနာ code ကို production system ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

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

torch.randn သုံးပြီး shape (5, 4, 3) ရှိတဲ့ 3D tensor တစ်ခုကို random value တွေနဲ့ create လုပ်ပါ၊ shape နဲ့ dtype ကို print လုပ်ပါ၊ ပြီးရင် .view() ဒါမှမဟုတ် .reshape() သုံးပြီး shape (5, 12) အဖြစ် ပြောင်းပြီး shape အသစ်ကို print လုပ်ပါ။

You'll know it worked when: Tensor a ရဲ့ shape (torch.Size([2, 2])) နဲ့ dtype (torch.float32)၊ element-wise addition a + b ရဲ့ ရလဒ်၊ matrix multiplication a @ b ရဲ့ ရလဒ်၊ ပြီးတော့ 4D batch tensor ရဲ့ shape (torch.Size([8, 3, 28, 28])) တို့ကို print ထုတ်ပေးပါတယ်။

Tensors နှင့် PyTorch အခြေခံများ | Thuta Learning