def test_ranking(model, test_batches): num_batches = len(test_batches) map, mrr, ndcg_1, ndcg_3, ndcg_5, ndcg_10 = 0, 0, 0, 0, 0, 0 for batch_no in range(1, num_batches + 1): test_queries, test_docs, test_labels = helper.batch_to_tensor( test_batches[batch_no - 1], model.dictionary, model.config.max_query_length, model.config.max_doc_length) if model.config.cuda: test_queries = test_queries.cuda() test_docs = test_docs.cuda() test_labels = test_labels.cuda() softmax_prob = model(test_queries, test_docs) map += mean_average_precision(softmax_prob, test_labels) mrr += MRR(softmax_prob, test_labels) ndcg_1 += NDCG(softmax_prob, test_labels, 1) ndcg_3 += NDCG(softmax_prob, test_labels, 3) ndcg_5 += NDCG(softmax_prob, test_labels, 5) ndcg_10 += NDCG(softmax_prob, test_labels, 10) map = map / num_batches mrr = mrr / num_batches ndcg_1 = ndcg_1 / num_batches ndcg_3 = ndcg_3 / num_batches ndcg_5 = ndcg_5 / num_batches ndcg_10 = ndcg_10 / num_batches print('MAP - ', map) print('MRR - ', mrr) print('NDCG@1 - ', ndcg_1) print('NDCG@3 - ', ndcg_3) print('NDCG@5 - ', ndcg_5) print('NDCG@10 - ', ndcg_10)
def test_ranking(model, test_batches): num_batches = len(test_batches) map, ndcg_1, ndcg_3, ndcg_10 = 0, 0, 0, 0 for batch_no in range(1, num_batches + 1): test_queries, test_clicks, click_labels = helper.batch_to_tensor( test_batches[batch_no - 1], model.dictionary, model.config) if model.config.cuda: test_queries = test_queries.cuda() test_clicks = test_clicks.cuda() click_labels = click_labels.cuda() score = model(test_queries, test_clicks) map += mean_average_precision(score, click_labels) ndcg_1 += NDCG(score, click_labels, 1) ndcg_3 += NDCG(score, click_labels, 3) ndcg_10 += NDCG(score, click_labels, 5) map = map / num_batches ndcg_1 = ndcg_1 / num_batches ndcg_3 = ndcg_3 / num_batches ndcg_10 = ndcg_10 / num_batches print('MAP - ', map) print('NDCG@1 - ', ndcg_1) print('NDCG@3 - ', ndcg_3) print('NDCG@10 - ', ndcg_10)
def validate(self, dev_corpus): # Turn on evaluation mode which disables dropout. self.model.eval() dev_batches = helper.batchify(dev_corpus.data, self.config.batch_size) print('number of dev batches = ', len(dev_batches)) dev_loss = 0 num_batches = len(dev_batches) for batch_no in range(1, num_batches + 1): queries, docs, click_labels = helper.batch_to_tensor(dev_batches[batch_no - 1], self.dictionary, self.config) if self.config.cuda: # batch_size x max_query_length queries = queries.cuda() # batch_size x num_clicks_per_query x max_document_length docs = docs.cuda() # batch_size x num_clicks_per_query click_labels = click_labels.cuda() score = self.model(queries, docs) loss = self.compute_loss(score, click_labels) dev_loss += loss.data[0] return dev_loss / num_batches
def test_ranking(model, test_batches): num_batches = len(test_batches) map, ndcg_1, ndcg_3, ndcg_10 = 0, 0, 0, 0 for batch_no in range(1, num_batches + 1): test_queries, test_docs, test_labels = helper.batch_to_tensor(test_batches[batch_no - 1], model.dictionary, model.config.max_query_length, model.config.max_doc_length) if model.config.cuda: test_queries = test_queries.cuda() test_docs = test_docs.cuda() test_labels = test_labels.cuda() ret_val = compute_ranking_performance(model, test_queries, test_docs, test_labels) map += ret_val[0] ndcg_1 += ret_val[1] ndcg_3 += ret_val[2] ndcg_10 += ret_val[3] map = map / num_batches ndcg_1 = ndcg_1 / num_batches ndcg_3 = ndcg_3 / num_batches ndcg_10 = ndcg_10 / num_batches print('MAP - ', map) print('NDCG@1 - ', ndcg_1) print('NDCG@3 - ', ndcg_3) print('NDCG@10 - ', ndcg_10)
def test_ranking(model, test_batches): num_batches = len(test_batches) _map, mrr, ndcg_1, ndcg_3, ndcg_5, ndcg_10 = 0, 0, 0, 0, 0, 0 for batch_no in range(1, num_batches + 1): queries, docs, click_labels = helper.batch_to_tensor(test_batches[batch_no - 1], model.dictionary) if model.config.cuda: queries = queries.cuda() docs = docs.cuda() click_labels = click_labels.cuda() score = model(queries, docs) _map += mean_average_precision(score, click_labels) mrr += MRR(score, click_labels) ndcg_1 += NDCG(score, click_labels, 1) ndcg_3 += NDCG(score, click_labels, 3) ndcg_5 += NDCG(score, click_labels, 5) ndcg_10 += NDCG(score, click_labels, 10) _map = _map / num_batches mrr = mrr / num_batches ndcg_1 = ndcg_1 / num_batches ndcg_3 = ndcg_3 / num_batches ndcg_5 = ndcg_5 / num_batches ndcg_10 = ndcg_10 / num_batches print('MAP - ', _map) print('MRR - ', mrr) print('NDCG@1 - ', ndcg_1) print('NDCG@3 - ', ndcg_3) print('NDCG@5 - ', ndcg_5) print('NDCG@10 - ', ndcg_10)
def test_mrr(model, filter_test_dataset, anchor_candidates, sess): batches_idx = helper.get_batches_idx(len(filter_test_dataset), args.batch_size) print('number of mrr test batches = ', len(batches_idx)) num_batches = len(batches_idx) mrr = 0 for batch_no in range(1, num_batches + 1): #1,...,num_batches batch_idx = batches_idx[batch_no - 1] batch_data = [filter_test_dataset.dataset[i] for i in batch_idx] #将一批数据转换为模型输入的格式 (hist_query_input, hist_doc_input, session_num, hist_query_num, hist_query_len, hist_click_num, hist_doc_len, cur_query_input, cur_doc_input, cur_query_num, cur_query_len, cur_click_num, cur_doc_len, query, q_len, doc, d_len, y, next_q, next_q_len, maximum_iterations) = helper.batch_to_tensor(batch_data, args.max_query_len, args.max_doc_len) indices, slots_num = model.get_memory_input(session_num) candid_next_q, candid_next_q_len, label = helper.get_candid_tensor( batch_data, anchor_candidates, args.candid_query_num) idx = model.get_test_input(candid_next_q) feed_dict = { model.hist_query_input: hist_query_input, model.hist_doc_input: hist_doc_input, model.session_num: session_num, model.hist_query_num: hist_query_num, model.hist_query_len: hist_query_len, model.hist_click_num: hist_click_num, model.hist_doc_len: hist_doc_len, model.cur_query_input: cur_query_input, model.cur_doc_input: cur_doc_input, model.cur_query_num: cur_query_num, model.cur_query_len: cur_query_len, model.cur_click_num: cur_click_num, model.cur_doc_len: cur_doc_len, model.q: query, model.q_len: q_len, model.d: doc, model.d_len: d_len, model.indices: indices, model.slots_num: slots_num, model.candid_next_q: candid_next_q, model.candid_next_q_len: candid_next_q_len, model.idx: idx } candid_query_score_ = sess.run(model.candid_query_score, feed_dict=feed_dict) mrr += MRR(candid_query_score_, label) mrr = mrr / num_batches print('Query Suggestion MRR - ', mrr)
def train(self, train_corpus): # Turn on training mode which enables dropout. self.model.train() # splitting the data in batches train_batches = helper.batchify(train_corpus.data, self.config.batch_size) print('number of train batches = ', len(train_batches)) start = time.time() print_loss_total = 0 plot_loss_total = 0 num_batches = len(train_batches) for batch_no in range(1, num_batches + 1): # Clearing out all previous gradient computations. self.optimizer.zero_grad() train_queries, query_len, train_clicks, doc_len, click_labels = helper.batch_to_tensor( train_batches[batch_no - 1], self.dictionary) if self.config.cuda: # batch_size x max_query_length train_queries = train_queries.cuda() # batch_size x num_clicks_per_query x max_document_length train_clicks = train_clicks.cuda() # batch_size x num_clicks_per_query click_labels = click_labels.cuda() score = self.model(train_queries, query_len, train_clicks, doc_len) # loss = self.compute_loss(score, click_labels) loss = f.binary_cross_entropy_with_logits(score, click_labels) # Important if we are using nn.DataParallel() if loss.size(0) > 1: loss = loss.mean() loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs. clip_grad_norm(filter(lambda p: p.requires_grad, self.model.parameters()), self.config.max_norm) self.optimizer.step() print_loss_total += loss.data[0] plot_loss_total += loss.data[0] if batch_no % self.config.print_every == 0: print_loss_avg = print_loss_total / self.config.print_every print_loss_total = 0 print('%s (%d %d%%) %.4f' % ( helper.show_progress(start, batch_no / num_batches), batch_no, batch_no / num_batches * 100, print_loss_avg)) if batch_no % self.config.plot_every == 0: plot_loss_avg = plot_loss_total / self.config.plot_every self.train_losses.append(plot_loss_avg) plot_loss_total = 0
def train(self, train_corpus): # Turn on training mode which enables dropout. self.model.train() # splitting the data in batches train_batches = helper.batchify(train_corpus.data, self.config.batch_size) print('number of train batches = ', len(train_batches)) start = time.time() print_loss_total = 0 plot_loss_total = 0 num_batches = len(train_batches) for batch_no in range(1, num_batches + 1): # Clearing out all previous gradient computations. self.optimizer.zero_grad() q1_var, q1_len, q2_var, q2_len = helper.batch_to_tensor( train_batches[batch_no - 1], self.dictionary, reverse=self.config.reverse) if self.config.cuda: q1_var = q1_var.cuda() # batch_size x max_len q2_var = q2_var.cuda() # batch_size x max_len q2_len = q2_len.cuda() # batch_size loss = self.model(q1_var, q1_len, q2_var, q2_len) loss.backward() # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs. clip_grad_norm( filter(lambda p: p.requires_grad, self.model.parameters()), self.config.max_norm) self.optimizer.step() print_loss_total += loss.data[0] plot_loss_total += loss.data[0] if batch_no % self.config.print_every == 0: print_loss_avg = print_loss_total / self.config.print_every print_loss_total = 0 print('%s (%d %d%%) %.4f' % (helper.show_progress(start, batch_no / num_batches), batch_no, batch_no / num_batches * 100, print_loss_avg)) if batch_no % self.config.plot_every == 0: plot_loss_avg = plot_loss_total / self.config.plot_every self.train_losses.append(plot_loss_avg) plot_loss_total = 0
def train(self, train_corpus): # Turn on training mode which enables dropout. self.model.train() # splitting the data in batches train_batches = helper.batchify(train_corpus.data, self.config.batch_size) print('Number of train batches = ', len(train_batches)) start = time.time() print_loss_total = 0 plot_loss_total = 0 num_batches = len(train_batches) for batch_no in range(1, num_batches + 1): # Clearing out all previous gradient computations. self.optimizer.zero_grad() train_queries, train_docs, click_labels = helper.batch_to_tensor( train_batches[batch_no - 1], self.dictionary) if self.config.cuda: # batch_size x vocab_size train_queries = train_queries.cuda() # batch_size x x num_rel_docs_per_query x vocab_size train_docs = train_docs.cuda() # batch_size x num_rel_docs_per_query click_labels = click_labels.cuda() softmax_prob = self.model(train_queries, train_docs) loss = self.compute_loss(softmax_prob, click_labels) # Important if we are using nn.DataParallel() if loss.size(0) > 1: loss = loss.mean() loss.backward() self.optimizer.step() print_loss_total += loss.data[0] plot_loss_total += loss.data[0] if batch_no % self.config.print_every == 0: print_loss_avg = print_loss_total / self.config.print_every print_loss_total = 0 print('%s (%d %d%%) %.4f' % (helper.show_progress(start, batch_no / num_batches), batch_no, batch_no / num_batches * 100, print_loss_avg)) if batch_no % self.config.plot_every == 0: plot_loss_avg = plot_loss_total / self.config.plot_every self.train_losses.append(plot_loss_avg) plot_loss_total = 0
def validate(self, dev_corpus): # Turn on evaluation mode which disables dropout. self.model.eval() dev_batches = helper.batchify(dev_corpus.data, self.config.batch_size) print('number of dev batches = ', len(dev_batches)) dev_loss = 0 num_batches = len(dev_batches) for batch_no in range(1, num_batches + 1): dev_queries, dev_clicks, click_labels = helper.batch_to_tensor(dev_batches[batch_no - 1], self.dictionary) if self.config.cuda: dev_queries = dev_queries.cuda() dev_clicks = dev_clicks.cuda() click_labels = click_labels.cuda() score = self.model(dev_queries, dev_clicks) loss = self.compute_loss(score, click_labels) dev_loss += loss.data[0] return dev_loss / num_batches
def validate(self, dev_corpus): # Turn on evaluation mode which disables dropout. self.model.eval() dev_batches = helper.batchify(dev_corpus.data, self.config.batch_size) print('Number of dev batches = ', len(dev_batches)) dev_loss = 0 num_batches = len(dev_batches) for batch_no in range(1, num_batches + 1): dev_queries, dev_docs, click_labels = helper.batch_to_tensor(dev_batches[batch_no - 1], self.dictionary, self.config.max_query_length, self.config.max_doc_length) if self.config.cuda: dev_queries = dev_queries.cuda() dev_docs = dev_docs.cuda() click_labels = click_labels.cuda() loss = self.model(dev_queries, dev_docs, click_labels) if loss.size(0) > 1: loss = loss.mean() dev_loss += loss.data[0] return dev_loss / num_batches
def validate(self, dev_corpus): # Turn on evaluation mode which disables dropout. self.model.eval() dev_batches = helper.batchify(dev_corpus.data, self.config.batch_size) print('number of dev batches = ', len(dev_batches)) dev_loss = 0 num_batches = len(dev_batches) for batch_no in range(1, num_batches + 1): dev_queries, q_len, dev_clicks, d_len, click_labels = helper.batch_to_tensor(dev_batches[batch_no - 1], self.dictionary, True) if self.config.cuda: dev_queries = dev_queries.cuda() dev_clicks = dev_clicks.cuda() click_labels = click_labels.cuda() score = self.model(dev_queries, q_len, dev_clicks, d_len) loss = f.binary_cross_entropy_with_logits(score, click_labels) if loss.size(0) > 1: loss = loss.mean() dev_loss += loss.data[0] return dev_loss / num_batches
def validate(self, dev_corpus): # Turn on evaluation mode which disables dropout. self.model.eval() dev_batches = helper.batchify(dev_corpus.data, self.config.batch_size) print('number of dev batches = ', len(dev_batches)) dev_loss = 0 num_batches = len(dev_batches) for batch_no in range(1, num_batches + 1): q1_var, q1_len, q2_var, q2_len = helper.batch_to_tensor( dev_batches[batch_no - 1], self.dictionary, reverse=self.config.reverse, iseval=True) if self.config.cuda: q1_var = q1_var.cuda() # batch_size x max_len q2_var = q2_var.cuda() # batch_size x max_len q2_len = q2_len.cuda() # batch_size loss = self.model(q1_var, q1_len, q2_var, q2_len) dev_loss += loss.data[0] return dev_loss / num_batches
def validate(self, dev_dataset): batches_idx = helper.get_batches_idx(len(dev_dataset), self.args.batch_size) print('number of dev batches = ', len(batches_idx)) num_batches = len(batches_idx) predicts, targets = [], [] map, mrr, ndcg_1, ndcg_3, ndcg_5, ndcg_10 = 0, 0, 0, 0, 0, 0 for batch_no in range(1, num_batches + 1): #1,...,num_batches batch_idx = batches_idx[batch_no - 1] batch_data = [dev_dataset.dataset[i] for i in batch_idx] #将一批数据转换为模型输入的格式 (hist_query_input, hist_doc_input, session_num, hist_query_num, hist_query_len, hist_click_num, hist_doc_len, cur_query_input, cur_doc_input, cur_query_num, cur_query_len, cur_click_num, cur_doc_len, query, q_len, doc, d_len, y, next_q, next_q_len, maximum_iterations) = helper.batch_to_tensor( batch_data, self.args.max_query_len, self.args.max_doc_len) indices, slots_num = self.model.get_memory_input(session_num) feed_dict = { self.model.hist_query_input: hist_query_input, self.model.hist_doc_input: hist_doc_input, self.model.session_num: session_num, self.model.hist_query_num: hist_query_num, self.model.hist_query_len: hist_query_len, self.model.hist_click_num: hist_click_num, self.model.hist_doc_len: hist_doc_len, self.model.cur_query_input: cur_query_input, self.model.cur_doc_input: cur_doc_input, self.model.cur_query_num: cur_query_num, self.model.cur_query_len: cur_query_len, self.model.cur_click_num: cur_click_num, self.model.cur_doc_len: cur_doc_len, self.model.q: query, self.model.q_len: q_len, self.model.d: doc, self.model.d_len: d_len, self.model.indices: indices, self.model.slots_num: slots_num, self.model.maximum_iterations: maximum_iterations } click_prob_, predicting_ids_, predicting_len_ = self.sess.run( [ self.model.click_prob, self.model.predicting_ids, self.model.predicting_len ], feed_dict=feed_dict) map += mean_average_precision(click_prob_, y) mrr += MRR(click_prob_, y) ndcg_1 += NDCG(click_prob_, y, 1) ndcg_3 += NDCG(click_prob_, y, 3) ndcg_5 += NDCG(click_prob_, y, 5) ndcg_10 += NDCG(click_prob_, y, 10) batch_predicting_text = helper.generate_predicting_text( predicting_ids_, predicting_len_, self.dictionary) batch_target_text, batch_query_text = helper.generate_target_text( batch_data, self.dictionary, self.args.max_query_len) predicts += batch_predicting_text targets += batch_target_text map = map / num_batches mrr = mrr / num_batches ndcg_1 = ndcg_1 / num_batches ndcg_3 = ndcg_3 / num_batches ndcg_5 = ndcg_5 / num_batches ndcg_10 = ndcg_10 / num_batches score, precisions, brevity_penalty, cand_tot_length, ref_closest_length = multi_bleu.multi_bleu( predicts, targets) metrics_sum = map + mrr + ndcg_1 + ndcg_3 + ndcg_5 + ndcg_10 + ( precisions[0] + precisions[1] + precisions[2] + precisions[3]) * 0.01 print('validation metrics: ') print('MAP = %.4f' % map) print('MRR = %.4f' % mrr) print("NDCG = {:.4f}/{:.4f}/{:.4f}/{:.4f}".format( ndcg_1, ndcg_3, ndcg_5, ndcg_10)) print("BLEU = {:.1f}/{:.1f}/{:.1f}/{:.1f}".format( precisions[0], precisions[1], precisions[2], precisions[3])) return metrics_sum
mrr, ndcg_1, ndcg_3, ndcg_5, ndcg_10 = 0, 0, 0, 0, 0 total_eval_batch = 0 for current_query, next_query in test_corpus.data: current_query_text = ' '.join(current_query.query_terms[1:-1]) if current_query_text not in candidate_map: continue cands = [] for query_text in candidate_map[current_query_text]: query = data.Query() query.add_text(query_text, args.tokenize, args.max_query_length) cands.append(query) cands.append(next_query) scores = [] for cand in cands: q1_var, q1_len, q2_var, q2_len = helper.batch_to_tensor( [(current_query, cand)], dictionary) if model.config.cuda: q1_var = q1_var.cuda() # batch_size x max_len q2_var = q2_var.cuda() # batch_size x max_len score = suggest_next_query(model, q1_var, q1_len, q2_var) scores.append(score) score_batch.append(scores) target.append([0] * 20 + [1]) if len(score_batch) == 256: batch_scores = Variable( torch.from_numpy(numpy.asarray(score_batch))) batch_target = Variable(torch.from_numpy(numpy.asarray(target))) if model.config.cuda: batch_scores = batch_scores.cuda()
def test(model, test_dataset, dictionary, sess): batches_idx = helper.get_batches_idx(len(test_dataset), args.batch_size) print('number of test batches = ', len(batches_idx)) num_batches = len(batches_idx) predicts, targets = [], [] map, mrr, ndcg_1, ndcg_3, ndcg_5, ndcg_10 = 0, 0, 0, 0, 0, 0 for batch_no in range(1, num_batches + 1): #1,...,num_batches batch_idx = batches_idx[batch_no - 1] batch_data = [test_dataset.dataset[i] for i in batch_idx] #将一批数据转换为模型输入的格式 (hist_query_input, hist_doc_input, session_num, hist_query_num, hist_query_len, hist_click_num, hist_doc_len, cur_query_input, cur_doc_input, cur_query_num, cur_query_len, cur_click_num, cur_doc_len, query, q_len, doc, d_len, y, next_q, next_q_len, maximum_iterations) = helper.batch_to_tensor(batch_data, args.max_query_len, args.max_doc_len) indices, slots_num = model.get_memory_input(session_num) feed_dict = { model.hist_query_input: hist_query_input, model.hist_doc_input: hist_doc_input, model.session_num: session_num, model.hist_query_num: hist_query_num, model.hist_query_len: hist_query_len, model.hist_click_num: hist_click_num, model.hist_doc_len: hist_doc_len, model.cur_query_input: cur_query_input, model.cur_doc_input: cur_doc_input, model.cur_query_num: cur_query_num, model.cur_query_len: cur_query_len, model.cur_click_num: cur_click_num, model.cur_doc_len: cur_doc_len, model.q: query, model.q_len: q_len, model.d: doc, model.d_len: d_len, model.indices: indices, model.slots_num: slots_num, model.maximum_iterations: maximum_iterations } click_prob_, predicting_ids_, predicting_len_ = sess.run( [model.click_prob, model.predicting_ids, model.predicting_len], feed_dict=feed_dict) map += mean_average_precision(click_prob_, y) mrr += MRR(click_prob_, y) ndcg_1 += NDCG(click_prob_, y, 1) ndcg_3 += NDCG(click_prob_, y, 3) ndcg_5 += NDCG(click_prob_, y, 5) ndcg_10 += NDCG(click_prob_, y, 10) batch_predicting_text = helper.generate_predicting_text( predicting_ids_, predicting_len_, dictionary) batch_target_text, batch_query_text = helper.generate_target_text( batch_data, dictionary, args.max_query_len) predicts += batch_predicting_text targets += batch_target_text map = map / num_batches mrr = mrr / num_batches ndcg_1 = ndcg_1 / num_batches ndcg_3 = ndcg_3 / num_batches ndcg_5 = ndcg_5 / num_batches ndcg_10 = ndcg_10 / num_batches print('MAP - ', map) print('MRR - ', mrr) print('NDCG@1 - ', ndcg_1) print('NDCG@3 - ', ndcg_3) print('NDCG@5 - ', ndcg_5) print('NDCG@10 - ', ndcg_10) print("targets size = ", len(targets)) print("predicts size = ", len(predicts)) multi_bleu.print_multi_bleu(predicts, targets)
def train(self, train_dataset): batches_idx = helper.get_batches_idx(len(train_dataset), self.args.batch_size) print('number of train batches = ', len(batches_idx)) start = time.time() print_loss_total = 0 plot_loss_total = 0 num_batches = len(batches_idx) for batch_no in range(1, num_batches + 1): #1,...num_batches batch_idx = batches_idx[batch_no - 1] batch_data = [train_dataset.dataset[i] for i in batch_idx] #将一批数据转换为模型输入的格式 (hist_query_input, hist_doc_input, session_num, hist_query_num, hist_query_len, hist_click_num, hist_doc_len, cur_query_input, cur_doc_input, cur_query_num, cur_query_len, cur_click_num, cur_doc_len, query, q_len, doc, d_len, y, next_q, next_q_len, _) = helper.batch_to_tensor(batch_data, self.args.max_query_len, self.args.max_doc_len) indices, slots_num = self.model.get_memory_input(session_num) feed_dict = { self.model.hist_query_input: hist_query_input, self.model.hist_doc_input: hist_doc_input, self.model.session_num: session_num, self.model.hist_query_num: hist_query_num, self.model.hist_query_len: hist_query_len, self.model.hist_click_num: hist_click_num, self.model.hist_doc_len: hist_doc_len, self.model.cur_query_input: cur_query_input, self.model.cur_doc_input: cur_doc_input, self.model.cur_query_num: cur_query_num, self.model.cur_query_len: cur_query_len, self.model.cur_click_num: cur_click_num, self.model.cur_doc_len: cur_doc_len, self.model.q: query, self.model.q_len: q_len, self.model.d: doc, self.model.d_len: d_len, self.model.y: y, # 0/1 self.model.indices: indices, self.model.slots_num: slots_num, self.model.next_q: next_q, self.model.next_q_len: next_q_len } #计算loss + 优化参数 loss_ = self.sess.run(self.model.loss, feed_dict=feed_dict) train_op_ = self.sess.run(self.model.train_op, feed_dict=feed_dict) print_loss_total += loss_ plot_loss_total += loss_ if batch_no % self.args.print_every == 0: print_loss_avg = print_loss_total / self.args.print_every print_loss_total = 0 print('%s (%d %d%%) %.4f' % (helper.show_progress(start, batch_no / num_batches), batch_no, batch_no / num_batches * 100, print_loss_avg)) if batch_no % self.args.plot_every == 0: plot_loss_avg = plot_loss_total / self.args.plot_every self.train_losses.append(plot_loss_avg) plot_loss_total = 0
# load the test dataset test_corpus = data.Corpus(args.tokenize, args.max_query_length) test_corpus.parse(args.data + 'test.txt', args.max_example, whole_session=True) print('test set size = ', len(test_corpus.data)) targets, candidates = [], [] if args.attn_type: fw = open(args.save_path + 'seq2seq_attn_predictions.txt', 'w') else: fw = open(args.save_path + 'seq2seq_predictions.txt', 'w') for prev_q, current_q in test_corpus.data: q1_var, q1_len, q2_var, q2_len = helper.batch_to_tensor( [(prev_q, current_q)], dictionary, reverse=args.reverse, iseval=True) if args.cuda: q1_var = q1_var.cuda() # batch_size x max_len q2_var = q2_var.cuda() # batch_size x max_len q2_len = q2_len.cuda() # batch_size target = generate_next_query(model, q1_var, q1_len, dictionary) candidate = " ".join(current_q.query_terms[1:-1]) targets.append(target) candidates.append(candidate) fw.write(candidate + '\t' + target + '\n') fw.close() print("target size = ", len(targets))