코딩걸음마

[딥러닝] Pytorch Regression with DNN(Deep Neural Network) 본문

딥러닝_Pytorch

[딥러닝] Pytorch Regression with DNN(Deep Neural Network)

코딩걸음마 2022. 6. 30. 02:01
728x90

보스턴 집값예측 데이터로 DNN을 활용한 회귀식모델을 만들어봅시다.

1. 데이터 준비하기

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
df["TARGET"] = boston.target

scaler = StandardScaler()
scaler.fit(df.values[:, :-1])
df.values[:, :-1] = scaler.transform(df.values[:, :-1])

StandardScaler를 이용해서 Data를 scaling한 후 분석해봅시다.

 

2. 데이터 전처리 및 모델 params 설정

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim


data = torch.from_numpy(df.values).float()

y = data[:, -1:]
x = data[:, :-1]

n_epochs = 100000
learning_rate = 1e-4
print_interval = 5000

 

3. 모델 생성

relu = nn.ReLU()
leaky_relu = nn.LeakyReLU(0.1)

class MyModel(nn.Module):
    
    def __init__(self, input_dim, output_dim):
        self.input_dim = input_dim
        self.output_dim = output_dim
        
        super().__init__()
        
        self.linear1 = nn.Linear(input_dim, 3)
        self.linear2 = nn.Linear(3, 3)
        self.linear3 = nn.Linear(3, output_dim)
        self.act = nn.ReLU()
        
    def forward(self, x):
        # |x| = (batch_size, input_dim)
        h = self.act(self.linear1(x)) # |h| = (batch_size, 3)
        h = self.act(self.linear2(h))
        y = self.linear3(h)
        # |y| = (batch_size, output_dim)
        
        return y
    
model = MyModel(x.size(-1), y.size(-1))

model

 

MyModel(
  (linear1): Linear(in_features=13, out_features=3, bias=True)
  (linear2): Linear(in_features=3, out_features=3, bias=True)
  (linear3): Linear(in_features=3, out_features=1, bias=True)
  (act): ReLU()
)

LeakyReLU 활성화 함수 적용

model = nn.Sequential(
    nn.Linear(x.size(-1), 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, y.size(-1)),
)

model

 

Sequential(
  (0): Linear(in_features=13, out_features=3, bias=True)
  (1): LeakyReLU(negative_slope=0.01)
  (2): Linear(in_features=3, out_features=3, bias=True)
  (3): LeakyReLU(negative_slope=0.01)
  (4): Linear(in_features=3, out_features=3, bias=True)
  (5): LeakyReLU(negative_slope=0.01)
  (6): Linear(in_features=3, out_features=3, bias=True)
  (7): LeakyReLU(negative_slope=0.01)
  (8): Linear(in_features=3, out_features=3, bias=True)
  (9): LeakyReLU(negative_slope=0.01)
  (10): Linear(in_features=3, out_features=1, bias=True)
)

모델 적용 완료 문구로 다시 확인할 수 있다.

 

최적화 함수를 적용해줍시다.

optimizer = optim.SGD(model.parameters(),
                      lr=learning_rate)

 

4. 모델 실행

이제 모델훈련을 시행합니다.

for i in range(n_epochs):
    y_hat = model(x)
    loss = F.mse_loss(y_hat, y)
    
    optimizer.zero_grad()
    loss.backward()
    
    optimizer.step()
    
    if (i + 1) % print_interval == 0:
        print('Epoch %d: loss=%.4e' % (i + 1, loss))
Epoch 5000: loss=1.2523e+01
Epoch 10000: loss=1.1477e+01
Epoch 15000: loss=1.0641e+01
.......
Epoch 90000: loss=7.8523e+00
Epoch 95000: loss=7.7403e+00
Epoch 100000: loss=7.6845e+00

 

5. 시각화

df = pd.DataFrame(torch.cat([y, y_hat], dim=1).detach().numpy(),
                  columns=["y", "y_hat"])

sns.pairplot(df, height=5)
plt.show()

728x90
Comments