(프로그래머스 DevCourse Week9)
참고자료 :https://wikidocs.net/book/2788
자동미분과 Autograd
import torch
w = torch.tensor(2.0, requires_grad=True)
y = w**2
z = 2*y + 5
z.backward()
print(w.grad) #z의 미분 값에, w를 대입한다. 결과는 8.
여기서, requires_grad = True 란, 텐서에 대한 기울기를 저장한다는 의미를 뜻한다.
Iris 를 통한 Layer 만들기 예제.
import pandas as pd
from sklearn.datasets import load_iris
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
data load 및, train, test set 분리.
dataset = load_iris()
data = dataset.data
label = dataset.target
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.25)
# DataLoader 생성
X_train = torch.from_numpy(X_train).float()
y_train = torch.from_numpy(y_train).long()
X_test = torch.from_numpy(X_test).float()
y_test = torch.from_numpy(y_test).long()
train_set = TensorDataset(X_train, y_train)
train_loader = DataLoader(train_set, batch_size=4, shuffle=True)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.layer0 = nn.Linear(4, 128)
self.layer1 = nn.Linear(128, 64)
self.layer2 = nn.Linear(64, 32)
self.layer3 = nn.Linear(32, 16)
self.layer4 = nn.Linear(16, 3)
self.bn0 = nn.BatchNorm1d(128) # 정규화
self.bn1 = nn.BatchNorm1d(64)
self.bn2 = nn.BatchNorm1d(32)
self.act = nn.ReLU()
def forward(self, x):
x = self.act(self.bn0(self.layer0(x)))
x = self.act(self.bn1(self.layer1(x)))
x = self.act(self.bn2(self.layer2(x)))
x = self.act(self.layer3(x))
x = self.layer4(x)
return x
optimizer 및 오차함수 생성
net = Net()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
epochs = 200
실제 예측 과정. 분류 문제의 경우, 예측 output 값 중 가장 큰 값의 index를 정답으로 한다.
losses = list()
accuracies = list()
for epoch in range(epochs):
epoch_loss = 0
epoch_accuracy = 0
for X, y in train_loader:
optimizer.zero_grad()
output = net(X)
loss = criterion(output, y)
loss.backward()
optimizer.step()
# output = [0.11, 0.5, 0.8] --> 예측 클래스 값
_, predicted = torch.max(output, dim=1) # 최대값의 index를 뱉는다.
accuracy = (predicted == y).sum().item()
epoch_loss += loss.item()
epoch_accuracy += accuracy
epoch_loss /= len(train_loader)
epoch_accuracy /= len(X_train)
print("epoch :{}, \tloss :{}, \taccuracy :{}".format(str(epoch+1).zfill(3),round(epoch_loss,4), round(epoch_accuracy,4)))
losses.append(epoch_loss)
accuracies.append(epoch_accuracy)
Test
# Test
output = net(X_test)
print(torch.max(output, dim=1))
_, predicted = torch.max(output, dim=1)
accuracy = round((predicted == y_test).sum().item() / len(y_test),4)
print("test_set accuracy :", round(accuracy,4))
'ML & DL' 카테고리의 다른 글
[DL] 순환 신경망 RNN과 장단기메모리 LSTM 개요 (0) | 2021.08.21 |
---|---|
[DL] 컨볼루션 신경망, CNN 이해하기 (0) | 2021.06.24 |
[DL] Multi Layer Perceptron, Back Propagation 감잡기 (0) | 2021.06.22 |
[DL] Pytorch Tutorial (0) | 2021.06.21 |
[DL] Single Layer Perceptron (0) | 2021.06.21 |