test_Evaluator.cpp 8.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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"
18
#include "paddle/testing/TestUtil.h"
Y
Yu Yang 已提交
19
#include "paddle/trainer/Trainer.h"
Z
zhangjinchao01 已提交
20 21 22 23

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

24 25 26
DECLARE_bool(use_gpu);
DECLARE_int32(gpu_id);
DECLARE_bool(thread_local_rand_use_global_seed);
Z
zhangjinchao01 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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) {}
};

50 51 52 53
void testEvaluator(TestConfig testConf,
                   string testEvaluatorName,
                   size_t batchSize,
                   bool useGpu) {
Z
zhangjinchao01 已提交
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
#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:
83 84 85 86
        data.value = makeRandomSparseMatrix(batchSize,
                                            dim,
                                            /* withValue= */ false,
                                            useGpu);
Z
zhangjinchao01 已提交
87 88 89 90 91 92
        break;
      default:
        LOG(FATAL) << " unknown inputType ";
        return;
    }

93 94 95 96 97 98 99 100 101
    ICpuGpuVectorPtr sequenceStartPositions;
    if (testConf.inputDefs[i].inputType == INPUT_SEQUENCE_DATA ||
        testConf.inputDefs[i].inputType == INPUT_SEQUENCE_LABEL) {
      if (!sequenceStartPositions) {
        generateSequenceStartPositions(batchSize, sequenceStartPositions);
      }
      data.sequenceStartPositions = sequenceStartPositions;
    }

Z
zhangjinchao01 已提交
102 103 104 105 106
    arguments.push_back(data);
  }

  Evaluator* testEvaluator = Evaluator::create(testConf.evaluatorConfig);
  double totalScore = 0.0;
107
  testEvaluator->start();
Z
zhangjinchao01 已提交
108 109
  totalScore += testEvaluator->evalImp(arguments);
  testEvaluator->updateSamplesNum(arguments);
110
  testEvaluator->finish();
Z
zhangjinchao01 已提交
111 112
  LOG(INFO) << *testEvaluator;

Y
Yu Yang 已提交
113 114 115 116
  std::vector<std::string> names;
  testEvaluator->getNames(&names);
  paddle::Error err;
  for (auto& name : names) {
Y
Yu Yang 已提交
117
    auto value = testEvaluator->getValue(name, &err);
Y
Yu Yang 已提交
118 119 120 121 122 123 124
    ASSERT_TRUE(err.isOK());
    LOG(INFO) << name << " " << value;
    auto tp = testEvaluator->getType(name, &err);
    ASSERT_TRUE(err.isOK());
    ASSERT_EQ(testConf.evaluatorConfig.type(), tp);
  }

Z
zhangjinchao01 已提交
125 126
  double totalScore2 = 0.0;
  if (testConf.testAccumulate) {
127
    testEvaluator->start();
Z
zhangjinchao01 已提交
128
    totalScore2 += testEvaluator->evalImp(arguments);
129
    testEvaluator->finish();
Z
zhangjinchao01 已提交
130 131 132 133
    EXPECT_LE(fabs(totalScore - totalScore2), 1.0e-5);
  }
}

134 135 136
void testEvaluatorAll(TestConfig testConf,
                      string testEvaluatorName,
                      size_t batchSize) {
Z
zhangjinchao01 已提交
137 138 139 140
  testEvaluator(testConf, testEvaluatorName, batchSize, true);
  testEvaluator(testConf, testEvaluatorName, batchSize, false);
}

Y
yangyaming 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
TEST(Evaluator, detection_map) {
  TestConfig config;
  config.evaluatorConfig.set_type("detection_map");
  config.evaluatorConfig.set_overlap_threshold(0.5);
  config.evaluatorConfig.set_background_id(0);
  config.evaluatorConfig.set_ap_type("Integral");
  config.evaluatorConfig.set_evaluate_difficult(0);

  config.inputDefs.push_back({INPUT_DATA, "output", 7});
  config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "label", 6});
  config.evaluatorConfig.set_evaluate_difficult(false);
  testEvaluatorAll(config, "detection_map", 100);

  config.evaluatorConfig.set_evaluate_difficult(true);
  testEvaluatorAll(config, "detection_map", 100);
}

Z
zhangjinchao01 已提交
158 159 160
TEST(Evaluator, classification_error) {
  TestConfig config;
  config.evaluatorConfig.set_type("classification_error");
L
Liang Zhao 已提交
161
  config.evaluatorConfig.set_top_k(5);
Z
zhangjinchao01 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

  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
179 180
  testEvaluator(
      config, "classification_error_weight_multi_binary_label", 50, false);
Z
zhangjinchao01 已提交
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

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
248 249
  testEvaluator(
      config, "precision_recall_weight_multi_binary_label", 100, false);
Z
zhangjinchao01 已提交
250 251
}

252 253 254 255 256 257 258 259 260
TEST(Evaluator, ctc_error_evaluator) {
  TestConfig config;
  config.evaluatorConfig.set_type("ctc_edit_distance");

  config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "output", 32});
  config.inputDefs.push_back({INPUT_SEQUENCE_LABEL, "label", 1});
  testEvaluatorAll(config, "ctc_error_evaluator", 100);
}

Z
zhangjinchao01 已提交
261 262 263 264 265 266 267
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();
}