Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
deeplearningzerotoall
GitHub Repository: deeplearningzerotoall/PyTorch
Path: blob/master/RNN/season1_refactored/RNN_intro_2.py
629 views
1
# Lab 12 RNN
2
import torch
3
import torch.nn as nn
4
5
torch.manual_seed(777) # reproducibility
6
7
# 0 1 2 3 4
8
idx2char = ['h', 'i', 'e', 'l', 'o']
9
10
# Teach hihell -> ihello
11
x_data = [0, 1, 0, 2, 3, 3] # hihell
12
one_hot_lookup = [[1, 0, 0, 0, 0], # 0
13
[0, 1, 0, 0, 0], # 1
14
[0, 0, 1, 0, 0], # 2
15
[0, 0, 0, 1, 0], # 3
16
[0, 0, 0, 0, 1]] # 4
17
18
y_data = [1, 0, 2, 3, 3, 4] # ihello
19
x_one_hot = [one_hot_lookup[x] for x in x_data]
20
21
# As we have one batch of samples, we will change them to variables only once
22
inputs = torch.Tensor(x_one_hot)
23
labels = torch.LongTensor(y_data)
24
25
num_classes = 5
26
input_size = 5 # one-hot size
27
hidden_size = 5 # output from the RNN. 5 to directly predict one-hot
28
batch_size = 1 # one sentence
29
sequence_length = 1 # One by one
30
num_layers = 1 # one-layer rnn
31
32
33
class Model(nn.Module):
34
35
def __init__(self):
36
super(Model, self).__init__()
37
self.rnn = nn.RNN(input_size=input_size,
38
hidden_size=hidden_size,
39
batch_first=True)
40
41
def forward(self, hidden, x):
42
# Reshape input (batch first)
43
x = x.view(batch_size, sequence_length, input_size)
44
45
# Propagate input through RNN
46
# Input: (batch, seq_len, input_size)
47
# hidden: (num_layers * num_directions, batch, hidden_size)
48
out, hidden = self.rnn(x, hidden)
49
return out.view(-1, num_classes), hidden
50
51
def init_hidden(self):
52
# Initialize hidden and cell states
53
# (num_layers * num_directions, batch, hidden_size)
54
return torch.zeros(num_layers, batch_size, hidden_size)
55
56
57
# Instantiate RNN model
58
model = Model()
59
print(model)
60
61
# Set loss and optimizer function
62
# CrossEntropyLoss = LogSoftmax + NLLLoss
63
criterion = nn.CrossEntropyLoss()
64
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
65
66
print(inputs.size(), labels.size())
67
# Train the model
68
for epoch in range(100):
69
optimizer.zero_grad()
70
loss = 0
71
hidden = model.init_hidden()
72
73
print("predicted string: ", end='')
74
for input, label in zip(inputs, labels):
75
output, hidden = model(hidden, input)
76
val, idx = output.max(1)
77
print(idx2char[idx.data[0]], end='')
78
loss += criterion(output, label.reshape(-1))
79
80
print(f', epoch: {epoch + 1}, loss: {loss.item():1.3f}')
81
82
loss.backward()
83
optimizer.step()
84
85
print("Learning finished!")
86
87