Пример #1
0
def train_rnn(file, batch_size, layers, learning_rate, dropout, num_steps,
              cell_size, epochs, cell, test_seed, delim, save):
    """ Train neural network """
    model_name = "cell-{}-size-{}-batch-{}-steps-{}-layers-{}-lr-{}-dropout-{}".format(
        cell, cell_size, batch_size, num_steps, layers, learning_rate, dropout)
    ds = Dataset(file,
                 batch_size=batch_size,
                 num_steps=num_steps,
                 with_delim=delim)
    n = RNN(data=ds,
            cell=cell,
            num_layers=layers,
            dropout=dropout,
            learning_rate=learning_rate,
            cell_size=cell_size,
            num_epochs=epochs)
    n.train(save=save,
            model_name=model_name,
            test_output=True,
            test_seed=test_seed,
            with_delim=delim)
    if save:
        n.save(model_name)
Пример #2
0
import torch
import torch.nn as nn
import matplotlib.pyplot as plt

import dataset
from network import RNN

dataset = dataset.Dataset()

rnn = RNN(dataset.n_letters, 128, dataset.n_letters, dataset.n_categories)

criterion = nn.NLLLoss()

learning_rate = 0.0005

rnn.train()


def timeSince(since):
    now = time.time()
    s = now - since
    m = math.floor(s / 60)
    s -= m * 60
    return '%dm %ds' % (m, s)


def savepoint(iter, total_loss):
    torch.save(
        {
            'epoch': iter,
            'nn_state_dict': rnn.state_dict(),