<jupyter_start><jupyter_text>https://i.imgur.com/Z2xbySO.png<jupyter_code>import torch.nn as nn
from torch.autograd import Variable

class RNN(nn.Module):
    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()

    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 initHidden(self):
        return Variable(torch.zeros(1, self.hidden_size))

n_hidden = 128
rnn = RNN(n_letters, n_hidden, n_categories)
input = Variable(lineToTensor('Albert'))
hidden = Variable(torch.zeros(1, n_hidden))

output, next_hidden = rnn(input[0], hidden)
print(output)
def categoryFromOutput(output):
    top_n, top_i = output.data.topk(1) # Tensor out of Variable with .data
    category_i = top_i[0][0]
    return all_categories[category_i], category_i

print(categoryFromOutput(output))
import random

def randomChoice(l):
    return l[random.randint(0, len(l) - 1)]

def randomTrainingExample():
    category = randomChoice(all_categories)
    line = randomChoice(category_lines[category])
    category_tensor = Variable(torch.LongTensor([all_categories.index(category)]))
    line_tensor = Variable(lineToTensor(line))
    return category, line, category_tensor, line_tensor

for i in range(10):
    category, line, category_tensor, line_tensor = randomTrainingExample()
    print('category =', category, '/ line =', line)<jupyter_output>category = Chinese / line = Chi
category = Vietnamese / line = Lieu
category = Portuguese / line = Maria
category = Italian / line = Ardovini
category = Russian / line = Batsev
category = Irish / line = O'Donoghue
category = Chinese / line = Lau
category = Portuguese / line = Delgado
category = Spanish / line = Vasquez
category = English / line = Hawkins<jupyter_text>For the loss function nn.NLLLoss is appropriate, since the last layer of the RNN is nn.LogSoftmax<jupyter_code>criterion = nn.NLLLoss()
a = torch<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10928013876843018, 'fraction_numerical': 0.00823937554206418, 'mean_word_length': 4.291284403669724, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '457103', 'license': 'Apache-2.0', 'n_tokens_mistral': 759, 'n_tokens_neox': 732, 'n_words': 177, 'path': 'notebooks/Untitled1.ipynb', 'repo_name': 'ZmeiGorynych/audio_models', 'size': '2306'}