PyTorch และการเขียนโปรแกรมเชิงวัตถุ

Deep Learning ระดับกลางด้วย PyTorch

Michal Oleszak

Machine Learning Engineer

สิ่งที่จะได้เรียนรู้

วิธีฝึกโมเดล deep learning ให้มีประสิทธิภาพ:

  • การปรับปรุงการฝึกด้วย optimizer
  • การจัดการปัญหา vanishing และ exploding gradients
  • Convolutional Neural Networks (CNNs)
  • Recurrent Neural Networks (RNNs)
  • โมเดลแบบ multi-input และ multi-output

 

 

โลโก้ PyTorch

Deep Learning ระดับกลางด้วย PyTorch

ความรู้พื้นฐาน

คอร์สนี้ต้องการให้คุ้นเคยกับหัวข้อต่อไปนี้:

  • การฝึก Neural network:

    • Forward pass
    • การคำนวณ Loss
    • Backward pass (backpropagation)
  • การฝึกโมเดลด้วย PyTorch:

    • Datasets และ DataLoaders
    • ลูปการฝึกโมเดล
    • การประเมินโมเดล
  • คอร์สที่ต้องเรียนก่อน: Introduction to Deep Learning with PyTorch

Deep Learning ระดับกลางด้วย PyTorch

การเขียนโปรแกรมเชิงวัตถุ (OOP)

  • จะใช้ OOP ในการกำหนด:

    • PyTorch Datasets
    • PyTorch Models
  • ใน OOP เราสร้างออบเจกต์ที่มี:

    • ความสามารถ (methods)
    • ข้อมูล (attributes)
Deep Learning ระดับกลางด้วย PyTorch

การเขียนโปรแกรมเชิงวัตถุ (OOP)

class BankAccount:
    def __init__(self, balance):
        self.balance = balance
  • __init__ จะถูกเรียกเมื่อสร้างออบเจกต์ BankAccount
  • balance คือ attribute ของออบเจกต์ BankAccount
account = BankAccount(100)
print(account.balance)
100
Deep Learning ระดับกลางด้วย PyTorch

การเขียนโปรแกรมเชิงวัตถุ (OOP)

  • Methods: ฟังก์ชัน Python สำหรับดำเนินงานต่าง ๆ
  • method deposit เพิ่มยอดคงเหลือ
class BankAccount:
    def __init__(self, balance):
        self.balance = balance


def deposit(self, amount): self.balance += amount
account = BankAccount(100)
account.deposit(50)
print(account.balance)
150
Deep Learning ระดับกลางด้วย PyTorch

ชุดข้อมูลคุณภาพน้ำดื่ม

pandas DataFrame แสดงแถวแรกและแถวสุดท้ายบางส่วนของข้อมูลคุณภาพน้ำดื่ม

Deep Learning ระดับกลางด้วย PyTorch

PyTorch Dataset

from torch.utils.data import Dataset

class WaterDataset(Dataset):

def __init__(self, csv_path): super().__init__() df = pd.read_csv(csv_path) self.data = df.to_numpy()
def __len__(self): return self.data.shape[0]
def __getitem__(self, idx): features = self.data[idx, :-1] label = self.data[idx, -1] return features, label
  • init: โหลดข้อมูลและเก็บเป็น numpy array
    • super().__init__() ทำให้ WaterDataset ทำงานเหมือน torch Dataset
  • len: คืนค่าขนาดของชุดข้อมูล
  • getitem:
    • รับอาร์กิวเมนต์หนึ่งตัวชื่อ idx
    • คืนค่า features และ label ของตัวอย่างที่ index idx
Deep Learning ระดับกลางด้วย PyTorch

PyTorch DataLoader

dataset_train = WaterDataset(
    "water_train.csv"
)
from torch.utils.data import DataLoader

dataloader_train = DataLoader(
    dataset_train,
    batch_size=2,
    shuffle=True,
)
features, labels = next(iter(dataloader_train))
print(f"Features: {features},\nLabels: {labels}")
Features: tensor([
  [0.4899, 0.4180, 0.6299, 0.3496, 0.4575,
   0.3615, 0.3259, 0.5011, 0.7545],
  [0.7953, 0.6305, 0.4480, 0.6549, 0.7813,
   0.6566, 0.6340, 0.5493, 0.5789]
]),
Labels: tensor([1., 0.])
Deep Learning ระดับกลางด้วย PyTorch

PyTorch Model

การกำหนดโมเดลแบบ Sequential:

net = nn.Sequential(
  nn.Linear(9, 16),
  nn.ReLU(),
  nn.Linear(16, 8),
  nn.ReLU(),
  nn.Linear(8, 1),
  nn.Sigmoid(),
)

การกำหนดโมเดลแบบ class:

class Net(nn.Module):

def __init__(self): super().__init__() self.fc1 = nn.Linear(9, 16) self.fc2 = nn.Linear(16, 8) self.fc3 = nn.Linear(8, 1)
def forward(self, x): x = nn.functional.relu(self.fc1(x)) x = nn.functional.relu(self.fc2(x)) x = nn.functional.sigmoid(self.fc3(x)) return x net = Net()
Deep Learning ระดับกลางด้วย PyTorch

มาฝึกกันเถอะ!

Deep Learning ระดับกลางด้วย PyTorch

Preparing Video For Download...