diff --git a/src/gbm/gblinear_model.h b/src/gbm/gblinear_model.h index 80dd1ac049c0..91760346ca47 100644 --- a/src/gbm/gblinear_model.h +++ b/src/gbm/gblinear_model.h @@ -125,7 +125,7 @@ class GBLinearModel : public Model { } } fo << std::endl << " ]" << std::endl << " }"; - } else { + } else if (format == "text") { fo << "bias:\n"; for (int gid = 0; gid < ngroup; ++gid) { fo << this->Bias()[gid] << std::endl; @@ -136,6 +136,8 @@ class GBLinearModel : public Model { fo << (*this)[i][gid] << std::endl; } } + } else { + LOG(FATAL) << "Dump format `" << format << "` is not supported by the gblinear model."; } std::vector v; v.push_back(fo.str()); diff --git a/tests/cpp/gbm/test_gblinear.cc b/tests/cpp/gbm/test_gblinear.cc index 6294e381f087..731c22f172c9 100644 --- a/tests/cpp/gbm/test_gblinear.cc +++ b/tests/cpp/gbm/test_gblinear.cc @@ -1,21 +1,18 @@ -/*! - * Copyright 2019 by Contributors +/** + * Copyright 2019-2024, XGBoost Contributors */ #include +#include // for FeatureMap #include -#include #include "../helpers.h" #include "xgboost/context.h" #include "xgboost/gbm.h" #include "xgboost/json.h" #include "xgboost/learner.h" -#include "xgboost/logging.h" - -namespace xgboost { -namespace gbm { +namespace xgboost::gbm { TEST(GBLinear, JsonIO) { size_t constexpr kRows = 16, kCols = 16; @@ -40,5 +37,16 @@ TEST(GBLinear, JsonIO) { ASSERT_EQ(weights.size(), 17); } } -} // namespace gbm -} // namespace xgboost + +TEST(GBLinear, Dump) { + Context ctx; + size_t constexpr kRows = 16, kCols = 16; + LearnerModelParam mparam{MakeMP(kCols, .5, 1)}; + + std::unique_ptr gbm{ + CreateTrainedGBM("gblinear", Args{}, kRows, kCols, &mparam, &ctx)}; + FeatureMap fmap; + ASSERT_THAT([&] { [[maybe_unused]] auto vec = gbm->DumpModel(fmap, true, "dot"); }, + GMockThrow(R"(`dot` is not supported)")); +} +} // namespace xgboost::gbm