| 249 | } |
| 250 | |
| 251 | std::string GBDT::SaveModelToString(int start_iteration, int num_iteration) const { |
| 252 | std::stringstream ss; |
| 253 | |
| 254 | // output model type |
| 255 | ss << SubModelName() << '\n'; |
| 256 | ss << "version=" << kModelVersion << '\n'; |
| 257 | // output number of class |
| 258 | ss << "num_class=" << num_class_ << '\n'; |
| 259 | ss << "num_tree_per_iteration=" << num_tree_per_iteration_ << '\n'; |
| 260 | // output label index |
| 261 | ss << "label_index=" << label_idx_ << '\n'; |
| 262 | // output max_feature_idx |
| 263 | ss << "max_feature_idx=" << max_feature_idx_ << '\n'; |
| 264 | // output objective |
| 265 | if (objective_function_ != nullptr) { |
| 266 | ss << "objective=" << objective_function_->ToString() << '\n'; |
| 267 | } |
| 268 | |
| 269 | if (average_output_) { |
| 270 | ss << "average_output" << '\n'; |
| 271 | } |
| 272 | |
| 273 | ss << "feature_names=" << Common::Join(feature_names_, " ") << '\n'; |
| 274 | |
| 275 | if (monotone_constraints_.size() != 0) { |
| 276 | ss << "monotone_constraints=" << Common::Join(monotone_constraints_, " ") |
| 277 | << '\n'; |
| 278 | } |
| 279 | |
| 280 | ss << "feature_infos=" << Common::Join(feature_infos_, " ") << '\n'; |
| 281 | |
| 282 | int num_used_model = static_cast<int>(models_.size()); |
| 283 | int total_iteration = num_used_model / num_tree_per_iteration_; |
| 284 | start_iteration = std::max(start_iteration, 0); |
| 285 | start_iteration = std::min(start_iteration, total_iteration); |
| 286 | if (num_iteration > 0) { |
| 287 | int end_iteration = start_iteration + num_iteration; |
| 288 | num_used_model = std::min(end_iteration * num_tree_per_iteration_, num_used_model); |
| 289 | } |
| 290 | |
| 291 | int start_model = start_iteration * num_tree_per_iteration_; |
| 292 | |
| 293 | if (save_num_label == -1){ |
| 294 | std::vector<std::string> tree_strs(num_used_model - start_model); |
| 295 | std::vector<size_t> tree_sizes(num_used_model - start_model); |
| 296 | // output tree models |
| 297 | #pragma omp parallel for schedule(static) |
| 298 | for (int i = start_model; i < num_used_model; ++i) { |
| 299 | const int idx = i - start_model; |
| 300 | tree_strs[idx] = "Tree=" + std::to_string(idx) + '\n'; |
| 301 | tree_strs[idx] += models_[i]->ToString() + '\n'; |
| 302 | tree_sizes[idx] = tree_strs[idx].size(); |
| 303 | } |
| 304 | |
| 305 | ss << "tree_sizes=" << Common::Join(tree_sizes, " ") << '\n'; |
| 306 | ss << '\n'; |
| 307 | |
| 308 | for (int i = 0; i < num_used_model - start_model; ++i) { |