| 896 | explicit LearnerIO(std::vector<std::shared_ptr<DMatrix>> cache) : LearnerConfiguration{cache} {} |
| 897 | |
| 898 | void LoadModel(Json const& in) override { |
| 899 | CHECK(IsA<Object>(in)); |
| 900 | auto version = Version::Load(in); |
| 901 | if (std::get<0>(version) == 1 && std::get<1>(version) < 6) { |
| 902 | LOG(WARNING) |
| 903 | << "Found JSON model saved before XGBoost 1.6, please save the model using current " |
| 904 | "version again. The support for old JSON model will be discontinued in XGBoost 3.2"; |
| 905 | } |
| 906 | |
| 907 | auto const& learner = get<Object>(in["learner"]); |
| 908 | mparam_.FromJson(learner.at("learner_model_param")); |
| 909 | |
| 910 | auto const& objective_fn = learner.at("objective"); |
| 911 | |
| 912 | std::string name = get<String>(objective_fn["name"]); |
| 913 | tparam_.UpdateAllowUnknown(Args{{"objective", name}}); |
| 914 | obj_.reset(ObjFunction::Create(name, &ctx_)); |
| 915 | obj_->LoadConfig(objective_fn); |
| 916 | |
| 917 | auto const& gradient_booster = learner.at("gradient_booster"); |
| 918 | name = get<String>(gradient_booster["name"]); |
| 919 | tparam_.UpdateAllowUnknown(Args{{"booster", name}}); |
| 920 | tparam_.booster = CanonicalizeBoosterName(tparam_.booster); |
| 921 | gbm_.reset(GradientBooster::Create(tparam_.booster, &ctx_, &learner_model_param_)); |
| 922 | gbm_->LoadModel(gradient_booster); |
| 923 | |
| 924 | auto const& j_attributes = get<Object const>(learner.at("attributes")); |
| 925 | attributes_.clear(); |
| 926 | for (auto const& kv : j_attributes) { |
| 927 | attributes_[kv.first] = get<String const>(kv.second); |
| 928 | } |
| 929 | |
| 930 | // feature names and types are saved in xgboost 1.4 |
| 931 | auto it = learner.find("feature_names"); |
| 932 | if (it != learner.cend()) { |
| 933 | auto const& feature_names = get<Array const>(it->second); |
| 934 | feature_names_.resize(feature_names.size()); |
| 935 | std::transform(feature_names.cbegin(), feature_names.cend(), feature_names_.begin(), |
| 936 | [](Json const& fn) { return get<String const>(fn); }); |
| 937 | } |
| 938 | it = learner.find("feature_types"); |
| 939 | if (it != learner.cend()) { |
| 940 | auto const& feature_types = get<Array const>(it->second); |
| 941 | feature_types_.resize(feature_types.size()); |
| 942 | std::transform(feature_types.cbegin(), feature_types.cend(), feature_types_.begin(), |
| 943 | [](Json const& fn) { return get<String const>(fn); }); |
| 944 | } |
| 945 | |
| 946 | this->need_configuration_ = true; |
| 947 | this->ClearCaches(); |
| 948 | } |
| 949 | |
| 950 | void SaveModel(Json* p_out) const override { |
| 951 | CHECK(!this->need_configuration_) << "Call Configure before saving model."; |
no test coverage detected