-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-mnist-twolayers-gpu.py
96 lines (74 loc) · 2.31 KB
/
03-mnist-twolayers-gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import time
#begin time
start = time.clock()
#Hyper parameters
input_size = 784
hidden_size = 500
num_class = 10
num_epochs = 50
batch_size = 1000
learning_rate = 0.001
#load data
train_dataset = dsets.MNIST(root='mnist-data',
train =True,
transform = transforms.ToTensor(),
download= True)
test_dataset = dsets.MNIST(root='mnist-data',
train = False,
transform = transforms.ToTensor())
# Dataset Loader (Input Pipline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
#model
class Net(nn.Module):
def __init__(self, input_size, hidden_size, num_class):
super(Net, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_class)
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
net = Net(input_size, hidden_size, num_class)
net.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), learning_rate)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images.view(-1, 28*28).cuda())
labels = Variable(labels.cuda())
#forward + backward + optimizer
optimizer.zero_grad()
outputs = net(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if ((epoch+1) %3 ==0):
#Test the model
correct = 0
total = 0
for images, labels in test_loader:
images = Variable(images.view(-1, 28*28).cuda())
labels = Variable(labels.cuda())
outputs = net(images)
_, pred = torch.max(outputs.data,1)
total += labels.size(0)
correct += (pred == labels).sum()
print('Epoch is %d Accuracy test images: %d %%' %( epoch+1,(100 * correct / total)))
#end time
end = time.clock()
second = end-start
minute = int(second /60)
second = int(second - minute*60)
print ("time is {0} minute {1} second ".format(minute, second))