Config for linear updaters. (#5222)
This commit is contained in:
parent
40680368cf
commit
3eb1279bbf
@ -8,6 +8,7 @@
|
||||
#include <xgboost/data.h>
|
||||
#include <xgboost/generic_parameters.h>
|
||||
#include <xgboost/host_device_vector.h>
|
||||
#include <xgboost/model.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
@ -17,6 +18,8 @@
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
class Json;
|
||||
|
||||
namespace gbm {
|
||||
class GBLinearModel;
|
||||
} // namespace gbm
|
||||
@ -24,7 +27,7 @@ class GBLinearModel;
|
||||
/*!
|
||||
* \brief interface of linear updater
|
||||
*/
|
||||
class LinearUpdater {
|
||||
class LinearUpdater : public Configurable {
|
||||
protected:
|
||||
GenericParameter const* learner_param_;
|
||||
|
||||
|
||||
@ -102,11 +102,18 @@ class GBLinear : public GradientBooster {
|
||||
void LoadConfig(Json const& in) override {
|
||||
CHECK_EQ(get<String>(in["name"]), "gblinear");
|
||||
fromJson(in["gblinear_train_param"], ¶m_);
|
||||
updater_.reset(LinearUpdater::Create(param_.updater, generic_param_));
|
||||
this->updater_->LoadConfig(in["updater"]);
|
||||
}
|
||||
void SaveConfig(Json* p_out) const override {
|
||||
auto& out = *p_out;
|
||||
out["name"] = String{"gblinear"};
|
||||
out["gblinear_train_param"] = toJson(param_);
|
||||
|
||||
out["updater"] = Object();
|
||||
auto& j_updater = out["updater"];
|
||||
CHECK(this->updater_);
|
||||
this->updater_->SaveConfig(&j_updater);
|
||||
}
|
||||
|
||||
void DoBoost(DMatrix *p_fmat,
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include "./param.h"
|
||||
#include "../common/timer.h"
|
||||
#include "coordinate_common.h"
|
||||
#include "xgboost/json.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace linear {
|
||||
@ -32,6 +33,18 @@ class CoordinateUpdater : public LinearUpdater {
|
||||
selector_.reset(FeatureSelector::Create(tparam_.feature_selector));
|
||||
monitor_.Init("CoordinateUpdater");
|
||||
}
|
||||
|
||||
void LoadConfig(Json const& in) override {
|
||||
auto const& config = get<Object const>(in);
|
||||
fromJson(config.at("linear_train_param"), &tparam_);
|
||||
fromJson(config.at("coordinate_param"), &cparam_);
|
||||
}
|
||||
void SaveConfig(Json* p_out) const override {
|
||||
auto& out = *p_out;
|
||||
out["linear_train_param"] = toJson(tparam_);
|
||||
out["coordinate_param"] = toJson(cparam_);
|
||||
}
|
||||
|
||||
void Update(HostDeviceVector<GradientPair> *in_gpair, DMatrix *p_fmat,
|
||||
gbm::GBLinearModel *model, double sum_instance_weight) override {
|
||||
tparam_.DenormalizePenalties(sum_instance_weight);
|
||||
|
||||
@ -37,10 +37,22 @@ class GPUCoordinateUpdater : public LinearUpdater { // NOLINT
|
||||
// set training parameter
|
||||
void Configure(Args const& args) override {
|
||||
tparam_.UpdateAllowUnknown(args);
|
||||
coord_param_.UpdateAllowUnknown(args);
|
||||
selector_.reset(FeatureSelector::Create(tparam_.feature_selector));
|
||||
monitor_.Init("GPUCoordinateUpdater");
|
||||
}
|
||||
|
||||
void LoadConfig(Json const& in) override {
|
||||
auto const& config = get<Object const>(in);
|
||||
fromJson(config.at("linear_train_param"), &tparam_);
|
||||
fromJson(config.at("coordinate_param"), &coord_param_);
|
||||
}
|
||||
void SaveConfig(Json* p_out) const override {
|
||||
auto& out = *p_out;
|
||||
out["linear_train_param"] = toJson(tparam_);
|
||||
out["coordinate_param"] = toJson(coord_param_);
|
||||
}
|
||||
|
||||
void LazyInitDevice(DMatrix *p_fmat, const LearnerModelParam &model_param) {
|
||||
if (learner_param_->gpu_id < 0) return;
|
||||
|
||||
|
||||
@ -23,6 +23,15 @@ class ShotgunUpdater : public LinearUpdater {
|
||||
}
|
||||
selector_.reset(FeatureSelector::Create(param_.feature_selector));
|
||||
}
|
||||
void LoadConfig(Json const& in) override {
|
||||
auto const& config = get<Object const>(in);
|
||||
fromJson(config.at("linear_train_param"), ¶m_);
|
||||
}
|
||||
void SaveConfig(Json* p_out) const override {
|
||||
auto& out = *p_out;
|
||||
out["linear_train_param"] = toJson(param_);
|
||||
}
|
||||
|
||||
void Update(HostDeviceVector<GradientPair> *in_gpair, DMatrix *p_fmat,
|
||||
gbm::GBLinearModel *model, double sum_instance_weight) override {
|
||||
auto &gpair = in_gpair->HostVector();
|
||||
|
||||
@ -112,7 +112,7 @@ TEST(c_api, ConfigIO) {
|
||||
delete pp_dmat;
|
||||
}
|
||||
|
||||
TEST(c_api, Json_ModelIO) {
|
||||
TEST(c_api, JsonModelIO) {
|
||||
size_t constexpr kRows = 10;
|
||||
dmlc::TemporaryDirectory tempdir;
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
namespace xgboost {
|
||||
namespace gbm {
|
||||
|
||||
TEST(GBLinear, Json_IO) {
|
||||
TEST(GBLinear, JsonIO) {
|
||||
size_t constexpr kRows = 16, kCols = 16;
|
||||
|
||||
LearnerModelParam param;
|
||||
|
||||
@ -103,8 +103,8 @@ TEST(GBTree, ChoosePredictor) {
|
||||
}
|
||||
#endif // XGBOOST_USE_CUDA
|
||||
|
||||
// Some other parts of test are in `Tree.Json_IO'.
|
||||
TEST(GBTree, Json_IO) {
|
||||
// Some other parts of test are in `Tree.JsonIO'.
|
||||
TEST(GBTree, JsonIO) {
|
||||
size_t constexpr kRows = 16, kCols = 16;
|
||||
|
||||
LearnerModelParam mparam;
|
||||
@ -143,7 +143,7 @@ TEST(GBTree, Json_IO) {
|
||||
ASSERT_EQ(get<String>(j_train_param["num_parallel_tree"]), "1");
|
||||
}
|
||||
|
||||
TEST(Dart, Json_IO) {
|
||||
TEST(Dart, JsonIO) {
|
||||
size_t constexpr kRows = 16, kCols = 16;
|
||||
|
||||
LearnerModelParam mparam;
|
||||
|
||||
41
tests/cpp/linear/test_json_io.h
Normal file
41
tests/cpp/linear/test_json_io.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*!
|
||||
* Copyright 2020 XGBoost contributors
|
||||
*/
|
||||
#ifndef XGBOOST_TEST_JSON_IO_H_
|
||||
#define XGBOOST_TEST_JSON_IO_H_
|
||||
|
||||
#include <xgboost/linear_updater.h>
|
||||
#include <xgboost/json.h>
|
||||
#include <string>
|
||||
#include "../helpers.h"
|
||||
#include "../../../src/gbm/gblinear_model.h"
|
||||
|
||||
namespace xgboost {
|
||||
inline void TestUpdaterJsonIO(std::string updater_str) {
|
||||
auto runtime = xgboost::CreateEmptyGenericParam(GPUIDX);
|
||||
Json config_0 {Object() };
|
||||
|
||||
{
|
||||
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
|
||||
xgboost::LinearUpdater::Create(updater_str, &runtime));
|
||||
updater->Configure({{"eta", std::to_string(3.14)}});
|
||||
updater->SaveConfig(&config_0);
|
||||
}
|
||||
|
||||
{
|
||||
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
|
||||
xgboost::LinearUpdater::Create(updater_str, &runtime));
|
||||
updater->LoadConfig(config_0);
|
||||
Json config_1 { Object() };
|
||||
updater->SaveConfig(&config_1);
|
||||
|
||||
ASSERT_EQ(config_0, config_1);
|
||||
auto eta = atof(get<String const>(config_1["linear_train_param"]["eta"]).c_str());
|
||||
ASSERT_NEAR(eta, 3.14, kRtEps);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace xgboost
|
||||
|
||||
#endif // XGBOOST_TEST_JSON_IO_H_
|
||||
@ -5,12 +5,13 @@
|
||||
#include <xgboost/gbm.h>
|
||||
|
||||
#include "../helpers.h"
|
||||
|
||||
#include "test_json_io.h"
|
||||
#include "../../../src/gbm/gblinear_model.h"
|
||||
#include "xgboost/base.h"
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
TEST(Linear, shotgun) {
|
||||
TEST(Linear, Shotgun) {
|
||||
size_t constexpr kRows = 10;
|
||||
size_t constexpr kCols = 10;
|
||||
|
||||
@ -45,6 +46,10 @@ TEST(Linear, shotgun) {
|
||||
delete pp_dmat;
|
||||
}
|
||||
|
||||
TEST(Shotgun, JsonIO) {
|
||||
TestUpdaterJsonIO("shotgun");
|
||||
}
|
||||
|
||||
TEST(Linear, coordinate) {
|
||||
size_t constexpr kRows = 10;
|
||||
size_t constexpr kCols = 10;
|
||||
@ -72,4 +77,8 @@ TEST(Linear, coordinate) {
|
||||
delete pp_dmat;
|
||||
}
|
||||
|
||||
TEST(Coordinate, JsonIO){
|
||||
TestUpdaterJsonIO("coord_descent");
|
||||
}
|
||||
|
||||
} // namespace xgboost
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <xgboost/gbm.h>
|
||||
|
||||
#include "../helpers.h"
|
||||
#include "test_json_io.h"
|
||||
#include "../../../src/gbm/gblinear_model.h"
|
||||
|
||||
namespace xgboost {
|
||||
@ -33,4 +34,8 @@ TEST(Linear, GPUCoordinate) {
|
||||
|
||||
delete mat;
|
||||
}
|
||||
|
||||
TEST(GPUCoordinate, JsonIO) {
|
||||
TestUpdaterJsonIO("gpu_coord_descent");
|
||||
}
|
||||
} // namespace xgboost
|
||||
@ -36,7 +36,7 @@ TEST(Objective, DeclareUnifiedTest(PairwiseRankingGPair)) {
|
||||
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
||||
}
|
||||
|
||||
TEST(Objective, DeclareUnifiedTest(NDCG_Json_IO)) {
|
||||
TEST(Objective, DeclareUnifiedTest(NDCG_JsonIO)) {
|
||||
xgboost::GenericParameter tparam;
|
||||
tparam.UpdateAllowUnknown(Args{});
|
||||
|
||||
|
||||
@ -134,7 +134,7 @@ TEST(Learner, Configuration) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Learner, Json_ModelIO) {
|
||||
TEST(Learner, JsonModelIO) {
|
||||
// Test of comparing JSON object directly.
|
||||
size_t constexpr kRows = 8;
|
||||
int32_t constexpr kIters = 4;
|
||||
|
||||
@ -220,7 +220,7 @@ TEST(Tree, DumpDot) {
|
||||
ASSERT_NE(str.find(R"(graph [ bgcolor="#FFFF00" ])"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(Tree, Json_IO) {
|
||||
TEST(Tree, JsonIO) {
|
||||
RegTree tree;
|
||||
tree.ExpandNode(0, 0, 0.0f, false, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
|
||||
Json j_tree{Object()};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user