def generateName(start_char='M'): input = Variable(inputVector(start_char)) hidden1, hidden2, hidden3 = rnn.initHidden() if uses_cuda: input = input.cuda() hidden1 = hidden1.cuda() hidden2 = hidden2.cuda() hidden3 = hidden3.cuda() final_text = start_char for i in range(length_lim): output, hidden1, hidden2, hidden3 = rnn(input[0], hidden1, hidden2, hidden3) topvec, topidx = output.data.topk(1) topidx = topidx[0][0] if topidx == characterSetSize() - 1: break else: letter = characterSet()[topidx] final_text += letter input = Variable(inputVector(letter)) if uses_cuda: input = input.cuda() return final_text
def targetVector(line): # Target has to be a longTensor of indices for NLLLoss target_vec = [] for char_idx in range(1,len(line)): char_cur = line[char_idx] target_vec.append(all_chars.find(char_cur)) # Add an EOF as the final target target_vec.append(characterSetSize()-1) return torch.LongTensor(target_vec)
def generateName(category, start_char='A'): category_vec = Variable(class2Vector(category)) input = Variable(inputVector(start_char)) hidden = rnn.initHidden() final_name = start_char for i in range(length_lim): output, hidden = rnn(category_vec, input[0], hidden) topvec, topidx = output.data.topk(1) topidx = topidx[0][0] if topidx == characterSetSize() - 1: break else: letter = characterSet()[topidx] final_name += letter input = Variable(inputVector(letter)) return final_name
import torch import random import string from torch.autograd import Variable from textUtils import class2Vector, inputVector from processData import loadNames, characterSetSize, characterSet from categoryRNN import categoryRNN length_lim = 15 all_names = loadNames() all_types = all_names.keys() save_file = 'names-model-checkpoint.dat' rnn = categoryRNN(characterSetSize(), 128, characterSetSize(), len(all_types)) rnn.eval() if os.path.isfile(save_file): rnn.load_state_dict(torch.load(save_file)) # Names start with a capital letter def generateName(category, start_char='A'): category_vec = Variable(class2Vector(category)) input = Variable(inputVector(start_char)) hidden = rnn.initHidden() final_name = start_char for i in range(length_lim): output, hidden = rnn(category_vec, input[0], hidden)
from torch.autograd import Variable from textUtils import inputVector from processData import loadShakespeare, characterSetSize, characterSet from vanillaLargeRNN import vanillaLargeRNN uses_cuda = True length_lim = 150 all_names = loadShakespeare() save_file = 'shakespeare-model-checkpoint.dat' if uses_cuda: save_file = 'shakespeare-model-checkpoint-cuda.dat' rnn = vanillaLargeRNN(characterSetSize(), 256, characterSetSize()) rnn.eval() if uses_cuda: rnn = rnn.cuda() if os.path.isfile(save_file): rnn.load_state_dict(torch.load(save_file)) # Paragraphs start with a capital letter def generateName(start_char='M'): input = Variable(inputVector(start_char)) hidden1, hidden2, hidden3 = rnn.initHidden() if uses_cuda: input = input.cuda()
def inputVector(line): input_vec = torch.zeros(len(line), 1, characterSetSize()) for char_idx in range(len(line)): char_cur = line[char_idx] input_vec[char_idx][0][all_chars.find(char_cur)] = 1 return input_vec