示例#1
0
def main(model, data_dirpath, model_dirpath):
    """
    Trains model from data found in data_dirpath, and stores it in
    model_dirpath.
    """
    logger = logging.getLogger(__name__)

    logger.info("Loading data")
    X_train = sp.load_npz(os.path.join(data_dirpath, "X_train.npz")).tocsr()
    y_train = np.load(os.path.join(data_dirpath, "y_train.npy"))

    if model == "dnn":
        import dnn
        logger.info("Training DNN")
        dnn_model = dnn.train(X_train, y_train)
        dnn.save(dnn_model, model_dirpath)
        logger.info("DNN train done")
    if model == "rnn":
        import rnn
        logger.info("Training RNN")
        labels = open(os.path.join(data_dirpath,
                                   "labels.txt")).read().split(",")
        X_train = rnn.prepare(X_train, labels)
        rnn_model = rnn.train(X_train, y_train)
        rnn.save(rnn_model, model_dirpath)
        logger.info("RNN train done")
    elif model == "xgb":
        import xgb
        logger.info("Training XGB")
        xgb_model = xgb.train(X_train, y_train)
        xgb.save(xgb_model, model_dirpath)
        logger.info("XGB train done")
    else:
        logger.error("Invalid model: {}".format(model))
示例#2
0
def getrnnembs(data, cencoder, embchars, tencoder, cdecoder, tdecoder, lan):
    if allembeddings["rnn"][lan] == None:
        print("Training RNN embeddings for %s" % lan)
        embs = []
        for dim in [5, 15, 30]:
            char_embedding = train(data, cencoder, cdecoder, tencoder,
                                   tdecoder, dim)
            embs.append(char_embedding)
        allembeddings["rnn"][lan] = embs
    return allembeddings["rnn"][lan]
	[298, 304, 908, 1176, 453],  
	[281, 947, 203, 1443, 159],
	[870, 109, 103, 1206, 1084], 
	[615, 1013, 324, 470, 901],
	[1025, 260, 1106, 551, 82], 
	[903, 1137, 1477, 210, 388],
	[824, 270,  595, 269, 454],
	[291, 637, 930, 292, 360],
	[832, 29, 320, 498, 1181], 
	[1352, 732, 1164, 488, 1124]]


group = int(sys.argv[1])

future = 7
for r in choices[group]:
	center = meta.index[r]

	tile = meta.loc[center, ['lat1', 'lat2','lon1', 'lon2']]

	X_train, Y_train, X_test, Y_test = rnn.getData(meta, X_raw, center, future)

	model = rnn.train(X_train, Y_train, epochs=200)
	prediction = model.predict(X_test)
	
	figTitle = './results/random_100-10_200/' + '#'.join(map(str, tile.values)) + '_{}'.format(future)
	dfName = figTitle + '.rnnres'
	rnn.Analysis(Y_test, prediction, figTitle, dfName)
	# rnnres = rnn.flexroc(dfName)
	# print(rnnres)
示例#4
0
def train():
    # Turn on training mode which enables dropout.
    if args.model == 'QRNN': rnn.reset()
    total_loss = 0
    start_time = time.time()
    ntokens = len(corpus.dictionary)
    hidden = rnn.init_hidden(args.batch_size)
    batch, i = 0, 0
    while i < train_data.size(0) - 1 - 1:
        bptt = args.bptt if np.random.random() < 0.95 else args.bptt / 2.
        # Prevent excessively small or negative sequence lengths
        seq_len = max(5, int(np.random.normal(bptt, 5)))
        # There's a very small chance that it could select a very long sequence length resulting in OOM
        seq_len = min(seq_len, args.bptt + 10)

        lr2 = optimizer.param_groups[0]['lr']
        optimizer.param_groups[0]['lr'] = lr2 * seq_len / args.bptt
        rnn.train()
        data, targets = get_batch(train_data, i, args, seq_len=seq_len)

        # Starting each batch, we detach the hidden state from how it was previously produced.
        # If we didn't, the model would try backpropagating all the way to start of the dataset.
        hidden = repackage_hidden(hidden)
        optimizer.zero_grad()

        output, hidden, rnn_hs, dropped_rnn_hs = rnn(data,
                                                     hidden,
                                                     return_h=True)
        raw_loss = criterion(output.view(-1, ntokens), targets)

        loss = raw_loss
        # Activiation Regularization
        loss = loss + sum(args.alpha * dropped_rnn_h.pow(2).mean()
                          for dropped_rnn_h in dropped_rnn_hs[-1:])
        # Temporal Activation Regularization (slowness)
        loss = loss + sum(args.beta * (rnn_h[1:] - rnn_h[:-1]).pow(2).mean()
                          for rnn_h in rnn_hs[-1:])
        loss.backward()

        # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
        torch.nn.utils.clip_grad_norm(rnn.parameters(), args.clip)
        optimizer.step()

        total_loss += raw_loss.data
        optimizer.param_groups[0]['lr'] = lr2
        if batch % args.log_interval == 0 and batch > 0:
            cur_loss = total_loss[0] / args.log_interval
            elapsed = time.time() - start_time
            print(
                '| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
                'loss {:5.2f} | ppl {:8.2f}'.format(
                    epoch, batch,
                    len(train_data) // args.bptt,
                    optimizer.param_groups[0]['lr'],
                    elapsed * 1000 / args.log_interval, cur_loss,
                    math.exp(cur_loss)))
            total_loss = 0
            start_time = time.time()
        ###
        batch += 1
        i += seq_len
示例#5
0
def trainModel(params_dict):
    model = getModel(params_dict)
    model.train()
    model.delete()
    del model
    return 0
print_cycle = 10
save_cycle = 10000
start_sequence = cb.get_number_batch("apple")

optimizer = torch.optim.Adam(decoder.parameters(), lr)
criterion = nn.CrossEntropyLoss()
start = time.time()

try:
    if (n_epochs == -1):
        epoch = 0
        while (True):
            epoch += 1
            print(epoch)
            loss = train(
                decoder, optimizer, criterion, sequence_len, batch_size,
                *random_training_set(data_batch, sequence_len, batch_size))

            if epoch % print_cycle == 0:
                print('[Time : %s, Epoach : (%d), Loss : %.4f]' %
                      (time_since(start), epoch, loss))
                print(cb.get_string(predict(decoder, start_sequence, 100)))
            if (epoch % save_cycle == 0):
                save(
                    decoder, "./" + model_dir + "/" + save_model_name + "-" +
                    str(model_num))
                print("Save %d model" % (model_num))

    else:
        print("Training for %d epochs..." % n_epochs)
        for epoch in range(1, n_epochs + 1):
示例#7
0
          checkr(civals[0], rs[0]), rs[0])
    print("  %s,DIM=15" % lan, confidenceival(randrs[1]),
          checkr(civals[1], rs[1]), rs[1])
    print("  %s,DIM=30" % lan, confidenceival(randrs[2]),
          checkr(civals[2], rs[2]), rs[2])
    print()


if __name__ == "__main__":
    print("1. CORRELATION EXPERIMENTS")
    print("--------------------------")
    print()
    #    correlation_experiment("../data/finnish","FI",getsvdembs,"SVD")
    #    correlation_experiment("../data/turkish","TUR",getsvdembs,"SVD")
    #    correlation_experiment("../data/spanish","ES",getsvdembs,"SVD")

    #    correlation_experiment("../data/finnish","FI",getw2vembs,"W2V")
    #    correlation_experiment("../data/turkish","TUR",getw2vembs,"W2V")
    #    correlation_experiment("../data/spanish","ES",getw2vembs,"W2V")

    #    correlation_experiment("../data/finnish","FI",getrnnembs,"RNN")
    #    correlation_experiment("../data/turkish","TUR",getrnnembs,"RNN")
    #    correlation_experiment("../data/spanish","ES",getrnnembs,"RNN")

    data, cencoder, tencoder, embchars = readdata('../data/finnish', "FI")
    modeld = initmodel(cencoder, tencoder, 15)
    encoded = encode(data[0][1], data[0][2], modeld)
    train(data, modeld)
#    for i in range(100):
#        print(update(data[0][1],data[0][2],data[0][0],modeld))
示例#8
0
                           train=False,
                           sort=False,
                           repeat=False,
                           device=DEVICE)

PAD_INDEX = TRG.vocab.stoi["<pad>"]
model = make_model(len(SRC.vocab),
                   len(TRG.vocab),
                   emb_size=256,
                   hidden_size=256,
                   num_layers=1,
                   dropout=0.2)
dev_perplexities = train(model,
                         print_every=100,
                         train_iter=train_iter,
                         valid_iter=valid_iter,
                         SRC=SRC,
                         TRG=TRG,
                         PAD_INDEX=PAD_INDEX)

import sacrebleu

references = [" ".join(example.trg) for example in val]
print(references[0])

hypothesis = []
alphas = []

for batch in valid_iter:
    batch = rebatch(PAD_INDEX, batch)
    pred, attention = greedy_decode(model,
示例#9
0
if __name__ == "__main__":
    print("1. CORRELATION EXPERIMENTS")
    print("--------------------------")
    print()
    correlation_experiment("data/finnish", "FI", get_svd_embeddings, "SVD")
    # correlation_experiment("data/finnish", "FI", getw2vembs, "W2V")
    #
    # correlation_experiment("data/spanish", "ES", getsvdembs, "SVD")
    # correlation_experiment("data/spanish", "ES", getw2vembs, "W2V")
    #
    # correlation_experiment("data/turkish", "TUR", getsvdembs, "SVD")
    # correlation_experiment("data/turkish", "TUR", getw2vembs, "W2V")

    # TODO getrnnembs is missing
    # correlation_experiment("data/finnish", "FI", getrnnembs, "RNN")
    # correlation_experiment("data/turkish", "TUR", getrnnembs, "RNN")
    # correlation_experiment("data/spanish", "ES", getrnnembs, "RNN")

    training_data, training_character_encoder, training_tag_encoder, training_embedded_characters = \
        read_data('data/finnish', "FI")
    training_modeld = initmodel(training_character_encoder,
                                training_tag_encoder, 15)
    training_encoded = encode(training_data[0][1], training_data[0][2],
                              training_modeld)
    train(training_data, training_modeld)
    for _ in range(100):
        print(
            update(training_data[0][1], training_data[0][2],
                   training_data[0][0], training_modeld))