def __init__(self, word, pos1, pos2, ind, en, y, sf): self.word_o = np.load(word) self.pos1_o = np.load(pos1) self.pos2_o = np.load(pos2) self.ind_o = np.load(ind) self.en_o = np.load(en) self.y_o = np.load(y) self.word = [] self.pos1 = [] self.pos2 = [] self.ind = [] self.en = [] self.y = [] self.episode_index = -1 pathname = "./model/ATT_GRU_model-" self.embedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 114044 test_settings.num_classes = 53 test_settings.big_num = 1 with tf.Graph().as_default(): self.sess = tf.Session() with self.sess.as_default(): with tf.variable_scope("model"): self.mtest = network.GRU(is_training=False, word_embeddings=self.embedding, settings=test_settings) saver = tf.train.Saver() saver.restore(self.sess, pathname + str(11000)) if sf == True: self.s_f() else: self.word = np.array(self.word_o) self.pos1 = np.array(self.pos1_o) self.pos2 = np.array(self.pos2_o) self.ind = np.array(self.ind_o) self.en = np.array(self.en_o) self.y = np.array(self.y_o)
total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob, feature_map = sess.run( [mtest.loss, mtest.accuracy, mtest.prob, mtest.attention_r], feed_dict) return prob, accuracy, feature_map with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) print('reading word embedding data...') vec = [] word2id = {} f = open('./origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline()
def main(_): # ATTENTION: change pathname before you load your model pathname = "./model/ATT_GRU_model-" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 114044 test_settings.num_classes = 53 test_settings.big_num = 262 * 9 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy # evaluate p@n def eval_pn(test_y, test_word, test_pos1, test_pos2, test_settings): allprob = [] acc = [] for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) eval_y = [] for i in test_y: eval_y.append(i[1:]) allans = np.reshape(eval_y, (-1)) order = np.argsort(-allprob) print('P@100:') top100 = order[:100] correct_num_100 = 0.0 for i in top100: if allans[i] == 1: correct_num_100 += 1.0 print(correct_num_100 / 100) print('P@200:') top200 = order[:200] correct_num_200 = 0.0 for i in top200: if allans[i] == 1: correct_num_200 += 1.0 print(correct_num_200 / 200) print('P@300:') top300 = order[:300] correct_num_300 = 0.0 for i in top300: if allans[i] == 1: correct_num_300 += 1.0 print(correct_num_300 / 300) if itchat_run: tempstr = 'P@100\n' + str( correct_num_100 / 100) + '\n' + 'P@200\n' + str( correct_num_200 / 200) + '\n' + 'P@300\n' + str( correct_num_300 / 300) itchat.send(tempstr, FLAGS.wechat_name) with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} if USE_LEGACY: names_to_vars = transform_name_var_dict(names_to_vars) saver = tf.train.Saver(names_to_vars) # ATTENTION: change the list to the iters you want to test !! # testlist = range(9025,14000,25) testlist = [10900] for model_iter in testlist: # for compatibility purposes only, name key changes from tf 0.x to 1.x, compat_layer saver.restore(sess, pathname + str(model_iter)) print("Evaluating P@N for iter " + str(model_iter)) if itchat_run: itchat.send("Evaluating P@N for iter " + str(model_iter), FLAGS.wechat_name) print('Evaluating P@N for one') if itchat_run: itchat.send('Evaluating P@N for one', FLAGS.wechat_name) test_y = np.load('./data/pone_test_y.npy') test_word = np.load('./data/pone_test_word.npy') test_pos1 = np.load('./data/pone_test_pos1.npy') test_pos2 = np.load('./data/pone_test_pos2.npy') eval_pn(test_y, test_word, test_pos1, test_pos2, test_settings) print('Evaluating P@N for two') if itchat_run: itchat.send('Evaluating P@N for two', FLAGS.wechat_name) test_y = np.load('./data/ptwo_test_y.npy') test_word = np.load('./data/ptwo_test_word.npy') test_pos1 = np.load('./data/ptwo_test_pos1.npy') test_pos2 = np.load('./data/ptwo_test_pos2.npy') eval_pn(test_y, test_word, test_pos1, test_pos2, test_settings) print('Evaluating P@N for all') if itchat_run: itchat.send('Evaluating P@N for all', FLAGS.wechat_name) test_y = np.load('./data/pall_test_y.npy') test_word = np.load('./data/pall_test_word.npy') test_pos1 = np.load('./data/pall_test_pos1.npy') test_pos2 = np.load('./data/pall_test_pos2.npy') eval_pn(test_y, test_word, test_pos1, test_pos2, test_settings) time_str = datetime.datetime.now().isoformat() print(time_str) print('Evaluating all test data and save data for PR curve') if itchat_run: itchat.send( 'Evaluating all test data and save data for PR curve', FLAGS.wechat_name) test_y = np.load('./data/testall_y.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') allprob = [] acc = [] for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) print('saving all test result...') current_step = model_iter # ATTENTION: change the save path before you save your result !! np.save('./out/allprob_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') # caculate the pr curve area average_precision = average_precision_score(allans, allprob) print('PR curve area:' + str(average_precision)) if itchat_run: itchat.send('PR curve area:' + str(average_precision), FLAGS.wechat_name) time_str = datetime.datetime.now().isoformat() print(time_str) print('P@N for all test data:') print('P@100:') top100 = order[:100] correct_num_100 = 0.0 for i in top100: if allans[i] == 1: correct_num_100 += 1.0 print(correct_num_100 / 100) print('P@200:') top200 = order[:200] correct_num_200 = 0.0 for i in top200: if allans[i] == 1: correct_num_200 += 1.0 print(correct_num_200 / 200) print('P@300:') top300 = order[:300] correct_num_300 = 0.0 for i in top300: if allans[i] == 1: correct_num_300 += 1.0 print(correct_num_300 / 300) if itchat_run: tempstr = 'P@100\n' + str( correct_num_100 / 100) + '\n' + 'P@200\n' + str( correct_num_200 / 200) + '\n' + 'P@300\n' + str( correct_num_300 / 300) itchat.send(tempstr, FLAGS.wechat_name)
def main(_): # the path to save models save_path = './model/' print('reading wordembedding') wordembedding = np.load('./data/vec.npy') print('reading training data') train_y = np.load('./data/train_y.npy') train_word = np.load('./data/train_word.npy') train_pos1 = np.load('./data/train_pos1.npy') train_pos2 = np.load('./data/train_pos2.npy') settings = network.Settings() settings.vocab_size = len(wordembedding) settings.num_classes = len(train_y[0]) big_num = settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): initializer = tf.contrib.layers.xavier_initializer() with tf.variable_scope( "model", reuse=None, initializer=initializer): m = network.GRU( is_training=True, word_embeddings=wordembedding, settings=settings) global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(0.0005) train_op = optimizer.minimize(m.final_loss, global_step=global_step) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=None) merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter( FLAGS.summary_dir + '/train_loss', sess.graph) def train_step(word_batch, pos1_batch, pos2_batch, y_batch, big_num): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch temp, step, loss, accuracy, summary, l2_loss, final_loss = sess.run( [ train_op, global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss ], feed_dict) time_str = datetime.datetime.now().isoformat() accuracy = np.reshape(np.array(accuracy), (big_num)) acc = np.mean(accuracy) summary_writer.add_summary(summary, step) if step % 50 == 0: tempstr = "{}: step {}, softmax_loss {:g}, acc {:g}".format( time_str, step, loss, acc) print(tempstr) for one_epoch in range(settings.num_epochs): temp_order = list(range(len(train_word))) np.random.shuffle(temp_order) for i in range(int(len(temp_order) / float(settings.big_num))): temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] temp_input = temp_order[i * settings.big_num:( i + 1) * settings.big_num] for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) num = 0 for single_word in temp_word: num += len(single_word) if num > 1500: print('out of range') continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) train_step(temp_word, temp_pos1, temp_pos2, temp_y, settings.big_num) current_step = tf.train.global_step(sess, global_step) if current_step > 200 and current_step % 20 == 0: # if current_step > 100 and current_step % 100 == 0: print('saving model') path = saver.save( sess, save_path + 'ATT_GRU_model', global_step=current_step) tempstr = 'have saved model to ' + path print(tempstr)
def main(args): #If you retrain the model, please remember to change the path to your own model below: pathname = "./relation_extract/model/ATT_GRU_model-180" wordembedding = np.load('./relation_extract/data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 14123 test_settings.num_classes = 4 test_settings.big_num = 1 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(char_batch, word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_char = [] total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for char in char_batch[i]: total_char.append(char) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_char = np.array(total_char) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_char] = total_char feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) print('reading word embedding data...') vec = [] word2id = {} f = open('./relation_extract/origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) print('reading relation to id') relation2id = {'has_product': 0, 'is_version_of': 1, 'has_vulnerability': 2, 'no_relationship': 3} id2relation = {0: 'has_product', 1: 'is_version_of', 2: 'has_vulnerability', 3: 'no_relationship'} #while True: #try: #BUG: Encoding error if user input directly from command line. #line = input('name1 name2 sentence:') if not(args.input == None or args.output == None): out = open(args.output, "w") sentence_tag = open(args.input, "r").read() sentence_t = sentence_tag.split("\n") if (sentence_t[0]) == "No relationship detected!": out.write(re_sentences) else: sentence = sentence_t[0] out.write(sentence+"\n") tag_sens = sentence_t[1:] for tag_sen in tag_sens: print(tag_sen) en1_en2_sen = tag_sen.strip().split() print(en1_en2_sen) en1 = en1_en2_sen[0] en2 = en1_en2_sen[1] sentence = list(en1_en2_sen[2:]) print("name1: " + en1) print("name2: " + en2) print(" ".join(sentence)) relation = 0 en1pos = sentence.index(en1) en2pos = sentence.index(en2) output = [] # length of sentence is 70 fixlen = 72 # max length of position embedding is 60 (-60~+60) maxlen = 60 #Encoding test x en1_en2_appear_1 = 0 for i in range(fixlen): if i in [en1pos, en2pos]: en1_en2_appear_1 += 1 continue word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) en1_en2_appear_2 = 0 for i in range(min(fixlen, len(sentence))): if i in [en1pos, en2pos]: en1_en2_appear_2 += 1 continue word = 0 if sentence[i] not in word2id: ps = sentence[i].split('_') # avg_vec = np.zeros(commons.word_emb_dim) c = 0 for p in ps: if p in word2id: c += 1 # avg_vec += vec[word2id[p]] if c > 0: # avg_vec = avg_vec / c word2id[sentence[i]] = len(word2id) #vec.append(avg_vec) else: word = word2id['UNK'] else: word = word2id[sentence[i]] output[i-en1_en2_appear_2][0] = word test_x = [] test_x.append([output]) #Encoding test y test_y=[[0,1,2,3]] test_x = np.array(test_x) test_y = np.array(test_y) test_char = [] test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(test_x)): char = [] word = [] pos1 = [] pos2 = [] for j in test_x[i]: temp_char = [] temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_char = get_char_data_from_word_data(test_word) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) #print("test_word Matrix:") #print(test_word) #print("test_pos1 Matrix:") #print(test_pos1) #print("test_pos2 Matrix:") #print(test_pos2) prob, accuracy = test_step(test_char, test_word, test_pos1, test_pos2, test_y) prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0] #print("relationship: ") #print(prob) pre = prob.argsort()[-1:][::-1] for n, rel_id in enumerate(pre): out.write(id2relation[rel_id]+"(" + en1 + "," + en2 + ")"+"\n")
def main(_): args = get_args_parser() wordembedding = np.load('./data/vec.npy',allow_pickle=True) test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = get_relation_num(args.rel_id_file) test_settings.big_num = args.batch_size test_settings.regularizer = args.regularizer precision = {} with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings, session=sess) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) testlist = get_parameter() for model_iter in testlist: # for compatibility purposes only, name key changes from tf 0.x to 1.x, compat_layer saver.restore(sess, PATH_NAME_PREFIX + "-" + str(model_iter)) time_str = datetime.datetime.now().isoformat() print(time_str) print('Evaluating all test data and save data for PR curve') test_y = np.load('./data/testall_y.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') allprob = [] acc = [] for i in range(int(len(test_word) / float(test_settings.big_num))): prob, accuracy = mtest.process(test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append(np.mean(np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape(np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) print('saving all test result...') current_step = model_iter np.save('./out/allprob_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') # caculate the pr curve area average_precision = average_precision_score(allans, allprob) print(str(model_iter) + ' ' + 'PR curve area:' + str(average_precision)) precision[model_iter]=average_precision model_id = max(precision,key=precision.get) save_parameter(model_id)
def main(_): fout = open('extracted.json', 'w') data_out = {} # ATTENTION: change pathname before you load your model pathname = "./model/kbp/ATT_GRU_model-" test_model_id = int(sys.argv[1]) none_ind = utils.get_none_id('./origin_data/KBP/relation2id.txt') print("None index: ", none_ind) wordembedding = np.load('./data/KBP/vec.npy') test_y = np.load('./data/KBP/testall_y.npy') test_word = np.load('./data/KBP/testall_word.npy') test_pos1 = np.load('./data/KBP/testall_pos1.npy') test_pos2 = np.load('./data/KBP/testall_pos2.npy') test_o = np.load('./data/KBP/testall_o.npy') print(test_y[0]) test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = len(test_y[0]) test_settings.big_num = 179 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch, o_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, predictions, word_attention, sentence_attention, prob = sess.run( [ mtest.loss, mtest.accuracy, mtest.predictions, mtest.word_attention, mtest.sentence_attention, mtest.prob ], feed_dict) for i in range(len(word_batch)): new_out = {} pos1 = 0 pos2 = 0 for pos_ind in range(len(total_pos1[total_shape[i]])): if total_pos1[total_shape[i]][pos_ind] == 61: pos1 = pos_ind break for pos_ind in range(len(total_pos2[total_shape[i]])): if total_pos2[total_shape[i]][pos_ind] == 61: pos2 = pos_ind break entities_pair = o_batch[i][0][pos1] + ' ' + o_batch[i][0][ pos2] print(entities_pair) # p: predictions # a: accuracy # w: word_attention # s: sentence_attention # o: original sentence # t: true relation new_out['p'] = predictions[i].item() new_out['a'] = prob[i][predictions[i]].item() new_out['w'] = word_attention[ total_shape[i]:total_shape[i + 1]].tolist() new_out['s'] = sentence_attention[i][0].tolist() new_out['o'] = o_batch[i] new_out['t'] = np.argmax(y_batch[i], 0).item() data_out[entities_pair] = new_out return predictions, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() # ATTENTION: change the list to the iters you want to test !! # testlist = range(9025,14000,25) testlist = [test_model_id] for model_iter in testlist: saver.restore(sess, pathname + str(model_iter)) time_str = datetime.datetime.now().isoformat() print(time_str) all_pred = [] all_true = [] all_accuracy = [] for i in range( int(len(test_word) / float(test_settings.big_num))): predictions, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_o[i * test_settings.big_num:(i + 1) * test_settings.big_num]) pred = np.array(predictions) all_pred.append(pred) all_true.append(test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) all_accuracy.append(accuracy) all_pred = np.concatenate(all_pred, axis=0) all_true = np.concatenate(all_true, axis=0) accu = float(np.mean(all_accuracy)) all_true_inds = np.argmax(all_true, 1) precision, recall, f1 = utils.evaluate_rm_neg( all_pred, all_true_inds, none_ind) print( 'Accu = %.4f, F1 = %.4f, recall = %.4f, precision = %.4f)' % (accu, f1, recall, precision)) fout.write(json.dumps(data_out))
def main(_): starttime = datetime.datetime.now().isoformat() print('main函数开始执行' + starttime) # the path to save models save_path = './model/' print('reading wordembedding') wordembedding = np.load('./data/vec.npy') print('reading training data') train_y = np.load('./data/train_y.npy') train_word = np.load('./data/train_word.npy') train_pos1 = np.load('./data/train_pos1.npy') train_pos2 = np.load('./data/train_pos2.npy') settings = network.Settings() settings.vocab_size = len(wordembedding) settings.num_classes = len(train_y[0]) big_num = settings.big_num #你一旦开始你的任务,就已经有一个默认的图已经创建好了。 # 而且可以通过调用tf.get_default_graph()来访问到。 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): #训练以及加载数据并不费时,费时的是加载网络 initializer = tf.contrib.layers.xavier_initializer() # 返回一个用于定义创建variable(层)的op的上下文管理器。 time_str1 = datetime.datetime.now().isoformat() print(time_str1) with tf.variable_scope("model", reuse=None, initializer=initializer): m = network.GRU(is_training=True, word_embeddings=wordembedding, settings=settings) time_str2 = datetime.datetime.now().isoformat() print(time_str2) global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(0.0005) train_op = optimizer.minimize(m.final_loss, global_step=global_step) sess.run(tf.global_variables_initializer()) #我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数, #以便下次迭代的训练或者用作测试。Tensorflow针对这一需求提供了Saver类。 saver = tf.train.Saver(max_to_keep=None) #为了使用Tensorboard来可视化我们的数据, #我们会经常使用Summary,最终都会用一个简单的merge_all函数来管理我们的Summary #状态可视化,为了释放TensorBoard所使用的事件文件(events file), #所有的即时数据(在这里只有一个)都要在图表构建阶段合并至一个操作(op)中。 merged_summary = tf.summary.merge_all() #在创建好会话(session)之后,可以实例化一个tf.train.SummaryWriter, #用于写入包含了图表本身和即时数据具体值的事件文件。 summary_writer = tf.summary.FileWriter( FLAGS.summary_dir + '/train_loss', sess.graph) def train_step(word_batch, pos1_batch, pos2_batch, y_batch, big_num): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch temp, step, loss, accuracy, summary, l2_loss, final_loss = sess.run( [ train_op, global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss ], feed_dict) time_str = datetime.datetime.now().isoformat() accuracy = np.reshape(np.array(accuracy), (big_num)) acc = np.mean(accuracy) #状态可视化 #最后,每次运行summary_op时,都会往事件文件中写入最新的即时数据, #函数的输出会传入事件文件读写器(writer)的add_summary()函数。。 summary_writer.add_summary(summary, step) #if step % 50 == 0: #改成每一个step输出一条,最终结果如下 996/50 = 19,打乱分10次训练,所以是19*10 #2018-03-11T09:52:58.812304: step 190, softmax_loss 56.0816, acc 0.62 tempstr = "{}: step {}, softmax_loss {:g}, acc {:g}".format( time_str, step, loss, acc) print(tempstr) #self.num_epochs = 10 训练十回 for one_epoch in range(settings.num_epochs): #train_word为读取的narray数据 temp_order = list(range(len(train_word))) #shuffle为打乱顺序函数,打乱10次 np.random.shuffle(temp_order) #打乱顺序后按big_num=50一批分批次训练 for i in range(int(len(temp_order) / float(settings.big_num))): temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] #因为是按big_num=50一批分批次训练,所以 #从 i * settings.big_num #到(i + 1) * settings.big_num #temp_order为打乱顺序后的输入train_x列表 temp_input = temp_order[i * settings.big_num:(i + 1) * settings.big_num] #将输入的nparray赋成列表 for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) num = 0 #统计单个单词的字符数 for single_word in temp_word: num += len(single_word) if num > 1500: print('out of range') continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) #train_step的参数都是np.array数据类型,即数组类型 train_step(temp_word, temp_pos1, temp_pos2, temp_y, settings.big_num) current_step = tf.train.global_step(sess, global_step) #为了节省不必要的空间,现在的设定是8000以后的step才会每过100个step存储一次模型。 if current_step > 100 and current_step % 10 == 0: print('saving model') path = saver.save(sess, save_path + 'ATT_GRU_model', global_step=current_step) tempstr = 'have saved model to ' + path print(tempstr) #太早开始存储模型,早期的模型效果非常差,没有用处的;这就是为什么我设置8000以后才会存储模型。 #8000以后的每隔100存储的模型,可以互相比较效果取最好的,因为有可能太后期的模型又会overfit. #项目中的training data仅仅是示例,远远不够训练出一个可用的模型。 #step>8000是在足够多的训练集上训练时用到的参数。 endtime = datetime.datetime.now().isoformat() print(endtime)
def main(_): # def main_for_evaluation(): pathname = "./model/ATT_GRU_model-" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = 4 test_settings.big_num = 1 # test_settings.big_num = 96 #################注意修改 # test_settings.big_num=5561 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] # total_rel = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) # for rel in rel_batch[i]: # total_rel.append(rel) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) # total_rel = np.array(total_rel) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch # feed_dict[mtest.input_rel] = total_rel loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) # testlist = range(1000, 1800, 100) # testlist = [9000] # testlist = [110] # testlist = range(85,171,5) testlist = [340] print('reading word embedding data...') vec = [] word2id = {} f = open('./origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) print('reading relation to id') relation2id = {} id2relation = {} f = open('./origin_data/relation2id.txt', 'r', encoding='utf-8') while True: content = f.readline() if content == '': break content = content.strip().split() relation2id[content[0]] = int(content[1]) id2relation[int(content[1])] = content[0] f.close() for model_iter in testlist: print('model_iter', model_iter) # for compatibility purposes only, name key changes from tf 0.x to 1.x, compat_layer saver.restore(sess, pathname + str(model_iter)) time_str = datetime.datetime.now().isoformat() print('Evaluating all test data and save data for PR curve') test_y = np.load('./data/testall_y.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') # test_rel = np.load('./data/testall_rel.npy') allprob = [] # 概率 acc = [] result = [] # 每个句子最后预测的关系类别 # print(test_word) #每句中每个字对应的id数组 # print(len(test_word)) #结果为202 测试集中一共202条句子 # print(np.array(test_word)) for i in range(int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step(test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append(np.mean(np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape(np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: top3_id = single_prob.argsort()[-3:][::-1] a = top3_id[0] result.append(a) allprob.append(single_prob[1:]) result_dict = {} # 被正确判定的类别 test_rel_dict = {} # 测试集中每个类别的数量 f1 = open('./data/test_q&a.txt', 'r', encoding='utf-8') lines = f1.readlines() i = 0 for line in lines: line = line.strip().split() rel = int(line[3]) test_rel_dict[rel] = test_rel_dict.get(rel, 0) + 1 if rel == result[i]: result_dict[rel] = result_dict.get(rel, 0) + 1 i = i + 1 print('result_dict', result_dict) print('test_rel_dict', test_rel_dict) average_recall = 0 for key, value in result_dict.items(): print(id2relation[key] + '的recall为:' + str(value / test_rel_dict[key])) average_recall += value / test_rel_dict[key] print('平均recall为:' + str(average_recall / 4)) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) # print(allprob) print('saving all test result...') current_step = model_iter np.save('./out/allprob_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') # caculate the pr curve area # print(len(allans)) #保存的是测试集中所有关系向量数组中的后三个数字所组成的一行数组 结果为202*3=606 # print("----:") # print(len(allprob)) average_precision = average_precision_score(allans, allprob) print('PR curve area:' + str(average_precision)) precision, recall, _ = precision_recall_curve(allans, allprob) # In matplotlib < 1.5, plt.fill_between does not have a 'step' argument step_kwargs = ({'step': 'post'} if 'step' in signature(plt.fill_between).parameters else {}) plt.step(recall, precision, color='b', alpha=0.2, where='post') plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs) plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.0]) plt.xlim([0.2, 1.0]) plt.title(str(model_iter) + 'Precision-Recall curve: AP={0:0.4f}'.format( average_precision)) plt.figure(1) # plt.figure(1)是新建一个名叫 Figure1的画图窗口 plt.plot(precision, recall) plt.savefig('p-r1.png') plt.show() # def main(_): # If you retrain the model, please remember to change the path to your own model below: # pathname = "./model/ATT_GRU_model-9000" pathname = "./model/ATT_GRU_model-340" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = 4 test_settings.big_num = 1 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] # total_rel = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) # for rel in rel_batch[i]: # total_rel.append(rel) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) # total_rel = np.array(total_rel) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch # feed_dict[mtest.input_rel] = total_rel loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) print('reading word embedding data...') vec = [] word2id = {} f = open('./origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) print('reading relation to id') relation2id = {} id2relation = {} f = open('./origin_data/relation2id.txt', 'r', encoding='utf-8') while True: content = f.readline() if content == '': break content = content.strip().split() relation2id[content[0]] = int(content[1]) id2relation[int(content[1])] = content[0] f.close() # try: # BUG: Encoding error if user input directly from command line. line = input('请输入中文句子,格式为 "name1 name2 sentence":') # Read file from test file ''' infile = open('test.txt', encoding='utf-8') line = '' for orgline in infile: line = orgline.strip() break infile.close() ''' ''' infile = open('./origin_data/test.txt', encoding='utf-8') line = '' for orgline in infile: line = orgline.strip() break infile.close() ''' en1, en2, sentence = line.strip().split() print("实体1: " + en1) print("实体2: " + en2) print(sentence) relation = 0 en1pos = sentence.find(en1) # 找到实体在句子中的位置 if en1pos == -1: en1pos = 0 en2pos = sentence.find(en2) if en2pos == -1: en2post = 0 output = [] # length of sentence is 70 fixlen = 70 # max length of position embedding is 60 (-60~+60) maxlen = 60 # Encoding test x 得到输入句子中每个字的id和距离两个实体的距离 for i in range(fixlen): word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) for i in range(min(fixlen, len(sentence))): word = 0 if sentence[i] not in word2id: # print(sentence[i]) # print('==') word = word2id['UNK'] # print(word) else: # print(sentence[i]) # print('||') word = word2id[sentence[i]] # print(word) output[i][0] = word test_x = [] test_x.append([output]) # print('test_x',test_x) # Encoding test y label = [0 for i in range(len(relation2id))] label[0] = 1 test_y = [] test_y.append(label) # print('test_y',test_y) test_x = np.array(test_x) # print('123') # print(test_x) test_y = np.array(test_y) # print(test_y) test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(test_x)): word = [] pos1 = [] pos2 = [] for j in test_x[i]: temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) # print("test_word Matrix:") # print(test_word) # print("test_pos1 Matrix:") # print(test_pos1) # print("test_pos2 Matrix:") # print(test_pos2) prob, accuracy = test_step(test_word, test_pos1, test_pos2, test_y) prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0] print("关系是:") # print(prob) top3_id = prob.argsort()[-3:][::-1] for n, rel_id in enumerate(top3_id): print("No." + str(n + 1) + ": " + id2relation[rel_id] + ", Probability is " + str(prob[rel_id]))
def test(en1, en2, rel, sentence): print(en1) print(en2) # embedding the position def pos_embed(x): if x < -60: return 0 if x >= -60 and x <= 60: return x + 61 if x > 60: return 122 # find the index of x in y, if x not in y, return -1 def find_index(x, y): flag = -1 for i in range(len(y)): if x != y[i]: continue else: return i return flag print 'reading word embedding data...' vec = [] word2id = {} id2word = [] f = open( '/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/origin_data/vec.txt' ) f.readline() i = 0 while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) ## word list in dic 1,2, 3 .. id2word.append(content[0]) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) dim = 50 print 'reading relation to id' relation2id = {} id2relation = [] f = open( '/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/origin_data/relation2id.txt', 'r') while True: content = f.readline() if content == '': break content = content.strip().split() relation2id[content[0]] = int(content[1]) id2relation.append(content[0]) f.close() # length of sentence is 70 fixlen = 70 # max length of position embedding is 60 (-60~+60) maxlen = 60 train_sen = { } # {entity pair:[[[label1-sentence 1],[label1-sentence 2]...],[[label2-sentence 1],[label2-sentence 2]...]} train_ans = { } # {entity pair:[label1,label2,...]} the label is one-hot vector print('reading test data ...') test_sen = {} # {entity pair:[[sentence 1],[sentence 2]...]} test_ans = { } # {entity pair:[labels,...]} the labels is N-hot vector (N is the number of multi-label) # f = open('/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/origin_data/my_test.txt', 'r') en1 = en1 en2 = en2 # positions of 2 entities imp: entities must be sanke_cased fixed from stanford nlp OPEN ER relation = 0 if rel not in relation2id: relation = relation2id['NA'] else: relation = relation2id[rel] tup = (en1, en2) # ('aaa', 'bbb') if tup not in test_sen: # put the same entity pair sentences into a dict # {('aaa', 'bbb'): [[11, 12, 13, 0.0, 0.0], [21, 22, 23, 0.0, 0.0]]} test_sen[tup] = [] y_id = relation label_tag = 0 label = [0 for i in range(len(relation2id))] label[y_id] = 1 test_ans[tup] = label else: y_id = relation test_ans[tup][y_id] = 1 sentence = sentence.strip().split() # print ("sentence:"+ sentence) en1pos = 0 en2pos = 0 for i in range(len(sentence)): # print("sentence i:",sentence[i]) if sentence[i] == en1: print("en1:", en1) en1pos = i if sentence[i] == en2: print("en2:", en2) en2pos = i output = [] for i in range(fixlen): word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) for i in range(min(fixlen, len(sentence))): word = 0 if sentence[i] not in word2id: word = word2id['UNK'] else: word = word2id[sentence[i]] output[i][0] = word test_sen[tup].append(output) print(test_sen) # numpy arrys without tuple id's [[11, 12, 13, 0.0, 0.0], [21, 22, 23, 0.0, 0.0]] train_x = [] train_y = [] test_x = [] test_y = [] print(train_x) # print 'organizing train data' f = open( '/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/data/my_train_q&a.txt', 'w') temp = 0 for i in train_sen: if len(train_ans[i]) != len(train_sen[i]): print 'ERROR' lenth = len(train_ans[i]) for j in range(lenth): train_x.append(train_sen[i][j]) train_y.append(train_ans[i][j]) f.write( str(temp) + '\t' + i[0] + '\t' + i[1] + '\t' + str(np.argmax(train_ans[i][j])) + '\n') temp += 1 f.close() # print 'organizing test data' f = open( '/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/data/my_test_q&a.txt', 'w') temp = 0 for i in test_sen: test_x.append(test_sen[i]) test_y.append(test_ans[i]) tempstr = '' for j in range(len(test_ans[i])): if test_ans[i][j] != 0: tempstr = tempstr + str(j) + '\t' f.write(str(temp) + '\t' + i[0] + '\t' + i[1] + '\t' + tempstr + '\n') temp += 1 f.close() test_x = np.array(test_x) test_y = np.array(test_y) pall_test_x = [] pall_test_y = [] for i in range(len(test_x)): print("len test_x[i]" + str(len(test_x[i]))) if len(test_x[i]) > 0: pall_test_x.append(test_x[i]) pall_test_y.append(test_y[i]) pall_test_x = np.array(pall_test_x) pall_test_y = np.array(pall_test_y) # print 'reading p-all test data' x_test = pall_test_x # print 'seperating p-all test data' test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(x_test)): word = [] pos1 = [] pos2 = [] for j in x_test[i]: temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) # np.save('/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/data/gpall_test_word.npy', test_word) # np.save('/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/data/gpall_test_pos1.npy', test_pos1) # np.save('/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/data/gpall_test_pos2.npy', test_pos2) # np.save('/Users/samitha/Documents/Project/new_may01/TensorFlow-NRE-master/data/gpall_test_y.npy', pall_test_y) # ATTENTION: change pathname before you load your model pathname = os.path.normpath( os.path.join(SCRIPT_DIR, '../nre/model/ATT_GRU_model-')) wordembedding = np.load( os.path.normpath(os.path.join(SCRIPT_DIR, '../nre/data/vec.npy'))) test_settings = network.Settings() test_settings.vocab_size = 114044 test_settings.num_classes = 53 test_settings.big_num = 1 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob, predictions = sess.run([ mtest.loss, mtest.accuracy, mtest.prob, mtest.predictions ], feed_dict) return prob, accuracy, predictions # evaluate p@n def eval_pn(test_y, test_word, test_pos1, test_pos2, test_settings): allprob = [] acc = [] print("test word length") print(len(test_word)) for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy, predictions = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) print("predictions" + str(id2relation[predictions[0]])) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) eval_y = [] for i in test_y: eval_y.append(i[1:]) allans = np.reshape(eval_y, (-1)) order = np.argsort(-allprob) # print 'P@100:' # top100 = order[:100] # correct_num_100 = 0.0 # for i in top100: # if allans[i] == 1: # correct_num_100 += 1.0 # print correct_num_100/100 # # print 'P@200:' # top200 = order[:200] # correct_num_200 = 0.0 # for i in top200: # if allans[i] == 1: # correct_num_200 += 1.0 # print correct_num_200/200 # # print 'P@300:' # top300 = order[:300] # correct_num_300 = 0.0 # for i in top300: # if allans[i] == 1: # correct_num_300 += 1.0 # print correct_num_300/300 return id2relation[predictions[0]] with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() # testlist = [10900] model_iter = 10900 # for model_iter in testlist: saver.restore(sess, pathname + str(model_iter)) print("Evaluating P@N for iter " + str(model_iter)) print 'Evaluating P@N for all' test_y = pall_test_y # test_word = np.load('./data/gpall_test_word.npy') # # test_pos1 = np.load('./data/gpall_test_pos1.npy') # test_pos2 = np.load('./data/gpall_test_pos2.npy') print(test_word) ggwp = eval_pn(test_y, test_word, test_pos1, test_pos2, test_settings) time_str = datetime.datetime.now().isoformat() print time_str return ggwp # tf.app.run() # print("sddssdssdsd") # print(test("Pitabaddra", "dnkoluwaththa", "/location/location/contains","400 victims in dnkoluwaththa in Pitabaddra . Not enough food to eat. Very hard situation there"))
def main(_): #If you retrain the model, please remember to change the path to your own model below: pathname = "./model/ATT_GRU_model-14000" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 16693 test_settings.num_classes = 12 test_settings.big_num = 1 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) print('reading word embedding data...') vec = [] word2id = {} f = open('./origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) print('reading relation to id') relation2id = {} id2relation = {} f = open('./origin_data/relation2id.txt', 'r', encoding='utf-8') reader = csv.reader(f) for row in reader: relation2id[row[0]] = int(row[1]) id2relation[int(row[1])] = row[0] print(relation2id) print(id2relation) f.close() infile = open('../Evaluator/BookKnowledge.csv', encoding='utf-8') outfile = open('../Evaluator/CalculationResult.csv', 'w', encoding='utf-8') line = '' reader = csv.reader(infile) cnt = 0 correct = 0 for line in reader: en1, en2, sentence = line[0], line[1], line[2] # print("实体1: " + en1) # print("实体2: " + en2) print(cnt) # print(sentence) relation = 0 en1pos = sentence.find(en1) if en1pos == -1: en1pos = 0 en2pos = sentence.find(en2) if en2pos == -1: en2post = 0 output = [] # length of sentence is 70 fixlen = 70 # max length of position embedding is 60 (-60~+60) maxlen = 60 #Encoding test x for i in range(fixlen): word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) for i in range(min(fixlen, len(sentence))): word = 0 if sentence[i] not in word2id: #print(sentence[i]) #print('==') word = word2id['UNK'] #print(word) else: #print(sentence[i]) #print('||') word = word2id[sentence[i]] #print(word) output[i][0] = word test_x = [] test_x.append([output]) #Encoding test y label = [0 for i in range(len(relation2id))] label[0] = 1 test_y = [] test_y.append(label) test_x = np.array(test_x) test_y = np.array(test_y) test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(test_x)): word = [] pos1 = [] pos2 = [] for j in test_x[i]: temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) prob, accuracy = test_step(test_word, test_pos1, test_pos2, test_y) prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0] # print("关系是:") #print(prob) cnt += 1 top3_id = prob.argsort()[-3:][::-1] for n, rel_id in enumerate(top3_id): if n == 0 and prob[rel_id] > 0.99: outcontent = '\"' + line[3] + '\",\"' + line[0] + '\",\"' + line[1] + '\",\"' + id2relation[rel_id] + '\",\"' + str(prob[rel_id]) + '\",\"' + line[2] + '\"' outfile.write(outcontent + '\n') break
def main(_): pathname = "./model/ATT_GRU_model-1700" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 16693 test_settings.num_classes = 12 test_settings.big_num = 1 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) print('reading word embedding data...') vec = [] word2id = {} f = open('./origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) print('reading relation to id') relation2id = {} id2relation = {} f = open('./origin_data/relation2id.txt', 'r', encoding='utf-8') while True: content = f.readline() if content == '': break content = content.strip().split() relation2id[content[0]] = int(content[1]) id2relation[int(content[1])] = content[0] f.close() import xlwt book = xlwt.Workbook() sheet1 = book.add_sheet('sheet1', cell_overwrite_ok=True) heads = [u'entity1', u'entity2', u'relation', u'sentence'] ii = 0 for head in heads: sheet1.write(0, ii, head) ii += 1 excel_i = 1 import xlrd data = xlrd.open_workbook("BatchProcess2.xls") table = data.sheet_by_name(u'sheet1') # 通过名称获取 nrows = table.nrows ncols = table.ncols #print(nrows) #print(ncols) for i in range(nrows): #print(table.row_values(i)) #with open('./BatchProcess2.txt', encoding='utf-8') as f: #for line in f: try: #en1, en2, sentence = line.strip().split() en1 = table.row_values(i)[1] en2 = table.row_values(i)[3] sentence = table.row_values(i)[6] #print("实体1: " + en1) #print("实体2: " + en2) #print(sentence) relation = 0 en1pos = sentence.find(en1) if en1pos == -1: en1pos = 0 en2pos = sentence.find(en2) if en2pos == -1: en2post = 0 output = [] # length of sentence is 70 fixlen = 70 # max length of position embedding is 60 (-60~+60) maxlen = 60 # Encoding test x for i in range(fixlen): word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) for i in range(min(fixlen, len(sentence))): word = 0 if sentence[i] not in word2id: # print(sentence[i]) # print('==') word = word2id['UNK'] # print(word) else: # print(sentence[i]) # print('||') word = word2id[sentence[i]] # print(word) output[i][0] = word test_x = [] test_x.append([output]) # Encoding test y label = [0 for i in range(len(relation2id))] label[0] = 1 test_y = [] test_y.append(label) test_x = np.array(test_x) test_y = np.array(test_y) test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(test_x)): word = [] pos1 = [] pos2 = [] for j in test_x[i]: temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) # print("test_word Matrix:") # print(test_word) # print("test_pos1 Matrix:") # print(test_pos1) # print("test_pos2 Matrix:") # print(test_pos2) prob, accuracy = test_step(test_word, test_pos1, test_pos2, test_y) prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0] #print("关系是:") # print(prob) top3_id = prob.argsort()[-3:][::-1] for n, rel_id in enumerate(top3_id): if n == 0: sheet1.write(excel_i, 0, en1) sheet1.write(excel_i, 1, en2) sheet1.write(excel_i, 2, id2relation[rel_id]) sheet1.write(excel_i, 3, sentence) excel_i += 1 print(excel_i) #print("No." + str(n + 1) + ": " + id2relation[rel_id] + ", Probability is " + str(prob[rel_id])) except: pass book.save('Prediction3.xls') print('保存成功')
def CRET(test_data): pathname = "./model/ATT_GRU_model-15000" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 16693 test_settings.num_classes =49 test_settings.big_num = 1 schemas_path = './test/schemas.json' # save_path = './test/valid_final_result.json' # database = [] # with open(test_path, 'r', encoding='utf8')as f: # lines = f.readlines() # for line in lines: # database.append(json.loads(line)) # total_test = len(database) # print("一共有%d个测试样本" % total_test) with open(schemas_path, 'r', encoding='utf8') as f: schemas = json.load(f) # 定义一个相近的实体类别的字典 similar_entity_dict = {"历史人物" : "人物", "人物" : "历史人物", "书籍" : "图书作品", "图书作品" : "书籍", "影视作品" : "电视综艺", "电视综艺" : "影视作品" } with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) # print('reading word embedding data...') vec = [] word2id = {} f = open('./data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() # dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) # print('reading relation to id') relation2id = {} id2relation = {} f = open('./data/relation2id.txt', 'r', encoding='utf-8') while True: content = f.readline() if content == '': break content = content.strip().split() relation2id[content[0]] = int(content[1]) id2relation[int(content[1])] = content[0] f.close() #################################################################### sentence = "".join(test_data['string'].lower().split()) entities_dict = {entity['word'] : entity['type'] for entity in test_data['entities']} couples = [(x,y) for x in entities_dict.keys() for y in entities_dict.keys() if x != y] new_couples = [] for (x,y) in couples: if (y,x) not in new_couples: new_couples.append((x,y)) spo_list = [] for (en1, en2) in new_couples: # en1, en2, sentence = line.strip().split() #print("实体1: " + en1) #print("实体2: " + en2) #print("句子:" + sentence) #relation = 0 en1pos = sentence.find(en1) if en1pos == -1: en1pos = 0 en2pos = sentence.find(en2) if en2pos == -1: en2post = 0 output = [] # length of sentence is 70 fixlen = 70 # max length of position embedding is 60 (-60~+60) maxlen = 60 #Encoding test x for i in range(fixlen): word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) for i in range(min(fixlen, len(sentence))): word = 0 if sentence[i] not in word2id: #print(sentence[i]) #print('==') word = word2id['UNK'] #print(word) else: #print(sentence[i]) #print('||') word = word2id[sentence[i]] #print(word) output[i][0] = word test_x = [] test_x.append([output]) #Encoding test y label = [0 for i in range(len(relation2id))] label[0] = 1 test_y = [] test_y.append(label) test_x = np.array(test_x) test_y = np.array(test_y) test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(test_x)): word = [] pos1 = [] pos2 = [] for j in test_x[i]: temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) prob, accuracy = test_step(test_word, test_pos1, test_pos2, test_y) prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0] #print("关系是:") # print(prob) top3_id = prob.argsort()[-3:][::-1] rel_id = prob.argsort()[-1:][::-1][0] #print(id2relation[rel_id] + ", Probability is " + str(prob[rel_id])) rel = id2relation[rel_id] prob = prob[rel_id] rel_dict = {} en1_type = entities_dict[en1] en2_type = entities_dict[en2] if prob > 0.9: process_Result(en1, en2, en1_type, en2_type, schemas, rel_dict, rel) if not rel_dict: if en1_type in similar_entity_dict: en1_type = similar_entity_dict[en1_type] process_Result(en1, en2, en1_type, en2_type, schemas, rel_dict, rel) if not rel_dict and en2_type in similar_entity_dict: en2_type = similar_entity_dict[en2_type] process_Result(en1, en2, en1_type, en2_type, schemas, rel_dict, rel) elif en2_type in similar_entity_dict: en2_type = similar_entity_dict[en2_type] process_Result(en1, en2, en1_type, en2_type, schemas, rel_dict, rel) if rel_dict: spo_list.append(rel_dict) print({"text":sentence, "spo_list":spo_list})
def main(_): args = get_args_parser() model_id = get_parameter(MODEL_ID_FILE) pathname = PATH_NAME_PREFIX + "-" + str(model_id) wordembedding = np.load('./data/vec.npy') predict_settings = network.Settings() predict_settings.vocab_size = len(wordembedding) predict_settings.num_classes = get_relation_num(args.rel_id_file) predict_settings.big_num = 1 predict_settings.num_steps = args.max_sentence_len predict_settings.regularizer = args.regularizer with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): with tf.variable_scope("model"): mpredict = network.GRU(is_training=False, word_embeddings=wordembedding, settings=predict_settings, session=sess) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) word2id = get_word_id(args.vec_file) relation2id = get_relation_id(args.rel_id_file) id2relation = get_id_relation(args.rel_id_file) input_data = codecs.open(args.pre_file, 'r', 'utf-8') # 删除预测的结果文件 if os.path.exists(args.result_file): os.remove(args.result_file) for line in input_data.readlines(): line = line.strip() pos_embedding = mpredict.get_pos_embedding(line, word2id) batch = mpredict.get_batch(pos_embedding, relation2id) prob, accuracy = mpredict.process(batch.word_batch, batch.pos1_batch, batch.pos2_batch, batch.relation_batch) prob = np.reshape(np.array(prob), (1, predict_settings.num_classes))[0] top1_id = prob.argsort()[-1:] rel_id = top1_id[0] en1, en2, _ = line.strip().split() write_to_file(args.result_file, en1, en2, id2relation[rel_id]) input_data.close()
def main(_): # the path to save models save_path = './model/' print 'reading wordembedding' wordembedding = np.load('./data/vec.npy') print 'reading training data' train_y = np.load('./data/small_y.npy') train_word = np.load('./data/small_word.npy') train_pos1 = np.load('./data/small_pos1.npy') train_pos2 = np.load('./data/small_pos2.npy') settings = network.Settings() settings.vocab_size = len(wordembedding) settings.num_classes = len(train_y[0]) big_num = settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): initializer = tf.contrib.layers.xavier_initializer() with tf.variable_scope("model", reuse=None, initializer = initializer): m = network.GRU(is_training=True, word_embeddings = wordembedding, settings = settings) global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(0.001) #train_op=optimizer.minimize(m.total_loss,global_step=global_step) train_op=optimizer.minimize(m.final_loss,global_step=global_step) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver(max_to_keep=None) #merged_summary = tf.summary.merge_all() merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.summary_dir+'/train_loss',sess.graph) #summary for embedding #it's not available in tf 0.11,(because there is no embedding panel in 0.11's tensorboard) so I delete it =.= #you can try it on 0.12 or higher versions but maybe you should change some function name at first. # summary_embed_writer = tf.train.SummaryWriter('./model',sess.graph) # config = projector.ProjectorConfig() # embedding_conf = config.embedding.add() # embedding_conf.tensor_name = 'word_embedding' # embedding_conf.metadata_path = './data/metadata.tsv' # projector.visualize_embeddings(summary_embed_writer, config) def train_step(word_batch, pos1_batch, pos2_batch, y_batch,big_num): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch temp, step, loss, accuracy,summary,l2_loss,final_loss= sess.run([train_op, global_step, m.total_loss, m.accuracy,merged_summary,m.l2_loss,m.final_loss], feed_dict) time_str = datetime.datetime.now().isoformat() accuracy = np.reshape(np.array(accuracy),(big_num)) acc = np.mean(accuracy) summary_writer.add_summary(summary,step) if step % 50 == 0: tempstr = "{}: step {}, softmax_loss {:g}, acc {:g}".format(time_str, step, loss, acc) print(tempstr) if itchat_run: itchat.send(tempstr,FLAGS.wechat_name) for one_epoch in range(settings.num_epochs): if itchat_run: itchat.send('epoch '+str(one_epoch)+' starts!',FLAGS.wechat_name) temp_order = range(len(train_word)) np.random.shuffle(temp_order) for i in range(int(len(temp_order)/float(settings.big_num))): temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] temp_input = temp_order[i*settings.big_num:(i+1)*settings.big_num] for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) num = 0 for single_word in temp_word: num += len(single_word) if num > 1500: print 'out of range' continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) train_step(temp_word,temp_pos1,temp_pos2,temp_y,settings.big_num) current_step = tf.train.global_step(sess, global_step) if current_step > 9000 and current_step%500==0: #if current_step == 50: print 'saving model' path = saver.save(sess,save_path +'ATT_GRU_model',global_step=current_step) tempstr = 'have saved model to '+path print tempstr if itchat_run: itchat.send('training has been finished!',FLAGS.wechat_name)
def main(_): my_env = os.environ.copy() my_env["CUDA_VISIBLE_DEVICES"] = "3" # the path to save models save_path = './model/kbp/' print('reading wordembedding') wordembedding = np.load('./data/KBP/vec.npy') print('reading training data') train_y = np.load('./data/KBP/train_y.npy') train_word = np.load('./data/KBP/train_word.npy') train_pos1 = np.load('./data/KBP/train_pos1.npy') train_pos2 = np.load('./data/KBP/train_pos2.npy') none_ind = utils.get_none_id('./origin_data/KBP/relation2id.txt') print("None index: ", none_ind) settings = network.Settings() settings.vocab_size = len(wordembedding) settings.num_classes = len(train_y[0]) print("vocab_size: ", settings.vocab_size) print("num_classes: ", settings.num_classes) best_f1 = float('-inf') best_recall = 0 best_precision = 0 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): initializer = tf.contrib.layers.xavier_initializer() with tf.variable_scope("model", reuse=None, initializer=initializer): m = network.GRU(is_training=True, word_embeddings=wordembedding, settings=settings) global_step = tf.Variable(0, name="global_step", trainable=False) # optimizer = tf.train.GradientDescentOptimizer(0.001) optimizer = tf.train.AdamOptimizer(0.001) # train_op=optimizer.minimize(m.total_loss,global_step=global_step) train_op = optimizer.minimize(m.final_loss, global_step=global_step) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=None) # merged_summary = tf.summary.merge_all() merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter( FLAGS.summary_dir + '/train_loss', sess.graph) # summary for embedding # it's not available in tf 0.11, # (because there is no embedding panel in 0.11's tensorboard) so I delete it =.= # you can try it on 0.12 or higher versions but maybe you should change some function name at first. # summary_embed_writer = tf.train.SummaryWriter('./model',sess.graph) # config = projector.ProjectorConfig() # embedding_conf = config.embedding.add() # embedding_conf.tensor_name = 'word_embedding' # embedding_conf.metadata_path = './data/metadata.tsv' # projector.visualize_embeddings(summary_embed_writer, config) def train_step(word_batch, pos1_batch, pos2_batch, y_batch, big_num): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch temp, step, loss, accuracy, summary, l2_loss, final_loss, debug_output_forward, debug_prob = sess.run( [ train_op, global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss, m.output_forward, m.prob ], feed_dict) if step % 50 == 0: accuracy = np.reshape(np.array(accuracy), big_num) acc = np.mean(accuracy) print(debug_output_forward[0][1]) print('\n') print('step ' + str(step) + ' with loss ' + str(loss) + ' acc ' + str(acc) + '\n') label_not_NA_num = 0 for i in y_batch: if i[0] != 1: label_not_NA_num += 1 print('not NA num : ' + str(label_not_NA_num) + '\n') return step, loss, accuracy # training process for one_epoch in range(settings.num_epochs): print("Starting Epoch: ", one_epoch) epoch_loss = 0 temp_order = list(range(len(train_word))) np.random.shuffle(temp_order) all_prob = [] all_true = [] all_accuracy = [] for i in range(int(len(temp_order) / float(settings.big_num))): temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] temp_input = temp_order[i * settings.big_num:(i + 1) * settings.big_num] for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) num = 0 for single_word in temp_word: num += len(single_word) if num > 1500: print('out of range') continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) step, loss, accuracy = train_step(temp_word, temp_pos1, temp_pos2, temp_y, settings.big_num) epoch_loss += loss all_accuracy.append(accuracy) all_true.append(temp_y) accu = np.mean(all_accuracy) print("Epoch finished, loss:, ", epoch_loss, "accu: ", accu) # all_prob = np.concatenate(all_prob, axis=0) # all_true = np.concatenate(all_true, axis=0) # # all_pred_inds = utils.calcInd(all_prob) # entropy = utils.calcEntropy(all_prob) # all_true_inds = np.argmax(all_true, 1) # f1score, recall, precision, meanBestF1 = utils.CrossValidation(all_pred_inds, entropy, # all_true_inds, none_ind) # print('F1 = %.4f, recall = %.4f, precision = %.4f, val f1 = %.4f)' % # (f1score, # recall, # precision, # meanBestF1)) print('saving model') current_step = tf.train.global_step(sess, global_step) path = saver.save(sess, save_path + 'ATT_GRU_model', global_step=current_step) print(path)
def test_GRU(agent): # ATTENTION: change pathname before you load your model pathname = "./model/%sATT_GRU_model-" % agent wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 114044 test_settings.num_classes = 53 test_settings.big_num = 262 * 9 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) #print np.shape(total_shape) print np.shape(total_word) #print np.shape(total_pos1) #print np.shape(total_pos2) #print np.shape(y_batch) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() # ATTENTION: change the list to the iters you want to test !! #testlist = range(9025,14000,25) testlist = [3000] for model_iter in testlist: saver.restore(sess, pathname + str(model_iter)) print("Evaluating P@N for iter " + str(model_iter)) test_y = np.load("./data/testall_y.npy") test_word = np.load("./data/testall_word.npy") test_pos1 = np.load("./data/testall_pos1.npy") test_pos2 = np.load("./data/testall_pos2.npy") print np.shape(test_word) allprob = [] acc = [] for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) print np.shape(prob) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) print 'saving all test result...' current_step = model_iter # ATTENTION: change the save path before you save your result !! np.save( './out/%sallprob_iter_' % agent + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') #print np.shape(allans) #caculate the pr curve area average_precision = average_precision_score(allans, allprob) print '%s PR curve area:' % agent + str(average_precision) time_str = datetime.datetime.now().isoformat() print time_str
def main(_): # ATTENTION: change pathname before you load your model pathname = "./model/MT_GRU_model-" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 114044 test_settings.num_classes = 53 test_settings.big_num = 262 * 9 big_num_test = test_settings.big_num with tf.Graph().as_default(): config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) #sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, ab_pos1_batch, ab_pos2_batch, y_batch, y_head_batch, y_tail_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] total_ab_pos1 = [] total_ab_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) for ab_pos1 in ab_pos1_batch[i]: total_ab_pos1.append(ab_pos1) for ab_pos2 in ab_pos2_batch[i]: total_ab_pos2.append(ab_pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) total_ab_pos1 = np.array(total_ab_pos1) total_ab_pos2 = np.array(total_ab_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.absolute_pos1] = total_ab_pos1 feed_dict[mtest.absolute_pos2] = total_ab_pos2 feed_dict[mtest.input_y] = y_batch feed_dict[mtest.input_y_head] = y_head_batch feed_dict[mtest.input_y_tail] = y_tail_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy # evaluate p@n def eval_pn(test_y, test_y_head, test_y_tail, test_word, test_pos1, test_pos2, test_ab_pos1, test_ab_pos2, test_settings): allprob = [] acc = [] for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_ab_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_ab_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y_head[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y_tail[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) eval_y = [] for i in test_y: eval_y.append(i[1:]) allans = np.reshape(eval_y, (-1)) order = np.argsort(-allprob) print 'P@100:' top100 = order[:100] correct_num_100 = 0.0 for i in top100: if allans[i] == 1: correct_num_100 += 1.0 print correct_num_100 / 100 print 'P@200:' top200 = order[:200] correct_num_200 = 0.0 for i in top200: if allans[i] == 1: correct_num_200 += 1.0 print correct_num_200 / 200 print 'P@300:' top300 = order[:300] correct_num_300 = 0.0 for i in top300: if allans[i] == 1: correct_num_300 += 1.0 print correct_num_300 / 300 if itchat_run: tempstr = 'P@100\n' + str( correct_num_100 / 100) + '\n' + 'P@200\n' + str( correct_num_200 / 200) + '\n' + 'P@300\n' + str( correct_num_300 / 300) itchat.send(tempstr, FLAGS.wechat_name) with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() # ATTENTION: change the list to the iters you want to test !! #testlist = range(9100,15000,100) #testlist = [9500,10000,10500,11000,11500,12000,12500,13000,13500,14000] testlist = [9300] for model_iter in testlist: saver.restore(sess, pathname + str(model_iter)) print("Evaluating P@N for iter " + str(model_iter)) if itchat_run: itchat.send("Evaluating P@N for iter " + str(model_iter), FLAGS.wechat_name) print 'Evaluating P@N for one' #if itchat_run: # itchat.send('Evaluating P@N for one',FLAGS.wechat_name) test_y = np.load('./data/pone_test_y.npy') test_y_head = np.load('./data/pone_test_y_head.npy') test_y_tail = np.load('./data/pone_test_y_tail.npy') test_word = np.load('./data/pone_test_word.npy') test_pos1 = np.load('./data/pone_test_pos1.npy') test_pos2 = np.load('./data/pone_test_pos2.npy') test_ab_pos1 = np.load('./data/pone_test_ab_pos1.npy') test_ab_pos2 = np.load('./data/pone_test_ab_pos2.npy') eval_pn(test_y, test_y_head, test_y_tail, test_word, test_pos1, test_pos2, test_ab_pos1, test_ab_pos2, test_settings) print 'Evaluating P@N for two' #if itchat_run: # itchat.send('Evaluating P@N for two',FLAGS.wechat_name) test_y = np.load('./data/ptwo_test_y.npy') test_y_head = np.load('./data/ptwo_test_y_head.npy') test_y_tail = np.load('./data/ptwo_test_y_tail.npy') test_word = np.load('./data/ptwo_test_word.npy') test_pos1 = np.load('./data/ptwo_test_pos1.npy') test_pos2 = np.load('./data/ptwo_test_pos2.npy') test_ab_pos1 = np.load('./data/ptwo_test_ab_pos1.npy') test_ab_pos2 = np.load('./data/ptwo_test_ab_pos2.npy') eval_pn(test_y, test_y_head, test_y_tail, test_word, test_pos1, test_pos2, test_ab_pos1, test_ab_pos2, test_settings) # #print 'Evaluating P@N for all' #if itchat_run: # itchat.send('Evaluating P@N for all',FLAGS.wechat_name) #test_y = np.load('./data/pall_test_y.npy') #test_y_head = np.load('./data/pall_test_y_head.npy') #test_y_tail = np.load('./data/pall_test_y_tail.npy') #test_word = np.load('./data/pall_test_word.npy') #test_pos1 = np.load('./data/pall_test_pos1.npy') #test_pos2 = np.load('./data/pall_test_pos2.npy') #test_ab_pos1 = np.load('./data/test_ab_pos1.npy') #test_ab_pos2 = np.load('./data/test_ab_pos2.npy') #eval_pn(test_y,test_y_head,test_y_tail,test_word,test_pos1,test_pos2, test_ab_pos1,test_ab_pos2,test_settings) time_str = datetime.datetime.now().isoformat() print time_str print 'Evaluating all test data and save data for PR curve' if itchat_run: itchat.send( 'Evaluating all test data and save data for PR curve', FLAGS.wechat_name) test_y = np.load('./data/testall_y.npy') test_y_head = np.load('./data/test_new_y_head.npy') test_y_tail = np.load('./data/test_new_y_tail.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') test_ab_pos1 = np.load('./data/test_ab_pos1.npy') test_ab_pos2 = np.load('./data/test_ab_pos2.npy') allprob = [] acc = [] for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_ab_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_ab_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y_head[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y_tail[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) print 'saving all test result...' current_step = model_iter # ATTENTION: change the save path before you save your result !! np.save( './out/allprob_final_mt_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') #caculate the pr curve area average_precision = average_precision_score(allans, allprob) print 'PR curve area:' + str(average_precision) if itchat_run: itchat.send('PR curve area:' + str(average_precision), FLAGS.wechat_name) time_str = datetime.datetime.now().isoformat() print time_str print 'P@N for all test data:' print 'P@100:' top100 = order[:100] correct_num_100 = 0.0 for i in top100: if allans[i] == 1: correct_num_100 += 1.0 print correct_num_100 / 100 print 'P@200:' top200 = order[:200] correct_num_200 = 0.0 for i in top200: if allans[i] == 1: correct_num_200 += 1.0 print correct_num_200 / 200 print 'P@300:' top300 = order[:300] correct_num_300 = 0.0 for i in top300: if allans[i] == 1: correct_num_300 += 1.0 print correct_num_300 / 300 if itchat_run: tempstr = 'P@100\n' + str( correct_num_100 / 100) + '\n' + 'P@200\n' + str( correct_num_200 / 200) + '\n' + 'P@300\n' + str( correct_num_300 / 300) itchat.send(tempstr, FLAGS.wechat_name)
def main(_): # ATTENTION: change pathname before you load your model pathname = "./model/kbp/ATT_GRU_model-" predict_pathname = './out/' if not os.path.exists(predict_pathname): os.makedirs(predict_pathname) test_model_id = int(sys.argv[1]) #none_ind = utils.get_none_id('./origin_datarelation2id.txt') #print("None index: ", none_ind) wordembedding = np.load('./data/vec.npy') test_y = np.load('./data/testall_y_submit.npy') test_word = np.load('./data/testall_word_submit.npy') test_pos1 = np.load('./data/testall_pos1_submit.npy') test_pos2 = np.load('./data/testall_pos2_submit.npy') test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = len(test_y[0]) #test_settings.big_num = len(test_y) print(test_word.shape) test_settings.big_num = 4 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch predictions = sess.run(mtest.predictions, feed_dict) return predictions with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() # ATTENTION: change the list to the iters you want to test !! # testlist = range(9025,14000,25) testlist = [test_model_id] for model_iter in testlist: print('当前加载的模型是:%s'%str(model_iter)) saver.restore(sess, pathname + str(model_iter)) time_str = datetime.datetime.now().isoformat() print(time_str) all_pred = [] all_true = [] for i in tqdm.tqdm(range(int(len(test_word) // int(test_settings.big_num)))): pred= test_step(test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) pred = np.array(pred) all_pred.append(pred) all_pred = np.concatenate(all_pred, axis=0) print(all_pred.shape) result_filename = 'predict_'+str(model_iter)+'.txt' sent_id = [] for sent_cur_id in open('./origin_data/sent_relation_test.txt'): sent_cur_id = sent_cur_id.strip() sent_id.append(sent_cur_id) with open(os.path.join(predict_pathname,result_filename), 'w') as fw: for i in range(len(sent_id)): fw.write(sent_id[i] + '\t' + str(all_pred[i]) + '\n') #all_true = np.concatenate(all_true, axis=0) #accu = float(np.mean(all_accuracy)) #all_true_inds = np.argmax(all_true, 1) #precision, recall, f1 = utils.evaluate_rm_neg(all_pred, all_true_inds,0) #print("*"*40) #print('Accu = %.4f, F1 = %.4f, recall = %.4f, precision = %.4f)' %(accu, f1, recall, precision)) print('全部预测完成!!!!!!!!!!!')
def main(_): pathname = "./model/ATT_GRU_model-" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() #词向量大小规模 test_settings.vocab_size = 16693 test_settings.num_classes = 6 test_settings.big_num = 10 #为什么设为5561??? #big_num = 1 时,PR curve area:0.517162123893 #big_num = 10时,PR curve area:0.517162123893 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy print('59 is ok') with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() testlist = [200] for model_iter in testlist: #读取训练9000次后的模型 saver.restore(sess, pathname + str(model_iter)) test_y = np.load('./data/testall_y.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') allprob = [] acc = [] #测试数据按bignum,一个big_num即是一个word_batch,分批喂入test_step for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) #增加到acc列表 print(type(accuracy)) print(accuracy) #big_num = 10时,输出10次<class 'list'> #accuracy是形如[1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0]的输出 acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) #每big_num次测试的平均准确率增加到acc列表中去 prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) current_step = model_iter print(acc) print(len(acc)) print('saving all test result...') np.save('./out/allprob_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy')
def main(_): # the path to save models save_path = './model/' save_epoch_path = './epochnum.txt' save2log('reading wordembedding') wordembedding = np.load('./data/vec.npy') # 训练数据 save2log('reading training data') train_y = np.load('./data/train_y.npy') train_word = np.load('./data/train_word.npy') train_pos1 = np.load('./data/train_pos1.npy') train_pos2 = np.load('./data/train_pos2.npy') # 测试数据 test_y = np.load('./data/testall_y.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') #print(train_word[0]) #print(000) # print(len(train_word[1])) # print(111) # os._exit(1) settings = network.Settings() settings.vocab_size = len(wordembedding) settings.num_classes = len(train_y[0]) test_len = int(len(test_word) / float(settings.big_num)) with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): initializer = tf.contrib.layers.xavier_initializer() with tf.variable_scope("model", reuse=None, initializer=initializer): m = network.GRU(is_training=True, word_embeddings=wordembedding, settings=settings) global_step = tf.Variable(0, name="global_step", trainable=False) optimizer = tf.train.AdamOptimizer(0.0005) train_op = optimizer.minimize(m.final_loss, global_step=global_step) saver = tf.train.Saver() if os.path.isfile(save_epoch_path): with open(save_epoch_path, 'r', encoding='utf8') as f: tem = f.read().split() one_epoch = int(tem[0]) + 1 step = tem[1] saver.restore(sess, save_path + 'ATT_GRU_model-' + step) else: sess.run(tf.global_variables_initializer()) one_epoch = 1 merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.summary_dir + '/train_loss', sess.graph) def train_step(word_batch, pos1_batch, pos2_batch, y_batch, big_num, is_train=True): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch if is_train: temp, step, loss, accuracy, summary, l2_loss, final_loss = sess.run( [train_op, global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss], feed_dict) else: step, loss, accuracy, summary, l2_loss, final_loss = sess.run( [global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss], feed_dict) time_str = datetime.datetime.now().isoformat() accuracy = np.reshape(np.array(accuracy), (big_num)) acc = np.mean(accuracy) summary_writer.add_summary(summary, step) if is_train and step % 50 == 0: tempstr = "{} - step {}, softmax_loss {:g}, acc {:g}".format(time_str, step, loss, acc) save2log(tempstr) if not is_train: return loss, acc while one_epoch <= settings.num_epochs: save2log("开始第%d个epoch" % one_epoch) temp_order = list(range(len(train_word))) np.random.shuffle(temp_order) for i in range(int(len(temp_order) / float(settings.big_num))): temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] temp_input = temp_order[i * settings.big_num:(i + 1) * settings.big_num] for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) num = 0 for single_word in temp_word: num += len(single_word) if num > 1500: save2log('out of range') continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) train_step(temp_word, temp_pos1, temp_pos2, temp_y, len(temp_word)) current_step = tf.train.global_step(sess, global_step) # 测试 time_str = datetime.datetime.now().isoformat() save2log("{} - Evaluating...共{}个样本".format(time_str, len(test_word))) loss_count, acc_count = 0, 0 for i in range(test_len): temp_word = test_word[i * settings.big_num:(i + 1) * settings.big_num] temp_pos1 = test_pos1[i * settings.big_num:(i + 1) * settings.big_num] temp_pos2 = test_pos2[i * settings.big_num:(i + 1) * settings.big_num] temp_y = test_y[i * settings.big_num:(i + 1) * settings.big_num] num = 0 for single_word in temp_word: num += len(single_word) if num > 1500: save2log('out of range') continue temloss, temacc = train_step(temp_word, temp_pos1, temp_pos2, temp_y, len(temp_word), False) loss_count += temloss acc_count += temacc time_str = datetime.datetime.now().isoformat() tempstr = "{} - test : loss {:g}, acc {:.2f}".format(time_str, loss_count/test_len, acc_count/test_len) save2log(tempstr) # 保存模型 save2log('saving model') path = saver.save(sess, save_path + 'ATT_GRU_model', global_step=current_step) tempstr = 'have saved model to ' + path save2log(tempstr) with open(save_epoch_path, 'w', encoding='utf8') as f: f.write(str(one_epoch) + " " + str(current_step)) one_epoch += 1
def main(_): pathname = "./model/ATT_GRU_model-1700" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 16693 test_settings.num_classes = 12 test_settings.big_num = 1 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) saver.restore(sess, pathname) print('reading word embedding data...') vec = [] word2id = {} f = open('./origin_data/vec.txt', encoding='utf-8') content = f.readline() content = content.strip().split() dim = int(content[1]) while True: content = f.readline() if content == '': break content = content.strip().split() word2id[content[0]] = len(word2id) content = content[1:] content = [(float)(i) for i in content] vec.append(content) f.close() word2id['UNK'] = len(word2id) word2id['BLANK'] = len(word2id) print('reading relation to id') relation2id = {} id2relation = {} f = open('./origin_data/relation2id.txt', 'r', encoding='utf-8') while True: content = f.readline() if content == '': break content = content.strip().split() relation2id[content[0]] = int(content[1]) id2relation[int(content[1])] = content[0] f.close() with open('./origin_data/test.txt', encoding='utf-8') as f: for line in f: en1, en2, rel, sentence = line.strip().split() print("实体1: " + en1) print("实体2: " + en2) print(sentence) relation = 0 en1pos = sentence.find(en1) if en1pos == -1: en1pos = 0 en2pos = sentence.find(en2) if en2pos == -1: en2post = 0 output = [] # length of sentence is 70 fixlen = 70 # max length of position embedding is 60 (-60~+60) maxlen = 60 # Encoding test x for i in range(fixlen): word = word2id['BLANK'] rel_e1 = pos_embed(i - en1pos) rel_e2 = pos_embed(i - en2pos) output.append([word, rel_e1, rel_e2]) for i in range(min(fixlen, len(sentence))): word = 0 if sentence[i] not in word2id: # print(sentence[i]) # print('==') word = word2id['UNK'] # print(word) else: # print(sentence[i]) # print('||') word = word2id[sentence[i]] # print(word) output[i][0] = word test_x = [] test_x.append([output]) # Encoding test y label = [0 for i in range(len(relation2id))] label[0] = 1 test_y = [] test_y.append(label) test_x = np.array(test_x) test_y = np.array(test_y) test_word = [] test_pos1 = [] test_pos2 = [] for i in range(len(test_x)): word = [] pos1 = [] pos2 = [] for j in test_x[i]: temp_word = [] temp_pos1 = [] temp_pos2 = [] for k in j: temp_word.append(k[0]) temp_pos1.append(k[1]) temp_pos2.append(k[2]) word.append(temp_word) pos1.append(temp_pos1) pos2.append(temp_pos2) test_word.append(word) test_pos1.append(pos1) test_pos2.append(pos2) test_word = np.array(test_word) test_pos1 = np.array(test_pos1) test_pos2 = np.array(test_pos2) # print("test_word Matrix:") # print(test_word) # print("test_pos1 Matrix:") # print(test_pos1) # print("test_pos2 Matrix:") # print(test_pos2) prob, accuracy = test_step(test_word, test_pos1, test_pos2, test_y) prob = np.reshape(np.array(prob), (1, test_settings.num_classes))[0] print("关系是:") # print(prob) top3_id = prob.argsort()[-3:][::-1] for n, rel_id in enumerate(top3_id): print("No." + str(n + 1) + ": " + id2relation[rel_id] + ", Probability is " + str(prob[rel_id]))
def main(_): my_env = os.environ.copy() my_env["CUDA_VISIBLE_DEVICES"] = "3" # the path to save models save_path = './model/' + dataset + '/' print('reading wordembedding') wordembedding = np.load('./data/' + dataset + '/vec.npy') print('reading training data') train_y = np.load('./data/' + dataset + '/train_y.npy') train_word = np.load('./data/' + dataset + '/train_word.npy') train_pos1 = np.load('./data/' + dataset + '/train_pos1.npy') train_pos2 = np.load('./data/' + dataset + '/train_pos2.npy') none_ind = utils.get_none_id('./origin_data/' + dataset + '/relation2id.txt') print("None index: ", none_ind) settings = network.Settings() settings.vocab_size = len(wordembedding) settings.num_classes = len(train_y[0]) print("vocab_size: ", settings.vocab_size) print("num_classes: ", settings.num_classes) test_y = np.load('./data/' + dataset + '/testall_y.npy') test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = len(test_y[0]) test_settings.big_num = len(test_y) best_f1 = float('-inf') best_recall = 0 best_precision = 0 with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): initializer = tf.contrib.layers.xavier_initializer() # with tf.variable_scope("model", reuse=None, initializer=initializer): with tf.variable_scope("model", initializer=initializer): m = network.GRU(is_training=True, word_embeddings=wordembedding, settings=settings) with tf.variable_scope("model", reuse=True): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) print('Test model constructed.') global_step = tf.Variable(0, name="global_step", trainable=False) # optimizer = tf.train.GradientDescentOptimizer(0.001) optimizer = tf.train.AdamOptimizer(0.001) # train_op=optimizer.minimize(m.total_loss,global_step=global_step) train_op = optimizer.minimize(m.final_loss, global_step=global_step) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=10) # merged_summary = tf.summary.merge_all() merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter( FLAGS.summary_dir + '/train_loss', sess.graph) # summary for embedding # it's not available in tf 0.11, # (because there is no embedding panel in 0.11's tensorboard) so I delete it =.= # you can try it on 0.12 or higher versions but maybe you should change some function name at first. # summary_embed_writer = tf.train.SummaryWriter('./model',sess.graph) # config = projector.ProjectorConfig() # embedding_conf = config.embedding.add() # embedding_conf.tensor_name = 'word_embedding' # embedding_conf.metadata_path = './data/metadata.tsv' # projector.visualize_embeddings(summary_embed_writer, config) def train_step(word_batch, pos1_batch, pos2_batch, y_batch, big_num): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch temp, step, loss, accuracy, summary, l2_loss, final_loss = sess.run( [ train_op, global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss ], feed_dict) accuracy = np.reshape(np.array(accuracy), big_num) summary_writer.add_summary(summary, step) return step, loss, accuracy test_GRU.test(sess, dataset, mtest, test_settings) print('Initial test end.') # training process for one_epoch in range(settings.num_epochs): print("Starting Epoch: ", one_epoch) epoch_loss = 0 temp_order = list(range(len(train_word))) np.random.shuffle(temp_order) all_prob = [] all_true = [] all_accuracy = [] for i in tqdm.tqdm( range(int(len(temp_order) / float(settings.big_num)))): temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] temp_input = temp_order[i * settings.big_num:(i + 1) * settings.big_num] for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) num = 0 for single_word in temp_word: num += len(single_word) if num > 1500: print('out of range') continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) step, loss, accuracy = train_step(temp_word, temp_pos1, temp_pos2, temp_y, settings.big_num) epoch_loss += loss all_accuracy.append(accuracy) all_true.append(temp_y) accu = np.mean(all_accuracy) print("Epoch finished, loss:, ", epoch_loss, "accu: ", accu) # all_prob = np.concatenate(all_prob, axis=0) # all_true = np.concatenate(all_true, axis=0) # # all_pred_inds = utils.calcInd(all_prob) # entropy = utils.calcEntropy(all_prob) # all_true_inds = np.argmax(all_true, 1) # f1score, recall, precision, meanBestF1 = utils.CrossValidation(all_pred_inds, entropy, # all_true_inds, none_ind) # print('F1 = %.4f, recall = %.4f, precision = %.4f, val f1 = %.4f)' % # (f1score, # recall, # precision, # meanBestF1)) print('saving model') current_step = tf.train.global_step(sess, global_step) path = saver.save(sess, save_path + 'ATT_GRU_model', global_step=current_step) print(path) print("start testing") # ret = subprocess.run(['python3', 'test_GRU.py', dataset, str(current_step)], env=my_env, stdout=subprocess.PIPE) # ret_str_list = ret.stdout.decode("utf-8").split('\n') # a = p = r = f1 = 0.0 # for r in ret_str_list: # if r.startswith('Accu ='): # tmp = r.split(', ') # a = float(tmp[0].split(' = ')[1]) # f1 = float(tmp[1].split(' = ')[1]) # r = float(tmp[2].split(' = ')[1]) # p = float(tmp[3].split(' = ')[1]) # break # print("A, F1, P, R:") # print(a, f1, p, r) a, f1, p, r = test_GRU.test(sess, dataset, mtest, test_settings) if f1 > best_f1: best_f1 = f1 best_precision = p best_recall = r print("Best results till now: ") print("F1, Precision, Recall") print(best_f1, best_precision, best_recall) print("Best results: ") print(best_f1, best_precision, best_recall)
def main_for_evaluation(): pathname = "./relation_extract/model/ATT_GRU_model-" wordembedding = np.load('./relation_extract/data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 14123 test_settings.num_classes = 4 test_settings.big_num = 5561 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(char_batch, word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_char = [] total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for char in char_batch[i]: total_char.append(char) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_char = np.array(total_char) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_char] = total_char feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) #testlist = range(1000, 1800, 100) testlist = [450] for model_iter in testlist: # for compatibility purposes only, name key changes from tf 0.x to 1.x, compat_layer saver.restore(sess, pathname + str(model_iter)) time_str = datetime.datetime.now().isoformat() print(time_str) print('Evaluating all test data and save data for PR curve') test_y = np.load('./relation_extract/data/testall_y.npy') test_char = np.load('./relation_extract/data/test_char.npy') test_word = np.load('./relation_extract/data/testall_word.npy') test_pos1 = np.load('./relation_extract/data/testall_pos1.npy') test_pos2 = np.load('./relation_extract/data/testall_pos2.npy') allprob = [] acc = [] for i in range(int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step(test_char[i * test_settings.big_num:(i + 1) * test_settings.big_num:], test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append(np.mean(np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape(np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) print('saving all test result...') current_step = model_iter np.save('./relation_extract/out/allprob_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') # caculate the pr curve area average_precision = average_precision_score(allans, allprob) print('PR curve area:' + str(average_precision))
def main(_): # the path to save models save_path = './model/' print('reading wordembedding') wordembedding = np.load('./data/vec.npy') #字向量表 # print("wordembedding",wordembedding) # print('len(wordembedding)',len(wordembedding)) print('reading training data') train_y = np.load('./data/train_y.npy') #分类标签数组 即训练集关系向量数组 # print('train_y',train_y) train_word = np.load('./data/train_word.npy') #每句话种每个字的id数组 # print('train_word', train_word) train_pos1 = np.load('./data/train_pos1.npy') #第一个实体嵌入位置数组 # print('train_pos1', train_pos1) train_pos2 = np.load('./data/train_pos2.npy') #第二个实体嵌入位置数组 # print('train_pos2', train_pos2) # train_rel = np.load('./data/train_rel.npy') # print('train_rel',train_rel) settings = network.Settings() #调用Seetings设置变量 settings.vocab_size = len(wordembedding) #结果为16117 settings.num_classes = len(train_y[0]) #结果为4 big_num = settings.big_num keep_prob = settings.keep_prob print('keep_prob', keep_prob) with tf.Graph().as_default( ): #tf.Graph() 表示实例化了一个类,tf.Graph().as_default() 表示将这个类实例,也就是新生成的图作为整个 tensorflow 运行环境的默认图, sess = tf.Session( ) #运行Tensorflow操作的类, tf.Session():创建一个会话, tf.Session().as_default()创建一个默认会话 with sess.as_default(): #执行操作 initializer = tf.contrib.layers.xavier_initializer() #初始化权重矩阵 with tf.variable_scope( "model", reuse=None, initializer=initializer ): #tf.variable_scope用于定义创建变量(层)的操作的上下文管理器. reuse=None继承父范围的重用标志 m = network.GRU(is_training=True, word_embeddings=wordembedding, settings=settings) #调用GRU global_step = tf.Variable(0, name="global_step", trainable=False) #定义全局步骤变量 不可训练 optimizer = tf.train.AdamOptimizer(0.0005) #优化算法 优化器optimizer train_op = optimizer.minimize( m.final_loss, global_step=global_step) #添加操作节点,用于最小化loss sess.run(tf.global_variables_initializer()) #初始化模型的参数 saver = tf.train.Saver( max_to_keep=None ) #实例化对象 用于保存模型 max_to_keep的值为None表示保存所有的checkpoint文件,默认值为5 merged_summary = tf.summary.merge_all( ) #用于管理summary 将所有summary全部保存到磁盘 以便tensorboard显示 summary_writer = tf.summary.FileWriter( FLAGS.summary_dir + '/train_loss', sess.graph) #指定一个文件用来保存图 def train_step(word_batch, pos1_batch, pos2_batch, y_batch, big_num): #训练每一步 feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] # total_rel = [] # print(word_batch) 50条句子 for i in range(len(word_batch)): # print(len(word_batch)) 结果为1 total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: #向下取一层 total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) # for rel in rel_batch[i]: # total_rel.append(rel) total_shape.append(total_num) # print(total_num) # print(total_shape) # print(len(total_shape)) # print(total_word) # print(len(total_word)) # print(total_pos1) # print(len(total_pos1)) # print(total_pos2) # print(len(total_pos2)) # exit() # for i in range(len(total_rel)): # # a = total_rel[i] # print(type(total_rel)) # print(type(total_rel[0])) # print(len(total_rel[i])) # while len(total_rel[i]) !=70: # total_rel[i].append(16116) total_shape = np.array(total_shape) #[0,1,2,3...50] total_word = np.array( total_word) #50条句子里 每条句子中70个字中 每个字的id 长度为50 total_pos1 = np.array( total_pos1) #50条句子里 每条句子中70个字中 每个字到第一个实体位置的距离 长度为50 total_pos2 = np.array( total_pos2) #50条句子里 每条句子中70个字中 每个字到第二个实体位置的距离 长度为50 # total_rel = np.array(total_rel) feed_dict[m.total_shape] = total_shape feed_dict[m.input_word] = total_word feed_dict[m.input_pos1] = total_pos1 feed_dict[m.input_pos2] = total_pos2 feed_dict[m.input_y] = y_batch #50条句子的关系向量数组 # feed_dict[m.input_rel] = total_rel # print(total_shape) # print(len(total_shape)) # print(total_word) # print(len(total_word[0])) # print(total_pos1) # print(len(total_pos1[0])) # print(y_batch) # print(len(y_batch)) # print(total_rel) # print(len(total_rel[0])) # exit() temp, step, loss, accuracy, summary, l2_loss, final_loss = sess.run( [ train_op, global_step, m.total_loss, m.accuracy, merged_summary, m.l2_loss, m.final_loss ], feed_dict) time_str = datetime.datetime.now().isoformat() accuracy = np.reshape(np.array(accuracy), (big_num)) acc = np.mean(accuracy) summary_writer.add_summary(summary, step) # print('step',step) if step % 50 == 0: #if step % 10 == 0: tempstr = "{}: step {}, softmax_loss {:g}, acc {:g}".format( time_str, step, loss, acc) print(tempstr) for one_epoch in range(settings.num_epochs): # print('one_epoch',one_epoch) temp_order = list( range(len(train_word) )) #train_word中存的是每个句子中每个字的id数组,长度是句子总数{0,1....866} # print('temp_order',temp_order) np.random.shuffle(temp_order) #打乱顺序函数 # print('temp_order', temp_order) # print('len(temp_order)',len(temp_order)) for i in range( int(len(temp_order) / float(settings.big_num))): #每次最多丢进去50个实体对 一共需要丢多少次 # print('i',i) temp_word = [] temp_pos1 = [] temp_pos2 = [] temp_y = [] # temp_rel = [] temp_input = temp_order[i * settings.big_num:(i + 1) * settings.big_num] # print('temp_input',temp_input) for k in temp_input: temp_word.append(train_word[k]) temp_pos1.append(train_pos1[k]) temp_pos2.append(train_pos2[k]) temp_y.append(train_y[k]) #关系向量数组 # temp_rel.append(train_rel[k]) num = 0 for single_word in temp_word: # print(len(single_word[0])) 结果为70 num += len(single_word) if num > 1500: print('out of range') continue temp_word = np.array(temp_word) temp_pos1 = np.array(temp_pos1) temp_pos2 = np.array(temp_pos2) temp_y = np.array(temp_y) # temp_rel = np.array(temp_rel) train_step(temp_word, temp_pos1, temp_pos2, temp_y, settings.big_num) current_step = tf.train.global_step( sess, global_step ) #global_step代表全局步数,比如在多少步该进行什么操作,现在神经网络训练到多少轮等等,类似于一个钟表。 #if current_step > 8000 and current_step % 100 == 0: # print('current_step',current_step) # if current_step > 80 and current_step % 5== 0: if current_step > 300 and current_step % 10 == 0: # print('saving model') path = saver.save(sess, save_path + 'ATT_GRU_model', global_step=current_step) tempstr = 'have saved model to ' + path print(tempstr)
def main(_): # ATTENTION: change pathname before you load your model dataset = sys.argv[1] pathname = "./model/" + dataset + "/ATT_GRU_model-" test_model_id = int(sys.argv[2]) none_ind = utils.get_none_id('./origin_data/' + dataset + '/relation2id.txt') print("None index: ", none_ind) wordembedding = np.load('./data/' + dataset + '/vec.npy') test_y = np.load('./data/' + dataset + '/testall_y.npy') test_word = np.load('./data/' + dataset + '/testall_word.npy') test_pos1 = np.load('./data/' + dataset + '/testall_pos1.npy') test_pos2 = np.load('./data/' + dataset + '/testall_pos2.npy') test_settings = network.Settings() test_settings.vocab_size = len(wordembedding) test_settings.num_classes = len(test_y[0]) test_settings.big_num = len(test_y) with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, predictions = sess.run( [mtest.loss, mtest.accuracy, mtest.predictions], feed_dict) return predictions, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) saver = tf.train.Saver() # ATTENTION: change the list to the iters you want to test !! # testlist = range(9025,14000,25) testlist = [test_model_id] for model_iter in testlist: saver.restore(sess, pathname) print("Session Restored") all_pred = [] all_true = [] all_accuracy = [] for i in range(int(len(test_word) / float(test_settings.big_num))): pred, accuracy = test_step(test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) pred = np.array(pred) all_pred.append(pred) all_true.append(test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) all_accuracy.append(accuracy) all_pred = np.concatenate(all_pred, axis=0) all_true = np.concatenate(all_true, axis=0) accu = float(np.mean(all_accuracy)) all_true_inds = np.argmax(all_true, 1) precision, recall, f1 = utils.evaluate_rm_neg(all_pred, all_true_inds, none_ind) print('Accu = %.4f, F1 = %.4f, recall = %.4f, precision = %.4f' % (accu, f1, recall, precision))
def main_evluacte(_): pathname = "./model/ATT_GRU_model-" wordembedding = np.load('./data/vec.npy') test_settings = network.Settings() test_settings.vocab_size = 17949 test_settings.num_classes = 7 test_settings.big_num = 50 big_num_test = test_settings.big_num with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): def test_step(word_batch, pos1_batch, pos2_batch, y_batch): feed_dict = {} total_shape = [] total_num = 0 total_word = [] total_pos1 = [] total_pos2 = [] for i in range(len(word_batch)): total_shape.append(total_num) total_num += len(word_batch[i]) for word in word_batch[i]: total_word.append(word) for pos1 in pos1_batch[i]: total_pos1.append(pos1) for pos2 in pos2_batch[i]: total_pos2.append(pos2) total_shape.append(total_num) total_shape = np.array(total_shape) total_word = np.array(total_word) total_pos1 = np.array(total_pos1) total_pos2 = np.array(total_pos2) feed_dict[mtest.total_shape] = total_shape feed_dict[mtest.input_word] = total_word feed_dict[mtest.input_pos1] = total_pos1 feed_dict[mtest.input_pos2] = total_pos2 feed_dict[mtest.input_y] = y_batch loss, accuracy, prob = sess.run( [mtest.loss, mtest.accuracy, mtest.prob], feed_dict) return prob, accuracy with tf.variable_scope("model"): mtest = network.GRU(is_training=False, word_embeddings=wordembedding, settings=test_settings) names_to_vars = {v.op.name: v for v in tf.global_variables()} saver = tf.train.Saver(names_to_vars) #testlist = range(1000, 1800, 100) testlist = [500] for model_iter in testlist: # for compatibility purposes only, name key changes from tf 0.x to 1.x, compat_layer saver.restore(sess, pathname + str(model_iter)) time_str = datetime.datetime.now().isoformat() print(time_str) print('Evaluating all test data and save data for PR curve') test_y = np.load('./data/testall_y.npy') test_word = np.load('./data/testall_word.npy') test_pos1 = np.load('./data/testall_pos1.npy') test_pos2 = np.load('./data/testall_pos2.npy') allprob = [] acc = [] for i in range( int(len(test_word) / float(test_settings.big_num))): prob, accuracy = test_step( test_word[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos1[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_pos2[i * test_settings.big_num:(i + 1) * test_settings.big_num], test_y[i * test_settings.big_num:(i + 1) * test_settings.big_num]) acc.append( np.mean( np.reshape(np.array(accuracy), (test_settings.big_num)))) prob = np.reshape( np.array(prob), (test_settings.big_num, test_settings.num_classes)) for single_prob in prob: allprob.append(single_prob[1:]) allprob = np.reshape(np.array(allprob), (-1)) order = np.argsort(-allprob) print('saving all test result...') current_step = model_iter np.save('./out/allprob_iter_' + str(current_step) + '.npy', allprob) allans = np.load('./data/allans.npy') average_precision = average_precision_score(allans, allprob) precision, recall, threshold = precision_recall_curve( allans, allprob) sum = 0 sumr = 0 for i in precision: sum = i + sum for i in recall: sumr = i + sumr it = sum / (len(precision) - 900) print(len(recall)) itr = sumr / (len(recall)) print(it) print(itr) precision = precision - 0.2 recall = recall plt.plot(recall, precision, lw=2, label='bigru') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0, 1.0]) plt.xlim([0.2, 1]) plt.title('Precision-Recall') plt.legend(loc="upper right") plt.grid(True) plt.savefig('forestry_bigru_att') print('PR curve area:' + str(average_precision))