forked from catziyan/DRLPytorch-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecurrent_Neural_net.py
More file actions
107 lines (92 loc) · 3.58 KB
/
Recurrent_Neural_net.py
File metadata and controls
107 lines (92 loc) · 3.58 KB
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
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:CatZiyan
# @Time :2019/9/24 9:36
import torch
import torchvision
import time
import os
import GPUtil
start = time.clock()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.backends.cudnn.enabled = False
# hyper parameters
batch_size = 100
input_size = 28
sequence_length = 28
hidden_size = 32
output_size = 10
num_layers = 2
Epoch = 5
learning_rate = 0.001
if not (os.path.exists('./data/')) or not (os.listdir('./data/')):
DOWNLOAD = True
else:
DOWNLOAD = False
# download data
train_dataset = torchvision.datasets.MNIST(root='./data/',
train=True,
transform=torchvision.transforms.ToTensor(),
download=DOWNLOAD)
test_dataset = torchvision.datasets.MNIST(root='./data/',
train=False,
transform=torchvision.transforms.ToTensor())
# load dataset
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)
# RNN
class RNN(torch.nn.Module):
def __init__(self, input_size, hidden_size, num_layer, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layer = num_layer
self.lstm = torch.nn.LSTM(input_size, hidden_size, num_layer, batch_first=True)
self.fc = torch.nn.Linear(hidden_size, output_size)
def forward(self, x):
# h0 = torch.zeros(self.num_layer, x.size(0), self.hidden_size).to(device)
# c0 = torch.zeros(self.num_layer, x.size(0), self.hidden_size).to(device)
# out,_ = self.lstm(x,(h0,c0))
out, _ = self.lstm(x, None)
out = self.fc(out[:,-1,:])
return out
net = RNN(input_size, hidden_size, num_layers, output_size).to(device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(),lr=learning_rate)
#
# train net
for epoch in range(Epoch):
GPUtil.showUtilization()
for i,(images, labels) in enumerate(train_loader):
images = images.to(device).view(-1, sequence_length, input_size)
labels = labels.to(device)
predicted = net(images)
optimizer.zero_grad()
loss = criterion(predicted, labels)
loss.backward()
optimizer.step()
if (i+1)%100 == 0:
print('Epoch [%d/%d], step [%d/%d], loss: %.4f' %(epoch+1, Epoch, i+1, len(train_dataset)//batch_size, loss.item()))
torch.save(net.state_dict(), 'rnn.pkl')
# test data
net2 = RNN(input_size, hidden_size, num_layers, output_size).to(device)
net2.load_state_dict(torch.load('rnn.pkl'))
total = 0
correct = 0
for images,labels in test_loader:
images = images.to(device).view(-1, sequence_length, input_size)
labels = labels.to(device)
predicted = net2(images)
_, predicted = torch.max(predicted.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
# correct += (predicted == labels).sum().float()
# print(type(total),type(correct))
# print(correct.dtype)
# print('Test Accuracy of the model on the %d test images:%d%%' %(total, 100*(correct/total)))
print('Test Accuracy of the model on the %d test images: %d %%' %(total, 100*correct/total))
elapsed = time.clock()-start
print('Time used:', elapsed)