Use `UpdateAllowUnknown' for non-model related parameter. (#4961)
* Use `UpdateAllowUnknown' for non-model related parameter. Model parameter can not pack an additional boolean value due to binary IO format. This commit deals only with non-model related parameter configuration. * Add tidy command line arg for use-dmlc-gtest.
This commit is contained in:
@@ -16,6 +16,8 @@ TEST(Monitor, Logging) {
|
||||
|
||||
Args args = {std::make_pair("verbosity", "3")};
|
||||
ConsoleLogger::Configure(args);
|
||||
ASSERT_EQ(ConsoleLogger::GlobalVerbosity(), ConsoleLogger::LogVerbosity::kDebug);
|
||||
|
||||
testing::internal::CaptureStderr();
|
||||
run_monitor();
|
||||
std::string output = testing::internal::GetCapturedStderr();
|
||||
@@ -28,6 +30,8 @@ TEST(Monitor, Logging) {
|
||||
run_monitor();
|
||||
output = testing::internal::GetCapturedStderr();
|
||||
ASSERT_EQ(output.size(), 0);
|
||||
|
||||
ConsoleLogger::Configure(Args{{"verbosity", "1"}});
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include <dmlc/parameter.h>
|
||||
/*!
|
||||
* Copyright (c) by Contributors 2019
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <xgboost/base.h>
|
||||
#include <xgboost/parameter.h>
|
||||
|
||||
enum class Foo : int {
|
||||
@@ -8,10 +12,10 @@ enum class Foo : int {
|
||||
|
||||
DECLARE_FIELD_ENUM_CLASS(Foo);
|
||||
|
||||
struct MyParam : dmlc::Parameter<MyParam> {
|
||||
struct MyEnumParam : xgboost::XGBoostParameter<MyEnumParam> {
|
||||
Foo foo;
|
||||
int bar;
|
||||
DMLC_DECLARE_PARAMETER(MyParam) {
|
||||
DMLC_DECLARE_PARAMETER(MyEnumParam) {
|
||||
DMLC_DECLARE_FIELD(foo)
|
||||
.set_default(Foo::kBar)
|
||||
.add_enum("bar", Foo::kBar)
|
||||
@@ -23,10 +27,10 @@ struct MyParam : dmlc::Parameter<MyParam> {
|
||||
}
|
||||
};
|
||||
|
||||
DMLC_REGISTER_PARAMETER(MyParam);
|
||||
DMLC_REGISTER_PARAMETER(MyEnumParam);
|
||||
|
||||
TEST(EnumClassParam, Basic) {
|
||||
MyParam param;
|
||||
MyEnumParam param;
|
||||
std::map<std::string, std::string> kwargs{
|
||||
{"foo", "frog"}, {"bar", "10"}
|
||||
};
|
||||
@@ -53,3 +57,44 @@ TEST(EnumClassParam, Basic) {
|
||||
kwargs["foo"] = "human";
|
||||
ASSERT_THROW(param.Init(kwargs), dmlc::ParamError);
|
||||
}
|
||||
|
||||
struct UpdatableParam : xgboost::XGBoostParameter<UpdatableParam> {
|
||||
float f { 0.0f };
|
||||
double d { 0.0 };
|
||||
|
||||
DMLC_DECLARE_PARAMETER(UpdatableParam) {
|
||||
DMLC_DECLARE_FIELD(f)
|
||||
.set_default(11.0f);
|
||||
DMLC_DECLARE_FIELD(d)
|
||||
.set_default(2.71828f);
|
||||
}
|
||||
};
|
||||
|
||||
DMLC_REGISTER_PARAMETER(UpdatableParam);
|
||||
|
||||
TEST(XGBoostParameter, Update) {
|
||||
{
|
||||
UpdatableParam p;
|
||||
auto constexpr kRtEps = xgboost::kRtEps;
|
||||
|
||||
p.UpdateAllowUnknown(xgboost::Args{});
|
||||
// When it's not initialized, perform set_default.
|
||||
ASSERT_NEAR(p.f, 11.0f, kRtEps);
|
||||
ASSERT_NEAR(p.d, 2.71828f, kRtEps);
|
||||
|
||||
p.d = 3.14149;
|
||||
|
||||
p.UpdateAllowUnknown(xgboost::Args{{"f", "2.71828"}});
|
||||
ASSERT_NEAR(p.f, 2.71828f, kRtEps);
|
||||
|
||||
// p.d is un-effected by the update.
|
||||
ASSERT_NEAR(p.d, 3.14149, kRtEps);
|
||||
}
|
||||
{
|
||||
UpdatableParam p;
|
||||
auto constexpr kRtEps = xgboost::kRtEps;
|
||||
p.UpdateAllowUnknown(xgboost::Args{{"f", "2.71828"}});
|
||||
ASSERT_NEAR(p.f, 2.71828f, kRtEps);
|
||||
ASSERT_NEAR(p.d, 2.71828, kRtEps); // default
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user