MCPcopy Create free account
hub / github.com/antmachineintelligence/mtgbmcode / Train

Method Train

src/treelearner/serial_tree_learner.cpp:173–237  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

SplitFunction · 0.50
is_nullMethod · 0.45
getMethod · 0.45
leaf_depthMethod · 0.45
num_leavesMethod · 0.45

Tested by

no test coverage detected