Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refinements for JSON model. #5112

Merged
merged 4 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/gbm/gbtree_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void GBTreeModel::Load(dmlc::Stream* fi) {
void GBTreeModel::SaveModel(Json* p_out) const {
auto& out = *p_out;
CHECK_EQ(param.num_trees, static_cast<int>(trees.size()));
out["model_param"] = toJson(param);
out["gbtree_model_param"] = toJson(param);
std::vector<Json> trees_json;
size_t t = 0;
for (auto const& tree : trees) {
Expand All @@ -61,7 +61,7 @@ void GBTreeModel::SaveModel(Json* p_out) const {
}

void GBTreeModel::LoadModel(Json const& in) {
fromJson(in["model_param"], &param);
fromJson(in["gbtree_model_param"], &param);

trees.clear();
trees_to_update.clear();
Expand Down
6 changes: 3 additions & 3 deletions src/learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class LearnerImpl : public Learner {
void LoadModel(Json const& in) override {
CHECK(IsA<Object>(in));
Version::Load(in, false);
auto const& learner = get<Object>(in["Learner"]);
auto const& learner = get<Object>(in["learner"]);
mparam_.FromJson(learner.at("learner_model_param"));

auto const& objective_fn = learner.at("objective");
Expand Down Expand Up @@ -305,8 +305,8 @@ class LearnerImpl : public Learner {
Version::Save(p_out);
Json& out { *p_out };

out["Learner"] = Object();
auto& learner = out["Learner"];
out["learner"] = Object();
auto& learner = out["learner"];

learner["learner_model_param"] = mparam_.ToJson();
learner["gradient_booster"] = Object();
Expand Down
12 changes: 2 additions & 10 deletions tests/cpp/gbm/test_gblinear.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,14 @@ TEST(GBLinear, Json_IO) {
std::string model_str;
Json::Dump(model, &model_str);

model = Json::Load({model_str.c_str(), model_str.size()});
model = Json::Load(StringView{model_str.c_str(), model_str.size()});
ASSERT_TRUE(IsA<Object>(model));
model = model["model"];

{
auto weights = get<Array>(model["weights"]);
ASSERT_EQ(weights.size(), 17);
}

{
model = Json::Load({model_str.c_str(), model_str.size()});
model = model["model"];
auto weights = get<Array>(model["weights"]);
ASSERT_EQ(weights.size(), 17); // 16 + 1 (bias)
ASSERT_EQ(weights.size(), 17);
}

}

} // namespace gbm
Expand Down
16 changes: 7 additions & 9 deletions tests/cpp/gbm/test_gbtree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ TEST(GBTree, ChoosePredictor) {
}
ASSERT_TRUE(data.HostCanWrite());
dmlc::TemporaryDirectory tempdir;
const std::string fname = tempdir.path + "/model_para.bst";
const std::string fname = tempdir.path + "/model_param.bst";

{
std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(fname.c_str(), "w"));
Expand Down Expand Up @@ -117,17 +117,15 @@ TEST(GBTree, Json_IO) {
CreateTrainedGBM("gbtree", Args{}, kRows, kCols, &mparam, &gparam) };

Json model {Object()};
model["model"] = Object();
auto& j_model = model["model"];

gbm->SaveModel(&j_model);
gbm->SaveModel(&model);

std::stringstream ss;
Json::Dump(model, &ss);
std::string model_str;
Json::Dump(model, &model_str);

auto model_str = ss.str();
model = Json::Load({model_str.c_str(), model_str.size()});
ASSERT_EQ(get<String>(model["model"]["name"]), "gbtree");
auto loaded_model = Json::Load(StringView{model_str.c_str(), model_str.size()});
ASSERT_EQ(get<String>(loaded_model["name"]), "gbtree");
ASSERT_TRUE(IsA<Object>(loaded_model["model"]["gbtree_model_param"]));
}

TEST(Dart, Json_IO) {
Expand Down
6 changes: 3 additions & 3 deletions tests/cpp/test_learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ TEST(Learner, Json_ModelIO) {
for (int32_t iter = 0; iter < kIters; ++iter) {
learner->UpdateOneIter(iter, p_dmat.get());
}
learner->SetAttr("bset_score", "15.2");
learner->SetAttr("best_score", "15.2");
hcho3 marked this conversation as resolved.
Show resolved Hide resolved

Json out { Object() };
learner->SaveModel(&out);
Expand All @@ -153,8 +153,8 @@ TEST(Learner, Json_ModelIO) {
learner->Configure();
learner->SaveModel(&new_in);

ASSERT_TRUE(IsA<Object>(out["Learner"]["attributes"]));
ASSERT_EQ(get<Object>(out["Learner"]["attributes"]).size(), 1);
ASSERT_TRUE(IsA<Object>(out["learner"]["attributes"]));
ASSERT_EQ(get<Object>(out["learner"]["attributes"]).size(), 1);
ASSERT_EQ(out, new_in);
}

Expand Down
1 change: 0 additions & 1 deletion tests/cpp/tree/test_gpu_hist.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "xgboost/json.h"
#include "../../../src/data/sparse_page_source.h"
#include "../../../src/gbm/gbtree_model.h"
#include "../../../src/tree/updater_gpu_hist.cu"
#include "../../../src/tree/updater_gpu_common.cuh"
#include "../../../src/common/common.h"
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_basic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ def test_json_model_io(self):

with open('./model.json', 'r') as fd:
j_model = json.load(fd)
assert isinstance(j_model['Learner'], dict)
assert isinstance(j_model['learner'], dict)

bst = xgb.Booster(model_file='./model.json')

with open('./model.json', 'r') as fd:
j_model = json.load(fd)
assert isinstance(j_model['Learner'], dict)
assert isinstance(j_model['learner'], dict)

os.remove('model.json')