# https://github.com/spro/char-rnn.pytorch import unidecode import string import random import time import math import torch all_characters = string.printable n_characters = len(all_characters) def read_file(filename): file = unidecode.unidecode(open(filename).read()) return file, len(file) # Turning a string into a tensor def char_tensor(string): tensor = torch.zeros(len(string), requires_grad=True).long() for c in range(len(string)): try: tensor[c] = all_characters.index(string[c]) except: continue return tensor # Readable time elapsed def time_since(since): s = time.time() - since m = math.floor(s / 60) s -= m * 60 return '%dm %ds' % (m, s)