test_Evaluator.cpp 6.9 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */


#include <gtest/gtest.h>
#include <vector>
#include "ModelConfig.pb.h"
#include "paddle/trainer/Trainer.h"
#include "TestUtil.h"

using namespace paddle;  // NOLINT
using namespace std;     // NOLINT

P_DECLARE_bool(use_gpu);
P_DECLARE_int32(gpu_id);
P_DECLARE_bool(thread_local_rand_use_global_seed);

enum InputType {
  INPUT_DATA,         // dense vector
  INPUT_LABEL,        // id
  INPUT_DATA_TARGET,  // dense vector, but no gradient
  INPUT_SEQUENCE_DATA,
  INPUT_SEQUENCE_LABEL,
  INPUT_SPARSE_NON_VALUE_DATA
};

struct InputDef {
  InputType inputType;
  string name;
  size_t dim;
};

struct TestConfig {
  EvaluatorConfig evaluatorConfig;
  std::vector<InputDef> inputDefs;
  bool testAccumulate;
  TestConfig() : testAccumulate(true) {}
};

void testEvaluator(TestConfig testConf, string testEvaluatorName,
                   size_t batchSize, bool useGpu) {
#ifdef PADDLE_ONLY_CPU
  if (useGpu) return;
#endif
  FLAGS_use_gpu = useGpu;
  testConf.evaluatorConfig.set_name(testEvaluatorName);
  LOG(INFO) << " evaluator_type=" << testConf.evaluatorConfig.type()
            << " useGpu=" << useGpu;

  std::vector<Argument> arguments;
  for (size_t i = 0; i < testConf.inputDefs.size(); ++i) {
    Argument data;
    size_t dim = testConf.inputDefs[i].dim;
    switch (testConf.inputDefs[i].inputType) {
      case INPUT_DATA:
      case INPUT_SEQUENCE_DATA:
      case INPUT_DATA_TARGET:
        data.value = Matrix::create(batchSize, dim, false, useGpu);
        data.value->randomizeUniform();

        // make sure output > 0 && output < 1
        data.value->add(-0.5);
        data.value->sigmoid(*data.value);
        break;
      case INPUT_LABEL:
      case INPUT_SEQUENCE_LABEL:
        data.ids = VectorT<int>::create(batchSize, useGpu);
        data.ids->rand(dim);  // now rand number can be 0 to inputDefs[i].dim.
        break;
      case INPUT_SPARSE_NON_VALUE_DATA:
        data.value = makeRandomSparseMatrix(batchSize, dim,
                                            /* withValue= */ false, useGpu);
        break;
      default:
        LOG(FATAL) << " unknown inputType ";
        return;
    }

    arguments.push_back(data);
  }

  Evaluator* testEvaluator = Evaluator::create(testConf.evaluatorConfig);
  double totalScore = 0.0;
  totalScore += testEvaluator->evalImp(arguments);
  testEvaluator->updateSamplesNum(arguments);
  LOG(INFO) << *testEvaluator;

  double totalScore2 = 0.0;
  if (testConf.testAccumulate) {
    totalScore2 += testEvaluator->evalImp(arguments);
    EXPECT_LE(fabs(totalScore - totalScore2), 1.0e-5);
  }
}

void testEvaluatorAll(TestConfig testConf, string testEvaluatorName,
                   size_t batchSize) {
  testEvaluator(testConf, testEvaluatorName, batchSize, true);
  testEvaluator(testConf, testEvaluatorName, batchSize, false);
}

TEST(Evaluator, classification_error) {
  TestConfig config;
  config.evaluatorConfig.set_type("classification_error");

  config.inputDefs.push_back({INPUT_DATA, "output", 50});
  config.inputDefs.push_back({INPUT_LABEL, "label", 50});
  testEvaluatorAll(config, "classification_error", 100);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  testEvaluatorAll(config, "classification_error_weight", 100);

  // multi binary labels
  config.inputDefs.clear();
  config.inputDefs.push_back({INPUT_DATA, "output", 100});
  config.inputDefs.push_back({INPUT_SPARSE_NON_VALUE_DATA, "label", 100});
  // Not support GPU
  testEvaluator(config, "classification_error_multi_binary_label", 50, false);

  config.evaluatorConfig.set_classification_threshold(0.4);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  // Not support GPU
  testEvaluator(config, "classification_error_weight_multi_binary_label", 50,
                false);
}

TEST(Evaluator, sum) {
  TestConfig config;
  config.evaluatorConfig.set_type("sum");

  // sum of output
  config.inputDefs.push_back({INPUT_DATA, "output", 10});
  testEvaluatorAll(config, "sum_output", 200);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  testEvaluatorAll(config, "sum_output_weight", 200);

  // sum of label
  config.inputDefs.clear();
  config.inputDefs.push_back({INPUT_LABEL, "label", 10});
  testEvaluatorAll(config, "sum_label", 200);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  testEvaluatorAll(config, "sum_label_weight", 200);
}

TEST(Evaluator, last_column_sum) {
  TestConfig config;
  config.evaluatorConfig.set_type("last-column-sum");

  config.inputDefs.push_back({INPUT_DATA, "output", 50});
  testEvaluatorAll(config, "last-column-sum", 200);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  testEvaluatorAll(config, "last-column-sum_weight", 200);
}

TEST(Evaluator, last_column_auc) {
  TestConfig config;
  config.evaluatorConfig.set_type("last-column-auc");

  config.inputDefs.push_back({INPUT_DATA, "output", 2});
  config.inputDefs.push_back({INPUT_LABEL, "label", 2});
  testEvaluatorAll(config, "last-column-auc", 500);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  testEvaluatorAll(config, "last-column-auc_weight", 200);
}

TEST(Evaluator, precision_recall) {
  TestConfig config;
  config.evaluatorConfig.set_type("precision_recall");

  config.inputDefs.push_back({INPUT_DATA, "output", 10});
  config.inputDefs.push_back({INPUT_LABEL, "label", 10});
  testEvaluatorAll(config, "precision_recall", 200);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  testEvaluatorAll(config, "precision_recall_weight", 200);

  LOG(INFO) << "positive_label = 5";
  config.evaluatorConfig.set_positive_label(5);
  testEvaluatorAll(config, "precision_recall_weight", 200);

  // multi binary labels
  config.inputDefs.clear();
  config.evaluatorConfig.set_positive_label(-1);
  config.inputDefs.push_back({INPUT_DATA, "output", 10});
  config.inputDefs.push_back({INPUT_SPARSE_NON_VALUE_DATA, "label", 10});
  // Not support GPU
  testEvaluator(config, "precision_recall_multi_binary_label", 100, false);

  LOG(INFO) << "classification_threshold = 0.4";
  config.evaluatorConfig.set_classification_threshold(0.4);
  config.inputDefs.push_back({INPUT_DATA, "weight", 1});
  // Not support GPU
  testEvaluator(config, "precision_recall_weight_multi_binary_label", 100,
                false);
}

int main(int argc, char** argv) {
  initMain(argc, argv);
  FLAGS_thread_local_rand_use_global_seed = true;
  srand(1);
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}