Пример #1
0
    def val(self):

        var_list = tf.global_variables()
        saver = tf.train.Saver(var_list,
                               max_to_keep=5,
                               filename=self.ckpt_name)
        logit_tensor = self.logit
        length_tensor = self.L

        with tf.Session(config=self.gpu_config) as sess:
            ckpt = tf.train.latest_checkpoint(self.ckpt_path, self.ckpt_name)
            saver.restore(sess, ckpt)

            logit_list = []
            length_list = []
            for _ in range(self.val_steps):
                logit_array, length_array = sess.run(
                    [logit_tensor, length_tensor],
                    feed_dict={self.drop_flag: False})
                logit_list.extend(sigmoid(logit_array))
                length_list.extend(length_array)
        with open('./data/dev_data_char.json', encoding='utf-8') as file:
            data = [json.loads(line) for line in file]
        for t in np.arange(0.1, 0.6, 0.1):
            decode_fn(logit_list,
                      length_list,
                      data,
                      './result_dev_%.3f.json' % t,
                      num_target=self.num_target,
                      threshold=t)
            p, r, f = compute_Fscore('./result_dev_%.3f.json' % t,
                                     './data/dev_data.json')
            print(p, r, f, t)
Пример #2
0
 def val_with_threshold(self, threshold=0.5):
     var_list = tf.global_variables()
     saver = tf.train.Saver(var_list,
                            max_to_keep=5,
                            filename=self.ckpt_name)
     logit_tensor = tf.sigmoid(self.logit)
     length_tensor = self.L
     e = 0
     with tf.Session(config=self.gpu_config) as sess:
         last_ckpt = ''
         while True:
             ckpt = tf.train.latest_checkpoint(self.ckpt_path,
                                               self.ckpt_name)
             if not ckpt or ckpt == last_ckpt:
                 time.sleep(60)
                 continue
             saver.restore(sess, ckpt)
             e = e + 1
             last_ckpt = ckpt
             print(ckpt)
             logit_list = []
             length_list = []
             for i in range(self.val_steps):
                 logit_array, length_array = sess.run(
                     [logit_tensor, length_tensor],
                     feed_dict={self.drop_flag: False})
                 logit_list.extend(logit_array)
                 length_list.extend(length_array)
                 # print(i)
             print(e)
             # logit_list = [sigmoid(logit) for logit in logit_list]
             with open('./data/dev_data_char.json',
                       encoding='utf-8') as file:
                 data = [json.loads(line) for line in file]
             decode_fn(logit_list,
                       length_list,
                       data,
                       './result_dev_epoch_%d.json' % e,
                       num_target=self.num_target,
                       threshold=threshold)
             p, r, f = compute_Fscore('./result_dev_epoch_%d.json' % e,
                                      './data/dev_data.json')
Пример #3
0
    def train_val(self, val_fn=None):
        assert self.loss != None, 'must compute loss before train'

        var_list = tf.global_variables()
        train_variables = tf.trainable_variables()
        global_step = self.global_step
        run_ops = {'step': global_step, 'loss': self.loss}
        grads_and_vars = self.optimizer.compute_gradients(
            self.loss, train_variables)
        run_ops['train_ops'] = self.optimizer.apply_gradients(
            grads_and_vars, global_step=global_step)
        saver = tf.train.Saver(var_list,
                               max_to_keep=5,
                               filename=self.ckpt_name)
        initializer = tf.global_variables_initializer()
        with tf.Session(config=self.gpu_config) as sess:
            sess.run(initializer)
            ckpt = tf.train.latest_checkpoint(self.ckpt_path, self.ckpt_name)
            if ckpt:
                saver.restore(sess, ckpt)
            train_handle = sess.run(self.train_iterator)
            val_handle = sess.run(self.val_iterator)
            best_score = 0
            with open('./data/dev_data_char.json', encoding='utf-8') as file:
                data = [json.loads(line) for line in file]
            for e in range(self.train_epochs):
                result = {}
                # train steps
                for i in range(self.steps_each_epoch):
                    result = sess.run(run_ops,
                                      feed_dict={
                                          self.handle_holder: train_handle,
                                          self.drop_flag: True
                                      })
                    if result['step'] % 100 == 0:
                        print('%d:\t%f' % (result['step'], result['loss']))
                logit_list = []
                label_list = []
                loss_list = []
                length_list = []

                #validation steps:
                for j in range(self.val_steps):
                    logit_array,label_array,length_array,loss_ =\
                     sess.run([self.logit,self.target,self.L,self.loss],
                        feed_dict={self.handle_holder:val_handle,self.drop_flag:False})
                    logit_list.extend(logit_array)
                    label_list.extend(label_array)
                    length_list.extend(length_array)
                    loss_list.append(loss_)
                logit_list = [sigmoid(logit) for logit in logit_list]
                print('epoch %d' % e)
                # score = val_fn(label_list,logit_list)
                print('loss:\t%f' % np.mean(loss_list))

                out_file = './reslut_dev_epoch_%d.json' % e

                decode_fn(logit_list,
                          length_list,
                          data,
                          out_file,
                          num_target=self.num_target)
                p, r, f = compute_Fscore(out_file, './data/dev_data.json')
                print('P:\t%f,R:\t%f,F1:\t%f' % (p, r, f))
                if f > best_score:
                    best_score = f
                    saver.save(sess,
                               self.ckpt_path,
                               global_step=result['step'],
                               latest_filename=self.ckpt_name)