def train_trigger(self): train, dev, test = self.t_train, self.t_dev, self.t_test saver = tf.train.Saver() maxlen = self.maxlen print('--Training Trigger--') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) devbest = 0 testbest = (0, 0, 0) from tqdm import tqdm for epoch in tqdm(range(constant.t_epoch)): loss_list = [] for batch in get_batch(train, constant.t_batch_size, True): loss, _ = sess.run([self.loss, self.train_op], feed_dict=get_trigger_feeddict( self, batch, self.stage, maxlen)) loss_list.append(loss) print('epoch:{}'.format(str(epoch))) print('loss:', np.mean(loss_list)) pred_labels = [] for batch in get_batch(dev, constant.t_batch_size, False): pred_label = sess.run(self.pred_label, feed_dict=get_trigger_feeddict( self, batch, self.stage, maxlen, is_train=False)) pred_labels.extend(list(pred_label)) golds = list(dev[0][4]) dev_p, dev_r, dev_f = f_score(pred_labels, golds) print("dev_Precision: {} dev_Recall:{} dev_F1:{}".format( str(dev_p), str(dev_r), str(dev_f))) pred_labels = [] for batch in get_batch(test, constant.t_batch_size, False): pred_label = sess.run(self.pred_label, feed_dict=get_trigger_feeddict( self, batch, self.stage, maxlen, is_train=False)) pred_labels.extend(list(pred_label)) golds = list(test[0][4]) test_p, test_r, test_f = f_score(pred_labels, golds) print("test_Precision: {} test_Recall:{} test_F1:{}\n".format( str(test_p), str(test_r), str(test_f))) if dev_f > devbest: devbest = dev_f testbest = (test_p, test_r, test_f) saver.save(sess, "saved_models/trigger.ckpt") test_p, test_r, test_f = testbest print( "test best Precision: {} test best Recall:{} test best F1:{}". format(str(test_p), str(test_r), str(test_f)))
def train_argument(self): print('--Training Argument--') train, dev, test = self.a_train, self.a_dev, self.a_test with tf.Session() as sess: devbest = 0 testbest = (0, 0, 0) sess.run(tf.global_variables_initializer()) for epoch in range(constant.a_epoch): loss_list = [] for batch in get_batch(train, constant.a_batch_size, shuffle=True): loss, _ = sess.run([self.loss, self.train_op], feed_dict=get_argument_feeddict( self, batch, True, "argument")) loss_list.append(loss) print('epoch:{}'.format(str(epoch))) print('loss:', np.mean(loss_list)) pred_labels = [] for batch in get_batch(dev, constant.a_batch_size, False): pred_event_types, feed_dict = get_argument_feeddict( self, batch, False, "argument") pred_label = sess.run(self.pred_label, feed_dict=feed_dict) pred_labels.extend( list(zip(list(pred_event_types), list(pred_label)))) golds = list(zip(list(dev[1]), list(dev[2]))) dev_p, dev_r, dev_f = f_score(pred_labels, golds, self.classify) print("dev_Precision: {} dev_Recall:{} dev_F1:{}".format( str(dev_p), str(dev_r), str(dev_f))) pred_labels = [] for batch in get_batch(test, constant.a_batch_size, False): pred_event_types, feed_dict = get_argument_feeddict( self, batch, False, "argument") pred_label = sess.run(self.pred_label, feed_dict=feed_dict) pred_labels.extend( list(zip(list(pred_event_types), list(pred_label)))) golds = list(zip(list(test[1]), list(test[2]))) test_p, test_r, test_f = f_score(pred_labels, golds, self.classify) print("test_Precision: {} test_Recall:{} test_F1:{}\n".format( str(test_p), str(test_r), str(test_f))) if dev_f > devbest: devbest = dev_f testbest = (test_p, test_r, test_f) test_p, test_r, test_f = testbest print( "test best Precision: {} test best Recall:{} test best F1:{}". format(str(test_p), str(test_r), str(test_f)))
def predict_trigger(self, sess): print('--Predict Trigger For Argument Stage--') train, dev, test = self.a_train, self.a_dev, self.a_test saver = tf.train.Saver() saver.restore(sess, "saved_models/trigger.ckpt") dev_pred_event_types = [] test_pred_event_types = [] for batch in get_batch(dev, constant.t_batch_size, False): pred_label = list( sess.run(self.pred_label, feed_dict=get_argument_feeddict( self, batch, False, 'trigger'))) dev_pred_event_types.extend(pred_label) for batch in get_batch(test, constant.t_batch_size, False): pred_label = list( sess.run(self.pred_label, feed_dict=get_argument_feeddict( self, batch, False, 'trigger'))) test_pred_event_types.extend(pred_label) return self.process_data_for_argument( np.array(dev_pred_event_types, np.int32), np.array(test_pred_event_types, np.int32))
def eval_trigger(self): test = self.t_test saver = tf.train.Saver() maxlen = self.maxlen from collections import defaultdict import json results = defaultdict(list) print('--Eval Trigger--') with tf.Session() as sess: saver.restore(sess, "saved_models/trigger.ckpt") pred_labels = [] for batch in get_batch(test, constant.t_batch_size, False): pred_label = sess.run(self.pred_label, feed_dict=get_trigger_feeddict( self, batch, self.stage, maxlen, is_train=False)) pred_labels.extend(list(pred_label)) with open('{}/id_align.json'.format(constant.maven_path), 'r') as f: ids = json.load(f) with open('test_idxs.json', 'r') as f: test_idxs = json.load(f) test_idxs = { test_idx: idx for idx, test_idx in enumerate(test_idxs) } assert len(test_idxs) == len(pred_labels) for idx in range(len(ids)): id_ = ids[idx] if idx in test_idxs: label = pred_labels[test_idxs[idx]] else: label = 0 results[id_[0]].append({'id': id_[1], 'type_id': int(label)}) with open('results.jsonl', 'w') as f: for key, val in results.items(): f.write(json.dumps({'id': key, 'predictions': val}) + '\n') print("--Eval Finish--")
print("nn time: {:.3f} sec".format(time.time() - self._startTime)) # parameters file_name = 'TNT_dataset.txt' learning_rate = 0.001 epochs = 100 batch_size = 100 garm_cnt = 1 win_len = 100 win_num = 10 q = 1 w_d = 10000 # kHz #get signal from dataset xx0, yy0, sig_len, fi_num = func.get_batch(file_name, garm_cnt) xx, yy, xx_ac, yy_ac = func.let_2_mass(xx0, yy0) print(sig_len, fi_num) out_y = len(yy[1]) s_1000 = int(sig_len / 1000) print(out_y) x = tf.placeholder(tf.float32, [None, sig_len]) x_shaped = tf.reshape(x, [-1, sig_len, 1]) y = tf.placeholder(tf.float32, [None, out_y]) print('begin')
import numpy as np import sounddevice as sd import tensorflow as tf from scipy import signal cl_num=10# количество классо при обучении ch_num=4 sig=[] y=[] sig_len=[] ch_num_list=[] preps=['are','dia'] for k in preps: sig_0,y_0,sig_len_0,ch_num_0=func.get_batch('train_dataset_'+k+'_test_2000.txt') sig.append(sig_0) y.append(y_0) sig_len.append(sig_len_0) ch_num_list.append(ch_num_0) learning_rate=0.00015 cl=[] cl_car=[] for i in range(len(y)): for j in y[i]: c=i cl.append([1 if j==c else 0 for j in range(cl_num)])