-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
140 lines (101 loc) · 4.35 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# ================================================================ #
# Recurrent Neural Networks #
# ================================================================ #
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Hyperparameters
batch_size = 64
num_epochs = 10
learning_rate = 0.01
sequence_dim = 28
input_dim = 28
hidden_dim = 100
layer_dim = 1
output_dim = 10
# ================================================================ #
# Load Data #
# ================================================================ #
# Dataset
train_dataset = torchvision.datasets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = torchvision.datasets.MNIST(root='./data',
train=False,
transform=transforms.ToTensor(),
download=True)
# Data Loader
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)
# ================================================================ #
# Create Model Class #
# ================================================================ #
class RNN(nn.Module):
def __init__(self, input_dim, hiddin_dim, layer_dim, output_dim):
super(RNN, self).__init__()
# Hidden dimension
self.hidden_dim = hidden_dim
# Number of hidden layers
self.layer_dim = layer_dim
# Recurrent layer
self.rnn = nn.RNN(input_dim, hiddin_dim, layer_dim, batch_first=True, nonlinearity='relu')
# Output layer
self.fc = nn.Linear(hiddin_dim, output_dim)
def forward(self, x):
# Initialize hidden state with zeros
h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).to(device)
# Forward propagate RNN
out, hn = self.rnn(x, h0.detach())
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out
model = RNN(input_dim, hidden_dim, layer_dim, output_dim).to(device)
# Instantiate Optimizer and Loss
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# ================================================================ #
# Training the Model #
# ================================================================ #
iterations = 0
for epoch in range(num_epochs):
# Training
for i, (images, labels) in enumerate(train_loader):
model.train()
images = images.view(-1, sequence_dim, input_dim).to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
# Loss calculate
loss = loss_fn(outputs, labels)
optimizer.zero_grad()
# Backward pass
loss.backward()
optimizer.step()
iterations += 1
if iterations % 500 == 0:
model.eval()
correct = 0
total = 0
# Testing
for images, labels in test_loader:
images = images.view(-1, sequence_dim, input_dim).to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
# Get predictions from the maximum value
_, predicted = torch.max(outputs.data, 1)
# Total number of labels
total += labels.size(0)
# Total correct predictions
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
# Print Loss, Accuracy
print('Iteration: {}\t Loss: {}\t Accuracy: {}\t'.format(iterations, loss.item(), accuracy))