Пример #1
0
    def Evaluate(self, sess, val_batches, score_file_path):
        labels = []
        self.all_candidate_scores = []
        val_batch_num = len(val_batches["response"])

        # eva_score_file = open(score_file_path, 'w')
        for batch_index in xrange(val_batch_num):
            feed_dict = {
                self.utterance_ph:
                np.array(val_batches["turns"][batch_index]),
                self.all_utterance_len_ph:
                np.array(val_batches["every_turn_len"][batch_index]),
                self.response_ph:
                np.array(val_batches["response"][batch_index]),
                self.response_len:
                np.array(val_batches["response_len"][batch_index]),
                self.y_true:
                np.array(val_batches["label"][batch_index])
            }
            val_loss = sess.run(self.total_loss, feed_dict=feed_dict)
            #  print('val_loss',val_loss)
            candidate_scores = sess.run(self.y_pred, feed_dict=feed_dict)
            self.all_candidate_scores.append(candidate_scores[:, 1])

            labels.extend(val_batches["label"][batch_index])
        #  for i in xrange(len(val_batches["label"][batch_index])):
        #  eva_score_file.write(str(candidate_scores[i]) +'\t'+str(val_batches["label"][batch_index][i])+ '\n')

    # eva_score_file.close()
        all_candidate_scores = np.concatenate(self.all_candidate_scores,
                                              axis=0)
        Evaluate.ComputeR10_1(all_candidate_scores, labels)
        Evaluate.ComputeR10_2(all_candidate_scores, labels)
        Evaluate.ComputeR10_5(all_candidate_scores, labels)
        Evaluate.ComputeR2_1(all_candidate_scores, labels)
Пример #2
0
    def TestModel(self, conf):
        start = time.time()
        conf['keep_prob'] = 1
        if not os.path.exists(conf['save_path']):
            os.makedirs(conf['save_path'])
        print('beging test starting loading data')
        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        train_data, val_data, test_data = pickle.load(
            open(conf["data_path"], 'rb'))
        print('finish loading data')

        test_batches = reader.build_batches(test_data, conf)

        print("finish building test batches")
        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

        # refine conf
        test_batch_num = len(test_batches["response"])

        with tf.Session() as sess:
            saver = tf.train.Saver()
            # with tf.Session() as sess:
            # sess.run(init)
            saver.restore(sess, os.path.join(conf["init_model"], "model.3"))
            print("sucess init %s" % conf["init_model"])

            score_file_path = conf['save_path'] + 'score.test'
            score_file = open(score_file_path, 'w')
            all_candidate_score = []
            labels = []
            for batch_index in xrange(test_batch_num):
                # print('utterance_ph',np.array(test_batches["turns"][batch_index]).shape)
                feed_dict = {
                    self.utterance_ph:
                    np.array(test_batches["turns"][batch_index]),
                    # _model.tt_turns_len: test_batches["tt_turns_len"][batch_index],
                    self.all_utterance_len_ph:
                    np.array(test_batches["every_turn_len"][batch_index]),
                    self.response_ph:
                    np.array(test_batches["response"][batch_index]),
                    self.response_len:
                    np.array(test_batches["response_len"][batch_index]),
                    # _model.label: test_batches["label"][batch_index]
                }
                #  last_hidden = sess.run(self.last_hidden, feed_dict=feed_dict)
                # print('last_hidden', last_hidden.shape)
                candidate_scores = sess.run(self.y_pred, feed_dict=feed_dict)
                all_candidate_score.append(candidate_scores[:, 1])
                # scores = sess.run(_model.logits, feed_dict=feed)

                for i in xrange(conf["batch_size"]):
                    score_file.write(
                        str(candidate_scores[i]) + '\t' +
                        str(test_batches["label"][batch_index][i]) + '\n')
                    labels.append(test_batches["label"][batch_index][i])
            score_file.close()

            all_candidate_scores = np.concatenate(all_candidate_score, axis=0)
            Evaluate.ComputeR10_1(all_candidate_scores, labels)
            Evaluate.ComputeR10_2(all_candidate_scores, labels)
            Evaluate.ComputeR10_5(all_candidate_scores, labels)
            Evaluate.ComputeR2_1(all_candidate_scores, labels)
            #douban_evaluation.evaluate(all_candidate_scores, labels)

        end = time.time()
        gap = (end - start) / 3600
        print('test time:%.4f h' % gap)