1
|
|
|
import torch |
2
|
|
|
import torch.nn as nn |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class RNN(nn.Module): |
6
|
|
|
# https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html |
7
|
|
|
def __init__(self, input_size, hidden_size, output_size): |
8
|
|
|
super(RNN, self).__init__() |
9
|
|
|
|
10
|
|
|
self.hidden_size = hidden_size |
11
|
|
|
|
12
|
|
|
self.i2h = nn.Linear(input_size + hidden_size, hidden_size) |
13
|
|
|
self.i2o = nn.Linear(input_size + hidden_size, output_size) |
14
|
|
|
self.softmax = nn.LogSoftmax(dim=1) |
15
|
|
|
self.learning_rate = 0.005 |
16
|
|
|
criterion = nn.NLLLoss() |
17
|
|
|
|
18
|
|
|
def forward(self, input_, hidden): |
19
|
|
|
combined = torch.cat((input_, hidden), 1) |
20
|
|
|
hidden = self.i2h(combined) |
21
|
|
|
output = self.i2o(combined) |
22
|
|
|
output = self.softmax(output) |
23
|
|
|
return output, hidden |
24
|
|
|
|
25
|
|
|
def init_hidden(self): |
26
|
|
|
return torch.zeros(1, self.hidden_size) |
27
|
|
|
|
28
|
|
|
def train_network(self, category_tensor, line_tensor): |
29
|
|
|
hidden = self.initHidden() |
30
|
|
|
|
31
|
|
|
self.zero_grad() |
32
|
|
|
output = None |
33
|
|
|
for i in range(line_tensor.size()[0]): |
34
|
|
|
output, hidden = self.rnn(line_tensor[i], hidden) |
35
|
|
|
|
36
|
|
|
loss = self.criterion(output, category_tensor) |
37
|
|
|
loss.backward() |
38
|
|
|
|
39
|
|
|
# Add parameters' gradients to their values, multiplied by learning rate |
40
|
|
|
for p in self.rnn.parameters(): |
41
|
|
|
p.data.add_(-self.learning_rate, p.grad.data) |
42
|
|
|
|
43
|
|
|
return output, loss.item() |
44
|
|
|
|