def get_self_critical_reward(netG, netW, sample_ans_input, ques_hidden, gen_result, ans_input, itows): batch_size = gen_result.size(0) # batch_size = sample_size * seq_per_img #seq_per_img = batch_size // len(data['gts']) # get greedy decoding baseline greedy_res, _ = netG.sample(netW, sample_ans_input, ques_hidden) ans_sample_txt = decode_txt(itows, greedy_res.t()) #print('greedy_ans: %s' % (ans_sample_txt)) res1 = OrderedDict() res2 = OrderedDict() # gen_result = gen_result.cpu().numpy() greedy_res = greedy_res.cpu().numpy() ans_input = ans_input.cpu().numpy() for i in range(batch_size): res1[i] = array_to_str(gen_result[i]) for i in range(batch_size): res2[i] = array_to_str(greedy_res[i]) # gts = OrderedDict() for i in range(len(ans_input)): gts[i] = array_to_str(ans_input[i]) # # # _, scores = Bleu(4).compute_score(gts, res) # # scores = np.array(scores[3]) # res = [{'image_id': i, 'caption': res[i]} for i in range(2 * batch_size)] # gts = {i: gts[i % batch_size] for i in range(2 * batch_size)} from nltk.translate import bleu from nltk.translate.bleu_score import SmoothingFunction smoothie = SmoothingFunction().method4 scores = [] for i in range(len(gen_result)): score = bleu(gts[i], res1[i], weights=(0.5, 0.5)) # if score != 0: # print i , ': ' , score scores.append(score) for i in range(len(greedy_res)): score = bleu(gts[i], res2[i], weights=(0.5, 0.5)) scores.append(score) # scores = bleu(gts, res, smoothing_function=smoothie) # _, scores = CiderD_scorer.compute_score(gts, res) # print('Cider scores:', _) scores = np.array(scores) scores = scores[:batch_size] - scores[batch_size:] rewards = np.repeat(scores[:, np.newaxis], gen_result.shape[1], 1) return rewards
def sent_bleu(ref_list, hyp): from nltk.translate import bleu from nltk.translate.bleu_score import SmoothingFunction smoothie = SmoothingFunction().method4 refs = [ref.split() for ref in ref_list] hyp = hyp.split() return bleu(refs, hyp, smoothing_function=smoothie)
def run_test(self, test_num=100, model_name='logs/model-zh2yue-300'): zh_vocab_in = self.zh_list_in zh_vocab_out = self.zh_list_out encoder_inputs = [[zh_vocab_in[word] for word in line] for line in tqdm(self.zh_list_in)] decoder_inputs = [[zh_vocab_out['<GO>']] + [zh_vocab_out[word] for word in line] for line in tqdm(self.zh_list_out)] decoder_targets = [[zh_vocab_out[word] for word in line] + [zh_vocab_out['<EOS>']] for line in tqdm(self.zh_list_out)] arg = Zh2yue.create_hparams() arg.input_vocab_size = len(zh_vocab_in) arg.label_vocab_size = len(zh_vocab_out) g = Transformer(arg) saver = tf.train.Saver() de_zh_vocab = {v: k for k, v in zh_vocab_out.items()} result = [] with tf.Session() as sess: saver.restore(sess, model_name) for i in range(test_num): line = encoder_inputs[i] x = np.array(line) x = x.reshape(1, -1) de_inp = [[zh_vocab_out['<GO>']]] while True: y = np.array(de_inp) preds = sess.run(g.preds, {g.x: x, g.de_inp: y}) if preds[0][-1] == zh_vocab_out['<EOS>']: break de_inp[0].append(preds[0][-1]) got = ''.join(de_zh_vocab[idx] for idx in de_inp[0][1:]) print('测试语言:', self.zh_list_in[i]) # print('sen2:', zh_list_out[i * 10]) print('生成语言:', got) sen1_lst = [word for word in self.zh_list_in[i]] sen2_lst = [word for word in self.zh_list_out[i]] sen3_lst = [word for word in got] score = bleu([sen1_lst], sen3_lst) print(score) strt = str( self.zh_list_in[i]) + "\t" + str(got) + "\t" + str(score) result.append(strt) fileObject = open('intial_result_zh2yue.txt', 'w') for ip in result: fileObject.write(ip) fileObject.write('\n') fileObject.close()
def evaluate(test_data,encoder,decoder, max_length, tokenizer, image_features_extract_model): images = test_data[0] captions = test_data[1] smoothie = SmoothingFunction().method4 score1 = [] # weights - 0.25 0.25 0.25 0.25 score2 = [] # weights - 0 0.33 0.33 0.33 score3 = [] # weights - 0 0.5 0.5 0 for image, caption in zip(images, captions): real_caption = ' '.join([tokenizer.index_word[i] for i in caption if i not in [0]]) hidden = decoder.reset_state(batch_size=1) temp_input = tf.expand_dims(load_image(image)[0], 0) #image_features_extract_model = img_extract_model() img_tensor_val = image_features_extract_model(temp_input) img_tensor_val = tf.reshape(img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3])) features = encoder(img_tensor_val) dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0) result = [] for i in range(max_length): #predictions, hidden = decoder(dec_input, features, hidden) predictions, hidden, attention_weights = decoder(dec_input, features, hidden) predicted_id = tf.random.categorical(predictions, 1)[0][0].numpy() result.append(tokenizer.index_word[predicted_id]) if tokenizer.index_word[predicted_id] == '<end>': result.insert(0,"<start>") result = " ".join(result) score1.append(bleu([real_caption], result, smoothing_function=smoothie,weights=(0.25, 0.25, 0.25, 0.25))) score2.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.33, 0.33, 0.33))) score3.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.5, 0.5, 0))) print("Score1 \n",bleu([real_caption], result, smoothing_function=smoothie,weights=(0.25, 0.25, 0.25, 0.25))) print("Score2 \n",bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.33, 0.33, 0.33))) print("Score3 \n", bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0., 0.5, 0))) print("Image name",image) print("Real caption",real_caption) print("Result",result) print("Scores are ",score1, score2, score3) break dec_input = tf.expand_dims([predicted_id], 0) score1.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0.25, 0.25, 0.25, 0.25))) score2.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.33, 0.33, 0.33))) score3.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.5, 0.5, 0))) print("Real caption outside", real_caption) print("Result outside", result) print("Score outside", score1, score2, score3) return statistics.mean(score1), statistics.mean(score2), statistics.mean(score3)
def bleu_batch(references_batch, hypothesis_batch): """Bilingual Evaluation Understudy(BLEU) Args: references_batch (list[list[list[str]]]) hypothesis_batch (list[list[str]]) Returns: bleu_score (double) """ count = len(references_batch) assert count == len(hypothesis_batch) bleu_sum = 0 for i in range(count): bleu_sum += bleu(references_batch[i], hypothesis_batch[i]) return bleu_sum / count
def bleu_score(prediction, ground_truth): prediction = prediction.max(2)[1] acc_bleu = 0 for x, y in zip(ground_truth, prediction): x = tokenizer.convert_ids_to_tokens(x.tolist()) y = tokenizer.convert_ids_to_tokens(y.tolist()) idx1 = x.index('[PAD]') if '[PAD]' in x else len(x) idx2 = y.index('[SEP]') if '[SEP]' in y else len(y) acc_bleu += bleu([x[1:idx1 - 1]], y[1:idx2 - 1], smoothing_function=SmoothingFunction().method4) return acc_bleu / prediction.size(0)
def calk_rouge(summary, references, rouge, lemmatizer): summary = "".join(lemmatizer.lemmatize(summary)).strip() references = [ "".join(lemmatizer.lemmatize(ref)).strip() for ref in references ] rouge_n = [] for n in range(1, 4): rouge_n.append( rouge.rouge_n(summary=summary, references=references, n=n)) rouge_l = rouge.rouge_l(summary=summary, references=references) bleu_score_2 = bleu(references, summary, weights=(1. / 2., 1. / 2.)) bleu_score_3 = bleu(references, summary, weights=(1. / 3., 1. / 3., 1. / 3.)) print( "ROUGE-1: {}, ROUGE-2: {}, ROUGE-3: {}, ROUGE-L: {}, BLEU-2: {}, BLEU-3: {}" .format(rouge_n[0], rouge_n[1], rouge_n[2], rouge_l, bleu_score_2, bleu_score_3).replace(", ", "\n")) return rouge_n + [rouge_l, bleu_score_2, bleu_score_3]
def get_bleu_score(review, test_data, verbose=False): try: # Try computing the BLEU scores based on available information candidate = tokenized( review[2]) # Get the drug review text in <review> references = [ tokenized(ref_review[2]) for ref_review in test_data if all(ref_review[[0, 1, 3]] == review[[0, 1, 3]]) ] # Extract all # relevant drug reviews that from <test_data> that have the same attributes as <review> return bleu(references, candidate, smoothing_function=SmoothingFunction().method4 ) # Compute the BLEU score using the NLTK # implementation for computing BLEU scores and return the result except KeyError as e: # If the NLTK implementation for computing BLEU scores could not be run due to no available reference drug reviews if verbose: print( "WARNING: No reference translations were obtainable for the following review:", review) # Print error message return -1 # Return -1 to indicate unsuccessful computation
def run_graph(data_comb, batch_size, num_units, model_cond, epoch, Bidirection, Embd_train, Attention): noisy_Id = data_comb['noisy_Id'] noisy_len = data_comb['noisy_len'] ground_truth = data_comb['ground_truth'] clean_len = data_comb['clean_len'] train_Id=data_comb["train_Id"] vocab = data_comb['vocab'] embd = data_comb['embd'] vocab_size = len(vocab) model_type=model_cond config = tf.ConfigProto() config.gpu_options.allow_growth = True if model_type == 'training': Seq2Seq_model = Bi_Att_Seq2Seq(batch_size, vocab_size, num_units, embd, model_type, Bidirection, Embd_train, Attention) max_batches = len(noisy_Id) / batch_size for epo in range(epoch): print('Epoch {}'.format(epo)) with tf.Session(config=config) as sess: if os.path.isfile("../Seq_ckpt/pretrain-seq2seq_Bi_" + str(Bidirection) + "_Att_" + str( Attention) + "_Emb_" + str(Embd_train)): saver = tf.train.Saver() saver.restore(sess, "../Seq_ckpt/pretrain-seq2seq_Bi_" + str(Bidirection) + "_Att_" + str( Attention) + "_Emb_" + str(Embd_train)) sess.run(tf.global_variables_initializer()) # saver.restore(sess, "../Seq_ckpt/pretrain-model") idx = np.arange(0, len(noisy_Id)) idx = list(np.random.permutation(idx)) for batch in range(max_batches): source_shuffle, source_len, train_shuffle, target_shuffle, target_len = next_batch(noisy_Id, noisy_len, train_Id, clean_len, ground_truth, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences(source_shuffle, maxlen=None, padding='post', value=EOS) train_shuffle = tf.keras.preprocessing.sequence.pad_sequences(train_shuffle, maxlen=None, padding='post', value=EOS) target_shuffle = tf.keras.preprocessing.sequence.pad_sequences(target_shuffle, maxlen=None, padding='post', value=EOS) target_len = np.tile(max(target_len) + 1, batch_size) fd = {Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, Seq2Seq_model.decoder_inputs: train_shuffle, Seq2Seq_model.decoder_length: target_len, Seq2Seq_model.ground_truth: target_shuffle} if model_type == "training": logit_id, l_fun, pro_op = sess.run( [Seq2Seq_model.ids, Seq2Seq_model.loss_seq2seq, Seq2Seq_model.train_op], fd) elif model_type == "validation": logit_id, l_fun = sess.run( [Seq2Seq_model.ids, Seq2Seq_model.loss_seq2seq], fd) # summary_writer.add_summary(summary, batch) if batch == 0 or batch % batches_in_epoch == 0: ##print the training print('batch {}'.format(batch)) print(' minibatch loss: {}'.format(l_fun)) for t in range(5): print 'Question {}'.format(t) print " ".join(map(lambda i: vocab[i], list(target_shuffle[t]))).strip() print " ".join(map(lambda i: vocab[i], list(logit_id[t, :]))).strip() # if batch == max_batches -1: # print("validation dataset\n") # print(' minibatch loss: {}'.format(logit_id, l_fun = sess.run([Seq2Seq_model.ids, Seq2Seq_model.loss_seq2seq], fd))) saver.save(sess, "../Seq_ckpt/pretrain-seq2seq_Bi_"+str(Bidirection) + "_Att_"+ str(Attention) + "_Emb_"+str(Embd_train)) elif model_type == 'testing': print "starting Seq2Seq testing" Seq2Seq_model = Bi_Att_Seq2Seq(batch_size, vocab_size, num_units, embd, model_type, Bidirection, Embd_train, Attention) with tf.Session(config=config) as sess: saver_word_rw = tf.train.Saver() saver_word_rw.restore(sess, "../Seq_ckpt/pretrain-seq2seq_Bi_"+str(Bidirection) + "_Att_"+ str(Attention) + "_Emb_"+str(Embd_train)) max_batches = len(noisy_Id) / batch_size idx = np.arange(0, len(noisy_Id)) idx = list(np.random.permutation(idx)) Bleu_score1 = [] Bleu_score2 = [] ex_match = [] for batch in range(max_batches): source_shuffle, source_len, train_shuffle, target_shuffle, target_len = next_batch(noisy_Id, noisy_len, train_Id, clean_len, ground_truth, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences(source_shuffle, maxlen=None, padding='post', value=EOS) fd = {Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len} ids = sess.run([Seq2Seq_model.ids], fd) for t in range(batch_size): ref = [] hyp = [] for tar_id in target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for pre_id in ids[0][t]: if pre_id != 2 and pre_id != 0: hyp.append(vocab[pre_id]) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) sen_bleu2 = bleu([ref], hyp, weights=(0.5, 0.5, 0, 0)) Bleu_score1.append(sen_bleu1) Bleu_score2.append(sen_bleu2) if ref == hyp: ex_match.append(1) else: ex_match.append(0) print "the current whole bleu_score1 is:", sum(Bleu_score1) / float(len(Bleu_score1)) print "the current whole bleu_score2 is:", sum(Bleu_score2) / float(len(Bleu_score2)) print "the whole accuracy is:", sum(ex_match) / float(len(ex_match))
def RL_tuning_model(data_comb, epoch, batch_size=64, num_units=200, beam_width=5, discount_factor=0.1, sen_reward_rate=2, Bidirection=False, Embd_train=True, Attention=False): if len(data_comb) > 1: noisy_Id = data_comb[0]['noisy_Id'] noisy_len = data_comb[0]['noisy_len'] ground_truth = data_comb[0]['ground_truth'] clean_len = data_comb[0]['clean_len'] answer_Id = data_comb[0]['answer_Id'] answer_len = data_comb[0]['answer_len'] train_Id = data_comb[0]['train_Id'] vocab = data_comb[0]['vocab'] embd = data_comb[0]['embd'] max_batches = len(noisy_Id) / batch_size val_noisy_Id = data_comb[1]['noisy_Id'] val_noisy_len = data_comb[1]['noisy_len'] val_ground_truth = data_comb[1]['ground_truth'] val_clean_len = data_comb[1]['clean_len'] val_answer_Id = data_comb[1]['answer_Id'] val_answer_len = data_comb[1]['answer_len'] val_train_Id = data_comb[1]['train_Id'] max_val_batches = len(val_noisy_Id) / batch_size elif len(data_comb) == 1: noisy_Id = data_comb['noisy_Id'] noisy_len = data_comb['noisy_len'] ground_truth = data_comb['ground_truth'] clean_len = data_comb['clean_len'] answer_Id = data_comb['answer_Id'] answer_len = data_comb['answer_len'] train_Id = data_comb['train_Id'] vocab = data_comb['vocab'] embd = data_comb['embd'] embd = np.array(embd) max_batches = len(noisy_Id) / batch_size vocab_size = len(vocab) config = tf.ConfigProto() config.gpu_options.allow_growth = True model_name = "BS_" # values = {} # checkpoint_path = "../Seq_ckpt/pretrain-seq2seq_Bi_"+str(Bidirection) + "_Att_"+ str(Attention) + "_Emb_"+str(Embd_train) # reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) # var_to_shape_map = reader.get_variable_to_shape_map() # for key in var_to_shape_map: # if 'loss_fun' not in key: # values[model_name + key + ':0'] = reader.get_tensor(key) model_cond = "training" G_Seq2Seq = tf.Graph() sess_word_rw = tf.Session(config=config, graph=G_Seq2Seq) with G_Seq2Seq.as_default(): Seq2Seq_model = BA_Seq2Seq.Bi_Att_Seq2Seq(batch_size, vocab_size, num_units, embd, model_cond, Bidirection, Embd_train, Attention) saver_word_rw = tf.train.Saver() saver_word_rw.restore( sess_word_rw, "../Seq_ckpt/pretrainALL-seq2seq_Bi_" + str(Bidirection) + "_Att_" + str(Attention) + "_Emb_" + str(Embd_train)) model_type = "testing" G_QA_similiarity = tf.Graph() sess_QA_rw = tf.Session(config=config, graph=G_QA_similiarity) with G_QA_similiarity.as_default(): QA_simi_model = QA_similiarity.QA_similiarity(batch_size, num_units, embd, model_type) saver_sen_rw = tf.train.Saver() saver_sen_rw.restore(sess_QA_rw, "../Seq_ckpt/qa_similiarity") G_BeamSearch = tf.Graph() with G_BeamSearch.as_default(): BeamSearch_seq2seq = BeamSearch_Seq2seq.BeamSearch_Seq2seq( vocab_size=vocab_size, num_units=num_units, beam_width=beam_width, model_name=model_name, embd=embd, Bidirection=Bidirection, Embd_train=Embd_train, Attention=Attention) with tf.Session(config=config, graph=G_BeamSearch) as sess_beamsearch: # if os.path.isfile("../Seq_ckpt/BS_Bi_" + str(Bidirection) + "_Att_" + str( # Attention) + "_Emb_" + str(Embd_train) + "_SenRewRate_" + str(sen_reward_rate)): # saver_word_rw = tf.train.Saver() # saver_word_rw.restore(sess_beamsearch, "../Seq_ckpt/BS_Bi_" + str(Bidirection) + "_Att_" + str( # Attention) + "_Emb_" + str(Embd_train) + "_SenRewRate_" + str(sen_reward_rate)) sess_beamsearch.run(tf.global_variables_initializer()) for RL_loop in range(epoch): Seq2seq_loop = epoch - RL_loop for epo in range(Seq2seq_loop): if epo == 0: print("Staring Seq2Seq:") print('Epoch {}'.format(epo)) # print [v for v in tf.trainable_variables()] idx = np.arange(0, len(noisy_Id)) idx = list(np.random.permutation(idx)) for batch in range(len(noisy_Id) / batch_size): source_shuffle, source_len, train_shuffle, target_shuffle, target_len, answer_shuffle, ans_len = next_batch( noisy_Id, noisy_len, train_Id, ground_truth, clean_len, answer_Id, answer_len, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', value=EOS) target_shuffle = tf.keras.preprocessing.sequence.pad_sequences( target_shuffle, maxlen=None, padding='post', value=EOS) train_shuffle = tf.keras.preprocessing.sequence.pad_sequences( train_shuffle, maxlen=None, padding='post', value=EOS) target_len = np.tile(max(target_len) + 1, batch_size) fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.decoder_length: target_len, BeamSearch_seq2seq.decoder_inputs: train_shuffle, BeamSearch_seq2seq.decoder_targets: target_shuffle } cl_loss, _ = sess_beamsearch.run([ BeamSearch_seq2seq.loss_seq2seq, BeamSearch_seq2seq.train_op ], fd) if batch == 0 or batch % batches_in_epoch == 0: print(' batch {}'.format(batch)) print(' minibatch loss of training: {}'.format( cl_loss)) val_loss = [] for batch_val in range(len(val_noisy_Id) / batch_size): idx_v = np.arange(0, len(val_noisy_Id)) idx_v = list(np.random.permutation(idx_v)) val_source_shuffle, val_source_len, val_train_shuffle, val_target_shuffle, val_target_len, val_answer_shuffle, val_ans_len = next_batch( val_noisy_Id, val_noisy_len, val_train_Id, val_ground_truth, val_clean_len, val_answer_Id, val_answer_len, batch_size, batch_val, idx_v) val_source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( val_source_shuffle, maxlen=None, padding='post', value=EOS) val_target_shuffle = tf.keras.preprocessing.sequence.pad_sequences( val_target_shuffle, maxlen=None, padding='post', value=EOS) val_train_shuffle = tf.keras.preprocessing.sequence.pad_sequences( val_target_shuffle, maxlen=None, padding='post', value=EOS) val_target_len = np.tile( max(val_target_len) + 1, batch_size) fd = { BeamSearch_seq2seq.encoder_inputs: val_source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: val_source_len, BeamSearch_seq2seq.decoder_length: val_target_len, BeamSearch_seq2seq.decoder_inputs: val_train_shuffle, BeamSearch_seq2seq.decoder_targets: val_target_shuffle } val_loss.append( sess_beamsearch.run( BeamSearch_seq2seq.loss_seq2seq, fd)) print( ' minibatch loss of training on validation: {}'. format(sum(val_loss) / float(len(val_loss)))) gc.collect() # saver_word_rw.save(sess_beamsearch, "../Seq_ckpt/BS_Bi_" + str(Bidirection) + "_Att_" + str(Attention) + "_Emb_" + str( # Embd_train)) # # with tf.Session(config=config, graph=G_BeamSearch) as sess_beamsearch: # # saver = tf.train.Saver() # # saver= tf.train.Saver(var_list=['encoder_model/rnn/basic_lstm_cell/bias','encoder_model/rnn/basic_lstm_cell/kernel']) # # saver.restore(sess_beamsearch,"../Seq_ckpt/pretrain-seq2seq") # # if os.path.isfile("../Seq_ckpt/BS_Bi_" + str(Bidirection) + "_Att_" + str( # Attention) + "_Emb_" + str(Embd_train)): # saver = tf.train.Saver() # saver.restore(sess_beamsearch, "../Seq_ckpt/BS_Bi_" + str(Bidirection) + "_Att_" + str( # Attention) + "_Emb_" + str(Embd_train)) # # sess_beamsearch.run(tf.global_variables_initializer()) # print [v for v in tf.trainable_variables()] # # for v in tf.trainable_variables(): # if v.name in values.keys(): # v.load(values[v.name], sess_beamsearch) for epo in range(RL_loop): if epo == 0: print("staring RL tuning:") print('Epoch {}'.format(epo)) idx = np.arange(0, len(noisy_Id)) idx = list(np.random.permutation(idx)) # print [v for v in tf.trainable_variables()] Bleu_score1 = [] for batch in range(len(noisy_Id) / batch_size): beam_QA_similar = [] beam_cnQA_similar = [] source_shuffle, source_len, train_shuffle, target_shuffle, target_len, answer_shuffle, ans_len = next_batch( noisy_Id, noisy_len, train_Id, ground_truth, clean_len, answer_Id, answer_len, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', value=EOS) dis_rewards = np.zeros( (batch_size, max(source_len), beam_width)) fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.dis_rewards: dis_rewards } predicting_scores1, logits, log_pro = sess_beamsearch.run([ BeamSearch_seq2seq.predicting_scores, BeamSearch_seq2seq.predicting_logits, BeamSearch_seq2seq.predicting_scores ], fd) max_target_length = logits.shape[1] # print "the max_target_length is:", max_target_length beam_rewards = np.zeros( (batch_size, max_target_length, beam_width)) for bw in range(beam_width): generated_que = logits[:, :, bw] logits_batch = [] for i in range(generated_que.shape[0]): for j in range(generated_que.shape[1]): if generated_que[i][ j] == 2 or j == generated_que.shape[ 1] - 1: logits_batch.append(j) continue reward = np.zeros((batch_size, max_target_length)) # generated_que = SOS + generated_que - PAD generated_que_input = np.insert(generated_que, 0, SOS, axis=1) target_shuffle = tf.keras.preprocessing.sequence.pad_sequences( target_shuffle, maxlen=None, padding='post', value=EOS) generated_target_len = np.tile( generated_que_input.shape[1], batch_size) fd = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, Seq2Seq_model.decoder_inputs: generated_que_input, Seq2Seq_model.decoder_length: generated_target_len, Seq2Seq_model.ground_truth: target_shuffle } logits_pro = sess_word_rw.run( Seq2Seq_model.softmax_logits, fd) logits_pro = logits_pro[:, :-1, :] # print "the logits_pro is:", logits_pro.shape[1] answer_shuffle = tf.keras.preprocessing.sequence.pad_sequences( answer_shuffle, maxlen=None, padding='post', value=EOS) generated_len = np.tile(generated_que.shape[1], batch_size) fd = { QA_simi_model.answer_inputs: answer_shuffle, QA_simi_model.answer_inputs_length: ans_len, QA_simi_model.question1_inputs: source_shuffle, QA_simi_model.question1_inputs_length: source_len, QA_simi_model.question2_inputs: generated_que, QA_simi_model.question2_inputs_length: generated_len } QA_similiarity_rd = sess_QA_rw.run( [QA_simi_model.two_distance], fd) beam_QA_similar.append(QA_similiarity_rd[0]) # print QA_similiarity_rd for i in range(generated_que.shape[0]): for j in range(generated_que.shape[1]): if j < logits_batch[i]: max_index = generated_que[i][j] reward[i][j] = logits_pro[i, j, max_index] if j == logits_batch[i]: reward[i][ j] = reward[i][j] + sen_reward_rate * ( QA_similiarity_rd[0][i]) continue # fd = {an_Lstm.answer_inputs: answer_shuffle, an_Lstm.answer_inputs_length: ans_len} # reward_similiarity = sess_sen_rw.run([an_Lstm.answer_state], fd) discounted_rewards = discounted_rewards_cal( reward, discount_factor) for i in range(batch_size): for j in range(max_target_length): beam_rewards[i][j][bw] = discounted_rewards[i][ j] # print np.asarray(beam_rewards).shape fd = { QA_simi_model.answer_inputs: answer_shuffle, QA_simi_model.answer_inputs_length: ans_len, QA_simi_model.question1_inputs: source_shuffle, QA_simi_model.question1_inputs_length: source_len, QA_simi_model.question2_inputs: target_shuffle, QA_simi_model.question2_inputs_length: target_len } cnQA_similiarity_rd = sess_QA_rw.run( [QA_simi_model.two_distance], fd) beam_cnQA_similar.append(cnQA_similiarity_rd[0]) for i in range(batch_size): discounted_rewards = beam_rewards[i] discounted_rewards = np.float32(discounted_rewards) discounted_rewards -= np.mean(discounted_rewards, axis=1).reshape( max_target_length, 1) beam_rewards[i] = discounted_rewards # print "the size of beam_rewards:", beam_rewards.shape[1] fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.dis_rewards: beam_rewards } predicting_scores2, _, rl_reward = sess_beamsearch.run([ BeamSearch_seq2seq.predicting_scores, BeamSearch_seq2seq.RL_train_op, BeamSearch_seq2seq.rl_reward ], fd) for t in range(batch_size): ref = [] hyp = [] bleu_s = [] for tar_id in target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for i in range(beam_width): for pre_id in logits[t, :, i]: if pre_id != 2 and pre_id != -1: hyp.append(vocab[pre_id]) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) bleu_s.append(sen_bleu1) Bleu_score1.append(bleu_s) top1_bleu = [] top2_bleu = [] top3_bleu = [] top4_bleu = [] top5_bleu = [] for i in range(len(Bleu_score1)): top1_bleu.append(Bleu_score1[i][0]) top2_bleu.append(Bleu_score1[i][1]) top3_bleu.append(Bleu_score1[i][2]) top4_bleu.append(Bleu_score1[i][3]) top5_bleu.append(Bleu_score1[i][4]) if batch == 0 or batch % 20 == 0: print(' batch {}'.format(batch)) print(' minibatch reward of training: {}'.format( -rl_reward)) for t in range(5): ## five sentences print('Question {}'.format(t)) print("noisy question:") print(" ".join( map(lambda i: vocab[i], list(source_shuffle[t]))).strip()) print("clean question:") print(" ".join( map(lambda i: vocab[i], list(target_shuffle[t]))).strip()) # print " QA similiarity of the noisy and clean question:", beam_cnQA_similar[t] # text_file.write( # " ".join(map(lambda i: vocab[i], list(target_shuffle[t]))).encode('utf-8').strip() + "\n") print("the beam search producted:\n") for i in range(beam_width): ##beam size pre_sen = [] for log_id in logits[t, :, i]: if log_id != -1: pre_sen.append(log_id) print(" ".join(map(lambda i: vocab[i], pre_sen)).strip()) print " QA similiarity:", beam_QA_similar[ t][i] # logP = [] # for j in range(predicting_scores1[t].shape[0]):t # logP.append(predicting_scores1[t][j][i]) # print "log P", logP # disrewards=[] # for j in range(beam_rewards[t].shape[0]): # disrewards.append(beam_rewards[t][j][i]) # print "Discounted Reward", disrewards # print " ".join(map(lambda i: vocab[i], list(logits[t, :, i]))).strip() # text_file.write( # " ".join(map(lambda i: vocab[i], list(logits[t, :, i]))).encode('utf-8').strip() + "\n") print("the current whole bleu_score1 is:", sum(top1_bleu) / float(len(top1_bleu))) print("the current whole bleu_score2 is:", sum(top2_bleu) / float(len(top2_bleu))) print("the current whole bleu_score3 is:", sum(top3_bleu) / float(len(top3_bleu))) print("the current whole bleu_score4 is:", sum(top4_bleu) / float(len(top4_bleu))) print("the current whole bleu_score4 is:", sum(top5_bleu) / float(len(top5_bleu))) gc.collect() saver_word_rw.save( sess_beamsearch, "../Seq_ckpt/BS_Bi_" + str(Bidirection) + "_Att_" + str(Attention) + "_Emb_" + str(Embd_train) + "_SenRewRate_" + str(sen_reward_rate))
def processAlignments(data, folder, inputfile, outputType, num, refs=False): with open(folder + "/" + ntpath.basename(inputfile) + '.ali.js', 'w', encoding='utf-8') as out_a_js: with open(folder + "/" + ntpath.basename(inputfile) + '.src.js', 'w', encoding='utf-8') as out_s_js: with open(folder + "/" + ntpath.basename(inputfile) + '.trg.js', 'w', encoding='utf-8') as out_t_js: with open(folder + "/" + ntpath.basename(inputfile) + '.con.js', 'w', encoding='utf-8') as out_c_js: with open(folder + "/" + ntpath.basename(inputfile) + '.sc.js', 'w', encoding='utf-8') as out_sc_js: out_a_js.write(u'var alignments = [\n') out_s_js.write(u'var sources = [\n') out_t_js.write(u'var targets = [\n') out_c_js.write(u'var confidences = [\n') out_sc_js.write(u'var sentence_confidences = [\n') num = int(num) - 1 if num > -1 and (num < len(data)): data = [data[num]] elif num >= len(data): print( 'The selected sentence number is higher than the sentence count!\n' ) printHelp() sys.exit() for i in range(0, len(data)): (src, tgt, rawAli) = data[i] #In case the source string is empty if rawAli.ndim == 1: rawAli = np.array([rawAli]) ali = [ l[:len(list(filter(None, tgt)))] for l in rawAli[:len(src)] ] srcTotal = [] trgTotal = [] tali = np.array(ali).transpose() for a in range(0, len(ali)): srcTotal.append( str( math.pow( math.e, -0.05 * math.pow( (getCP([ali[a]]) + getEnt([ali[a]]) + getRevEnt([ali[a]])), 2)))) for a in range(0, len(tali)): trgTotal.append( str( math.pow( math.e, -0.05 * math.pow( (getCP([tali[a]]) + getEnt([tali[a]]) + getRevEnt([tali[a]])), 2)))) JoinedSource = " ".join(src) JoinedTarget = " ".join(tgt) StrippedSource = ''.join( c for c in JoinedSource if unicodedata.category(c).startswith( 'L')).replace('EOS', '').replace( 'quot', '').replace('apos', '') StrippedTarget = ''.join( c for c in JoinedTarget if unicodedata.category(c).startswith( 'L')).replace('EOS', '').replace( 'quot', '').replace('apos', '') #Get the confidence metrics CDP = round(getCP(ali), 10) APout = round(getEnt(ali), 10) APin = round(getRevEnt(ali), 10) Total = round(CDP + APout + APin, 10) #Can we calculate BLEU? bleuNumber = -1 if (refs): try: #NLTK requires Python versions 3.5, 3.6, 3.7, or 3.8 version = sys.version_info if version.major == 3 and version.minor > 4: from nltk.translate import bleu from nltk.translate.bleu_score import SmoothingFunction sm = SmoothingFunction() refNumber = i if num < 0 else num deBpeRef = " ".join( refs[refNumber]).replace( '@@ ', '') deBpeHyp = JoinedTarget.replace( '@@ ', '').replace('<EOS>', '').strip() bleuNumber = round( bleu([deBpeRef.split()], deBpeHyp.split(), smoothing_function=sm.method3) * 100, 2) bleuScore = u', ' + repr(bleuNumber) else: refs = False bleuScore = u'' except ImportError: sys.stdout.write( 'NLTK not found! BLEU will not be calculated\n' ) refs = False bleuScore = u'' else: bleuScore = u'' jls = JoinedSource.replace('@@ ', '').replace( '<EOS>', '').replace('"', '"').replace( "'", "'").replace("&", "&").replace("@-@", "-").strip() jlt = JoinedTarget.replace('@@ ', '').replace( '<EOS>', '').replace('"', '"').replace( "'", "'").replace("&", "&").replace("@-@", "-").strip() longest = longestCommonSubstring(jls, jlt).strip() similarity = len(longest) / len(jlt) #Penalize sentences with more than 4 tokens if (len(tgt) > 4) and (similarity > 0.3): #The more similar, the higher penalty #It's worse to have more words with a higher similarity #Let's make it between 0.7 and about 1.5 for veeeery long sentences multiplier = ((0.8 + (len(tgt) * 0.01)) * (3 - ((1 - similarity) * 5)) * (0.7 + similarity) * math.tan(similarity)) Total = round(CDP + APout + APin - multiplier, 10) # e^(-1(x^2)) CDP_pr = round( math.pow(math.e, -1 * math.pow(CDP, 2)) * 100, 2) # e^(-0.05(x^2)) APout_pr = round( math.pow(math.e, -0.05 * math.pow(APout, 2)) * 100, 2) APin_pr = round( math.pow(math.e, -0.05 * math.pow(APin, 2)) * 100, 2) Total_pr = round( math.pow(math.e, -0.05 * math.pow(Total, 2)) * 100, 2) # 1-e^(-0.0001(x^2)) Len = round((1 - math.pow( math.e, -0.0001 * math.pow(len(JoinedSource), 2))) * 100, 2) out_s_js.write('["' + JoinedSource.replace(' ', '", "') + '"], \n') out_t_js.write('["' + JoinedTarget.replace(' ', '", "') + '"], \n') out_c_js.write(u'[' + repr(CDP_pr) + u', ' + repr(APout_pr) + u', ' + repr(APin_pr) + u', ' + repr(Total_pr) + u', ' + repr(Len) + u', ' + repr(len(JoinedSource)) + u', ' + repr(round(similarity * 100, 2)) + bleuScore + u'], \n') out_sc_js.write(u'[[' + ", ".join(srcTotal) + u'], ' + u'[' + ", ".join(trgTotal) + u'], ' + u'], \n') word = 0 out_a_js.write(u'[') for ali_i in ali: linePartC = 0 for ali_j in ali_i: # Maybe worth playing around with this for transformer (and convolutional) NMT output # if ali_j < 0.15: # ali_j = 0 out_a_js.write(u'[' + repr(word) + u', ' + str(np.round(ali_j, 8)) + u', ' + repr(linePartC) + u'], ') linePartC += 1 if outputType == 'color': printColor(ali_j) elif outputType == 'block': printBlock(ali_j) elif outputType == 'block2': printBlock2(ali_j) if outputType != 'web' and outputType != 'compare': sys.stdout.write(src[word].encode( 'utf-8', errors='replace').decode('utf-8')) word += 1 if outputType != 'web' and outputType != 'compare': sys.stdout.write('\n') # write target sentences #build 2d array occupied_to = [] outchars = [] outchars.append([]) tw = 0 for tword in tgt: columns = len(tgt) # Some characters use multiple symbols. Need to decode and then encode... twchars = list(tword) twlen = len(twchars) xpos = tw * 2 emptyline = 0 for el in range(0, len(occupied_to)): # if occupied, move to a new line! if occupied_to[el] < xpos: emptyline = el if len(outchars) < emptyline + 1: # add a new row outchars.append([]) break if el == len(occupied_to) - 1: emptyline = el + 1 if len(outchars) < emptyline + 1: outchars.append([]) for column in range(0, xpos): if len(outchars[emptyline]) <= column: outchars[emptyline].append(' ') for charindex in range(0, twlen): if xpos + charindex == len( outchars[emptyline]): outchars[emptyline].append( twchars[charindex]) else: outchars[emptyline][ charindex] = twchars[charindex] if len(occupied_to) <= emptyline: occupied_to.append(xpos + twlen + 1) else: occupied_to[emptyline] = xpos + twlen + 1 tw += 1 #print 2d array if outputType != 'web' and outputType != 'compare': for liline in outchars: sys.stdout.write(''.join(liline).encode( 'utf-8', errors='replace').decode( 'utf-8') + '\n') # print scores sys.stdout.write( '\nCoverage Deviation Penalty: \t\t' + repr(round(CDP, 8)) + ' (' + repr(CDP_pr) + '%)' + '\n') sys.stdout.write( 'Input Absentmindedness Penalty: \t' + repr(round(APin, 8)) + ' (' + repr(APin_pr) + '%)' + '\n') sys.stdout.write( 'Output Absentmindedness Penalty: \t' + repr(round(APout, 8)) + ' (' + repr(APout_pr) + '%)' + '\n') sys.stdout.write('Confidence: \t\t\t\t' + repr(round(Total, 8)) + ' (' + repr(Total_pr) + '%)' + '\n') sys.stdout.write( 'Similarity: \t\t\t\t' + repr(round(similarity * 100, 2)) + '%' + '\n') if bleuNumber > -1: sys.stdout.write('BLEU: \t\t\t\t\t' + repr(bleuNumber) + '\n') # write target sentences word = 0 out_a_js.write(u'], \n') if outputType != 'web' and outputType != 'compare': sys.stdout.write('\n') out_a_js.write(u'\n]') out_s_js.write(u']') out_t_js.write(u']') out_c_js.write(u']') out_sc_js.write(u']')
from nltk.translate import bleu # x = np.arange(0,4*np.pi,0.1) # start,stop,step # y = np.sin(x) # # fig = plt.figure(figsize=(10, 10)) # # # # for result in range(5,20): # for i in range(0,result): # print("Result and i",result,i) # ax = fig.add_subplot((result+1)//2, (result+1)//2, i+1) # ax.set_title("Check") # a = np.arange(i+1, i+17).reshape(4, 4) # ax.imshow(a, cmap='gray', alpha=0.6) # plt.savefig("test"+str(result)+"png") # real_caption = "<start> a man riding his bike across the bridge that is over the river <end>" result = ['<start>', 'a', 'man', 'in', 'shirts', 'scene', '<end>'] #result = ["<start>","a","man","<end>"] #result =" ".join(result) print() smoothie = SmoothingFunction().method4 score = bleu([real_caption], result, smoothing_function=smoothie, weights=(0.25, 0.25, 0.25, 0.25)) print(score)
from nltk.translate import bleu referrence = [['the', 'cat', 'is', 'now', 'on', 'the', 'mat']] candidate = ['the', 'cat', 'the', 'the', 'the', 'the'] print('bigram:', bleu(referrence, candidate, weights=(0, 1, 0, 0))) # print ('bleu:',bleu(referrence1,candidate1))
def RL_tuning_model( data_comb, epoch, batch_size=64, num_units=200, beam_width=5, discount_factor=0.1, sen_reward_rate=2, dropout=1, dec_L=1, Bidirection=False, Embd_train=True, Attention=False, ): max_noisy_len = data_comb[3]["max_noisy_len"] max_clean_len = data_comb[3]["max_clean_len"] max_answer_len = data_comb[3]["max_answer_len"] L = max_clean_len noisy_Id = data_comb[0]['noisy_Id'] noisy_len = data_comb[0]['noisy_len'] ground_truth = data_comb[0]['ground_truth'] clean_len = data_comb[0]['clean_len'] answer_Id = data_comb[0]['answer_Id'] answer_len = data_comb[0]['answer_len'] train_Id = data_comb[0]['train_Id'] vocab = data_comb[0]['vocab'] embd = data_comb[0]['embd'] embd = np.array(embd) val_noisy_Id = data_comb[1]['noisy_Id'] val_noisy_len = data_comb[1]['noisy_len'] val_ground_truth = data_comb[1]['ground_truth'] val_clean_len = data_comb[1]['clean_len'] val_answer_Id = data_comb[1]['answer_Id'] val_answer_len = data_comb[1]['answer_len'] val_train_Id = data_comb[1]['train_Id'] test_noisy_Id = data_comb[2]['noisy_Id'] test_noisy_len = data_comb[2]['noisy_len'] test_ground_truth = data_comb[2]['ground_truth'] test_clean_len = data_comb[2]['clean_len'] test_answer_Id = data_comb[2]['answer_Id'] test_answer_len = data_comb[2]['answer_len'] test_train_Id = data_comb[2]['train_Id'] vocab_size = len(vocab) config = tf.ConfigProto() config.gpu_options.allow_growth = True model_name = "BS_" values = {} checkpoint_path = "/home/ye/PycharmProjects/Qrefine/Seq_ckpt/Huawei/pretrainALL_seq2seq_Bi_" + str( Bidirection) + "_Att_" + str(Attention) + "_Emb_" + str( Embd_train) + "_hiddenunits_" + str(num_units) reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() for key in var_to_shape_map: if 'loss_fun' not in key: values[model_name + key + ':0'] = reader.get_tensor(key) model_cond = "training" G_Seq2Seq = tf.Graph() sess_word_rw = tf.Session(config=config, graph=G_Seq2Seq) with G_Seq2Seq.as_default(): Seq2Seq_model = BA_Seq2Seq.Bi_Att_Seq2Seq(batch_size, vocab_size, num_units, embd, model_cond, Bidirection, Embd_train, Attention) saver_word_rw = tf.train.Saver() # saver_word_rw.restore(sess_word_rw, # "Seq_ckpt/pretrainALL-seq2seq_Bi_" + str(Bidirection) + "_Att_" + str( # Attention) + "_Emb_" + str( # Embd_train)) saver_word_rw.restore( sess_word_rw, "/home/ye/PycharmProjects/Qrefine/Seq_ckpt/Huawei/ALL_seq2seq_Bi_" + str(Bidirection) + "_Att_" + str(Attention) + "_Emb_" + str(Embd_train) + "_hiddenunits_" + str(num_units)) # model_type = "testing" # G_QA_similiarity = tf.Graph() # sess_QA_rw = tf.Session(config=config, graph=G_QA_similiarity) # with G_QA_similiarity.as_default(): # QA_simi_model = QA_similiarity.QA_similiarity(batch_size, num_units, embd, model_type) # saver_sen_rw = tf.train.Saver() # # saver_sen_rw.restore(sess_QA_rw, "Seq_ckpt/Wiki_qa_similiarity") # saver_sen_rw.restore(sess_QA_rw, "/home/ye/PycharmProjects/Qrefine/Seq_ckpt/Huawei/qa_similiarity") G_BeamSearch = tf.Graph() with G_BeamSearch.as_default(): BeamSearch_seq2seq = seq_last.BeamSearch_Seq2seq( vocab_size=vocab_size, num_units=num_units, beam_width=beam_width, model_name=model_name, embd=embd, Bidirection=Bidirection, Embd_train=Embd_train, Attention=Attention, max_target_length=L) seq2seq_len = L RL_len = 0 with tf.Session(config=config, graph=G_BeamSearch) as sess_beamsearch: sess_beamsearch.run(tf.global_variables_initializer()) for v in tf.trainable_variables(): if v.name in values.keys(): v.load(values[v.name], sess_beamsearch) val_loss_epo = [] patience_cnt = 0 for epo in range(epoch): print(" epoch: {}".format(epo)) # RL_len = epo RL_len = L - seq2seq_len idx = np.arange(0, len(noisy_Id)) idx = list(np.random.permutation(idx)) Bleu_score1 = [] for batch in range(len(noisy_Id) / batch_size): if seq2seq_len < 0: seq2seq_len = 0 source_shuffle, source_len, train_shuffle, target_shuffle, target_len, answer_shuffle, ans_len = next_batch( noisy_Id, noisy_len, train_Id, ground_truth, clean_len, answer_Id, answer_len, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', truncating="post", value=EOS) target_shuffle = tf.keras.preprocessing.sequence.pad_sequences( target_shuffle, maxlen=seq2seq_len, padding='post', truncating="post", value=EOS) train_shuffle = tf.keras.preprocessing.sequence.pad_sequences( train_shuffle, maxlen=seq2seq_len, padding='post', truncating="post", value=EOS) initial_input_in = [[EOS] for i in range(batch_size)] target_len = np.tile(0, batch_size) fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.decoder_length: target_len, BeamSearch_seq2seq.decoder_inputs: train_shuffle, BeamSearch_seq2seq.decoder_targets: target_shuffle, BeamSearch_seq2seq.initial_input: initial_input_in } generated_que, policy = sess_beamsearch.run([ BeamSearch_seq2seq.RL_ids, BeamSearch_seq2seq.max_policy ], fd) generated_que_input = np.insert(generated_que, 0, SOS, axis=1)[:, 0:-1] generated_target_len = np.tile( generated_que_input.shape[1], batch_size) fd_seq = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, Seq2Seq_model.decoder_inputs: generated_que_input, Seq2Seq_model.decoder_length: generated_target_len, Seq2Seq_model.ground_truth: generated_que, Seq2Seq_model.dropout_rate: dropout } logits_pro = sess_word_rw.run(Seq2Seq_model.softmax_logits, fd_seq) answer_shuffle = tf.keras.preprocessing.sequence.pad_sequences( answer_shuffle, maxlen=None, padding='post', truncating="post", value=EOS) generated_len = np.tile(generated_que.shape[1], batch_size) # print QA_similiarity_rd reward = np.zeros((batch_size, RL_len)) for i in range(generated_que.shape[0]): for j in range(seq2seq_len, generated_que.shape[1]): max_index = generated_que[i][j] reward[i][L - 1 - j] = logits_pro[i, j, max_index] if j == generated_que.shape[1] - 1: reward[i][L - 1 - j] = reward[i][L - 1 - j] discounted_rewards = discounted_rewards_cal( reward, discount_factor) RL_rewards = discounted_rewards fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.decoder_length: target_len, BeamSearch_seq2seq.decoder_inputs: train_shuffle, BeamSearch_seq2seq.decoder_targets: target_shuffle, BeamSearch_seq2seq.initial_input: initial_input_in, BeamSearch_seq2seq.dis_rewards: RL_rewards } _, rl_reward = sess_beamsearch.run([ BeamSearch_seq2seq.RL_train_op, BeamSearch_seq2seq.rl_reward ], fd) for t in range(batch_size): ref = [] hyp = [] bleu_s = [] for tar_id in target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for pre_id in generated_que[t, :]: if pre_id != 0 and pre_id != -1 and pre_id != 2: hyp.append(vocab[pre_id]) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) bleu_s.append(sen_bleu1) Bleu_score1.append(bleu_s) top1_bleu = [] for i in range(len(Bleu_score1)): top1_bleu.append(Bleu_score1[i][0]) if batch == 0 or batch % 100 == 0: print(' batch {}'.format(batch)) print(' minibatch reward of training: {}'.format( -rl_reward)) print("the current whole bleu_score1 is:", sum(top1_bleu) / float(len(top1_bleu))) beam_QA_similar = [] # beam_cnQA_similar = [] source_shuffle, source_len, train_shuffle, target_shuffle, target_len, answer_shuffle, ans_len = next_batch( noisy_Id, noisy_len, train_Id, ground_truth, clean_len, answer_Id, answer_len, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', truncating="post", value=EOS) target_shuffle_in = tf.keras.preprocessing.sequence.pad_sequences( target_shuffle, maxlen=seq2seq_len, padding='post', truncating="post", value=EOS) train_shuffle = tf.keras.preprocessing.sequence.pad_sequences( train_shuffle, maxlen=seq2seq_len, padding='post', truncating="post", value=EOS) initial_input_in = [ target_shuffle_in[i][-1] for i in range(batch_size) ] target_len = np.tile(seq2seq_len, batch_size) fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.decoder_length: target_len, BeamSearch_seq2seq.decoder_inputs: train_shuffle, BeamSearch_seq2seq.decoder_targets: target_shuffle_in, BeamSearch_seq2seq.initial_input: initial_input_in } cl_loss, _, S2S_ids = sess_beamsearch.run([ BeamSearch_seq2seq.loss_seq2seq, BeamSearch_seq2seq.train_op, BeamSearch_seq2seq.ids ], fd) # tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto') if batch == 0 or batch % batches_in_epoch == 0: print(' batch {}'.format(batch)) print(' minibatch loss of training: {}'.format(cl_loss)) val_loss = [] for batch_val in range(len(val_noisy_Id) / batch_size): idx_v = np.arange(0, len(val_noisy_Id)) idx_v = list(np.random.permutation(idx_v)) val_source_shuffle, val_source_len, val_train_shuffle, val_target_shuffle, val_target_len, val_answer_shuffle, val_ans_len = next_batch( val_noisy_Id, val_noisy_len, val_train_Id, val_ground_truth, val_clean_len, val_answer_Id, val_answer_len, batch_size, batch_val, idx_v) val_source_shuffle_in = tf.keras.preprocessing.sequence.pad_sequences( val_source_shuffle, maxlen=None, padding='post', value=EOS) val_target_shuffle_in = tf.keras.preprocessing.sequence.pad_sequences( val_target_shuffle, maxlen=seq2seq_len, padding='post', value=EOS) val_train_shuffle_in = tf.keras.preprocessing.sequence.pad_sequences( val_train_shuffle, maxlen=seq2seq_len, padding='post', value=EOS) val_initial_input = [ val_target_shuffle_in[i][-1] for i in range(batch_size) ] val_target_len = np.tile(seq2seq_len, batch_size) fd_val = { BeamSearch_seq2seq.encoder_inputs: val_source_shuffle_in, BeamSearch_seq2seq.encoder_inputs_length: val_source_len, BeamSearch_seq2seq.decoder_length: val_target_len, BeamSearch_seq2seq.decoder_inputs: val_train_shuffle_in, BeamSearch_seq2seq.decoder_targets: val_target_shuffle_in, BeamSearch_seq2seq.initial_input: val_initial_input } val_loss.append( sess_beamsearch.run( BeamSearch_seq2seq.loss_seq2seq, fd_val)) avg_val_loss = sum(val_loss) / float(len(val_loss)) print(' minibatch loss of validation: {}'.format( avg_val_loss)) # val_loss_epo.append(avg_val_loss) gc.collect() if L - seq2seq_len == 0: continue RL_logits, policy = sess_beamsearch.run( [BeamSearch_seq2seq.RL_ids, BeamSearch_seq2seq.max_policy], fd) # print "The size of Rl:", RL_logits.shape[0], RL_logits.shape[1] # print "The size of Policy:", policy.shape[0], policy.shape[1] max_target_length = RL_logits.shape[1] # for batch in range(batch_size): # label = 0 # for i in range(RL_logits.shape[1]): # if RL_logits[batch][i]==2 and label ==0: # label = 1 # for id in range(i+1, max_target_length): # RL_logits[batch][id]=2 # continue Sequnce_len = seq2seq_len + max_target_length # print "the max_target_length is:", max_target_length RL_rewards = np.zeros((batch_size, max_target_length)) generated_que = [] for i in range(batch_size): generated_que.append( list(np.append(S2S_ids[i], RL_logits[i]))) generated_que = np.asarray(generated_que) logits_batch = [] # for i in range(generated_que.shape[0]): # for j in range(generated_que.shape[1]): # if generated_que[i][j] == 2 or j == generated_que.shape[1] - 1: # logits_batch.append(j) # continue # # reward = np.zeros((batch_size, max_target_length)) # # generated_que = SOS + generated_que - PAD generated_que_input = np.insert(generated_que, 0, SOS, axis=1)[:, 0:-1] # target_shuffle = tf.keras.preprocessing.sequence.pad_sequences(target_shuffle, maxlen=None, # padding='post',truncating="post", value=EOS) generated_target_len = np.tile(generated_que_input.shape[1], batch_size) fd_seq = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, Seq2Seq_model.decoder_inputs: generated_que_input, Seq2Seq_model.decoder_length: generated_target_len, Seq2Seq_model.ground_truth: generated_que, Seq2Seq_model.dropout_rate: dropout } logits_pro = sess_word_rw.run(Seq2Seq_model.softmax_logits, fd_seq) # logits_pro = logits_pro[:, :, :] # print "the logits_pro is:", logits_pro.shape[1] # print QA_similiarity_rd # reward = np.zeros((batch_size, RL_len)) for i in range(generated_que.shape[0]): label = 0 for j in range(seq2seq_len, generated_que.shape[1]): max_index = generated_que[i][j] RL_rewards[i][j - seq2seq_len] = logits_pro[i, j, max_index] if max_index == 2 and label == 0: label = 1 RL_rewards[i][j - seq2seq_len] = logits_pro[i, j, max_index] break # if j == generated_que.shape[1] - 1: # reward[i][j - seq2seq_len] = reward[i][j - seq2seq_len] + sen_reward_rate * (QA_similiarity_rd[0][i]) # fd = {an_Lstm.answer_inputs: answer_shuffle, an_Lstm.answer_inputs_length: ans_len} # reward_similiarity = sess_sen_rw.run([an_Lstm.answer_state], fd) discounted_rewards = discounted_rewards_cal( RL_rewards, discount_factor) RL_rewards = discounted_rewards fd = { BeamSearch_seq2seq.encoder_inputs: source_shuffle, BeamSearch_seq2seq.encoder_inputs_length: source_len, BeamSearch_seq2seq.decoder_length: target_len, BeamSearch_seq2seq.decoder_inputs: train_shuffle, BeamSearch_seq2seq.decoder_targets: target_shuffle_in, BeamSearch_seq2seq.initial_input: initial_input_in, BeamSearch_seq2seq.dis_rewards: RL_rewards } _, rl_reward, word_policy, policy = sess_beamsearch.run([ BeamSearch_seq2seq.RL_train_op, BeamSearch_seq2seq.rl_reward, BeamSearch_seq2seq.word_log_prob, BeamSearch_seq2seq.softmax_policy ], fd) for t in range(batch_size): ref = [] hyp = [] bleu_s = [] for tar_id in target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for pre_id in generated_que[t, :]: if pre_id != 0 and pre_id != -1 and pre_id != 2: hyp.append(vocab[pre_id]) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) bleu_s.append(sen_bleu1) Bleu_score1.append(bleu_s) top1_bleu = [] for i in range(len(Bleu_score1)): top1_bleu.append(Bleu_score1[i][0]) if batch == 0 or batch % 100 == 0: print(' batch {}'.format(batch)) print(' minibatch reward of training: {}'.format( -rl_reward)) #print the result for t in range(5): ## five sentences print('Question {}'.format(t)) print("noisy question:") print(" ".join( map(lambda i: vocab[i], list(source_shuffle[t]))).strip().replace( "<EOS>", " ")) print("clean question:") print(" ".join( map(lambda i: vocab[i], list(target_shuffle[t]))).strip().replace( "<EOS>", " ")) print("the generated question:") pre_sen = [] for log_id in generated_que[t, :]: if log_id != -1 and log_id != 2 and log_id != 0: pre_sen.append(log_id) print(" ".join(map(lambda i: vocab[i], pre_sen)).strip()) print("\n") print("the current whole bleu_score1 is:", sum(top1_bleu) / float(len(top1_bleu))) gc.collect() seq2seq_len = seq2seq_len - dec_L ##testing set result: val_bleu_s = [] generated_test_sen = [] test_answer_sen = [] test_noisy_sen = [] test_clean_sen = [] for batch_test in range(len(test_noisy_Id) / batch_size): idx_t = np.arange(0, len(test_noisy_Id)) idx_t = list(np.random.permutation(idx_t)) val_source_shuffle, val_source_len, val_train_shuffle, val_target_shuffle, val_target_len, val_answer_shuffle, val_ans_len = next_batch( test_noisy_Id, test_noisy_len, test_train_Id, test_ground_truth, test_clean_len, test_answer_Id, test_answer_len, batch_size, batch_test, idx_t) for an in val_answer_shuffle: test_answer_sen.append(vocab[anum] for anum in an) for no in val_source_shuffle: test_noisy_sen.append(vocab[si] for si in no) for cl in val_target_shuffle: test_clean_sen.append(vocab[ci] for ci in cl) val_source_shuffle_in = tf.keras.preprocessing.sequence.pad_sequences( val_source_shuffle, maxlen=None, padding='post', truncating="post", value=EOS) val_target_shuffle_in = [] for val in val_target_shuffle: val_target_shuffle_in.append([val[0]]) val_train_shuffle_in = [] for tra in val_train_shuffle: val_train_shuffle_in.append([tra[0]]) initial_input_in = [SOS for i in range(batch_size)] # [SOS for i in range(batch_size)] val_target_len_in = np.tile(1, batch_size) fd = { BeamSearch_seq2seq.encoder_inputs: val_source_shuffle_in, BeamSearch_seq2seq.encoder_inputs_length: val_source_len, BeamSearch_seq2seq.decoder_length: val_target_len_in, BeamSearch_seq2seq.decoder_inputs: val_train_shuffle_in, BeamSearch_seq2seq.decoder_targets: val_target_shuffle_in, BeamSearch_seq2seq.initial_input: initial_input_in } val_id = sess_beamsearch.run([BeamSearch_seq2seq.RL_ids], fd) final_id = val_id[0] for t in range(batch_size): ref = [] hyp = [] for tar_id in val_target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for pre_id in final_id[t, :]: if pre_id != 2 and pre_id != 0 and pre_id != -1: hyp.append(vocab[pre_id]) generated_test_sen.append(hyp) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) val_bleu_s.append(sen_bleu1) bleu_score = sum(val_bleu_s) / float(len(val_bleu_s)) print "the bleu score on test is:", bleu_score if bleu_score > 0.73: fname = "Huawei_result_word/Huawei_bleu_" + str(bleu_score) f = open(fname, "wb") f.write("reward_rate " + str(sen_reward_rate) + " discount_factor " + str(discount_factor) + " dec_L " + str(dec_L) + " batch_size " + str(batch_size) + "epoch" + str(epoch)) print "the length test set is:", len(generated_test_sen), len( test_answer_sen), len(test_noisy_sen), len(test_clean_sen) f.write("the bleu score is " + str(bleu_score) + "\n") for i in range(len(generated_test_sen)): f.write("question" + str(i) + "\n") f.write("answer: " + "".join(test_answer_sen[i]) + "\n") f.write("noisy question: " + "".join(test_noisy_sen[i]) + "\n") f.write("clean question: " + "".join(test_clean_sen[i]) + "\n") f.write("generated question: " + "".join(generated_test_sen[i]) + "\n")
def main(): SOS = 0 EOS = 1 PAD = 2 UNK = 3 epoch = 100 batches_in_epoch = 100 batch_size = 32 num_units = 200 char_dim = 50 ## the size of char dimention char_hidden_units = 100 ##the length of the char_lstm (word embedding:2 * char_hidden) epoch_print = 2 Bidirection = False Attention = False Embd_train = False model_type = 'training' Dataset_name = "HUAWEI" Dataset_name = "YAHOO" logs_path = "/mnt/WDRed4T/ye/Qrefine/ckpt/" + Dataset_name + "/seq2seq_BIA_board" # FileName = "/mnt/WDRed4T/ye/DataR/" + Dataset_name + "/wrongword_Id_1" FileName = '/mnt/WDRed4T/ye/DataR/YAHOO/wrongword1_final' input = open(FileName, 'rb') data_com = pickle.load(input) train_noisy_Id = data_com["train_noisy_Id"] test_noisy_Id = data_com["test_noisy_Id"] eval_noisy_Id = data_com["eval_noisy_Id"] train_noisy_len = data_com["train_noisy_len"] test_noisy_len = data_com["test_noisy_len"] eval_noisy_len = data_com["eval_noisy_len"] train_noisy_char_Id = data_com["train_noisy_char_Id"] test_noisy_char_Id = data_com["test_noisy_char_Id"] eval_noisy_char_Id = data_com["eval_noisy_char_Id"] train_noisy_char_len = data_com["train_noisy_char_len"] test_noisy_char_len = data_com["test_noisy_char_len"] eval_noisy_char_len = data_com["eval_noisy_char_len"] train_target_Id = data_com["train_ground_truth"] test_target_Id = data_com["test_ground_truth"] eval_target_Id = data_com["eval_ground_truth"] train_input_Id = data_com["train_input_Id"] test_input_Id = data_com["test_input_Id"] eval_input_Id = data_com["eval_input_Id"] train_clean_len = data_com["train_clean_len"] test_clean_len = data_com["test_clean_len"] eval_clean_len = data_com["eval_clean_len"] max_char = data_com['max_char'] char_num = 44 max_word = data_com['max_word'] output = '/mnt/WDRed4T/ye/DataR/YAHOO/wrongword1_vocab_embd' input = open(output, 'rb') vocab_embd = pickle.load(input) vocab = vocab_embd['vocab'] embd = vocab_embd['embd'] embd = np.asarray(embd) vocdic = zip(vocab, range(len(vocab))) index_voc = dict((index, vocab) for vocab, index in vocdic) voc_index = dict( (vocab, index) for vocab, index in vocdic) ##dic[char]= index SOS = voc_index[u"<SOS>"] EOS = voc_index[u"<EOS>"] PAD = voc_index[u"<PAD>"] vocab_size = len(vocab) model_type = 'testing' config = tf.ConfigProto() config.gpu_options.allow_growth = True if model_type == 'training': Seq2Seq_model = Seq2Seq(batch_size, vocab_size, num_units, embd, model_type) print "the number of training question is:", len(train_noisy_Id) # print "the number of evaluate question is:", len(eval_noisy_Id) summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) saver = tf.train.Saver(sharded=False) merge = tf.summary.merge_all() max_batches = len(train_noisy_Id) / batch_size with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) # saver.restore(sess, "../Seq_ckpt/pretrain-model") for epo in range(epoch): idx = np.arange(0, len(train_noisy_Id)) idx = list(np.random.permutation(idx)) # idx_e = np.arange(0, len(eval_noisy_Id)) # idx_e = list(np.random.permutation(idx_e)) print "Epoch {}".format(epo) for batch in range(max_batches): source_shuffle, source_len, train_shuffle, target_shuffle, target_len = next_batch( train_noisy_Id, train_noisy_len, train_input_Id, train_clean_len, train_target_Id, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', value=EOS) train_shuffle = tf.keras.preprocessing.sequence.pad_sequences( train_shuffle, maxlen=None, padding='post', value=EOS) target_shuffle = tf.keras.preprocessing.sequence.pad_sequences( target_shuffle, maxlen=None, padding='post', value=EOS) target_len = np.tile(max(target_len) + 1, batch_size) fd = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, Seq2Seq_model.decoder_inputs: train_shuffle, Seq2Seq_model.decoder_length: target_len, Seq2Seq_model.ground_truth: target_shuffle } summary, logits_padded, logits, ids, l, _ = sess.run([ merge, Seq2Seq_model.logits, Seq2Seq_model.logits_id, Seq2Seq_model.ids, Seq2Seq_model.loss_seq2seq, Seq2Seq_model.train_op ], fd) summary_writer.add_summary(summary, batch) if batch == 0 or batch % batches_in_epoch == 0: ##print the training print('batch {}'.format(batch)) print(' minibatch loss: {}'.format( sess.run(Seq2Seq_model.loss_seq2seq, fd))) # ##print the validation # source_shuffle, source_len, target_shuffle, target_len = next_batch(eval_noisy_Id, eval_noisy_len, # eval_clean_Id, eval_clean_len, batch_size, batch, idx_e) # fd_eval = {Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, # Seq2Seq_model.decoder_targets: target_shuffle, Seq2Seq_model.decoder_length: target_len} # print(' minibatch loss: {}'.format(sess.run(Seq2Seq_model.loss_seq2seq, fd_eval))) if epo % epoch_print == 0: for t in range(10): print 'Question {}'.format(t) print " ".join( map(lambda i: vocab[i], list(target_shuffle[t]))).strip() print " ".join(map(lambda i: vocab[i], list(ids[t, :]))).strip() saver.save( sess, "../Seq_ckpt/pretrain-seq2seq_unique_embedding_trainable") elif model_type == 'testing': Seq2Seq_model = Seq2Seq(batch_size, vocab_size, num_units, embd, model_type) with tf.Session(config=config) as sess: saver_word_rw = tf.train.Saver() saver_word_rw.restore(sess, "../Seq_ckpt/pretrain-seq2seq") max_batches = len(test_noisy_Id) / batch_size idx = np.arange(0, len(test_noisy_Id)) idx = list(np.random.permutation(idx)) Bleu_score1 = [] Bleu_score2 = [] Bleu_score3 = [] ex_match = [] for batch in range(max_batches): source_shuffle, source_len, train_shuffle, target_shuffle, target_len = next_batch( test_noisy_Id, test_noisy_len, test_input_Id, test_clean_len, test_target_Id, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', value=EOS) fd = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len } ids = sess.run([Seq2Seq_model.ids], fd) for t in range(batch_size): ref = [] hyp = [] for tar_id in target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for pre_id in ids[0][t]: if pre_id != 2 and pre_id != 0: hyp.append(vocab[pre_id]) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) sen_bleu2 = bleu([ref], hyp, weights=(0.5, 0.5, 0, 0)) Bleu_score1.append(sen_bleu1) Bleu_score2.append(sen_bleu2) if ref == hyp: ex_match.append(1) else: ex_match.append(0) print "the current whole bleu_score1 is:", sum( Bleu_score1) / float(len(Bleu_score1)) print "the current whole bleu_score2 is:", sum( Bleu_score2) / float(len(Bleu_score2)) print "the whole accuracy is:", sum(ex_match) / float( len(ex_match))
# else : for items in range(len(labels[0])): decoder_output, decoder_hidden = decoder(decoder_input, decoder_hidden) loss += criterion(decoder_output, labels[0][items].unsqueeze(0).to(device)) topv, topi = decoder_output.topk(1) output_sent.append(Data.language.index2word[topi.item()]) label_sent.append( Data.language.index2word[labels[0][items].item()]) decoder_input = topi.squeeze().detach() try: training_output.append( (' '.join(output_sent), ' '.join(label_sent))) total_bleu += bleu([label_sent], output_sent, smoothing_function=smoothie) total_loss += loss.item() loss.backward() encoder_optimizer.step() decoder_optimizer.step() except Exception as e: print(e) print(output_sent) try: train_log = open('training_outputs.txt', 'w', encoding='latin') train_log.write('Recording outputs for epoch {}\n'.format(epoch + 1)) train_log.write('Training_loss {}\n'.format(total_loss / len(train_loader))) train_log.write('Training_Accuracy {}\n'.format(total_bleu /
def main(): PAD = 0 SOS = 1 EOS = 2 epoch = 20 batches_in_epoch = 500 batch_size = 128 num_units = 200 epoch_print = 5 logs_path = "../Seq_ckpt/seq2seq_board" f = open('../result/Huawei/large_pretrained_unique', 'rb') data = pickle.load(f) noisy_Id = data['noisy_Id'] clean_Id = data['clean_Id'] gt_Id = [] train_Id = [] # answer_Id = data['answer_Id'] vocab = data['vocab'] embd = data['embd'] vocab_size = len(vocab) num_data = len(noisy_Id) noisy_len = [] for n in noisy_Id: noisy_len.append(len(n)) max_source_length = max(noisy_len) print "the noisy length:", max_source_length print "the average noisy length:", sum(noisy_len) / float(len(noisy_len)) clean_len = [] for c in clean_Id: clean_len.append(len(c) + 1) max_target_length = max(clean_len) max_target_length = max_target_length print "the clean length:", max_target_length print "the average clean length:", sum(clean_len) / float(len(noisy_len)) # answer_len = [] # for a in answer_Id: # answer_len.append(len(a)) # max_answer_length = max(answer_len) num_data = len(noisy_Id) for i in range(num_data): train_Id.append([SOS] + clean_Id[i]) gt_Id.append(clean_Id[i] + [EOS]) # noisy_Id[i] = noisy_Id[i] + ([EOS] * (max_source_length - noisy_len[i])) # train_Id.append([SOS] + clean_Id[i] + ([EOS] * (max_target_length - clean_len[i]))) # gt_Id.append(clean_Id[i] + ([EOS] * (max_target_length - clean_len[i]+1))) # answer_Id[i] = answer_Id[i] + ([PAD] * (max_answer_length - len(answer_Id[i]))) for i in range(len(clean_len)): clean_len[i] = clean_len[i] - 1 train_noisy_Id = noisy_Id[1:int(0.8 * num_data)] train_noisy_len = noisy_len[1:int(0.8 * num_data)] train_input_Id = train_Id[1:int(0.8 * num_data)] train_clean_len = clean_len[1:int(0.8 * num_data)] train_target_Id = gt_Id[1:int(0.8 * num_data)] test_noisy_Id = noisy_Id[int(0.8 * num_data):] test_noisy_len = noisy_len[int(0.8 * num_data):] test_input_Id = train_Id[int(0.8 * num_data):] test_clean_len = clean_len[int(0.8 * num_data):] test_target_Id = gt_Id[int(0.8 * num_data):] # eval_noisy_Id = noisy_Id[int(0.6 * num_data) + 1:int(0.8 * num_data)] # eval_noisy_len = noisy_len[int(0.6 * num_data) + 1:int(0.8 * num_data)] # eval_clean_Id = clean_Id[int(0.6 * num_data) + 1:int(0.8 * num_data)] # eval_clean_len = clean_len[int(0.6 * num_data) + 1:int(0.8 * num_data)] model_type = 'testing' config = tf.ConfigProto() config.gpu_options.allow_growth = True if model_type == 'training': max_t_length = max_target_length Seq2Seq_model = Seq2Seq(batch_size, vocab_size, num_units, embd, model_type) print "the number of training question is:", len(train_noisy_Id) # print "the number of evaluate question is:", len(eval_noisy_Id) summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) saver = tf.train.Saver(sharded=False) merge = tf.summary.merge_all() max_batches = len(train_noisy_Id) / batch_size with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) # saver.restore(sess, "../Seq_ckpt/pretrain-model") for epo in range(epoch): idx = np.arange(0, len(train_noisy_Id)) idx = list(np.random.permutation(idx)) # idx_e = np.arange(0, len(eval_noisy_Id)) # idx_e = list(np.random.permutation(idx_e)) print "Epoch {}".format(epo) for batch in range(max_batches): source_shuffle, source_len, train_shuffle, target_shuffle, target_len = next_batch( train_noisy_Id, train_noisy_len, train_input_Id, train_clean_len, train_target_Id, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', value=EOS) train_shuffle = tf.keras.preprocessing.sequence.pad_sequences( train_shuffle, maxlen=None, padding='post', value=EOS) target_shuffle = tf.keras.preprocessing.sequence.pad_sequences( target_shuffle, maxlen=None, padding='post', value=EOS) target_len = np.tile(max(target_len) + 1, batch_size) fd = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, Seq2Seq_model.decoder_inputs: train_shuffle, Seq2Seq_model.decoder_length: target_len, Seq2Seq_model.ground_truth: target_shuffle } summary, logits_padded, logits, ids, l, _ = sess.run([ merge, Seq2Seq_model.logits, Seq2Seq_model.logits_id, Seq2Seq_model.ids, Seq2Seq_model.loss_seq2seq, Seq2Seq_model.train_op ], fd) summary_writer.add_summary(summary, batch) if batch == 0 or batch % batches_in_epoch == 0: ##print the training print('batch {}'.format(batch)) print(' minibatch loss: {}'.format( sess.run(Seq2Seq_model.loss_seq2seq, fd))) # ##print the validation # source_shuffle, source_len, target_shuffle, target_len = next_batch(eval_noisy_Id, eval_noisy_len, # eval_clean_Id, eval_clean_len, batch_size, batch, idx_e) # fd_eval = {Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len, # Seq2Seq_model.decoder_targets: target_shuffle, Seq2Seq_model.decoder_length: target_len} # print(' minibatch loss: {}'.format(sess.run(Seq2Seq_model.loss_seq2seq, fd_eval))) if epo % epoch_print == 0: for t in range(10): print 'Question {}'.format(t) print " ".join( map(lambda i: vocab[i], list(target_shuffle[t]))).strip() print " ".join(map(lambda i: vocab[i], list(ids[t, :]))).strip() saver.save( sess, "../Seq_ckpt/pretrain-seq2seq_unique_embedding_trainable") elif model_type == 'testing': Seq2Seq_model = Seq2Seq(batch_size, vocab_size, num_units, embd, model_type) with tf.Session(config=config) as sess: saver_word_rw = tf.train.Saver() saver_word_rw.restore(sess, "../Seq_ckpt/pretrain-seq2seq") max_batches = len(test_noisy_Id) / batch_size idx = np.arange(0, len(test_noisy_Id)) idx = list(np.random.permutation(idx)) Bleu_score1 = [] Bleu_score2 = [] Bleu_score3 = [] ex_match = [] for batch in range(max_batches): source_shuffle, source_len, train_shuffle, target_shuffle, target_len = next_batch( test_noisy_Id, test_noisy_len, test_input_Id, test_clean_len, test_target_Id, batch_size, batch, idx) source_shuffle = tf.keras.preprocessing.sequence.pad_sequences( source_shuffle, maxlen=None, padding='post', value=EOS) fd = { Seq2Seq_model.encoder_inputs: source_shuffle, Seq2Seq_model.encoder_inputs_length: source_len } ids = sess.run([Seq2Seq_model.ids], fd) for t in range(batch_size): ref = [] hyp = [] for tar_id in target_shuffle[t]: if tar_id != 2: ref.append(vocab[tar_id]) for pre_id in ids[0][t]: if pre_id != 2 and pre_id != 0: hyp.append(vocab[pre_id]) sen_bleu1 = bleu([ref], hyp, weights=(1, 0, 0, 0)) sen_bleu2 = bleu([ref], hyp, weights=(0.5, 0.5, 0, 0)) Bleu_score1.append(sen_bleu1) Bleu_score2.append(sen_bleu2) if ref == hyp: ex_match.append(1) else: ex_match.append(0) print "the current whole bleu_score1 is:", sum( Bleu_score1) / float(len(Bleu_score1)) print "the current whole bleu_score2 is:", sum( Bleu_score2) / float(len(Bleu_score2)) print "the whole accuracy is:", sum(ex_match) / float( len(ex_match))