반응형
- sklearn load_wine 데이터로 와인의 등급 분류하기
- 분류(Classification)
- 대표적인 지도학습 방법으로 지도학습은 답이 주어진 데이터를 받아서 학습한 이후에 답이 주어지지 않은 데이터를 받았을 때 학습을 기반으로 답을 예측하는 것을 말한다.
# Data loading
import torch.nn.functional as F
from sklearn.datasets import load_wine
dataset = load_wine()
data = dataset.data
labels = dataset.target
print(data)
print(labels)
print(data.shape)
# Split data
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.3)
print(len(x_train), len(x_test), type(x_train))
print(x_test)
print(y_test)
# Convert to tensor
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
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()
print(type(x_train))
class Model(nn.Module):
def __init__(self):
super().__init__()
self.layer0 = nn.Linear(13, 256)
self.bn0 = nn.BatchNorm1d(256)
self.layer1 = nn.Linear(256, 128)
self.bn1 = nn.BatchNorm1d(128)
self.layer2 = nn.Linear(128, 64)
self.bn2 = nn.BatchNorm1d(64)
self.layer3 = nn.Linear(64, 32)
self.layer4 = nn.Linear(32, 16)
self.layer5 = nn.Linear(16, 3)
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.act(self.layer4(x))
x = self.layer5(x)
return x
model = Model()
print(model)
# Optimizer 생성
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum = 0.9)
# 손실 함수 생성
criterion = nn.CrossEntropyLoss()
# Training
epochs = 200
losses = list()
accuracies = list()
for epoch in range(epochs):
epoch_loss = 0
epoch_accuracy = 0
for x, y in train_loader:
# Training code
optimizer.zero_grad()
output = model(x)
loss = criterion(output, y)
loss.backward()
optimizer.step()
_, predicted = torch.max(output, dim=1)
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)
# Plot result
import matplotlib.pyplot as plt
plt.figure(figsize=(20,5))
plt.subplots_adjust(wspace=0.2)
plt.subplot(1,2,1)
plt.title("$loss$",fontsize = 18)
plt.plot(losses)
plt.grid()
plt.xlabel("$epochs$", fontsize = 16)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.subplot(1,2,2)
plt.title("$accuracy$", fontsize = 18)
plt.plot(accuracies)
plt.grid()
plt.xlabel("$epochs$", fontsize = 16)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.show()
# Test
output = model(x_test)
_, predicted = torch.max(output, dim=1)
accuracy = round((predicted == y_test).sum().item() / len(y_test),4)
print("test_set accuracy :", round(accuracy,4))
test_set accuracy : 0.8148
반응형
'Artificial intelligence' 카테고리의 다른 글
[AI] torchvision AlexNet, VGG16, ResNet18, DenseNet121 에 대한 성능 비교하기 (0) | 2021.12.16 |
---|---|
[AI] 평균 제곱 오차(MSE), 교차 엔트로피 오차(CEO), 로그우도(NLL) 계산 방법 (0) | 2021.12.03 |
[AI] K - 최근접 이웃 (KNN)을 이용한 Iris 데이터셋 다중 클래스 분류 (0) | 2021.10.29 |
[AI] 다층 퍼셉트론 - 특징공간 변환 (XOR 분류 테스트) (0) | 2021.10.27 |
[AI] 단층 퍼셉트론 (Single-layer Perceptron) (0) | 2021.09.23 |