text_plain_output_1.png
from io import open

from pytorch_toolz.functools  import Reduce

from pytorch_toolz.functools import Sequential

from pytorch_toolz.operator   import Apply

import glob

import os

import string

import torch

import torch.nn as nn

import unicodedata

from __future__ import unicode_literals, print_function, division
from io import open
import glob
import os
def findFiles(path):
    return glob.glob(path)
import unicodedata
import string
all_letters = string.ascii_letters + " .,;'"
n_letters = len(all_letters)
def unicodeToAscii(s):
    return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn' and c in all_letters))
category_lines = {}
all_categories = []
def readLines(filename):
    lines = open(filename, encoding='utf-8').read().strip().split('\n')
    return [unicodeToAscii(line) for line in lines]
for filename in findFiles('/kaggle/input/classifying-names-data-from-pytorch-tutorial/names/*.txt'):
    category = os.path.splitext(os.path.basename(filename))[0]
    all_categories.append(category)
    lines = readLines(filename)
    category_lines[category] = lines
n_categories = len(all_categories)

import torch
import torch.nn as nn
def letterToIndex(letter):
    return all_letters.find(letter)
def letterToTensor(letter):
    tensor = torch.zeros(1, n_letters)
    tensor[0][letterToIndex(letter)] = 1
    return tensor
def lineToList(line):
    lst = []
    for li, letter in enumerate(line):
        tensor = torch.zeros(1, n_letters)
        tensor[0][letterToIndex(letter)] = 1
        lst.append(tensor)
    return lst

from pytorch_toolz.operator import Apply
from pytorch_toolz.functools import Reduce
from itertools import accumulate, repeat, chain
from typing import TypeVar, Tuple, Generic, Callable, overload
from torch.nn import Module, Sequential as Sequential_
from torch import Tensor
from functools import reduce
from pytorch_toolz.functools import Sequential
class RNN(nn.Module):
    def __init__(self, input_size, hidden_size):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
    def forward(self, input, hidden):
        combined = torch.cat((input, hidden), 1)
        hidden = self.i2h(combined)
        return hidden
    def initHidden(self):
        return torch.zeros(1, self.hidden_size)
n_hidden = 128
rnn_cell = RNN(n_letters, n_hidden)
cell_swap_args = Sequential(Apply(lambda *args: reversed(args)), rnn_cell, unpack=True)
rnn = nn.Sequential(Reduce(cell_swap_args, rnn_cell.initHidden()), nn.Linear(n_hidden, n_categories), nn.LogSoftmax(dim=1))