forked from hleborgne/TDDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_LSTM_pytorch.py
123 lines (101 loc) · 3.91 KB
/
mnist_LSTM_pytorch.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Classification de MNIST avec un (bi)LSTM
#
# original code: https://github.com/aymericdamien/TensorFlow-Examples/
# https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/02-intermediate/recurrent_neural_network/main.py
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
# import datasets
from torchvision import datasets, transforms
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('device = {}'.format(device))
# Hyper-parameters
sequence_length = 28
input_size = 28
hidden_size = 128
num_layers = 1
num_classes = 10
batch_size = 100
num_epochs = 2
learning_rate = 0.001 # try 0.0005 for BiLSTM
trans = transforms.Compose( [ transforms.ToTensor(), transforms.Normalize( (0.1307,),(0.3081,))])
train_set = datasets.MNIST( './data', train=True, transform=trans, download=True )
test_set = datasets.MNIST( './data', train=False, transform=trans, download=True )
train_loader = torch.utils.data.DataLoader(
dataset=train_set,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(
dataset=test_set,
batch_size=batch_size,
shuffle=False)
# define LSTM model
class LSTMNet(nn.Module):
def __init__(self,in_size,hidden_size, nb_layer, nb_classes):
super(LSTMNet,self).__init__()
self.hidden_size = hidden_size
self.nb_layer = nb_layer
self.nb_classes = nb_classes
self.lstm = nn.LSTM(in_size,hidden_size,nb_layer,batch_first=True)
self.fc = nn.Linear(hidden_size,nb_classes)
def forward(self,x):
# initial states
h0 = torch.zeros(self.nb_layer, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.nb_layer, x.size(0), self.hidden_size).to(device)
out,_ = self.lstm(x, (h0,c0))
out = self.fc(out[:,-1,:])
return out
# define BiLSTM model
class BiLSTMNet(nn.Module):
def __init__(self,in_size,hidden_size, nb_layer, nb_classes):
super(BiLSTMNet,self).__init__()
self.hidden_size = hidden_size
self.nb_layer = nb_layer
self.nb_classes = nb_classes
self.lstm = nn.LSTM(in_size,hidden_size,nb_layer,batch_first=True,bidirectional=True)
self.fc = nn.Linear(hidden_size*2,nb_classes) # 2 for bidirection
def forward(self,x):
# initial states
h0 = torch.zeros(self.nb_layer*2, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.nb_layer*2, x.size(0), self.hidden_size).to(device)
out,_ = self.lstm(x, (h0,c0))
out = self.fc(out[:,-1,:])
return out
model = LSTMNet(input_size, hidden_size, num_layers, num_classes).to(device)
#model = BiLSTMNet(input_size, hidden_size, num_layers, num_classes).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr = 0.01)
loss_fn = nn.CrossEntropyLoss()
# training
total_step = len(train_loader)
start = time.time()
for epoch in range(num_epochs):
for i,(img,lab) in enumerate(train_loader):
img = img.reshape(-1,sequence_length,input_size).to(device)
lab = lab.to(device)
outputs = model(img)
loss = loss_fn(outputs,lab)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f} ({:.2f} s)'
.format(epoch+1, num_epochs, i+1, total_step,
loss.item(), time.time()-start))
# test
with torch.no_grad():
correct = 0
total = 0
for img, lab in test_loader:
img = img.reshape(-1,sequence_length,input_size).to(device)
lab = lab.to(device)
outputs = model(img)
_, pred = torch.max(outputs.data,1)
total += lab.size(0)
correct += (pred == lab).sum().item()
print('Test Accuracy: {}%'.format(100. * correct / total) )