In [5]:
# Pytorch Library
import torch
import torch.nn.init
from torch.autograd import Variable
import torchvision.utils as utils
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import random
torch.manual_seed(777) # reproducibility
Out[5]:
In [6]:
# MNIST dataset
mnist_train = dsets.MNIST(root='data/',
train=True,
transform=transforms.ToTensor(),
download=True)
mnist_test = dsets.MNIST(root='data/',
train=False,
transform=transforms.ToTensor(),
download=True)
# Hyper-parameters
batch_size = 100
# dataset loader
data_loader = torch.utils.data.DataLoader(dataset=mnist_train,
batch_size=batch_size,
shuffle=True,
num_workers=1)
In [13]:
# Deep Neural Network
linear1 = torch.nn.Linear(784, 512, bias=True)
linear2 = torch.nn.Linear(512, 512, bias=True)
linear3 = torch.nn.Linear(512, 512, bias=True)
linear4 = torch.nn.Linear(512, 512, bias=True)
linear5 = torch.nn.Linear(512, 256, bias=True)
linear6 = torch.nn.Linear(256, 128, bias=True)
linear7 = torch.nn.Linear(128, 10, bias=True)
relu = torch.nn.ReLU()
#sigmoid = torch.nn.Sigmoid()
# xavier initializer
# torch.nn.init.xavier_uniform(linear1.weight)
# torch.nn.init.xavier_uniform(linear2.weight)
# torch.nn.init.xavier_uniform(linear3.weight)
# torch.nn.init.xavier_uniform(linear4.weight)
# torch.nn.init.xavier_uniform(linear5.weight)
# torch.nn.init.xavier_uniform(linear6.weight)
# torch.nn.init.xavier_uniform(linear7.weight)
# model
model = torch.nn.Sequential(linear1, relu,
linear2, relu,
linear3, relu,
linear4, relu,
linear5, relu,
linear6, relu,
linear7, relu)
model.cuda()
#model.load_state_dict(torch.load('DNN.pkl')) # Load the Trained Model
print(model)
# print('Weight matrix: ', model.weight.data)
# print('bias vector: ', model.bias.data)
In [14]:
# Softmax 함수가 Cost를 계산할 때 내장되어 있다.
cost_func = torch.nn.CrossEntropyLoss()
# Hyper-parameters
learning_rate = 0.001
training_epochs = 30
# Adam Optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train model
for epoch in range(training_epochs):
avg_cost = 0
total_batch = len(mnist_train) // batch_size
for i, (batch_images, batch_labels) in enumerate(data_loader):
# 이미지를 [batch_size x 784] size 행렬로 변환
X = Variable(batch_images.view(-1, 28 * 28)).cuda()
Y = Variable(batch_labels).cuda() # label is not one-hot encoded
optimizer.zero_grad() # Zero Gradient Container
Y_prediction = model(X) # Forward Propagation
cost = cost_func(Y_prediction, Y) # compute cost
cost.backward() # compute gradient
optimizer.step() # gradient update
avg_cost += cost / total_batch
print("[Epoch: {:>4}] cost = {:>.9}".format(epoch + 1, avg_cost.data[0]))
print('Learning Finished!')
torch.save(model.state_dict(), 'DNN.pkl') # Save the Model
In [16]:
# Test the Model
correct = 0
total = 0
for images, labels in mnist_test:
images = Variable(images.view(-1, 28*28)).cuda()
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += 1
correct += (predicted == labels).sum()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
In [17]:
# Get one and predict
r = random.randint(0, len(mnist_test) - 1)
X_single_data = Variable(mnist_test.test_data[r:r + 1].view(-1, 28 * 28).float()).cuda()
Y_single_data = Variable(mnist_test.test_labels[r:r + 1]).cuda()
single_prediction = model(X_single_data)
print("Label: ", Y_single_data.data)
print("Prediction: ", torch.max(single_prediction.data, 1)[1])
plt.imshow(X_single_data.cpu().data.view(28,28).numpy() , cmap='gray')
plt.show()
In [ ]: