7 #ifndef XGBOOST_COMMON_RANDOM_H_
8 #define XGBOOST_COMMON_RANDOM_H_
10 #include <rabit/rabit.h>
11 #include <xgboost/logging.h>
32 #if XGBOOST_CUSTOMIZE_GLOBAL_PRNG
39 class CustomGlobalRandomEngine {
42 using result_type = uint32_t;
44 inline static constexpr result_type min() {
48 inline static constexpr result_type max() {
49 return std::numeric_limits<result_type>::max();
55 void seed(result_type val);
59 result_type operator()();
72 #endif // XGBOOST_CUSTOMIZE_GLOBAL_PRNG
90 std::vector<T>
const &array, std::vector<float>
const &weights,
size_t n) {
92 CHECK_EQ(array.size(), weights.size());
93 std::vector<float> keys(weights.size());
94 std::uniform_real_distribution<float> dist;
96 for (
size_t i = 0; i < array.size(); ++i) {
97 auto w = std::max(weights.at(i),
kRtEps);
99 auto k = std::log(u) / w;
102 auto ind = ArgSort<size_t>(keys, std::greater<>{});
105 std::vector<T> results(ind.size());
106 for (
size_t k = 0; k < ind.size(); ++k) {
108 results[k] = array[idx];
121 std::shared_ptr<HostDeviceVector<bst_feature_t>> feature_set_tree_;
122 std::map<int, std::shared_ptr<HostDeviceVector<bst_feature_t>>> feature_set_level_;
123 std::vector<float> feature_weights_;
124 float colsample_bylevel_{1.0f};
125 float colsample_bytree_{1.0f};
126 float colsample_bynode_{1.0f};
130 std::shared_ptr<HostDeviceVector<bst_feature_t>>
ColSample(
146 rabit::Broadcast(&seed,
sizeof(seed), 0);
159 void Init(int64_t num_col, std::vector<float> feature_weights,
160 float colsample_bynode,
float colsample_bylevel,
161 float colsample_bytree,
bool skip_index_0 =
false) {
162 feature_weights_ = std::move(feature_weights);
163 colsample_bylevel_ = colsample_bylevel;
164 colsample_bytree_ = colsample_bytree;
165 colsample_bynode_ = colsample_bynode;
167 if (feature_set_tree_ ==
nullptr) {
168 feature_set_tree_ = std::make_shared<HostDeviceVector<bst_feature_t>>();
172 int begin_idx = skip_index_0 ? 1 : 0;
173 feature_set_tree_->Resize(num_col - begin_idx);
174 std::iota(feature_set_tree_->HostVector().begin(),
175 feature_set_tree_->HostVector().end(), begin_idx);
177 feature_set_tree_ =
ColSample(feature_set_tree_, colsample_bytree_);
184 feature_set_tree_->Resize(0);
185 feature_set_level_.clear();
200 if (colsample_bylevel_ == 1.0f && colsample_bynode_ == 1.0f) {
201 return feature_set_tree_;
204 if (feature_set_level_.count(depth) == 0) {
206 feature_set_level_[depth] =
ColSample(feature_set_tree_, colsample_bylevel_);
208 if (colsample_bynode_ == 1.0f) {
210 return feature_set_level_[depth];
213 return ColSample(feature_set_level_[depth], colsample_bynode_);
219 #endif // XGBOOST_COMMON_RANDOM_H_