Model IO in JSON. (#5110)
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <xgboost/learner.h>
|
||||
#include <xgboost/version_config.h>
|
||||
#include "xgboost/json.h"
|
||||
#include "../../src/common/io.h"
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
@@ -112,83 +114,54 @@ TEST(Learner, Configuration) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Learner, ObjectiveParameter) {
|
||||
using Arg = std::pair<std::string, std::string>;
|
||||
size_t constexpr kRows = 10;
|
||||
TEST(Learner, Json_ModelIO) {
|
||||
// Test of comparing JSON object directly.
|
||||
size_t constexpr kRows = 8;
|
||||
int32_t constexpr kIters = 4;
|
||||
|
||||
auto pp_dmat = CreateDMatrix(kRows, 10, 0);
|
||||
auto p_dmat = *pp_dmat;
|
||||
|
||||
std::vector<bst_float> labels(kRows);
|
||||
for (size_t i = 0; i < labels.size(); ++i) {
|
||||
labels[i] = i;
|
||||
}
|
||||
p_dmat->Info().labels_.HostVector() = labels;
|
||||
std::vector<std::shared_ptr<DMatrix>> mat {p_dmat};
|
||||
|
||||
std::unique_ptr<Learner> learner {Learner::Create(mat)};
|
||||
learner->SetParams({Arg{"tree_method", "auto"},
|
||||
Arg{"objective", "multi:softprob"},
|
||||
Arg{"num_class", "10"}});
|
||||
learner->UpdateOneIter(0, p_dmat.get());
|
||||
auto attr_names = learner->GetConfigurationArguments();
|
||||
ASSERT_EQ(attr_names.at("objective"), "multi:softprob");
|
||||
|
||||
dmlc::TemporaryDirectory tempdir;
|
||||
const std::string fname = tempdir.path + "/model_para.bst";
|
||||
std::shared_ptr<DMatrix> p_dmat {*pp_dmat};
|
||||
p_dmat->Info().labels_.Resize(kRows);
|
||||
|
||||
{
|
||||
// Create a scope to close the stream before next read.
|
||||
std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(fname.c_str(), "w"));
|
||||
learner->Save(fo.get());
|
||||
std::unique_ptr<Learner> learner { Learner::Create({p_dmat}) };
|
||||
learner->Configure();
|
||||
Json out { Object() };
|
||||
learner->SaveModel(&out);
|
||||
|
||||
learner->LoadModel(out);
|
||||
learner->Configure();
|
||||
|
||||
Json new_in { Object() };
|
||||
learner->SaveModel(&new_in);
|
||||
ASSERT_EQ(new_in, out);
|
||||
}
|
||||
|
||||
std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(fname.c_str(), "r"));
|
||||
std::unique_ptr<Learner> learner1 {Learner::Create(mat)};
|
||||
learner1->Load(fi.get());
|
||||
auto attr_names1 = learner1->GetConfigurationArguments();
|
||||
ASSERT_EQ(attr_names1.at("objective"), "multi:softprob");
|
||||
{
|
||||
std::unique_ptr<Learner> learner { Learner::Create({p_dmat}) };
|
||||
learner->SetParam("verbosity", "3");
|
||||
for (int32_t iter = 0; iter < kIters; ++iter) {
|
||||
learner->UpdateOneIter(iter, p_dmat.get());
|
||||
}
|
||||
learner->SetAttr("bset_score", "15.2");
|
||||
|
||||
Json out { Object() };
|
||||
learner->SaveModel(&out);
|
||||
|
||||
learner->LoadModel(out);
|
||||
Json new_in { Object() };
|
||||
learner->Configure();
|
||||
learner->SaveModel(&new_in);
|
||||
|
||||
ASSERT_TRUE(IsA<Object>(out["Learner"]["attributes"]));
|
||||
ASSERT_EQ(get<Object>(out["Learner"]["attributes"]).size(), 1);
|
||||
ASSERT_EQ(out, new_in);
|
||||
}
|
||||
|
||||
delete pp_dmat;
|
||||
}
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
|
||||
TEST(Learner, IO) {
|
||||
using Arg = std::pair<std::string, std::string>;
|
||||
size_t constexpr kRows = 10;
|
||||
auto pp_dmat = CreateDMatrix(kRows, 10, 0);
|
||||
auto p_dmat = *pp_dmat;
|
||||
|
||||
std::vector<bst_float> labels(kRows);
|
||||
for (size_t i = 0; i < labels.size(); ++i) {
|
||||
labels[i] = i;
|
||||
}
|
||||
p_dmat->Info().labels_.HostVector() = labels;
|
||||
std::vector<std::shared_ptr<DMatrix>> mat {p_dmat};
|
||||
|
||||
std::unique_ptr<Learner> learner {Learner::Create(mat)};
|
||||
learner->SetParams({Arg{"tree_method", "auto"},
|
||||
Arg{"predictor", "gpu_predictor"},
|
||||
Arg{"gpu_id", "0"}});
|
||||
learner->UpdateOneIter(0, p_dmat.get());
|
||||
ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
|
||||
|
||||
dmlc::TemporaryDirectory tempdir;
|
||||
const std::string fname = tempdir.path + "/model.bst";
|
||||
|
||||
{
|
||||
// Create a scope to close the stream before next read.
|
||||
std::unique_ptr<dmlc::Stream> fo(dmlc::Stream::Create(fname.c_str(), "w"));
|
||||
learner->Save(fo.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(fname.c_str(), "r"));
|
||||
learner->Load(fi.get());
|
||||
ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
|
||||
|
||||
delete pp_dmat;
|
||||
}
|
||||
|
||||
// Tests for automatic GPU configuration.
|
||||
TEST(Learner, GPUConfiguration) {
|
||||
using Arg = std::pair<std::string, std::string>;
|
||||
@@ -242,6 +215,5 @@ TEST(Learner, GPUConfiguration) {
|
||||
|
||||
delete pp_dmat;
|
||||
}
|
||||
#endif // XGBOOST_USE_CUDA
|
||||
|
||||
#endif // defined(XGBOOST_USE_CUDA)
|
||||
} // namespace xgboost
|
||||
|
||||
Reference in New Issue
Block a user