def train(self, sess, save_file, X_train, y_train, X_val, y_val):
        count = 0
        max_f = -1
        writer = tf.summary.FileWriter('./graphs_ar', sess.graph)
        trainloss = tf.summary.scalar("training_loss", self.ner_model.loss)
        for epoch in range(self.num_epochs): 
            print "current epoch: %d" % (epoch)
            for iteration in range(self.num_iterations):
                print "this iteration is %d"%(iteration)
                X_train_batch, y_train_batch = helper.nextRandomBatch(X_train, y_train, batch_size=self.batch_size)
                transition_batch = helper.getTransition(y_train_batch)
                _, train_loss, loss_train, max_scores, max_scores_pre, length =\
                    sess.run([
                        self.ner_model.optimizer,
                        trainloss,
                        self.ner_model.loss,
                        self.ner_model.max_scores,
                        self.ner_model.max_scores_pre,
                        self.ner_model.length,
                    ],
                    feed_dict={
                        self.ner_model.input_data:X_train_batch,
                        self.ner_model.targets:y_train_batch,
                        self.ner_model.targets_transition:transition_batch
                    })
                print "the loss : %f"%loss_train#, X_train_batch
                writer.add_summary(train_loss, epoch*self.num_iterations+iteration)
                if iteration % 100 == 0:
                  presicion_loc, recall_loc, f_loc, presicion_org, recall_org, f_org, presicion_per, recall_per, f_per = \
                  self.validationBatch(sess, X_val, y_val)
                  print "iteration: %5d, valid , valid precision: LOC %.5f, ORG %.5f, PER %.5f, valid recall: LOC %.5f, ORG %.5f, PER %.5f, valid f1: LOC %.5f, ORG %.5f, PER %.5f" %\
                  (iteration, presicion_loc, presicion_org, presicion_per, recall_loc, recall_org, recall_per, f_loc, f_org, f_per)

                  if f_loc + f_org + f_per >= max_f:
                    max_f = f_loc + f_org + f_per

                    saver = tf.train.Saver()
                    save_path = saver.save(sess, save_file)
                    print "saved the best model with f1: %.5f" % (max_f / 3.0)

                  self.last_f = f_loc + f_org + f_per
Beispiel #2
0
    def train_an_iteration(self, sess, save_file, X_train, y_train, X_val, y_val, char2id, id2char, label2id, id2label, summary_writer_train_source, summary_writer_train_target, summary_writer_val_source, summary_writer_val_target, flag="source", is_summary=False, is_validation=False, X_test_source=None, y_test_source=None):
        
#        num_iterations = int(math.ceil(1.0 * len(X_train) / self.batch_size))
        num_iterations = 128       
        cnt = 0
        for iteration in range(num_iterations):
            print "this iteration is %d, the flag is %s"%(iteration, flag)
            # train, the flag indicate the data source
            X_train_batch, y_train_batch = helper.nextBatch(X_train, y_train, start_index=iteration * self.batch_size, batch_size=self.batch_size)
#            y_train_weight_batch = 1 + np.array((y_train_batch == label2id['B']) | (y_train_batch == label2id['E']), float)
            transition_batch = helper.getTransition(y_train_batch)
                
            _, loss_train, max_scores, max_scores_pre, length, train_summary =\
                sess.run([
                    self.optimizer_source if flag == "source" else self.optimizer_target,
                    self.loss_source if flag == "source" else self.loss_target,
                    self.max_scores_source if flag == "source" else self.max_scores_target,
                    self.max_scores_pre_source if flag == "source" else self.max_scores_pre_target,
                    self.length,
                    self.train_summary_source if flag == "source" else self.train_summary_target
                ], 
                feed_dict={
                    self.targets_transition:transition_batch, 
                    self.inputs:X_train_batch, 
                    self.targets:y_train_batch, 
 #                   self.targets_weight:y_train_weight_batch
                })
            print "the loss : %f"%loss_train
        if is_summary:
              presicion_loc, recall_loc, f_loc, presicion_org, recall_org, f_org, presicion_per, recall_per, f_per = self.predictBatch(sess, X_test_source, y_test_source, id2label, id2char, label2id, self.batch_size, "target")
              print "iteration: %5d, %s valid , valid precision: LOC %.5f, ORG %.5f, PER %.5f, valid recall: LOC %.5f, ORG %.5f, PER %.5f, valid f1: LOC %.5f, ORG %.5f, PER %.5f" % (iteration, flag, presicion_loc, presicion_org, presicion_per, recall_loc, recall_org, recall_per, f_loc, f_org, f_per)

              if f_loc + f_org + f_per >= self.max_f1:
                self.max_f1 = f_loc + f_org + f_per
                
                saver = tf.train.Saver()
                save_path = saver.save(sess, save_file)
                print "saved the best model with f1: %.5f" % (self.max_f1 / 3.0)

              self.last_f = f_loc + f_org + f_per
Beispiel #3
0
    def train(self, sess, save_file, X_train, y_train, X_val, y_val):
        saver = tf.train.Saver()

        char2id, id2char = helper.loadMap("char2id")
        label2id, id2label = helper.loadMap("label2id")

        merged = tf.contrib.deprecated.merge_all_summaries()
        summary_writer_train = tf.contrib.summary.SummaryWriter(
            'loss_log/train_loss', sess.graph)
        summary_writer_val = tf.contrib.summary.SummaryWriter(
            'loss_log/val_loss', sess.graph)

        num_iterations = int(math.ceil(1.0 * len(X_train) / self.batch_size))

        cnt = 0
        for epoch in range(self.num_epochs):
            # shuffle train in each epoch
            sh_index = np.arange(len(X_train))
            np.random.shuffle(sh_index)
            X_train = X_train[sh_index]
            y_train = y_train[sh_index]
            print("current epoch: %d" % (epoch))
            for iteration in range(num_iterations):
                # train
                X_train_batch, y_train_batch = helper.nextBatch(
                    X_train,
                    y_train,
                    start_index=iteration * self.batch_size,
                    batch_size=self.batch_size)
                y_train_weight_batch = 1 + np.array(
                    (y_train_batch == label2id['B']) |
                    (y_train_batch == label2id['E']), float)
                transition_batch = helper.getTransition(y_train_batch)

                _, loss_train, max_scores, max_scores_pre, length, train_summary = \
                 sess.run([
                  self.optimizer,
                  self.loss,
                  self.max_scores,
                  self.max_scores_pre,
                  self.length,
                  self.train_summary
                 ],
                  feed_dict={
                   self.targets_transition: transition_batch,
                   self.inputs: X_train_batch,
                   self.targets: y_train_batch,
                   self.targets_weight: y_train_weight_batch
                  })

                predicts_train = self.viterbi(max_scores,
                                              max_scores_pre,
                                              length,
                                              predict_size=self.batch_size)
                if iteration % 10 == 0:
                    cnt += 1
                    precision_train, recall_train, f1_train = self.evaluate(
                        X_train_batch, y_train_batch, predicts_train, id2char,
                        id2label)
                    summary_writer_train.add_summary(train_summary, cnt)
                    print(
                        "iteration: %5d, train loss: %5d, train precision: %.5f, train recall: %.5f, train f1: %.5f"
                        % (iteration, loss_train, precision_train,
                           recall_train, f1_train))

                # validation
                if iteration % 100 == 0:
                    X_val_batch, y_val_batch = helper.nextRandomBatch(
                        X_val, y_val, batch_size=self.batch_size)
                    y_val_weight_batch = 1 + np.array(
                        (y_val_batch == label2id['B']) |
                        (y_val_batch == label2id['E']), float)
                    transition_batch = helper.getTransition(y_val_batch)

                    loss_val, max_scores, max_scores_pre, length, val_summary = \
                     sess.run([
                      self.loss,
                      self.max_scores,
                      self.max_scores_pre,
                      self.length,
                      self.val_summary
                     ],
                      feed_dict={
                       self.targets_transition: transition_batch,
                       self.inputs: X_val_batch,
                       self.targets: y_val_batch,
                       self.targets_weight: y_val_weight_batch
                      })

                    predicts_val = self.viterbi(max_scores,
                                                max_scores_pre,
                                                length,
                                                predict_size=self.batch_size)
                    precision_val, recall_val, f1_val = self.evaluate(
                        X_val_batch, y_val_batch, predicts_val, id2char,
                        id2label)
                    summary_writer_val.add_summary(val_summary, cnt)
                    print(
                        "iteration: %5d, valid loss: %5d, valid precision: %.5f, valid recall: %.5f, valid f1: %.5f"
                        % (iteration, loss_val, precision_val, recall_val,
                           f1_val))

                    if f1_val > self.max_f1:
                        self.max_f1 = f1_val
                        save_path = saver.save(sess, save_file)
                        print("saved the best model with f1: %.5f" %
                              (self.max_f1))
    def train(self, sess, save_file, X_train, y_train, X_val, y_val):
        saver = tf.train.Saver()

        char2id, id2char = helper.loadMap("char2id")
        label2id, id2label = helper.loadMap("label2id")

        merged = tf.merge_all_summaries()
        summary_writer_train = tf.train.SummaryWriter('loss_log/train_loss', sess.graph)  
        summary_writer_val = tf.train.SummaryWriter('loss_log/val_loss', sess.graph)     
        
        num_iterations = int(math.ceil(1.0 * len(X_train) / self.batch_size))

        cnt = 0
        for epoch in range(self.num_epochs):
            # shuffle train in each epoch
            sh_index = np.arange(len(X_train))
            np.random.shuffle(sh_index)
            X_train = X_train[sh_index]
            y_train = y_train[sh_index]
            print "current epoch: %d" % (epoch)
            for iteration in range(num_iterations):
                # train
                X_train_batch, y_train_batch = helper.nextBatch(X_train, y_train, start_index=iteration * self.batch_size, batch_size=self.batch_size)
                y_train_weight_batch = 1 + np.array((y_train_batch == label2id['B']) | (y_train_batch == label2id['E']), float)
                transition_batch = helper.getTransition(y_train_batch)
                
                _, loss_train, max_scores, max_scores_pre, length, train_summary =\
                    sess.run([
                        self.optimizer, 
                        self.loss, 
                        self.max_scores, 
                        self.max_scores_pre, 
                        self.length,
                        self.train_summary
                    ], 
                    feed_dict={
                        self.targets_transition:transition_batch, 
                        self.inputs:X_train_batch, 
                        self.targets:y_train_batch, 
                        self.targets_weight:y_train_weight_batch
                    })

                predicts_train = self.viterbi(max_scores, max_scores_pre, length, predict_size=self.batch_size)
                if iteration % 10 == 0:
                    cnt += 1
                    precision_train, recall_train, f1_train = self.evaluate(X_train_batch, y_train_batch, predicts_train, id2char, id2label)
                    summary_writer_train.add_summary(train_summary, cnt)
                    print "iteration: %5d, train loss: %5d, train precision: %.5f, train recall: %.5f, train f1: %.5f" % (iteration, loss_train, precision_train, recall_train, f1_train)  
                    
                # validation
                if iteration % 100 == 0:
                    X_val_batch, y_val_batch = helper.nextRandomBatch(X_val, y_val, batch_size=self.batch_size)
                    y_val_weight_batch = 1 + np.array((y_val_batch == label2id['B']) | (y_val_batch == label2id['E']), float)
                    transition_batch = helper.getTransition(y_val_batch)
                    
                    loss_val, max_scores, max_scores_pre, length, val_summary =\
                        sess.run([
                            self.loss, 
                            self.max_scores, 
                            self.max_scores_pre, 
                            self.length,
                            self.val_summary
                        ], 
                        feed_dict={
                            self.targets_transition:transition_batch, 
                            self.inputs:X_val_batch, 
                            self.targets:y_val_batch, 
                            self.targets_weight:y_val_weight_batch
                        })
                    
                    predicts_val = self.viterbi(max_scores, max_scores_pre, length, predict_size=self.batch_size)
                    precision_val, recall_val, f1_val = self.evaluate(X_val_batch, y_val_batch, predicts_val, id2char, id2label)
                    summary_writer_val.add_summary(val_summary, cnt)
                    print "iteration: %5d, valid loss: %5d, valid precision: %.5f, valid recall: %.5f, valid f1: %.5f" % (iteration, loss_val, precision_val, recall_val, f1_val)

                    if f1_val > self.max_f1:
                        self.max_f1 = f1_val
                        save_path = saver.save(sess, save_file)
                        print "saved the best model with f1: %.5f" % (self.max_f1)
    def train_an_iteration(self, sess, save_file, X_train, y_train, X_val, y_val, char2id, id2char, label2id, id2label, summary_writer_train_source, summary_writer_train_target, summary_writer_val_source, summary_writer_val_target, flag="source", is_summary=False, is_validation=False, X_test_source=None, y_test_source=None):
        saver = tf.train.Saver()
        
#        num_iterations = int(math.ceil(1.0 * len(X_train) / self.batch_size))
        num_iterations = 1       
        cnt = 0
        for iteration in range(num_iterations):
            print "this iteration is %d, the flag is %s"%(iteration, flag)
            # train, the flag indicate the data source
            X_train_batch, y_train_batch = helper.nextBatch(X_train, y_train, start_index=iteration * self.batch_size, batch_size=self.batch_size)
#            y_train_weight_batch = 1 + np.array((y_train_batch == label2id['B']) | (y_train_batch == label2id['E']), float)
            transition_batch = helper.getTransition(y_train_batch)
                
            _, loss_train, max_scores, max_scores_pre, length, train_summary =\
                sess.run([
                    self.optimizer_source if flag == "source" else self.optimizer_target,
                    self.loss_source if flag == "source" else self.loss_target,
                    self.max_scores_source if flag == "source" else self.max_scores_target,
                    self.max_scores_pre_source if flag == "source" else self.max_scores_pre_target,
                    self.length,
                    self.train_summary_source if flag == "source" else self.train_summary_target
                ], 
                feed_dict={
                    self.targets_transition:transition_batch, 
                    self.inputs:X_train_batch, 
                    self.targets:y_train_batch, 
 #                   self.targets_weight:y_train_weight_batch
                })

##            predicts_train = self.viterbi(max_scores, max_scores_pre, length, predict_size=self.batch_size)
##            if is_summary:
##                cnt += 1
##                presicion_loc, recall_loc, f_loc, presicion_org, recall_org, f_org, presicion_per, recall_per, f_per  = self.evaluate(X_train_batch, y_train_batch, predicts_train, id2char, id2label)
#                if flag == "source":
#                    summary_writer_train_source.add_summary(train_summary, cnt)
#                else:
#                    summary_writer_train_target.add_summary(train_summary, cnt)
                 
##                print "iteration: %5d, %s train loss: %5d, train precision: LOC %.5f, ORG %.5f, PER %.5f, train recall: LOC %.5f, ORG %.5f, PER %.5f, train f1: LOC %.5f, ORG %.5f, PER %.5f" % (iteration, flag, loss_train, presicion_loc, presicion_org, presicion_per, recall_loc, recall_org, recall_per, f_loc, f_org, f_per)
            # validation
    ##        if is_validation:
    ##            X_val_batch, y_val_batch = helper.nextRandomBatch(X_test_source, y_test_source, batch_size=self.batch_size)
#   ##             y_val_weight_batch = 1 + np.array((y_val_batch == label2id['B']) | (y_val_batch == label2id['E']), float)
    ##            transition_batch = helper.getTransition(y_val_batch)
    ##                
    ##            loss_val, max_scores, max_scores_pre, length, val_summary =\
    ##                sess.run([
    ##                    self.loss_source if flag == "source" else self.loss_target,
    ##                    self.max_scores_source if flag == "source" else self.max_scores_target,
    ##                   self.max_scores_pre_source if flag == "source" else self.max_scores_pre_target,
    ##                  self.length,
    ##                 self.train_summary_source if flag == "source" else self.train_summary_target
    ##                ],
    ##                feed_dict={
    ##                    self.targets_transition:transition_batch, 
    ##                    self.inputs:X_val_batch, 
    ##                    self.targets:y_val_batch, 
 #  ##                     self.targets_weight:y_val_weight_batch
    ##                })
     ##           
     ##           predicts_val = self.viterbi(max_scores, max_scores_pre, length, predict_size=self.batch_size)
     ##           presicion_loc, recall_loc, f_loc, presicion_org, recall_org, f_org, presicion_per, recall_per, f_per  = self.evaluate(X_val_batch, y_val_batch, predicts_val, id2char, id2label)
#               if flag == "source":
#                    summary_writer_val_source.add_summary(val_summary, cnt)
#                else:
#                   summary_writer_val_target.add_summary(val_summary, cnt)
            if is_summary:
              presicion_loc, recall_loc, f_loc, presicion_org, recall_org, f_org, presicion_per, recall_per, f_per = self.predictBatch(sess, X_test_source, y_test_source, id2label, id2char, label2id, self.batch_size, "target")
              print "iteration: %5d, %s valid , valid precision: LOC %.5f, ORG %.5f, PER %.5f, valid recall: LOC %.5f, ORG %.5f, PER %.5f, valid f1: LOC %.5f, ORG %.5f, PER %.5f" % (iteration, flag, presicion_loc, presicion_org, presicion_per, recall_loc, recall_org, recall_per, f_loc, f_org, f_per)

              if f_loc + f_org + f_per >= self.max_f1:
                self.max_f1 = f_loc + f_org + f_per
                save_path = saver.save(sess, save_file)
                print "saved the best model with f1: %.5f" % (self.max_f1 / 3.0)


#                print "********************************************************"
#                print "********************************************************"
#                X = helper.getPredict("./a", char2id)
#                saver = tf.train.Saver()
#                saver.restore(sess, model_path)
#                model.predictBatch(sess, X, y_true, id2label, id2char, label2id, 128, "source")
#                print "********************************************************"
#                print "********************************************************"
              self.last_f = f_loc + f_org + f_per