반응형
라이브러리 Import하기
Pytorch에서 Deep Neural Network(DNN)를 설계하기 위해 필요한 라이브러리를 Import한다.
#Importing Library
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
DNN 모델
MNIST 데이터는 28x28로 총 784개의 픽셀로 이루어져 있다. 그렇기 때문에 784를 입력 크기 값으로 받는다. 네트워크는 총 6개의 레이어로 이루어져 있으며 숫자의 종류(0~9)에 따라 마지막 출력단은 10개로 설정한다. 각각의 활성함수(activation function)은 linear를 사용하고 마지막 출력단에서 SoftMax의 확률을 통해 0부터 9사이의 숫자로 결정된다.
#Define Neural Networks Model.
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, 128)
self.fc4 = nn.Linear(128, 64)
self.fc5 = nn.Linear(64, 32)
self.fc6 = nn.Linear(32, 10)
def forward(self, x):
x = x.float()
h1 = F.relu(self.fc1(x.view(-1, 784)))
h2 = F.relu(self.fc2(h1))
h3 = F.relu(self.fc3(h2))
h4 = F.relu(self.fc4(h3))
h5 = F.relu(self.fc5(h4))
h6 = self.fc6(h5)
return F.log_softmax(h6, dim=1)
print("init model done")
하이퍼 파라미터 설정
아래와 같이 하이퍼 파라미터를 설정한다.
# Set Hyper parameters and other variables to train the model.
batch_size = 64
test_batch_size = 1000
epochs = 10
lr = 0.01
momentum = 0.5
no_cuda = True
seed = 1
log_interval = 200
use_cuda = not no_cuda and torch.cuda.is_available()
torch.manual_seed(seed)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
print("set vars and device done")
데이터 로드
MNIST 데이터를 로드한다. 훈련 데이터는 6만개, 테스트 데이터는 1만개로 이루어져 있다.
#Prepare Data Loader for Training and Validation
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transform),
batch_size = batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, download=True,
transform=transform),
batch_size=test_batch_size, shuffle=True, **kwargs)
모델 불러오기 / 옵티마 선언
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum)
훈련 / 테스트 함수 구현
#Define Train function and Test function to validate.
def train(log_interval, model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(log_interval, model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction='sum').item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format
(test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
네트워크 훈련 및 테스트 적용
10 epoch동안 학습을 진행한다. 1epoch 학습을 진행할 때 마다 test를 진행한다.
# Train and Test the model and save it.
for epoch in range(1, 11):
train(log_interval, model, device, train_loader, optimizer, epoch)
test(log_interval, model, device, test_loader)
torch.save(model, './model.pt')
전체 코드
#Importing Library
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
#Define Neural Networks Model.
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, 128)
self.fc4 = nn.Linear(128, 64)
self.fc5 = nn.Linear(64, 32)
self.fc6 = nn.Linear(32, 10)
def forward(self, x):
x = x.float()
h1 = F.relu(self.fc1(x.view(-1, 784)))
h2 = F.relu(self.fc2(h1))
h3 = F.relu(self.fc3(h2))
h4 = F.relu(self.fc4(h3))
h5 = F.relu(self.fc5(h4))
h6 = self.fc6(h5)
return F.log_softmax(h6, dim=1)
print("init model done")
# Set Hyper parameters and other variables to train the model.
batch_size = 64
test_batch_size = 1000
epochs = 10
lr = 0.01
momentum = 0.5
no_cuda = True
seed = 1
log_interval = 200
use_cuda = not no_cuda and torch.cuda.is_available()
torch.manual_seed(seed)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
print("set vars and device done")
#Prepare Data Loader for Training and Validation
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transform),
batch_size = batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, download=True,
transform=transform),
batch_size=test_batch_size, shuffle=True, **kwargs)
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum)
#Define Train function and Test function to validate.
def train(log_interval, model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(log_interval, model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction='sum').item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format
(test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
# Train and Test the model and save it.
for epoch in range(1, 11):
train(log_interval, model, device, train_loader, optimizer, epoch)
test(log_interval, model, device, test_loader)
torch.save(model, './model.pt')
최종 결과
init model done
set vars and device done
Train Epoch: 1 [0/60000 (0%)] Loss: 2.319003
Train Epoch: 1 [12800/60000 (21%)] Loss: 2.308317
Train Epoch: 1 [25600/60000 (43%)] Loss: 2.259772
Train Epoch: 1 [38400/60000 (64%)] Loss: 2.114968
Train Epoch: 1 [51200/60000 (85%)] Loss: 1.092506
Test set: Average loss: 0.7195, Accuracy: 7655/10000 (77%)
Train Epoch: 2 [0/60000 (0%)] Loss: 0.710096
Train Epoch: 2 [12800/60000 (21%)] Loss: 0.628659
Train Epoch: 2 [25600/60000 (43%)] Loss: 0.429823
Train Epoch: 2 [38400/60000 (64%)] Loss: 0.444784
Train Epoch: 2 [51200/60000 (85%)] Loss: 0.320472
Test set: Average loss: 0.3158, Accuracy: 9107/10000 (91%)
Train Epoch: 3 [0/60000 (0%)] Loss: 0.226330
Train Epoch: 3 [12800/60000 (21%)] Loss: 0.206977
Train Epoch: 3 [25600/60000 (43%)] Loss: 0.111615
Train Epoch: 3 [38400/60000 (64%)] Loss: 0.287350
Train Epoch: 3 [51200/60000 (85%)] Loss: 0.149734
Test set: Average loss: 0.2045, Accuracy: 9436/10000 (94%)
Train Epoch: 4 [0/60000 (0%)] Loss: 0.096651
Train Epoch: 4 [12800/60000 (21%)] Loss: 0.152811
Train Epoch: 4 [25600/60000 (43%)] Loss: 0.121838
Train Epoch: 4 [38400/60000 (64%)] Loss: 0.128148
Train Epoch: 4 [51200/60000 (85%)] Loss: 0.209051
Test set: Average loss: 0.1371, Accuracy: 9585/10000 (96%)
Train Epoch: 5 [0/60000 (0%)] Loss: 0.123022
Train Epoch: 5 [12800/60000 (21%)] Loss: 0.047610
Train Epoch: 5 [25600/60000 (43%)] Loss: 0.025467
Train Epoch: 5 [38400/60000 (64%)] Loss: 0.047583
Train Epoch: 5 [51200/60000 (85%)] Loss: 0.054973
Test set: Average loss: 0.1139, Accuracy: 9663/10000 (97%)
Train Epoch: 6 [0/60000 (0%)] Loss: 0.065705
Train Epoch: 6 [12800/60000 (21%)] Loss: 0.029514
Train Epoch: 6 [25600/60000 (43%)] Loss: 0.029115
Train Epoch: 6 [38400/60000 (64%)] Loss: 0.048393
Train Epoch: 6 [51200/60000 (85%)] Loss: 0.101836
Test set: Average loss: 0.1020, Accuracy: 9697/10000 (97%)
Train Epoch: 7 [0/60000 (0%)] Loss: 0.011763
Train Epoch: 7 [12800/60000 (21%)] Loss: 0.026959
Train Epoch: 7 [25600/60000 (43%)] Loss: 0.093610
Train Epoch: 7 [38400/60000 (64%)] Loss: 0.022231
Train Epoch: 7 [51200/60000 (85%)] Loss: 0.013581
Test set: Average loss: 0.1001, Accuracy: 9699/10000 (97%)
Train Epoch: 8 [0/60000 (0%)] Loss: 0.034948
Train Epoch: 8 [12800/60000 (21%)] Loss: 0.077487
Train Epoch: 8 [25600/60000 (43%)] Loss: 0.025672
Train Epoch: 8 [38400/60000 (64%)] Loss: 0.022483
Train Epoch: 8 [51200/60000 (85%)] Loss: 0.016120
Test set: Average loss: 0.1090, Accuracy: 9687/10000 (97%)
Train Epoch: 9 [0/60000 (0%)] Loss: 0.053953
Train Epoch: 9 [12800/60000 (21%)] Loss: 0.016929
Train Epoch: 9 [25600/60000 (43%)] Loss: 0.067918
Train Epoch: 9 [38400/60000 (64%)] Loss: 0.026225
Train Epoch: 9 [51200/60000 (85%)] Loss: 0.009774
Test set: Average loss: 0.0981, Accuracy: 9720/10000 (97%)
Train Epoch: 10 [0/60000 (0%)] Loss: 0.056655
Train Epoch: 10 [12800/60000 (21%)] Loss: 0.033188
Train Epoch: 10 [25600/60000 (43%)] Loss: 0.020561
Train Epoch: 10 [38400/60000 (64%)] Loss: 0.003139
Train Epoch: 10 [51200/60000 (85%)] Loss: 0.007730
Test set: Average loss: 0.0914, Accuracy: 9758/10000 (98%)
Process finished with exit code 0
CNN 모델 사용하기 (additional)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
반응형
'Pytorch' 카테고리의 다른 글
[Pytorch] Custom dataset & dataloader 만들기 (0) | 2021.06.21 |
---|---|
[Pytorch] Numpy에서 Tensor로 (0) | 2021.06.21 |
[Pytorch] CNN을 이용한 MNIST (0) | 2021.06.07 |
[Pytorch] 튜토리얼(2) (0) | 2021.05.17 |
[Pytorch] 튜토리얼(1) (0) | 2021.05.17 |
댓글