| 172 | } |
| 173 | |
| 174 | Tree* SerialTreeLearner2::Train(const score_t* gradients, const score_t *hessians, bool is_constant_hessian, const Json& forced_split_json) { |
| 175 | gradients_ = gradients; |
| 176 | hessians_ = hessians; |
| 177 | is_constant_hessian_ = is_constant_hessian; |
| 178 | #ifdef TIMETAG |
| 179 | auto start_time = std::chrono::steady_clock::now(); |
| 180 | #endif |
| 181 | // some initial works before training |
| 182 | BeforeTrain(); |
| 183 | |
| 184 | #ifdef TIMETAG |
| 185 | init_train_time += std::chrono::steady_clock::now() - start_time; |
| 186 | #endif |
| 187 | |
| 188 | auto tree = std::unique_ptr<Tree>(new Tree(config_->num_leaves)); |
| 189 | // root leaf |
| 190 | int left_leaf = 0; |
| 191 | int cur_depth = 1; |
| 192 | // only root leaf can be splitted on first time |
| 193 | int right_leaf = -1; |
| 194 | |
| 195 | int init_splits = 0; |
| 196 | bool aborted_last_force_split = false; |
| 197 | if (!forced_split_json.is_null()) { |
| 198 | init_splits = ForceSplits(tree.get(), forced_split_json, &left_leaf, |
| 199 | &right_leaf, &cur_depth, &aborted_last_force_split); |
| 200 | } |
| 201 | |
| 202 | for (int split = init_splits; split < config_->num_leaves - 1; ++split) { |
| 203 | |
| 204 | |
| 205 | #ifdef TIMETAG |
| 206 | start_time = std::chrono::steady_clock::now(); |
| 207 | #endif |
| 208 | // some initial works before finding best split |
| 209 | if (!aborted_last_force_split && BeforeFindBestSplit(tree.get(), left_leaf, right_leaf)) { |
| 210 | #ifdef TIMETAG |
| 211 | init_split_time += std::chrono::steady_clock::now() - start_time; |
| 212 | #endif |
| 213 | // find best threshold for every feature |
| 214 | FindBestSplits(); |
| 215 | } else if (aborted_last_force_split) { |
| 216 | aborted_last_force_split = false; |
| 217 | } |
| 218 | |
| 219 | // Get a leaf with max split gain |
| 220 | int best_leaf = static_cast<int>(ArrayArgs<SplitInfo>::ArgMax(best_split_per_leaf_)); |
| 221 | // Get split information for best leaf |
| 222 | const SplitInfo& best_leaf_SplitInfo = best_split_per_leaf_[best_leaf]; |
| 223 | // cannot split, quit |
| 224 | if (best_leaf_SplitInfo.gain <= 0.0) { |
| 225 | Log::Warning("No further splits with positive gain, best gain: %f", best_leaf_SplitInfo.gain); |
| 226 | break; |
| 227 | } |
| 228 | #ifdef TIMETAG |
| 229 | start_time = std::chrono::steady_clock::now(); |
| 230 | #endif |
| 231 | // split tree with best leaf |
nothing calls this directly
no test coverage detected