diff --git a/extract_vocab.py b/extract_vocab.py index 970f488..5f71158 100644 --- a/extract_vocab.py +++ b/extract_vocab.py @@ -21,7 +21,7 @@ load_dataset(0, use_small=USE_SMALL) word_emb = load_word_emb('glove/glove.%dB.%dd.txt'%(B_word,N_word), use_small=USE_SMALL) -print "Length of word vocabulary: %d"%len(word_emb) +print ("Length of word vocabulary: %d"%len(word_emb)) word_to_idx = {'':0, '':1, '':2} word_num = 3 @@ -57,7 +57,7 @@ def check_and_add(tok): for tok in col: check_and_add(tok) -print "Length of used word vocab: %s"%len(word_to_idx) +print ("Length of used word vocab: %s"%len(word_to_idx)) emb_array = np.stack(embs, axis=0) with open('glove/word2idx.json', 'w') as outf: diff --git a/sqlnet/lib/dbengine.py b/sqlnet/lib/dbengine.py index 619c8b1..1a99c57 100644 --- a/sqlnet/lib/dbengine.py +++ b/sqlnet/lib/dbengine.py @@ -34,7 +34,7 @@ def execute(self, table_id, select_index, aggregation_index, conditions, lower=T where_clause = [] where_map = {} for col_index, op, val in conditions: - if lower and (isinstance(val, str) or isinstance(val, unicode)): + if lower and isinstance(val, str): val = val.lower() if schema['col{}'.format(col_index)] == 'real' and not isinstance(val, (int, float)): try: diff --git a/sqlnet/model/modules/aggregator_predict.py b/sqlnet/model/modules/aggregator_predict.py index 047d2ea..1b3f53e 100644 --- a/sqlnet/model/modules/aggregator_predict.py +++ b/sqlnet/model/modules/aggregator_predict.py @@ -4,7 +4,7 @@ import torch.nn.functional as F from torch.autograd import Variable import numpy as np -from net_utils import run_lstm, col_name_encode +from .net_utils import run_lstm, col_name_encode @@ -13,17 +13,17 @@ def __init__(self, N_word, N_h, N_depth, use_ca): super(AggPredictor, self).__init__() self.use_ca = use_ca - self.agg_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.agg_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) if use_ca: - print "Using column attention on aggregator predicting" + print ("Using column attention on aggregator predicting") self.agg_col_name_enc = nn.LSTM(input_size=N_word, - hidden_size=N_h/2, num_layers=N_depth, + hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.agg_att = nn.Linear(N_h, N_h) else: - print "Not using column attention on aggregator predicting" + print ("Not using column attention on aggregator predicting") self.agg_att = nn.Linear(N_h, 1) self.agg_out = nn.Sequential(nn.Linear(N_h, N_h), nn.Tanh(), nn.Linear(N_h, 6)) diff --git a/sqlnet/model/modules/selection_predict.py b/sqlnet/model/modules/selection_predict.py index 73d8a6a..9b8e7fa 100644 --- a/sqlnet/model/modules/selection_predict.py +++ b/sqlnet/model/modules/selection_predict.py @@ -4,23 +4,23 @@ import torch.nn.functional as F from torch.autograd import Variable import numpy as np -from net_utils import run_lstm, col_name_encode +from .net_utils import run_lstm, col_name_encode class SelPredictor(nn.Module): def __init__(self, N_word, N_h, N_depth, max_tok_num, use_ca): super(SelPredictor, self).__init__() self.use_ca = use_ca self.max_tok_num = max_tok_num - self.sel_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.sel_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) if use_ca: - print "Using column attention on selection predicting" + print ("Using column attention on selection predicting") self.sel_att = nn.Linear(N_h, N_h) else: - print "Not using column attention on selection predicting" + print ("Not using column attention on selection predicting") self.sel_att = nn.Linear(N_h, 1) - self.sel_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.sel_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.sel_out_K = nn.Linear(N_h, N_h) diff --git a/sqlnet/model/modules/seq2sql_condition_predict.py b/sqlnet/model/modules/seq2sql_condition_predict.py index 4bb34ba..6820ba2 100644 --- a/sqlnet/model/modules/seq2sql_condition_predict.py +++ b/sqlnet/model/modules/seq2sql_condition_predict.py @@ -4,12 +4,12 @@ import torch.nn.functional as F from torch.autograd import Variable import numpy as np -from net_utils import run_lstm +from .net_utils import run_lstm class Seq2SQLCondPredictor(nn.Module): def __init__(self, N_word, N_h, N_depth, max_col_num, max_tok_num, gpu): super(Seq2SQLCondPredictor, self).__init__() - print "Seq2SQL where prediction" + print ("Seq2SQL where prediction") self.N_h = N_h self.max_tok_num = max_tok_num self.max_col_num = max_col_num diff --git a/sqlnet/model/modules/sqlnet_condition_predict.py b/sqlnet/model/modules/sqlnet_condition_predict.py index 1eb5500..16236bc 100644 --- a/sqlnet/model/modules/sqlnet_condition_predict.py +++ b/sqlnet/model/modules/sqlnet_condition_predict.py @@ -4,7 +4,7 @@ import torch.nn.functional as F from torch.autograd import Variable import numpy as np -from net_utils import run_lstm, col_name_encode +from .net_utils import run_lstm, col_name_encode class SQLNetCondPredictor(nn.Module): def __init__(self, N_word, N_h, N_depth, max_col_num, max_tok_num, use_ca, gpu): @@ -15,36 +15,36 @@ def __init__(self, N_word, N_h, N_depth, max_col_num, max_tok_num, use_ca, gpu): self.gpu = gpu self.use_ca = use_ca - self.cond_num_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_num_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.cond_num_att = nn.Linear(N_h, 1) self.cond_num_out = nn.Sequential(nn.Linear(N_h, N_h), nn.Tanh(), nn.Linear(N_h, 5)) - self.cond_num_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_num_name_enc = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.cond_num_col_att = nn.Linear(N_h, 1) self.cond_num_col2hid1 = nn.Linear(N_h, 2*N_h) self.cond_num_col2hid2 = nn.Linear(N_h, 2*N_h) - self.cond_col_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_col_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) if use_ca: - print "Using column attention on where predicting" + print ("Using column attention on where predicting") self.cond_col_att = nn.Linear(N_h, N_h) else: - print "Not using column attention on where predicting" + print ("Not using column attention on where predicting") self.cond_col_att = nn.Linear(N_h, 1) - self.cond_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_col_name_enc = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.cond_col_out_K = nn.Linear(N_h, N_h) self.cond_col_out_col = nn.Linear(N_h, N_h) self.cond_col_out = nn.Sequential(nn.ReLU(), nn.Linear(N_h, 1)) - self.cond_op_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_op_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) if use_ca: @@ -59,13 +59,13 @@ def __init__(self, N_word, N_h, N_depth, max_col_num, max_tok_num, use_ca, gpu): self.cond_op_out = nn.Sequential(nn.Linear(N_h, N_h), nn.Tanh(), nn.Linear(N_h, 3)) - self.cond_str_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_str_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.cond_str_decoder = nn.LSTM(input_size=self.max_tok_num, hidden_size=N_h, num_layers=N_depth, batch_first=True, dropout=0.3) - self.cond_str_name_enc = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.cond_str_name_enc = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) self.cond_str_out_g = nn.Linear(N_h, N_h) @@ -123,9 +123,9 @@ def forward(self, x_emb_var, x_len, col_inp_var, col_name_len, num_col_att = self.softmax(num_col_att_val) K_num_col = (e_num_col * num_col_att.unsqueeze(2)).sum(1) cond_num_h1 = self.cond_num_col2hid1(K_num_col).view( - B, 4, self.N_h/2).transpose(0, 1).contiguous() + B, 4, int(self.N_h/2)).transpose(0, 1).contiguous() cond_num_h2 = self.cond_num_col2hid2(K_num_col).view( - B, 4, self.N_h/2).transpose(0, 1).contiguous() + B, 4, int(self.N_h/2)).transpose(0, 1).contiguous() h_num_enc, _ = run_lstm(self.cond_num_lstm, x_emb_var, x_len, hidden=(cond_num_h1, cond_num_h2)) diff --git a/sqlnet/model/modules/word_embedding.py b/sqlnet/model/modules/word_embedding.py index b41a0a7..3f26307 100644 --- a/sqlnet/model/modules/word_embedding.py +++ b/sqlnet/model/modules/word_embedding.py @@ -16,14 +16,14 @@ def __init__(self, word_emb, N_word, gpu, SQL_TOK, self.SQL_TOK = SQL_TOK if trainable: - print "Using trainable embedding" + print ("Using trainable embedding") self.w2i, word_emb_val = word_emb self.embedding = nn.Embedding(len(self.w2i), N_word) self.embedding.weight = nn.Parameter( torch.from_numpy(word_emb_val.astype(np.float32))) else: self.word_emb = word_emb - print "Using fixed embedding" + print ("Using fixed embedding") def gen_x_batch(self, q, col): @@ -32,9 +32,9 @@ def gen_x_batch(self, q, col): val_len = np.zeros(B, dtype=np.int64) for i, (one_q, one_col) in enumerate(zip(q, col)): if self.trainable: - q_val = map(lambda x:self.w2i.get(x, 0), one_q) + q_val = list(map(lambda x:self.w2i.get(x, 0), one_q)) # Py 3.x (list(map) for concat) else: - q_val = map(lambda x:self.word_emb.get(x, np.zeros(self.N_word, dtype=np.float32)), one_q) + q_val = list(map(lambda x:self.word_emb.get(x, np.zeros(self.N_word, dtype=np.float32)), one_q)) # Py 3.x (list(map) for concat) if self.our_model: if self.trainable: val_embs.append([1] + q_val + [2]) # and @@ -44,14 +44,14 @@ def gen_x_batch(self, q, col): else: one_col_all = [x for toks in one_col for x in toks+[',']] if self.trainable: - col_val = map(lambda x:self.w2i.get(x, 0), one_col_all) + col_val = list(map(lambda x:self.w2i.get(x, 0), one_col_all)) # Py 3.x (list(map) for concat) val_embs.append( [0 for _ in self.SQL_TOK] + col_val + [0] + q_val+ [0]) else: - col_val = map(lambda x:self.word_emb.get(x, np.zeros(self.N_word, dtype=np.float32)), one_col_all) + col_val = list(map(lambda x:self.word_emb.get(x, np.zeros(self.N_word, dtype=np.float32)), one_col_all)) # Py 3.x (list(map) for concat) val_embs.append( [np.zeros(self.N_word, dtype=np.float32) for _ in self.SQL_TOK] + col_val + [np.zeros(self.N_word, dtype=np.float32)] + q_val+ [np.zeros(self.N_word, dtype=np.float32)]) val_len[i] = len(self.SQL_TOK) + len(col_val) + 1 + len(q_val) + 1 max_len = max(val_len) - + if self.trainable: val_tok_array = np.zeros((B, max_len), dtype=np.int64) for i in range(B): diff --git a/sqlnet/model/seq2sql.py b/sqlnet/model/seq2sql.py index 651a4ea..f43bb03 100644 --- a/sqlnet/model/seq2sql.py +++ b/sqlnet/model/seq2sql.py @@ -4,10 +4,10 @@ import torch.nn.functional as F from torch.autograd import Variable import numpy as np -from modules.word_embedding import WordEmbedding -from modules.aggregator_predict import AggPredictor -from modules.selection_predict import SelPredictor -from modules.seq2sql_condition_predict import Seq2SQLCondPredictor +from .modules.word_embedding import WordEmbedding +from .modules.aggregator_predict import AggPredictor +from .modules.selection_predict import SelPredictor +from .modules.seq2sql_condition_predict import Seq2SQLCondPredictor # This is a re-implementation based on the following paper: @@ -80,8 +80,8 @@ def generate_gt_where_seq(self, q, col, query): cur_seq = [all_toks.index('')] if 'WHERE' in cur_query: cur_where_query = cur_query[cur_query.index('WHERE'):] - cur_seq = cur_seq + map(lambda tok:all_toks.index(tok) - if tok in all_toks else 0, cur_where_query) + cur_seq = cur_seq + list(map(lambda tok:all_toks.index(tok) + if tok in all_toks else 0, cur_where_query)) cur_seq.append(all_toks.index('')) ret_seq.append(cur_seq) return ret_seq @@ -146,7 +146,7 @@ def loss(self, score, truth_num, pred_entry, gt_where): agg_score, sel_score, cond_score = score loss = 0 if pred_agg: - agg_truth = map(lambda x:x[0], truth_num) + agg_truth = list(map(lambda x:x[0], truth_num)) data = torch.from_numpy(np.array(agg_truth)) if self.gpu: agg_truth_var = Variable(data.cuda()) @@ -156,7 +156,7 @@ def loss(self, score, truth_num, pred_entry, gt_where): loss += self.CE(agg_score, agg_truth_var) if pred_sel: - sel_truth = map(lambda x:x[1], truth_num) + sel_truth = list(map(lambda x:x[1], truth_num)) data = torch.from_numpy(np.array(sel_truth)) if self.gpu: sel_truth_var = Variable(data).cuda() @@ -199,9 +199,9 @@ def reinforce_backward(self, score, rewards): def check_acc(self, vis_info, pred_queries, gt_queries, pred_entry): def pretty_print(vis_data): - print 'question:', vis_data[0] - print 'headers: (%s)'%(' || '.join(vis_data[1])) - print 'query:', vis_data[2] + print ('question:', vis_data[0]) + print ('headers: (%s)'%(' || '.join(vis_data[1]))) + print ('query:', vis_data[2]) def gen_cond_str(conds, header): if len(conds) == 0: @@ -210,7 +210,7 @@ def gen_cond_str(conds, header): for cond in conds: cond_str.append( header[cond[0]] + ' ' + self.COND_OPS[cond[1]] + \ - ' ' + unicode(cond[2]).lower()) + ' ' + str(cond[2]).lower()) return 'WHERE ' + ' AND '.join(cond_str) pred_agg, pred_sel, pred_cond = pred_entry @@ -261,8 +261,8 @@ def gen_cond_str(conds, header): if not flag: break gt_idx = tuple(x[0] for x in cond_gt).index(cond_pred[idx][0]) - if flag and unicode(cond_gt[gt_idx][2]).lower() != \ - unicode(cond_pred[idx][2]).lower(): + if flag and str(cond_gt[gt_idx][2]).lower() != \ + str(cond_pred[idx][2]).lower(): flag = False cond_val_err += 1 @@ -349,7 +349,7 @@ def merge_tokens(tok_list, raw_tok_str): cond_toks.append(cond_val) if verbose: - print cond_toks + print (cond_toks) if len(cond_toks) > 0: cond_toks = cond_toks[1:] st = 0 diff --git a/sqlnet/model/sqlnet.py b/sqlnet/model/sqlnet.py index 7c3ebb4..1095cc1 100644 --- a/sqlnet/model/sqlnet.py +++ b/sqlnet/model/sqlnet.py @@ -4,10 +4,10 @@ import torch.nn.functional as F from torch.autograd import Variable import numpy as np -from modules.word_embedding import WordEmbedding -from modules.aggregator_predict import AggPredictor -from modules.selection_predict import SelPredictor -from modules.sqlnet_condition_predict import SQLNetCondPredictor +from .modules.word_embedding import WordEmbedding +from .modules.aggregator_predict import AggPredictor +from .modules.selection_predict import SelPredictor +from .modules.sqlnet_condition_predict import SQLNetCondPredictor class SQLNet(nn.Module): @@ -147,7 +147,7 @@ def loss(self, score, truth_num, pred_entry, gt_where): loss = 0 if pred_agg: - agg_truth = map(lambda x:x[0], truth_num) + agg_truth = list(map(lambda x:x[0], truth_num)) data = torch.from_numpy(np.array(agg_truth)) if self.gpu: agg_truth_var = Variable(data.cuda()) @@ -157,7 +157,7 @@ def loss(self, score, truth_num, pred_entry, gt_where): loss += self.CE(agg_score, agg_truth_var) if pred_sel: - sel_truth = map(lambda x:x[1], truth_num) + sel_truth = list(map(lambda x:x[1], truth_num)) data = torch.from_numpy(np.array(sel_truth)) if self.gpu: sel_truth_var = Variable(data.cuda()) @@ -171,7 +171,7 @@ def loss(self, score, truth_num, pred_entry, gt_where): cond_num_score, cond_col_score,\ cond_op_score, cond_str_score = cond_score #Evaluate the number of conditions - cond_num_truth = map(lambda x:x[2], truth_num) + cond_num_truth = list(map(lambda x:x[2], truth_num)) data = torch.from_numpy(np.array(cond_num_truth)) if self.gpu: cond_num_truth_var = Variable(data.cuda()) @@ -231,9 +231,9 @@ def loss(self, score, truth_num, pred_entry, gt_where): def check_acc(self, vis_info, pred_queries, gt_queries, pred_entry): def pretty_print(vis_data): - print 'question:', vis_data[0] - print 'headers: (%s)'%(' || '.join(vis_data[1])) - print 'query:', vis_data[2] + print ('question:', vis_data[0]) + print ('headers: (%s)'%(' || '.join(vis_data[1]))) + print ('query:', vis_data[2]) def gen_cond_str(conds, header): if len(conds) == 0: @@ -241,7 +241,7 @@ def gen_cond_str(conds, header): cond_str = [] for cond in conds: cond_str.append(header[cond[0]] + ' ' + - self.COND_OPS[cond[1]] + ' ' + unicode(cond[2]).lower()) + self.COND_OPS[cond[1]] + ' ' + str(cond[2]).lower()) return 'WHERE ' + ' AND '.join(cond_str) pred_agg, pred_sel, pred_cond = pred_entry @@ -294,8 +294,8 @@ def gen_cond_str(conds, header): break gt_idx = tuple( x[0] for x in cond_gt).index(cond_pred[idx][0]) - if flag and unicode(cond_gt[gt_idx][2]).lower() != \ - unicode(cond_pred[idx][2]).lower(): + if flag and str(cond_gt[gt_idx][2]).lower() != \ + str(cond_pred[idx][2]).lower(): flag = False cond_val_err += 1 diff --git a/sqlnet/utils.py b/sqlnet/utils.py index 2311671..a3836ca 100644 --- a/sqlnet/utils.py +++ b/sqlnet/utils.py @@ -1,5 +1,5 @@ import json -from lib.dbengine import DBEngine +from .lib.dbengine import DBEngine import re import numpy as np #from nltk.tokenize import StanfordTokenizer @@ -14,7 +14,7 @@ def load_data(sql_paths, table_paths, use_small=False): max_col_num = 0 for SQL_PATH in sql_paths: - print "Loading data from %s"%SQL_PATH + print ("Loading data from %s"%SQL_PATH) with open(SQL_PATH) as inf: for idx, line in enumerate(inf): if use_small and idx >= 1000: @@ -23,7 +23,7 @@ def load_data(sql_paths, table_paths, use_small=False): sql_data.append(sql) for TABLE_PATH in table_paths: - print "Loading data from %s"%TABLE_PATH + print ("Loading data from %s"%TABLE_PATH) with open(TABLE_PATH) as inf: for line in inf: tab = json.loads(line.strip()) @@ -36,7 +36,7 @@ def load_data(sql_paths, table_paths, use_small=False): def load_dataset(dataset_id, use_small=False): if dataset_id == 0: - print "Loading from original dataset" + print ("Loading from original dataset") sql_data, table_data = load_data('data/train_tok.jsonl', 'data/train_tok.tables.jsonl', use_small=use_small) val_sql_data, val_table_data = load_data('data/dev_tok.jsonl', @@ -47,7 +47,7 @@ def load_dataset(dataset_id, use_small=False): DEV_DB = 'data/dev.db' TEST_DB = 'data/test.db' else: - print "Loading from re-split dataset" + print ("Loading from re-split dataset") sql_data, table_data = load_data('data_resplit/train.jsonl', 'data_resplit/tables.jsonl', use_small=use_small) val_sql_data, val_table_data = load_data('data_resplit/dev.jsonl', @@ -142,7 +142,7 @@ def epoch_train(model, optimizer, batch_size, sql_data, table_data, pred_entry): score = model.forward(q_seq, col_seq, col_num, pred_entry, gt_where=gt_where_seq, gt_cond=gt_cond_seq, gt_sel=gt_sel_seq) loss = model.loss(score, ans_seq, pred_entry, gt_where_seq) - cum_loss += loss.data.cpu().numpy()[0]*(ed - st) + cum_loss += loss.data.cpu().numpy().flatten()[0]*(ed - st) optimizer.zero_grad() loss.backward() optimizer.step() @@ -276,7 +276,7 @@ def load_word_emb(file_name, load_used=False, use_small=False): break info = line.strip().split(' ') if info[0].lower() not in ret: - ret[info[0]] = np.array(map(lambda x:float(x), info[1:])) + ret[info[0]] = np.array(list(map(lambda x:float(x), info[1:]))) return ret else: print ('Load used word embedding') diff --git a/test.py b/test.py index 52dbff3..bb541fe 100644 --- a/test.py +++ b/test.py @@ -52,32 +52,32 @@ if args.train_emb: agg_m, sel_m, cond_m, agg_e, sel_e, cond_e = best_model_name(args) - print "Loading from %s"%agg_m + print ("Loading from %s"%agg_m) model.agg_pred.load_state_dict(torch.load(agg_m)) - print "Loading from %s"%sel_m + print ("Loading from %s"%sel_m) model.sel_pred.load_state_dict(torch.load(sel_m)) - print "Loading from %s"%cond_m + print ("Loading from %s"%cond_m) model.cond_pred.load_state_dict(torch.load(cond_m)) - print "Loading from %s"%agg_e + print ("Loading from %s"%agg_e) model.agg_embed_layer.load_state_dict(torch.load(agg_e)) - print "Loading from %s"%sel_e + print ("Loading from %s"%sel_e) model.sel_embed_layer.load_state_dict(torch.load(sel_e)) - print "Loading from %s"%cond_e + print ("Loading from %s"%cond_e) model.cond_embed_layer.load_state_dict(torch.load(cond_e)) else: agg_m, sel_m, cond_m = best_model_name(args) - print "Loading from %s"%agg_m + print ("Loading from %s"%agg_m) model.agg_pred.load_state_dict(torch.load(agg_m)) - print "Loading from %s"%sel_m + print ("Loading from %s"%sel_m) model.sel_pred.load_state_dict(torch.load(sel_m)) - print "Loading from %s"%cond_m + print ("Loading from %s"%cond_m) model.cond_pred.load_state_dict(torch.load(cond_m)) - print "Dev acc_qm: %s;\n breakdown on (agg, sel, where): %s"%epoch_acc( - model, BATCH_SIZE, val_sql_data, val_table_data, TEST_ENTRY) - print "Dev execution acc: %s"%epoch_exec_acc( - model, BATCH_SIZE, val_sql_data, val_table_data, DEV_DB) - print "Test acc_qm: %s;\n breakdown on (agg, sel, where): %s"%epoch_acc( - model, BATCH_SIZE, test_sql_data, test_table_data, TEST_ENTRY) - print "Test execution acc: %s"%epoch_exec_acc( - model, BATCH_SIZE, test_sql_data, test_table_data, TEST_DB) + print ("Dev acc_qm: %s;\n breakdown on (agg, sel, where): %s"%epoch_acc( + model, BATCH_SIZE, val_sql_data, val_table_data, TEST_ENTRY)) + print ("Dev execution acc: %s"%epoch_exec_acc( + model, BATCH_SIZE, val_sql_data, val_table_data, DEV_DB)) + print ("Test acc_qm: %s;\n breakdown on (agg, sel, where): %s"%epoch_acc( + model, BATCH_SIZE, test_sql_data, test_table_data, TEST_ENTRY)) + print ("Test execution acc: %s"%epoch_exec_acc( + model, BATCH_SIZE, test_sql_data, test_table_data, TEST_DB)) diff --git a/train.py b/train.py index ed0cab5..ce94711 100644 --- a/train.py +++ b/train.py @@ -66,38 +66,38 @@ if args.rl or args.train_emb: # Load pretrained model. agg_lm, sel_lm, cond_lm = best_model_name(args, for_load=True) - print "Loading from %s"%agg_lm + print ("Loading from %s"%agg_lm) model.agg_pred.load_state_dict(torch.load(agg_lm)) - print "Loading from %s"%sel_lm + print ("Loading from %s"%sel_lm) model.sel_pred.load_state_dict(torch.load(sel_lm)) - print "Loading from %s"%cond_lm + print ("Loading from %s"%cond_lm) model.cond_pred.load_state_dict(torch.load(cond_lm)) if args.rl: best_acc = 0.0 best_idx = -1 - print "Init dev acc_qm: %s\n breakdown on (agg, sel, where): %s"% \ + print ("Init dev acc_qm: %s\n breakdown on (agg, sel, where): %s"% \ epoch_acc(model, BATCH_SIZE, val_sql_data,\ - val_table_data, TRAIN_ENTRY) - print "Init dev acc_ex: %s"%epoch_exec_acc( - model, BATCH_SIZE, val_sql_data, val_table_data, DEV_DB) + val_table_data, TRAIN_ENTRY)) + print ("Init dev acc_ex: %s"%epoch_exec_acc( + model, BATCH_SIZE, val_sql_data, val_table_data, DEV_DB)) torch.save(model.cond_pred.state_dict(), cond_m) for i in range(100): - print 'Epoch %d @ %s'%(i+1, datetime.datetime.now()) - print ' Avg reward = %s'%epoch_reinforce_train( - model, optimizer, BATCH_SIZE, sql_data, table_data, TRAIN_DB) - print ' dev acc_qm: %s\n breakdown result: %s'% epoch_acc( - model, BATCH_SIZE, val_sql_data, val_table_data, TRAIN_ENTRY) + print ('Epoch %d @ %s'%(i+1, datetime.datetime.now())) + print (' Avg reward = %s'%epoch_reinforce_train( + model, optimizer, BATCH_SIZE, sql_data, table_data, TRAIN_DB)) + print (' dev acc_qm: %s\n breakdown result: %s'% epoch_acc( + model, BATCH_SIZE, val_sql_data, val_table_data, TRAIN_ENTRY)) exec_acc = epoch_exec_acc( model, BATCH_SIZE, val_sql_data, val_table_data, DEV_DB) - print ' dev acc_ex: %s', exec_acc + print (' dev acc_ex: %s', exec_acc) if exec_acc[0] > best_acc: best_acc = exec_acc[0] best_idx = i+1 torch.save(model.cond_pred.state_dict(), 'saved_model/epoch%d.cond_model%s'%(i+1, args.suffix)) torch.save(model.cond_pred.state_dict(), cond_m) - print ' Best exec acc = %s, on epoch %s'%(best_acc, best_idx) + print (' Best exec acc = %s, on epoch %s'%(best_acc, best_idx)) else: init_acc = epoch_acc(model, BATCH_SIZE, val_sql_data, val_table_data, TRAIN_ENTRY) @@ -107,8 +107,8 @@ best_sel_idx = 0 best_cond_acc = init_acc[1][2] best_cond_idx = 0 - print 'Init dev acc_qm: %s\n breakdown on (agg, sel, where): %s'%\ - init_acc + print ('Init dev acc_qm: %s\n breakdown on (agg, sel, where): %s'%\ + init_acc) if TRAIN_AGG: torch.save(model.agg_pred.state_dict(), agg_m) if args.train_emb: @@ -122,16 +122,16 @@ if args.train_emb: torch.save(model.cond_embed_layer.state_dict(), cond_e) for i in range(100): - print 'Epoch %d @ %s'%(i+1, datetime.datetime.now()) - print ' Loss = %s'%epoch_train( + print ('Epoch %d @ %s'%(i+1, datetime.datetime.now())) + print (' Loss = %s'%epoch_train( model, optimizer, BATCH_SIZE, - sql_data, table_data, TRAIN_ENTRY) - print ' Train acc_qm: %s\n breakdown result: %s'%epoch_acc( - model, BATCH_SIZE, sql_data, table_data, TRAIN_ENTRY) + sql_data, table_data, TRAIN_ENTRY)) + print (' Train acc_qm: %s\n breakdown result: %s'%epoch_acc( + model, BATCH_SIZE, sql_data, table_data, TRAIN_ENTRY)) #val_acc = epoch_token_acc(model, BATCH_SIZE, val_sql_data, val_table_data, TRAIN_ENTRY) val_acc = epoch_acc(model, BATCH_SIZE, val_sql_data, val_table_data, TRAIN_ENTRY) - print ' Dev acc_qm: %s\n breakdown result: %s'%val_acc + print (' Dev acc_qm: %s\n breakdown result: %s'%val_acc) if TRAIN_AGG: if val_acc[1][0] > best_agg_acc: best_agg_acc = val_acc[1][0] @@ -165,6 +165,6 @@ torch.save(model.cond_embed_layer.state_dict(), 'saved_model/epoch%d.cond_embed%s'%(i+1, args.suffix)) torch.save(model.cond_embed_layer.state_dict(), cond_e) - print ' Best val acc = %s, on epoch %s individually'%( + print (' Best val acc = %s, on epoch %s individually'%( (best_agg_acc, best_sel_acc, best_cond_acc), - (best_agg_idx, best_sel_idx, best_cond_idx)) + (best_agg_idx, best_sel_idx, best_cond_idx)))