| 50 | } |
| 51 | |
| 52 | void GBDT::Init(const Config* config, const Dataset* train_data, const ObjectiveFunction* objective_function, |
| 53 | const std::vector<const Metric*>& training_metrics) { |
| 54 | CHECK(train_data != nullptr); |
| 55 | train_data_ = train_data; |
| 56 | iter_ = 0; |
| 57 | num_iteration_for_pred_ = 0; |
| 58 | max_feature_idx_ = 0; |
| 59 | num_class_ = config->num_class; |
| 60 | num_labels_ = config->num_labels; |
| 61 | config_ = std::unique_ptr<Config>(new Config(*config)); |
| 62 | early_stopping_round_ = config_->early_stopping_round; |
| 63 | es_first_metric_only_ = config_->first_metric_only; |
| 64 | shrinkage_rate_ = config_->learning_rate; |
| 65 | |
| 66 | std::string forced_splits_path = config->forcedsplits_filename; |
| 67 | // load forced_splits file |
| 68 | if (forced_splits_path != "") { |
| 69 | std::ifstream forced_splits_file(forced_splits_path.c_str()); |
| 70 | std::stringstream buffer; |
| 71 | buffer << forced_splits_file.rdbuf(); |
| 72 | std::string err; |
| 73 | forced_splits_json_ = Json::parse(buffer.str(), err); |
| 74 | } |
| 75 | |
| 76 | objective_function_ = objective_function; |
| 77 | num_tree_per_iteration_ = num_class_; |
| 78 | if (objective_function_ != nullptr) { |
| 79 | is_constant_hessian_ = objective_function_->IsConstantHessian(); |
| 80 | num_tree_per_iteration_ = objective_function_->NumModelPerIteration(); |
| 81 | } else { |
| 82 | is_constant_hessian_ = false; |
| 83 | } |
| 84 | |
| 85 | tree_learner_ = std::unique_ptr<TreeLearner>(TreeLearner::CreateTreeLearner(config_->tree_learner, config_->device_type, config_.get())); |
| 86 | |
| 87 | // init tree learner |
| 88 | tree_learner_->Init(train_data_, is_constant_hessian_); |
| 89 | |
| 90 | // push training metrics |
| 91 | training_metrics_.clear(); |
| 92 | for (const auto& metric : training_metrics) { |
| 93 | training_metrics_.push_back(metric); |
| 94 | } |
| 95 | training_metrics_.shrink_to_fit(); |
| 96 | |
| 97 | train_score_updater_.reset(new ScoreUpdater(train_data_, num_tree_per_iteration_, num_labels_)); |
| 98 | |
| 99 | num_data_ = train_data_->num_data(); |
| 100 | // create buffer for gradients and hessians |
| 101 | if (objective_function_ != nullptr) { |
| 102 | size_t total_size = static_cast<size_t>(num_data_) * num_tree_per_iteration_; |
| 103 | gradients_.resize(total_size); |
| 104 | hessians_.resize(total_size); |
| 105 | } |
| 106 | // get max feature index |
| 107 | max_feature_idx_ = train_data_->num_total_features() - 1; |
| 108 | // get label index |
| 109 | label_idx_ = train_data_->label_idx(); |
nothing calls this directly
no test coverage detected