Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
26,786 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global: | ||
profile: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import string | ||
import torch | ||
|
||
|
||
all_letters = string.ascii_letters + " .,;'" | ||
n_letters = len(all_letters) | ||
|
||
|
||
def letter_to_index(letter): | ||
return all_letters.find(letter) | ||
|
||
|
||
def letter_to_tensor(letter): | ||
tensor = torch.zeros(1, n_letters) | ||
tensor[0][letter_to_index(letter)] = 1 | ||
return tensor | ||
|
||
|
||
def line_to_tensor(line): | ||
tensor = torch.zeros(len(line), 1, n_letters) | ||
for li, letter in enumerate(line): | ||
tensor[li][0][letter_to_index(letter)] = 1 | ||
return tensor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class RNN(nn.Module): | ||
# https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html | ||
def __init__(self, input_size, hidden_size, output_size): | ||
super(RNN, self).__init__() | ||
|
||
self.hidden_size = hidden_size | ||
|
||
self.i2h = nn.Linear(input_size + hidden_size, hidden_size) | ||
self.i2o = nn.Linear(input_size + hidden_size, output_size) | ||
self.softmax = nn.LogSoftmax(dim=1) | ||
self.learning_rate = 0.005 | ||
criterion = nn.NLLLoss() | ||
|
||
def forward(self, input_, hidden): | ||
combined = torch.cat((input_, hidden), 1) | ||
hidden = self.i2h(combined) | ||
output = self.i2o(combined) | ||
output = self.softmax(output) | ||
return output, hidden | ||
|
||
def init_hidden(self): | ||
return torch.zeros(1, self.hidden_size) | ||
|
||
def train_network(self, category_tensor, line_tensor): | ||
hidden = self.initHidden() | ||
|
||
self.zero_grad() | ||
output = None | ||
for i in range(line_tensor.size()[0]): | ||
output, hidden = self.rnn(line_tensor[i], hidden) | ||
|
||
loss = self.criterion(output, category_tensor) | ||
loss.backward() | ||
|
||
# Add parameters' gradients to their values, multiplied by learning rate | ||
for p in self.rnn.parameters(): | ||
p.data.add_(-self.learning_rate, p.grad.data) | ||
|
||
return output, loss.item() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters