def test(self, sess, X_char_test, y_test, epoch, cnt, istest = False): char2id, id2char = utils.loadMap(self.config.map_dict['char2id']) label2id, id2label = utils.loadMap(self.config.map_dict['label2id']) num_iterations = int(math.ceil(1.0 * len(X_char_test) / self.batch_size)) preds = [] for i in range(num_iterations): X_char_test_batch = X_char_test[i * self.batch_size : (i + 1) * self.batch_size] if i == num_iterations - 1 and len(X_char_test_batch) < self.batch_size: X_char_test_batch = list(X_char_test_batch) last_size = len(X_char_test_batch) X_char_test_batch += [[0 for j in range(self.num_steps)] for i in range(self.batch_size - last_size)] X_char_test_batch = np.array(X_char_test_batch) length, max_scores, max_scores_pre = sess.run([self.length, self.max_scores, self.max_scores_pre], feed_dict={self.inputs:X_char_test_batch, self.keep_prob:1}) predicts = self.viterbi(max_scores, max_scores_pre, length, self.batch_size) preds.extend(predicts[:last_size]) else: X_char_test_batch = np.array(X_char_test_batch) length, max_scores, max_scores_pre = sess.run([self.length, self.max_scores, self.max_scores_pre], feed_dict={self.inputs:X_char_test_batch, self.keep_prob:1}) predicts = self.viterbi(max_scores, max_scores_pre, length, self.batch_size) preds.extend(predicts) result = self.evaluate(y_test, preds, id2char, id2label, epoch, cnt, istest) if float(result.split("FB1:")[-1].strip()) > self.max_f1 and not istest: #saver = tf.train.Saver() self.overbest = 1 self.max_f1 = float(result.split("FB1:")[-1].strip()) #save_path = saver.save(sess, self.config.modelpath, global_step = epoch) print "saved the best model with f1: ", self.max_f1
def train(self, sess, X_char_merge_train, y_merge_train, X_char_dev, y_dev, X_char_test, y_test): char2id, id2char = utils.loadMap(self.config.map_dict['char2id']) label2id, id2label = utils.loadMap(self.config.map_dict['label2id']) merged = tf.summary.merge_all() num_iterations = int(math.ceil(1.0 * len(X_char_merge_train) / self.batch_size)) for epoch in range(self.num_epochs): print "current epoch: %d" % (epoch) cnt = 0 for iteration in range(num_iterations): # get PA data X_char_PA_batch, y_PA_batch = utils.nextBatch(X_char_merge_train, y_merge_train, start_index=iteration * self.batch_size, batch_size=self.batch_size) _, loss_PA, length=\ sess.run([ self.optimizer_PA, self.loss_PA, self.length, ], feed_dict={ self.inputs:X_char_PA_batch, self.keep_prob:1-self.dropout_rate, self.PA_targets:y_PA_batch }) if iteration == num_iterations-1: cnt += 1 print "epoch: %d, train_PA loss: %5d" % (epoch ,loss_PA) self.test(sess, X_char_dev, y_dev, epoch, cnt, istest = False) self.test(sess, X_char_test, y_test, epoch, cnt, istest = True)
import numpy as np import pandas as pd import tensorflow as tf from LSTM_CRF_PA import LSTM_CRF_PA from Config import Config print "preparing data" con = Config() (X_char_train, y_train), (X_char_dev, y_dev), (X_char_test, y_test), (X_char_PA, y_PA) = utils.get_AllData(con.maxlen) char2id, id2char = utils.loadMap(con.map_dict['char2id']) label2id, id2label = utils.loadMap(con.map_dict['label2id']) num_chars = len(id2char.keys()) num_classes = len(id2label.keys()) print 'num of chars:', num_chars print 'num of classes:', num_classes #X_char_train, y_train = utils.get_PA_data(con.dataset['traindata'], char2id, label2id) # merge export and PA train data X_char_merge_train, y_merge_train = utils.merge_export_and_PA_train_data( X_char_train, y_train, X_char_PA, y_PA) if con.model_para['emb_path'] != None:
def train(self, sess, X_char_merge_train, y_merge_train, sign_merge_train, X_char_train, y_train, X_char_dev, y_dev, X_char_test, y_test): char2id, id2char = utils.loadMap(self.config.map_dict['char2id']) label2id, id2label = utils.loadMap(self.config.map_dict['label2id']) for epoch in range(self.num_epochs): print "current epoch: %d" % (epoch) cnt = 0 exist_num_in_batch = 0 # instance num in this batch(expert + select_pa_sample) PA_num_in_batch = 0 # select_pa_sample_num in this sample y_label_in_batch = [] # all_pa_data action (0/1) PA_sentense_representation = [ ] # representation of every PA instance X_char_batch = [] y_batch = [] # not update total_PA_num = 0 X_char_all = [] y_all = [] for start_index in range(len(X_char_merge_train)): if exist_num_in_batch == self.batch_size: X_char_all.extend(X_char_batch) y_all.extend(y_batch) X_char_batch = np.array(X_char_batch) y_batch = np.array(y_batch) cnt += 1 ''' optimize the selector: 1. count reward: add all p(y|x) of dev_dataset and average 2. input1: average_reward 3. input2: all PA_sample in this step(0 or 1) ''' if len(y_label_in_batch) > 0: reward = self.get_reward(sess, X_char_batch, y_batch) reward_list = [ reward for i in range(len(y_label_in_batch)) ] self.selector_optimize( sess, np.array(PA_sentense_representation), np.array(y_label_in_batch), np.array(reward_list)) total_PA_num += PA_num_in_batch exist_num_in_batch = 0 PA_num_in_batch = 0 y_label_in_batch = [] PA_sentense_representation = [] X_char_batch = [] y_batch = [] if sign_merge_train[start_index] == 0: exist_num_in_batch += 1 X_char_batch.append(X_char_merge_train[start_index]) y_batch.append(y_merge_train[start_index]) elif sign_merge_train[start_index] == 1: X_char = [] y_char = [] X_char.append(X_char_merge_train[start_index]) y_char.append(y_merge_train[start_index]) X_char = np.array(X_char) y_char = np.array(y_char) this_representation = self.encode_sample( sess, X_char, y_char) PA_sentense_representation.append(this_representation[0]) action_point = self.select_action(sess, this_representation) if action_point > 0.5: X_char_batch.append(X_char_merge_train[start_index]) y_batch.append(y_merge_train[start_index]) PA_num_in_batch += 1 exist_num_in_batch += 1 y_label_in_batch.append(1) else: y_label_in_batch.append(0) if exist_num_in_batch <= self.batch_size and exist_num_in_batch > 0: cnt += 1 left_size = self.batch_size - exist_num_in_batch for i in range(left_size): index = np.random.randint(len(X_char_train)) X_char_batch.append(X_char_train[index]) y_batch.append(y_train[index]) X_char_all.extend(X_char_batch) y_all.extend(y_batch) X_char_batch = np.array(X_char_batch) y_batch = np.array(y_batch) if len(y_label_in_batch) > 0: reward = self.get_reward(sess, X_char_batch, y_batch) reward_list = [ reward for i in range(len(y_label_in_batch)) ] self.selector_optimize( sess, np.array(PA_sentense_representation), np.array(y_label_in_batch), np.array(reward_list)) # optimize baseline num_iterations = int( math.ceil(1.0 * len(X_char_all) / self.batch_size)) loss_PA = 0 for iteration in range(num_iterations): X_char_train_batch, y_train_batch = utils.nextBatch( X_char_all, y_all, start_index=iteration * self.batch_size, batch_size=self.batch_size) loss_PA = self.batch_optimize(sess, X_char_train_batch, y_train_batch) #if cnt % 20 == 0: #print "epoch %d iteration %d end, train_PA loss: %5d, total_PA_num: %5d" % (epoch ,cnt, loss_PA, total_PA_num) #self.test(sess, X_char_dev, y_dev, epoch, cnt, istest = False) #self.test(sess, X_char_test, y_test, epoch, cnt, istest = True) cnt += 1 print "epoch %d iteration %d end, train_PA loss: %5d, total_PA_num: %5d" % ( epoch, cnt, loss_PA, total_PA_num) self.test(sess, X_char_dev, y_dev, epoch, cnt, istest=False) self.test(sess, X_char_test, y_test, epoch, cnt, istest=True)