def main(): config = Config() vocab = Vocab(config.dict_file) dev_q, dev_c, dev_s, dev_spans, dev_s_idx, dev_answerable = load_data( config.dev_file, vocab, config.debug) dev_data = list( zip(dev_q, dev_c, dev_s, dev_s_idx, dev_answerable, dev_spans)) ssnet = SSQANet(config) ssnet.build_model() ssnet.restore_session(config.dir_model) batches = batch_loader(dev_data, config.batch_size, shuffle=False) acc_history = [] em_history = [] for batch in batches: batch_q, batch_c, batch_s, batch_s_idx, batch_ans, batch_spans = zip( *batch) question_lengths, padded_q = zero_padding(batch_q, level=1) context_lengths, padded_c = zero_padding(batch_c, level=1) sequence_lengths, sentence_lengths, padded_s = zero_padding(batch_s, level=2) batch_acc, batch_em, batch_loss = ssnet.eval( padded_q, question_lengths, padded_c, context_lengths, padded_s, sequence_lengths, sentence_lengths, batch_s_idx, batch_ans, batch_spans) acc_history.append(batch_acc) em_history.append(batch_em) dev_acc = np.mean(acc_history) dev_em = np.mean(em_history) print("classification acc :{}".format(dev_acc)) print("EM :{}".format(dev_em))
def main(): config = Config() logger, log_dir = get_logger(os.path.join(config.model, "logs/")) logger.info("=======Model Configuration=======") logger.info(config.desc) logger.info("=================================") try: _, _, test_x, _, _, test_y, _, _, test_m, test_dt = load_agg_selected_data_mem(data_path=config.data_path, \ x_len=config.x_len, \ y_len=config.y_len, \ foresight=config.foresight, \ cell_ids=config.test_cell_ids, \ dev_ratio=config.dev_ratio, \ test_len=config.test_len, \ seed=config.seed) model = Model(config) if config.latest_model: model_dir = find_latest_dir( os.path.join(config.model, 'model_save/')) else: if not model_dir: raise Exception( "model_dir or latest_model=True should be defined in config" ) model_dir = config.model_dir model.restore_session(model_dir) if len(test_y) > 100000: # Batch mode test_data = list(zip(test_x, test_m, test_y)) test_batches = batch_loader(test_data, config.batch_size) total_pred = np.empty(shape=(0, test_y.shape[1])) for batch in test_batches: batch_x, batch_m, batch_y = zip(*batch) pred, _, _, _, _ = model.eval(batch_x, batch_m, batch_y) total_pred = np.r_[total_pred, pred] else: # Not batch mode total_pred, test_loss, test_rse, test_smape, test_mae = model.eval( test_x, test_m, test_y) result_dir = make_date_dir(os.path.join(config.model, 'results/')) np.save(os.path.join(result_dir, 'pred.npy'), total_pred) np.save(os.path.join(result_dir, 'test_y.npy'), test_y) np.save(os.path.join(result_dir, 'test_dt.npy'), test_dt) logger.info("Saving results at {}".format(result_dir)) logger.info("Testing finished, exit program") except: logger.exception("ERROR")
def main(): config = Config() logger, log_dir = get_logger(os.path.join(config.model, "logs/")) logger.setLevel(30) # set loglevel WARNING(30) logger.info("=======Model Configuration=======") logger.info(config.desc) logger.info("=================================") try: _, _, test_x, _, _, test_y, _, _, test_m, test_dt = load_agg_selected_data_mem(data_path=config.data_path, \ x_len=config.x_len, \ y_len=config.y_len, \ foresight=config.foresight, \ cell_ids=config.test_cell_ids, \ dev_ratio=config.dev_ratio, \ test_len=config.test_len, \ seed=config.seed) # add dummy data test_x = np.concatenate([test_x] * 10, axis=0) test_m = np.concatenate([test_m] * 10, axis=0) test_y = np.concatenate([test_y] * 10, axis=0) print("Size of x,m,y : {}, {}, {} bytes, total {} GB".format( test_x.nbytes, test_m.nbytes, test_y.nbytes, (test_x.nbytes + test_m.nbytes + test_y.nbytes) / 1024 / 1024 / 1024)) print("Batch Size : {}".format(config.batch_size)) model = Model(config) if config.latest_model: model_dir = find_latest_dir( os.path.join(config.model, 'model_save/')) else: if not config.model_dir: raise Exception( "model_dir or latest_model=True should be defined in config" ) model_dir = config.model_dir model.restore_session(model_dir) # always run as batch mode test_data = list(zip(test_x, test_m, test_y)) test_batches = batch_loader(test_data, config.batch_size) total_pred = np.empty(shape=(0, test_y.shape[1])) time_start = time() for idx, batch in enumerate(test_batches): batch_x, batch_m, batch_y = zip(*batch) pred, _, _, _, _ = model.eval(batch_x, batch_m, batch_y) total_pred = np.r_[total_pred, pred] print("Batch looped {} times".format(idx + 1)) time_end = time() print("Elapsed Time in Inferencing: {}".format(time_end - time_start)) result_dir = make_date_dir(os.path.join(config.model, 'results/')) np.save(os.path.join(result_dir, 'pred.npy'), total_pred) np.save(os.path.join(result_dir, 'test_y.npy'), test_y) np.save(os.path.join(result_dir, 'test_dt.npy'), test_dt) logger.info("Saving results at {}".format(result_dir)) logger.info("Testing finished, exit program") except: logger.exception("ERROR")
def main(): config = Config() vocab = Vocab(config.dict_file) q, c, s, spans, s_idx, answerable = load_data(config.train_file, vocab, config.debug) dev_q, dev_c, dev_s, dev_spans, dev_s_idx, dev_answerable = load_data( config.dev_file, vocab, config.debug) train_data = list(zip(q, c, s, s_idx, answerable, spans)) dev_data = list( zip(dev_q, dev_c, dev_s, dev_s_idx, dev_answerable, dev_spans)) ssnet = SSQANet(config) ssnet.build_model() best_score = 0 for i in range(config.num_epochs): epoch = i + 1 batches = batch_loader(train_data, config.batch_size, shuffle=False) for batch in batches: batch_q, batch_c, batch_s, batch_s_idx, batch_ans, batch_spans = zip( *batch) question_lengths, padded_q = zero_padding(batch_q, level=1) context_lengths, padded_c = zero_padding(batch_c, level=1) sequence_lengths, sentence_lengths, padded_s = zero_padding( batch_s, level=2) loss, acc, pred, step = ssnet.train(padded_q, question_lengths, padded_c, context_lengths, padded_s, sequence_lengths, sentence_lengths, batch_s_idx, batch_ans, batch_spans, config.dropout) train_batch_acc, train_batch_em, train_batch_loss = ssnet.eval( padded_q, question_lengths, padded_c, context_lengths, padded_s, sequence_lengths, sentence_lengths, batch_s_idx, batch_ans, batch_spans) if step % 100 == 0: print("epoch: %d, step:%d, loss:%.4f, acc:%.2f, em:%.2f" % (epoch, step, loss, train_batch_acc, train_batch_em)) if step % 1000 == 0: dev_batches = batch_loader(dev_data, config.batch_size, shuffle=False) total_em = [] total_acc = [] total_loss = [] for dev_batch in dev_batches: dev_batch_q, dev_batch_c, dev_batch_s, \ dev_batch_s_idx, dev_batch_ans, dev_batch_spans = zip(*dev_batch) question_lengths, padded_q = zero_padding(dev_batch_q, level=1) context_lengths, padded_c = zero_padding(dev_batch_c, level=1) sequence_lengths, sentence_lengths, padded_s = zero_padding( dev_batch_s, level=2) dev_batch_acc, dev_batch_em, dev_batch_loss = ssnet.eval( padded_q, question_lengths, padded_c, context_lengths, padded_s, sequence_lengths, sentence_lengths, dev_batch_s_idx, dev_batch_ans, dev_batch_spans) total_loss.append(dev_batch_loss) total_em.append(dev_batch_em) total_acc.append(dev_batch_acc) dev_em = np.mean(total_em) dev_acc = np.mean(total_acc) dev_loss = np.mean(total_loss) ssnet.write_summary(dev_acc, dev_em, dev_loss, mode="dev") ssnet.write_summary(train_batch_acc, train_batch_em, train_batch_loss, mode="train") print("after %d step, dev_em:%.2f" % (step, dev_em)) if dev_em > best_score: best_score = dev_em print("new score! em: %.2f, acc:%.2f" % (dev_em, dev_acc)) ssnet.save_session(config.dir_model)
def main(): config = Config() logger, log_dir = get_logger(os.path.join(config.model, "logs/")) logger.info("=======Model Configuration=======") logger.info(config.desc) logger.info("=================================") try: train_x, dev_x, test_x, train_y, dev_y, test_y, test_dt = load_agg_data(data_path=config.data_path, \ x_len=config.x_len, \ y_len=config.y_len, \ ncells=config.ncells, \ foresight=config.foresight, \ dev_ratio=config.dev_ratio,\ test_len=config.test_len, \ seed=config.seed) model = Model(config) train_data = list(zip(train_x,train_y)) no_improv = 0 best_loss = 100 model_dir = make_date_dir(os.path.join(config.model, 'model_save/')) result_dir = make_date_dir(os.path.join(config.model, 'results/')) logger.info("Start training") dev_x = np.asarray(dev_x) dev_y = np.asarray(dev_y) start_time = time() for i in range(config.num_epochs): train_batches = batch_loader(train_data, config.batch_size) epoch = i+1 for batch in train_batches: batch_x, batch_y = zip(*batch) batch_x = np.asarray(batch_x) batch_y = np.asarray(batch_y) loss, rse, smape, mae, step = model.train(batch_x, batch_y) if step % 100 == 0: logger.info("epoch: %d, step: %d, loss: %.4f, rse: %.4f, smape: %.4f, mae: %.4f" % (epoch, step, loss, rse, smape, mae)) # dev score for each epoch (no mini batch) _, dev_loss, dev_rse, dev_smape, dev_mae = model.eval(dev_x, dev_y) if dev_loss < best_loss: best_loss = dev_loss no_improv = 0 logger.info("New score! : dev_loss: %.4f, dev_rse: %.4f, dev_smape: %.4f, dev_mae: %.4f" % (dev_loss, dev_rse, dev_smape, dev_mae)) logger.info("Saving model at {}".format(model_dir)) model.save_session(os.path.join(model_dir, config.model)) else: no_improv += 1 if no_improv == config.nepoch_no_improv: logger.info("No improvement for %d epochs" % no_improv) # break # model.save_session(os.path.join(model_dir, config.model)) elapsed = time() - start_time # generating results (no mini batch) model.restore_session(model_dir) pred, test_loss, test_rse, test_smape, test_mae = model.eval(test_x, test_y) logger.info("test_loss: %.4f, test_rse: %.4f, test_smape: %.4f, test_mae: %.4f" % (test_loss, test_rse, test_smape, test_mae)) # save results # np.save(os.path.join(result_dir, 'pred.npy'), pred) # np.save(os.path.join(result_dir, 'test_y.npy'), test_y) # np.save(os.path.join(result_dir, 'test_dt.npy'), test_dt) # logger.info("Saving results at {}".format(result_dir)) logger.info("Elapsed training time {0:0.2f} mins".format(elapsed/60)) logger.info("Training finished, exit program") except: logger.exception("ERROR")
def main(): parser = argparse.ArgumentParser() #Required parameters parser.add_argument("-m", "--model", type=str, default="Bin_normal", required=True, choices=["Bin_normal", "Bin-uniform", "Bin_3d"], help="Model selected in the list: Bin_normal, Bin-uniform,Bin_3d") #Optional parameters args = parser.parse_args() args = parser.parse_args() if args.model == "Bin_normal": from Bin_normal.config import Config from Bin_normal.model import Model config = Config() elif args.model == "Bin_3d": from Bin_3d.config import Config from Bin_3d.model import Model config = Config() else: from MANN.config import Config from MANN.model import Model config = Config() logger = get_logger(os.path.join(config.model, "logs/")) logger.info("=======Model Configuration======") logger.info(config.desc) logger.info("================================") try: train_x, dev_x, test_x, train_y, dev_y, test_y = load_data_from_csv(data_path=config.data_path, x_len=config.x_len, y_len=config.y_len, foresight=config.foresight, dev_ratio=config.dev_ratio, #test_ratio=config.test_ratio, seed=config.seed) logger.info("train_x shape: {}, dev_x shape: {}, test_x shape: {}" .format(train_x.shape, dev_x.shape, test_x.shape)) logger.info("train_y shape: {}, dev_y shape: {}, test_y shape: {}" .format(train_y.shape, dev_y.shape, test_y.shape)) model = Model(config) train_data = list(zip(train_x, train_y)) no_improv = 0 best_loss = 100 model_dir = make_date_dir(os.path.join(config.model, 'model_save/')) result_dir = make_date_dir(os.path.join(config.model, 'results/')) start_time = time() for i in range(config.num_epochs): train_batches = batch_loader(train_data, config.batch_size) epoch = i+1 for batch in train_batches: batch_x, batch_y = zip(*batch) loss, acc, step = model.train(batch_x, batch_y) if step % 100 == 0: logger.info("epoch: %d, step: %d, loss: %4f, acc: %4f" % (epoch, step, loss, acc)) # dev score for each epoch (no mini batch) _, dev_loss, dev_acc = model.eval(dev_x, dev_y) if dev_loss < best_loss: best_loss = dev_loss no_improv = 0 logger.info("New score! : dev_loss: %4f, dev_acc: %4f" % (dev_loss, dev_acc)) logger.info("Saving model at {}".format(model_dir)) model.save_session(os.path.join(model_dir, config.model)) else: no_improv += 1 if no_improv == config.nepoch_no_improv: logger.info("No improvement for %d epochs" % no_improv) break elapsed = time()-start_time # generating results (no mini batch) model.restore_session(model_dir) pred, test_loss, test_acc = model.eval(test_x, test_y) logger.info("test_loss: %4f, test_acc: %4f" % (test_loss, test_acc, )) # save results np.save(os.path.join(result_dir, 'pred.npy'), pred) np.save(os.path.join(result_dir, 'test_y.npy'), test_y) logger.info("Saving results at {}".format(result_dir)) logger.info("Elapsed training time {0:0.4f}".format(elapsed)) logger.info("Training finished, exit program") except: logger.exception("ERROR")
def train(n_epochs=10): data_file = '../data/train-stanford-raw.conll' # if vocab_file is given (ie for pretrained wordvectors), use x2i and i2x from this file. # If not given, create new vocab file in ../data vocab_file = None log_folder = '../logs' model_folder = '../models' model_name = 'wsj_3' model_file = os.path.join(model_folder, model_name + '_{}.model') log_file = open(os.path.join(log_folder, model_name + '.csv'), 'w', 1) print('epoch,train_loss,val_loss,arc_acc,lab_acc', file=log_file) batch_size = 64 prints_per_epoch = 10 n_epochs *= prints_per_epoch # load data print('loading data...') data, x2i, i2x = make_dataset(data_file) if not vocab_file: with open('../data/vocab_{}.pkl'.format(model_name), 'wb') as f: pickle.dump((x2i, i2x), f) # make train and val batch loaders train_data, val_data = split_train_test(data) print('# train sentences', len(train_data)) print('# val sentences', len(val_data)) train_loader = batch_loader(train_data, batch_size) val_loader = batch_loader(val_data, batch_size, shuffle=False) print('creating model...') # make model model = BiAffineParser(word_vocab_size=len(x2i['word']), word_emb_dim=100, pos_vocab_size=len(x2i['tag']), pos_emb_dim=28, emb_dropout=0.33, lstm_hidden=512, lstm_depth=3, lstm_dropout=.33, arc_hidden=256, arc_depth=1, arc_dropout=.33, arc_activation='ReLU', lab_hidden=128, lab_depth=1, lab_dropout=.33, lab_activation='ReLU', n_labels=len(x2i['label'])) print(model) model.cuda() base_params, arc_params, lab_params = model.get_param_groups() opt = Adam([ { 'params': base_params, 'lr': 2e-3 }, { 'params': arc_params, 'lr': 2e-3 }, { 'params': lab_params, 'lr': 1e-4 }, ], betas=[.9, .9]) sched = ReduceLROnPlateau(opt, threshold=1e-3, patience=8, factor=.4, verbose=True) n_train_batches = int(len(train_data) / batch_size) n_val_batches = int(len(val_data) / batch_size) batches_per_epoch = int(n_train_batches / prints_per_epoch) for epoch in range(n_epochs): t0 = time.time() # Training train_loss = 0 model.train() for i in range(batches_per_epoch): opt.zero_grad() # Load batch words, tags, arcs, lengths = next(train_loader) words = words.cuda() tags = tags.cuda() # Forward S_arc, S_lab = model(words, tags, lengths=lengths) # Calculate loss arc_loss = get_arc_loss(S_arc, arcs) lab_loss = get_label_loss(S_lab, arcs) loss = arc_loss + .025 * lab_loss train_loss += arc_loss.data[0] + lab_loss.data[0] # Backward loss.backward() opt.step() train_loss /= batches_per_epoch # Evaluation val_loss = 0 arc_acc = 0 lab_acc = 0 model.eval() for i in range(n_val_batches): words, tags, arcs, lengths = next(val_loader) words = words.cuda() tags = tags.cuda() S_arc, S_lab = model(words, tags, lengths=lengths) arc_loss = get_arc_loss(S_arc, arcs) lab_loss = get_label_loss(S_lab, arcs) loss = arc_loss + lab_loss val_loss += arc_loss.data[0] + lab_loss.data[0] arc_acc += get_arc_accuracy(S_arc, arcs) lab_acc += get_label_accuracy(S_lab, arcs) val_loss /= n_val_batches arc_acc /= n_val_batches lab_acc /= n_val_batches epoch_time = time.time() - t0 print( 'epoch {:.1f}\t train_loss {:.3f}\t val_loss {:.3f}\t arc_acc {:.3f}\t lab_acc {:.3f}\t time {:.1f} sec' .format(epoch / prints_per_epoch, train_loss, val_loss, arc_acc, lab_acc, epoch_time), end="\r") print('{:.3f},{:.3f},{:.3f},{:.3f},{:.3f}'.format( epoch / prints_per_epoch, train_loss, val_loss, arc_acc, lab_acc), file=log_file) sched.step(val_loss) print('Done!') torch.save(model, model_file.format(val_loss)) log_file.close()
def main(): parser = argparse.ArgumentParser() # Required parameters parser.add_argument( "-m", "--model", type=str, default="LSTNet", required=True, choices=["LSTNet", "MANN", "AR_reg", "AR", "AR_mem"], help="Model selected in the list: LSTNet, MANN, AR_reg, AR, AR_mem") # Optional parameters args = parser.parse_args() if args.model == "LSTNet": from LSTNet.config import Config from LSTNet.model import Model config = Config() elif args.model == "MANN": from MANN.config import Config from MANN.model import Model config = Config() elif args.model == "AR_reg": from AR_reg.config import Config from AR_reg.model import Model config = Config() elif args.model == "AR_mem": from AR_mem.config import Config from AR_mem.model import Model config = Config() elif args.model == "AR": from AR.config import Config from AR.model import Model config = Config() COL_LIST = ['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'] logger = get_logger(os.path.join(config.model, "logs/")) logger.info("=======Model Configuration======") logger.info(config.desc) logger.info("================================") try: train_x, dev_x, test_x, train_y, dev_y, test_y, train_m, dev_m, test_m, test_dt = load_data_mem( data_path=config.data_path, x_col_list=COL_LIST, y_col_list=COL_LIST, x_len=config.x_len, y_len=config.y_len, mem_len=config.mem_len, foresight=config.foresight, dev_ratio=config.dev_ratio, test_len=config.test_len, seed=config.seed) model = Model(config) train_data = list(zip(train_x, train_m, train_y)) no_improv = 0 best_loss = 100 model_dir = make_date_dir(os.path.join(config.model, 'model_save/')) result_dir = make_date_dir(os.path.join(config.model, 'results/')) logger.info("Start training") dev_x = np.asarray(dev_x) dev_y = np.asarray(dev_y) start_time = time() for i in range(config.num_epochs): train_batches = batch_loader(train_data, config.batch_size) epoch = i + 1 for batch in train_batches: batch_x, batch_m, batch_y = zip(*batch) loss, rmse, rse, smape, mae, step = model.train( batch_x, batch_m, batch_y) if step % 100 == 0: logger.info( "epoch: %d, step: %d, loss: %4f, rmse: %4f, rse: %4f, smape: %4f, mae: %4f" % (epoch, step, loss, rmse, rse, smape, mae)) # dev score for each epoch (no mini batch) _, dev_loss, dev_rmse, dev_rse, dev_smape, dev_mae = model.eval( dev_x, dev_m, dev_y) if dev_loss < best_loss: best_loss = dev_loss no_improv = 0 logger.info( "New score! : dev_loss: %4f, rmse: %4f, dev_rse: %4f, dev_smape: %4f, dev_mae: %4f" % (dev_loss, dev_rmse, dev_rse, dev_smape, dev_mae)) logger.info("Saving model at {}".format(model_dir)) model.save_session(os.path.join(model_dir, config.model)) else: no_improv += 1 if no_improv == config.nepoch_no_improv: logger.info("No improvement for %d epochs" % no_improv) break elapsed = time() - start_time # generating results (no mini batch) model.restore_session(model_dir) pred, test_loss, test_rmse, test_rse, test_smape, test_mae = model.eval( test_x, test_m, test_y) logger.info( "test_loss: %4f, test_rmse: %4f, test_rse: %4f, test_smape: %4f, test_mae: %4f" % (test_loss, test_rmse, test_rse, test_smape, test_mae)) # save results np.save(os.path.join(result_dir, 'pred.npy'), pred) np.save(os.path.join(result_dir, 'test_y.npy'), test_y) np.save(os.path.join(result_dir, 'test_dt.npy'), test_y) logger.info("Saving results at {}".format(result_dir)) logger.info("Elapsed training time {0:0.2f}".format(elapsed / 60)) logger.info("Training finished, exit program") t = np.linspace(0, pred.shape[0], num=pred.shape[0]) mae = np.mean(np.abs(test_y - pred)) mape = np.mean(np.abs((test_y - pred) / test_y)) plt.rcParams['figure.figsize'] = [20, 4] plt.plot(t, test_y, "r", alpha=0.5) #plt.ylim(0.5,1.0) plt.plot(t, pred, "b") #plt.title("{}, mape:{mape:.5f}, mae:{mae:.5f}".format(raw.columns[1], mape=mape, mae=mae), size=20) plt.legend(("actual", "pred"), loc="upper left") plt.grid() plt.show() plt.savefig(os.path.join(config.model, "image/figure.png")) except: logger.exception("ERROR")