예제 #1
0
    def train(self):
        self.opt_op()
        init = tf.global_variables_initializer()
        self.sess.run(init)

        # 划分数据batch
        iterations, next_iterator = dataset_iterator(
            self.train_data[tr_sents_up],
            self.train_data[tr_positions_up],
            self.train_data[tr_pos_up],
            self.train_data[tr_sts_up],
            self.train_data[tr_sents_down],
            self.train_data[tr_positions_down],
            self.train_data[tr_pos_down],
            self.train_data[tr_sts_down],
            self.train_data[tr_labels],
            len(self.train_data[tr_sents_up])
        )
        for epoch in range(self.n_epochs):
            count = 0
            train_loss = 0
            train_acc = 0
            print("-----------Now we begin the %dth epoch-----------" % (epoch))
            for iter in range(iterations):
                word_up_batch, position_up_batch, poses_up_batch, sts_up_batch, \
                word_down_batch, position_down_batch, poses_down_batch, sts_down_batch, \
                labels_batch = self.sess.run(next_iterator)

                if len(word_up_batch) < self.batch_size:
                    continue

                f_dict = {
                    self.word_up: word_up_batch,
                    self.positons_up: position_up_batch,
                    self.poses_up: poses_up_batch,
                    self.sts_up: sts_up_batch,
                    self.word_down: word_down_batch,
                    self.positons_down: position_down_batch,
                    self.poses_down: poses_down_batch,
                    self.sts_down: sts_down_batch,
                    self.labels: labels_batch,
                    self.dropout_keep_prob: 0.5
                }

                _, loss, acc = self.sess.run([self.train_op, self.loss_op, self.accuracy_op], feed_dict=f_dict)
                train_loss = train_loss+loss
                train_acc = train_acc+acc
                count = count + 1
            train_loss = train_loss / count
            train_acc = train_acc / count
            print("-----------After the %dth epoch, the train loss is: %f, the train acc is: %f-----------" % (epoch, train_loss, train_acc))

            # test
            iterations_test, next_iterator_test = dataset_iterator(
                self.test_data[te_sents_up],
                self.test_data[te_positions_up],
                self.test_data[te_pos_up],
                self.test_data[te_sts_up],
                self.test_data[te_sents_down],
                self.test_data[te_positions_down],
                self.test_data[te_pos_down],
                self.test_data[te_sts_down],
                self.test_data[te_labels],
                len(self.test_data[te_sents_up])
            )
            self.test(iterations_test, next_iterator_test, epoch, self.loss_op, self.accuracy_op)
예제 #2
0
    def train(self):
        self.opt_op()
        init = tf.global_variables_initializer()
        self.sess.run(init)
        saver = tf.train.Saver()

        # 划分数据batch
        iterations, next_iterator = dataset_iterator(
            self.train_data[tr_sents_up], self.train_data[tr_positions_up],
            self.train_data[tr_pos_up], self.train_data[tr_sts_up],
            self.train_data[tr_sents_down], self.train_data[tr_positions_down],
            self.train_data[tr_pos_down], self.train_data[tr_sts_down],
            self.train_data[tr_labels], len(self.train_data[tr_sents_up]))
        # max_test_acc = 0
        for epoch in range(self.n_epochs):
            count = 0
            train_loss = 0
            train_acc = 0
            print("-----------Now we begin the %dth epoch-----------" %
                  (epoch))
            for iter in range(iterations):
                word_up_batch, position_up_batch, poses_up_batch, sts_up_batch, \
                word_down_batch, position_down_batch, poses_down_batch, sts_down_batch, \
                labels_batch = self.sess.run(next_iterator)

                if len(word_up_batch) < self.batch_size:
                    continue

                f_dict = {
                    self.word_up: word_up_batch,
                    self.positons_up: position_up_batch,
                    self.poses_up: poses_up_batch,
                    self.sts_up: sts_up_batch,
                    self.word_down: word_down_batch,
                    self.positons_down: position_down_batch,
                    self.poses_down: poses_down_batch,
                    self.sts_down: sts_down_batch,
                    self.labels: labels_batch,
                    self.dropout_keep_prob: 0.5
                }

                _, loss, acc, att_up, att_down = self.sess.run(
                    [
                        self.train_op, self.loss_op, self.accuracy_op,
                        self.alpha_up, self.alpha_down
                    ],
                    feed_dict=f_dict)
                train_loss = train_loss + loss
                train_acc = train_acc + acc
                count = count + 1

                if iter == 2:
                    print(str(att_up.shape) + str(att_down.shape))
                    print("up att: ",
                          [float("%.3f" % e[0]) for e in att_up[3][:]])
                    print("%d up id: ", [e for e in word_up_batch[3][:]])
                    print("%d down att: ",
                          [float("%.3f" % e[0]) for e in att_down[3][:]])
                    print("%d down id: ", [e for e in word_down_batch[3][:]])

            train_loss = train_loss / count
            train_acc = train_acc / count
            print("---%dth epoch train loss: %f, train acc: %f---" %
                  (epoch, train_loss, train_acc))

            # test
            iterations_test, next_iterator_test = dataset_iterator(
                self.test_data[te_sents_up], self.test_data[te_positions_up],
                self.test_data[te_pos_up], self.test_data[te_sts_up],
                self.test_data[te_sents_down],
                self.test_data[te_positions_down], self.test_data[te_pos_down],
                self.test_data[te_sts_down], self.test_data[te_labels],
                len(self.test_data[te_sents_up]))
            self.test(iterations_test, next_iterator_test, epoch, self.loss_op,
                      self.accuracy_op)