Example #1
0
    # Print iter number, loss, name and guess
    if iter % print_every == 0:
        guess, guess_i = char_from_output(output)
        correct = '✓' if guess == target else '✗ (%s)' % target
        print('%d %d%% (%s) %.4f %s / %s %s' % (iter, iter / n_iters * 100, timeSince(start), loss, sequence, guess, correct))

    # Add current loss avg to list of losses
    if iter % plot_every == 0:
        all_losses.append(current_loss / plot_every)
        current_loss = 0

if args.sanity:
    print("-"*20)
    print("Testing generation")
    hidden = rnn.initHidden()
    start = Variable(sequence_to_tensor("he"))
    _, hidden = rnn(start[0], hidden)
    output, hidden = rnn(start[1], hidden)
    input = "he"
    print("{}".format(input),end='')
    for i in range(50):
        hidden = rnn.initHidden()
        start = Variable(sequence_to_tensor(input))
        _, hidden = rnn(start[0], hidden)
        output, hidden = rnn(start[1], hidden)
        guess, _ = char_from_output(output)
        input = input[1]+guess
        print("{}".format(guess),end='')

torch.save(rnn.state_dict(),".saved_model")
Example #2
0
print(
    f'There are {n_categories} languages.\nNumber of family name per language:'
)
for categ in train_data.keys():
    print('   {}\t {}'.format(categ, len(train_data[categ])))

### create model
n_hidden = 128
model_net = RNN(n_letters, n_hidden, n_categories)

### Tensorboard visualization of the network
# - create (any valid) input data
# - visualize the built model in tensorboeard
category, line, category_tensor, line_tensor = randomTrainingExample(
    all_categories, train_data)
hidden = model_net.initHidden()
tb_writer.add_graph(model_net, (line_tensor[0], hidden))
# tb_writer.close()

#### training
criterion = torch.nn.NLLLoss()  # the RNN already has a softmax as output
learning_rate = 0.005


def train(category_tensor, line_tensor):
    hidden = model_net.initHidden()

    model_net.zero_grad()

    for i in range(line_tensor.size()[0]):
        output, hidden = model_net(line_tensor[i], hidden)