Pass pointer to model parameters. (#5101)

* Pass pointer to model parameters.

This PR de-duplicates most of the model parameters except the one in
`tree_model.h`.  One difficulty is `base_score` is a model property but can be
changed at runtime by objective function.  Hence when performing model IO, we
need to save the one provided by users, instead of the one transformed by
objective.  Here we created an immutable version of `LearnerModelParam` that
represents the value of model parameter after configuration.
This commit is contained in:
Jiaming Yuan
2019-12-10 12:11:22 +08:00
committed by GitHub
parent 979f74d51a
commit e089e16e3d
33 changed files with 623 additions and 404 deletions

View File

@@ -8,20 +8,30 @@
#include "../../../src/gbm/gblinear_model.h"
namespace xgboost {
TEST(Linear, shotgun) {
auto mat = xgboost::CreateDMatrix(10, 10, 0);
size_t constexpr kRows = 10;
size_t constexpr kCols = 10;
auto pp_dmat = xgboost::CreateDMatrix(kRows, kCols, 0);
auto p_fmat {*pp_dmat};
auto lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
LearnerModelParam mparam;
mparam.num_feature = kCols;
mparam.num_output_group = 1;
mparam.base_score = 0.5;
{
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
xgboost::LinearUpdater::Create("shotgun", &lparam));
updater->Configure({{"eta", "1."}});
xgboost::HostDeviceVector<xgboost::GradientPair> gpair(
(*mat)->Info().num_row_, xgboost::GradientPair(-5, 1.0));
xgboost::gbm::GBLinearModel model;
model.param.num_feature = (*mat)->Info().num_col_;
model.param.num_output_group = 1;
p_fmat->Info().num_row_, xgboost::GradientPair(-5, 1.0));
xgboost::gbm::GBLinearModel model{&mparam};
model.LazyInitModel();
updater->Update(&gpair, (*mat).get(), &model, gpair.Size());
updater->Update(&gpair, p_fmat.get(), &model, gpair.Size());
ASSERT_EQ(model.bias()[0], 5.0f);
@@ -31,24 +41,35 @@ TEST(Linear, shotgun) {
xgboost::LinearUpdater::Create("shotgun", &lparam));
EXPECT_ANY_THROW(updater->Configure({{"feature_selector", "random"}}));
}
delete mat;
delete pp_dmat;
}
TEST(Linear, coordinate) {
auto mat = xgboost::CreateDMatrix(10, 10, 0);
size_t constexpr kRows = 10;
size_t constexpr kCols = 10;
auto pp_dmat = xgboost::CreateDMatrix(kRows, kCols, 0);
auto p_fmat {*pp_dmat};
auto lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
LearnerModelParam mparam;
mparam.num_feature = kCols;
mparam.num_output_group = 1;
mparam.base_score = 0.5;
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
xgboost::LinearUpdater::Create("coord_descent", &lparam));
updater->Configure({{"eta", "1."}});
xgboost::HostDeviceVector<xgboost::GradientPair> gpair(
(*mat)->Info().num_row_, xgboost::GradientPair(-5, 1.0));
xgboost::gbm::GBLinearModel model;
model.param.num_feature = (*mat)->Info().num_col_;
model.param.num_output_group = 1;
p_fmat->Info().num_row_, xgboost::GradientPair(-5, 1.0));
xgboost::gbm::GBLinearModel model{&mparam};
model.LazyInitModel();
updater->Update(&gpair, (*mat).get(), &model, gpair.Size());
updater->Update(&gpair, p_fmat.get(), &model, gpair.Size());
ASSERT_EQ(model.bias()[0], 5.0f);
delete mat;
delete pp_dmat;
}
} // namespace xgboost

View File

@@ -8,16 +8,24 @@
namespace xgboost {
TEST(Linear, GPUCoordinate) {
auto mat = xgboost::CreateDMatrix(10, 10, 0);
size_t constexpr kRows = 10;
size_t constexpr kCols = 10;
auto mat = xgboost::CreateDMatrix(kRows, kCols, 0);
auto lparam = CreateEmptyGenericParam(GPUIDX);
LearnerModelParam mparam;
mparam.num_feature = kCols;
mparam.num_output_group = 1;
mparam.base_score = 0.5;
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
xgboost::LinearUpdater::Create("gpu_coord_descent", &lparam));
updater->Configure({{"eta", "1."}});
xgboost::HostDeviceVector<xgboost::GradientPair> gpair(
(*mat)->Info().num_row_, xgboost::GradientPair(-5, 1.0));
xgboost::gbm::GBLinearModel model;
model.param.num_feature = (*mat)->Info().num_col_;
model.param.num_output_group = 1;
xgboost::gbm::GBLinearModel model{&mparam};
model.LazyInitModel();
updater->Update(&gpair, (*mat).get(), &model, gpair.Size());