Example #1
0
def main(args):
    # Dataset functions
    vocab = Vocabulary('./data/vocabulary.json', padding=args.padding)
    kb_vocab = Vocabulary('./data/vocabulary.json', padding=4)
    print('Loading datasets.')
    training = Data(args.training_data, vocab, kb_vocab)
    validation = Data(args.validation_data, vocab, kb_vocab)
    training.load()
    validation.load()
    training.transform()
    training.kb_out()
    validation.transform()
    validation.kb_out()
    print('Datasets Loaded.')
    print('Compiling Model.')

    model = KVMMModel(pad_length=args.padding,
                      embedding_size=args.embedding,
                      vocab_size=vocab.size(),
                      batch_size=batch_size,
                      n_chars=vocab.size(),
                      n_labels=vocab.size(),
                      encoder_units=200,
                      decoder_units=200).to(device)

    print(model)
    model_optimizer = optim.Adam(model.parameters(), lr=0.001)
    criterion = nn.CrossEntropyLoss()

    plot_losses = []
    print_loss_total = 0  # Reset every print_every
    plot_loss_total = 0  # Reset every plot_every
    print_every = 100
    start = time.time()
    n_iters = 500000

    iter = 0
    while iter < n_iters:
        training_data = training.generator(batch_size)
        input_tensors = training_data[0][0]
        target_tensors = training_data[1]
        kbs = training_data[0][1]
        iter += 1
        loss = train(input_tensors, target_tensors, kbs, model,
                     model_optimizer, criterion, vocab, kb_vocab)
        print_loss_total += loss
        plot_loss_total += loss
        if iter % print_every == 0:
            validation_data = validation.generator(batch_size)
            validation_inputs = validation_data[0][0]
            validation_kbs = validation_data[0][1]
            validation_targets = validation_data[1]
            accuracy = evaluate(model, validation_inputs, validation_targets,
                                validation_kbs)
            print_loss_avg = print_loss_total / print_every
            print_loss_total = 0
            print('%s (%d %d%%) %.4f - val_accuracy %f' %
                  (timeSince(start, iter / n_iters), iter,
                   iter / n_iters * 100, print_loss_avg, accuracy))
            torch.save(model.state_dict(), 'model_weights.pytorch')
Example #2
0
def main(args):
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
    # Dataset functions
    vocab = Vocabulary('./data/vocabulary.json', padding=args.padding)
    vocab = Vocabulary('./data/vocabulary.json', padding=args.padding)
    kb_vocab = Vocabulary('./data/vocabulary.json', padding=4)
    print('Loading datasets.')
    training = Data(args.training_data, vocab, kb_vocab)
    validation = Data(args.validation_data, vocab, kb_vocab)
    training.load()
    validation.load()
    training.transform()
    training.kb_out()
    validation.transform()
    validation.kb_out()
    print('Datasets Loaded.')
    print('Compiling Model.')

    model = memnn(pad_length=args.padding,
                  embedding_size=args.embedding,
                  vocab_size=vocab.size(),
                  batch_size=args.batch_size,
                  n_chars=vocab.size(),
                  n_labels=vocab.size(),
                  embedding_learnable=True,
                  encoder_units=200,
                  decoder_units=200,
                  trainable=True)

    model.summary()
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=[
                      'accuracy',
                  ])
    print('Model Compiled.')
    print('Training. Ctrl+C to end early.')

    try:
        model.fit_generator(generator=training.generator(args.batch_size),
                            steps_per_epoch=300,
                            validation_data=validation.generator(
                                args.batch_size),
                            validation_steps=10,
                            workers=1,
                            verbose=1,
                            epochs=args.epochs)

    except KeyboardInterrupt as e:
        print('Model training stopped early.')
    model.save_weights("model_weights_nkbb.hdf5")

    print('Model training complete.')
Example #3
0
# Configuration
training_data = './training.csv'
validation_data = './validation.csv'

# Dataset functions
input_vocab = Vocabulary('./human_vocab.json', padding=config.padding)
output_vocab = Vocabulary('./machine_vocab.json', padding=config.padding)

print('Loading datasets...')

training = Data(training_data, input_vocab, output_vocab)
validation = Data(validation_data, input_vocab, output_vocab)
training.load()
validation.load()
training.transform()
validation.transform()

print('Datasets Loaded.')


def build_models(pad_length=config.padding,
                 n_chars=input_vocab.size(),
                 n_labels=output_vocab.size(),
                 embedding_learnable=False,
                 encoder_units=32,
                 decoder_units=32,
                 trainable=True,
                 return_probabilities=False):
    """Build the model"""
    input_ = Input(shape=(pad_length, ), dtype='float32')
Example #4
0
def main(args):
    # Dataset functions
    vocab = Vocabulary(args.vocabulary_data, padding=args.padding)
    kb_vocab = Vocabulary(args.vocabulary_data, padding=4)  # 7
    print('Loading datasets.')
    # Callback.__init__(self)
    if args.training_data.find("schedule") != -1:
        train_file_name = "schedule"
    elif args.training_data.find("navigate") != -1:
        train_file_name = "navigate"
    elif args.training_data.find("weather") != -1:
        train_file_name = "weather"
    elif args.training_data.find("ubuntu") != -1:
        train_file_name = "ubuntu"
    elif args.training_data.find("original") != -1:
        train_file_name = "original"
    else:
        train_file_name = "unknown"
    if args.save_path == "default":
        args.save_path = "weights/model_weights_" + train_file_name
    training = Data(args.training_data, vocab, kb_vocab,
                    args.generated_training_data)
    validation = Data(args.validation_data, vocab, kb_vocab)
    training.load()
    validation.load()
    training.transform()
    training.kb_out()
    validation.transform()
    validation.kb_out()
    print('Datasets Loaded.')
    print('Compiling Model.')

    model = KVMMModel(pad_length=args.padding,
                      embedding_size=args.embedding,
                      vocab_size=vocab.size(),
                      batch_size=batch_size,
                      n_chars=vocab.size(),
                      n_labels=vocab.size(),
                      encoder_units=200,
                      decoder_units=200).to(device)

    print(model)
    # Training using Adam Optimizer
    model_optimizer = optim.Adam(model.parameters(), lr=0.001)
    # Training using cross-entropy loss
    criterion = nn.CrossEntropyLoss()

    plot_losses = []
    print_loss_total = 0  # Reset every print_every
    plot_loss_total = 0  # Reset every plot_every
    print_every = 100
    save_every = 10000
    start = time.time()
    n_iters = 500000

    iter = 0
    while iter < n_iters:
        training_data = training.generator(batch_size)
        input_tensors = training_data[0][0]
        target_tensors = training_data[1]
        kbs = training_data[0][1]
        iter += 1
        loss = train(input_tensors, target_tensors, kbs, model,
                     model_optimizer, criterion, vocab, kb_vocab)
        print_loss_total += loss
        plot_loss_total += loss
        if iter % print_every == 0:
            validation_data = validation.generator(batch_size)
            validation_inputs = validation_data[0][0]
            validation_kbs = validation_data[0][1]
            validation_targets = validation_data[1]
            accuracy = evaluate(model, validation_inputs, validation_targets,
                                validation_kbs)
            print_loss_avg = print_loss_total / print_every
            print_loss_total = 0
            print('%s (%d %d%%) %.4f - val_accuracy %f' %
                  (timeSince(start, iter / n_iters), iter,
                   iter / n_iters * 100, print_loss_avg, accuracy))
            if iter % save_every == 0:
                torch.save(model.state_dict(),
                           args.save_path + "_iter_" + str(iter) + ".pytorch")