| 49 | } |
| 50 | |
| 51 | void SerialTreeLearner::Init(const Dataset* train_data, bool is_constant_hessian) { |
| 52 | train_data_ = train_data; |
| 53 | num_data_ = train_data_->num_data(); |
| 54 | num_features_ = train_data_->num_features(); |
| 55 | is_constant_hessian_ = is_constant_hessian; |
| 56 | int max_cache_size = 0; |
| 57 | // Get the max size of pool |
| 58 | if (config_->histogram_pool_size <= 0) { |
| 59 | max_cache_size = config_->num_leaves; |
| 60 | } else { |
| 61 | size_t total_histogram_size = 0; |
| 62 | for (int i = 0; i < train_data_->num_features(); ++i) { |
| 63 | total_histogram_size += sizeof(HistogramBinEntry) * train_data_->FeatureNumBin(i); |
| 64 | } |
| 65 | max_cache_size = static_cast<int>(config_->histogram_pool_size * 1024 * 1024 / total_histogram_size); |
| 66 | } |
| 67 | // at least need 2 leaves |
| 68 | max_cache_size = std::max(2, max_cache_size); |
| 69 | max_cache_size = std::min(max_cache_size, config_->num_leaves); |
| 70 | |
| 71 | histogram_pool_.DynamicChangeSize(train_data_, config_, max_cache_size, config_->num_leaves); |
| 72 | // push split information for all leaves |
| 73 | best_split_per_leaf_.resize(config_->num_leaves); |
| 74 | // get ordered bin |
| 75 | train_data_->CreateOrderedBins(&ordered_bins_); |
| 76 | |
| 77 | // check existing for ordered bin |
| 78 | for (int i = 0; i < static_cast<int>(ordered_bins_.size()); ++i) { |
| 79 | if (ordered_bins_[i] != nullptr) { |
| 80 | has_ordered_bin_ = true; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | // initialize splits for leaf |
| 85 | smaller_leaf_splits_.reset(new LeafSplits(train_data_->num_data())); |
| 86 | larger_leaf_splits_.reset(new LeafSplits(train_data_->num_data())); |
| 87 | |
| 88 | // initialize data partition |
| 89 | data_partition_.reset(new DataPartition(num_data_, config_->num_leaves)); |
| 90 | is_feature_used_.resize(num_features_); |
| 91 | valid_feature_indices_ = train_data_->ValidFeatureIndices(); |
| 92 | // initialize ordered gradients and hessians |
| 93 | ordered_gradients_.resize(num_data_); |
| 94 | ordered_hessians_.resize(num_data_); |
| 95 | // if has ordered bin, need to allocate a buffer to fast split |
| 96 | if (has_ordered_bin_) { |
| 97 | is_data_in_leaf_.resize(num_data_); |
| 98 | std::fill(is_data_in_leaf_.begin(), is_data_in_leaf_.end(), static_cast<char>(0)); |
| 99 | ordered_bin_indices_.clear(); |
| 100 | for (int i = 0; i < static_cast<int>(ordered_bins_.size()); i++) { |
| 101 | if (ordered_bins_[i] != nullptr) { |
| 102 | ordered_bin_indices_.push_back(i); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | Log::Info("Number of data points in the train set: %d, number of used features: %d", num_data_, num_features_); |
| 107 | if (CostEfficientGradientBoosting::IsEnable(config_)) { |
| 108 | cegb_.reset(new CostEfficientGradientBoosting(this)); |
no test coverage detected