tests/cpp: Add tests for regression_obj.cc

Test the objective functions in regression_obj.cc

tests/cpp: Add tests for objective.cc and RegLossObj
This commit is contained in:
AbdealiJK
2016-12-03 14:06:51 +05:30
committed by Tianqi Chen
parent fd99d39372
commit d41aab4f61
4 changed files with 112 additions and 0 deletions

View File

@@ -24,3 +24,28 @@ std::string CreateSimpleTestData() {
fo.close();
return tmp_file;
}
void CheckObjFunction(xgboost::ObjFunction * obj,
std::vector<xgboost::bst_float> preds,
std::vector<xgboost::bst_float> labels,
std::vector<xgboost::bst_float> weights,
std::vector<xgboost::bst_float> out_grad,
std::vector<xgboost::bst_float> out_hess) {
xgboost::MetaInfo info;
info.num_row = labels.size();
info.labels = labels;
info.weights = weights;
std::vector<xgboost::bst_gpair> gpair;
obj->GetGradient(preds, info, 1, &gpair);
ASSERT_EQ(gpair.size(), preds.size());
for (int i = 0; i < gpair.size(); ++i) {
EXPECT_NEAR(gpair[i].grad, out_grad[i], 0.01)
<< "Unexpected grad for pred=" << preds[i] << " label=" << labels[i]
<< " weight=" << weights[i];
EXPECT_NEAR(gpair[i].hess, out_hess[i], 0.01)
<< "Unexpected hess for pred=" << preds[i] << " label=" << labels[i]
<< " weight=" << weights[i];
}
}