PyTorch로 배우는 Intermediate Deep Learning
Michal Oleszak
Machine Learning Engineer
견고한 딥러닝 모델을 학습하는 방법:

이 과목은 다음 주제에 익숙하다고 가정합니다:
신경망 학습:
PyTorch로 모델 학습:
OOP로 다음을 정의합니다:
OOP에서 객체는 다음을 가집니다:
class BankAccount:
def __init__(self, balance):
self.balance = balance
__init__는 BankAccount 객체가 생성될 때 호출됨balance는 BankAccount 객체의 속성임account = BankAccount(100)
print(account.balance)
100
deposit 메서드는 잔액을 증가시킴class BankAccount: def __init__(self, balance): self.balance = balancedef deposit(self, amount): self.balance += amount
account = BankAccount(100)
account.deposit(50)
print(account.balance)
150

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
super().__init__()로 WaterDataset이 torch Dataset처럼 동작하도록 함idx 인자 하나를 받음idx의 단일 샘플에 대한 특징과 레이블 반환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.])
Sequential 모델 정의:
net = nn.Sequential(
nn.Linear(9, 16),
nn.ReLU(),
nn.Linear(16, 8),
nn.ReLU(),
nn.Linear(8, 1),
nn.Sigmoid(),
)
클래스 기반 모델 정의:
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()
PyTorch로 배우는 Intermediate Deep Learning