| 828 | } |
| 829 | |
| 830 | void GBDT::ResetBaggingConfig(const Config* config, bool is_change_dataset) { |
| 831 | // if need bagging, create buffer |
| 832 | data_size_t num_pos_data = 0; |
| 833 | if (objective_function_ != nullptr) { |
| 834 | num_pos_data = objective_function_->NumPositiveData(); |
| 835 | } |
| 836 | bool balance_bagging_cond = (config->pos_bagging_fraction < 1.0 || config->neg_bagging_fraction < 1.0) && (num_pos_data > 0); |
| 837 | if ((config->bagging_fraction < 1.0 || balance_bagging_cond) && config->bagging_freq > 0) { |
| 838 | need_re_bagging_ = false; |
| 839 | if (!is_change_dataset && |
| 840 | config_.get() != nullptr && config_->bagging_fraction == config->bagging_fraction && config_->bagging_freq == config->bagging_freq |
| 841 | && config_->pos_bagging_fraction == config->pos_bagging_fraction && config_->neg_bagging_fraction == config->neg_bagging_fraction) { |
| 842 | return; |
| 843 | } |
| 844 | if (balance_bagging_cond) { |
| 845 | balanced_bagging_ = true; |
| 846 | bag_data_cnt_ = static_cast<data_size_t>(num_pos_data * config->pos_bagging_fraction) |
| 847 | + static_cast<data_size_t>((num_data_ - num_pos_data) * config->neg_bagging_fraction); |
| 848 | } else { |
| 849 | bag_data_cnt_ = static_cast<data_size_t>(config->bagging_fraction * num_data_); |
| 850 | } |
| 851 | bag_data_indices_.resize(num_data_); |
| 852 | tmp_indices_.resize(num_data_); |
| 853 | |
| 854 | offsets_buf_.resize(num_threads_); |
| 855 | left_cnts_buf_.resize(num_threads_); |
| 856 | right_cnts_buf_.resize(num_threads_); |
| 857 | left_write_pos_buf_.resize(num_threads_); |
| 858 | right_write_pos_buf_.resize(num_threads_); |
| 859 | |
| 860 | double average_bag_rate = (bag_data_cnt_ / num_data_) / config->bagging_freq; |
| 861 | int sparse_group = 0; |
| 862 | for (int i = 0; i < train_data_->num_feature_groups(); ++i) { |
| 863 | if (train_data_->FeatureGroupIsSparse(i)) { |
| 864 | ++sparse_group; |
| 865 | } |
| 866 | } |
| 867 | is_use_subset_ = false; |
| 868 | const int group_threshold_usesubset = 100; |
| 869 | const int sparse_group_threshold_usesubset = train_data_->num_feature_groups() / 4; |
| 870 | if (average_bag_rate <= 0.5 |
| 871 | && (train_data_->num_feature_groups() < group_threshold_usesubset || sparse_group < sparse_group_threshold_usesubset)) { |
| 872 | if (tmp_subset_ == nullptr || is_change_dataset) { |
| 873 | tmp_subset_.reset(new Dataset(bag_data_cnt_)); |
| 874 | tmp_subset_->CopyFeatureMapperFrom(train_data_); |
| 875 | } |
| 876 | is_use_subset_ = true; |
| 877 | Log::Debug("Use subset for bagging"); |
| 878 | } |
| 879 | |
| 880 | need_re_bagging_ = true; |
| 881 | |
| 882 | if (is_use_subset_ && bag_data_cnt_ < num_data_) { |
| 883 | if (objective_function_ == nullptr) { |
| 884 | size_t total_size = static_cast<size_t>(num_data_) * num_tree_per_iteration_; |
| 885 | gradients_.resize(total_size); |
| 886 | hessians_.resize(total_size); |
| 887 | } |
nothing calls this directly
no test coverage detected