첫 학습 루프 작성하기

PyTorch로 배우는 딥러닝 입문

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

신경망 학습

  1. 모델 생성
  2. 손실 함수 선택
  3. 데이터셋 정의
  4. 옵티마이저 설정
  5. 학습 루프 실행:
    • 손실 계산(순전파)
    • 그래디언트 계산(역전파)
    • 모델 파라미터 업데이트
PyTorch로 배우는 딥러닝 입문

Data Science Salary 데이터셋 소개

 experience_level  employment_type  remote_ratio  company_size  salary_in_usd  
        0                0               0.5             1            0.036 
        1                0               1.0             2            0.133     
        2                0               0.0             1            0.234  
        1                0               1.0             0            0.076  
        2                0               1.0             1            0.170

$$

  • 피처: 범주형, 타깃: 연봉(USD)
  • 최종 출력: 선형 레이어
  • 손실: 회귀용
PyTorch로 배우는 딥러닝 입문

평균제곱오차(MSE) 손실

$$

  • MSE 손실은 예측과 정답의 제곱 오차의 평균입니다
def mean_squared_loss(prediction, target):
  return np.mean((prediction - target)**2)

$$

  • PyTorch에서는:
criterion = nn.MSELoss()
# Prediction and target are float tensors
loss = criterion(prediction, target)
PyTorch로 배우는 딥러닝 입문

학습 루프 전에

# Create the dataset and the dataloader
dataset = TensorDataset(torch.tensor(features).float(),
                        torch.tensor(target).float())


dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
# Create the model model = nn.Sequential(nn.Linear(4, 2), nn.Linear(2, 1))
# Create the loss and optimizer criterion = nn.MSELoss() optimizer = optim.SGD(model.parameters(), lr=0.001)
PyTorch로 배우는 딥러닝 입문

학습 루프

for epoch in range(num_epochs):

for data in dataloader:
# Set the gradients to zero optimizer.zero_grad()
# Get feature and target from the data loader feature, target = data
# Run a forward pass pred = model(feature) # Compute loss and gradients loss = criterion(pred, target) loss.backward()
# Update the parameters optimizer.step()
PyTorch로 배우는 딥러닝 입문

연습해 봅시다!

PyTorch로 배우는 딥러닝 입문

Preparing Video For Download...