7 #ifndef XGBOOST_COMMON_RANDOM_H_ 8 #define XGBOOST_COMMON_RANDOM_H_ 10 #include <rabit/rabit.h> 11 #include <xgboost/logging.h> 30 #if XGBOOST_CUSTOMIZE_GLOBAL_PRNG 37 class CustomGlobalRandomEngine {
40 using result_type = uint32_t;
42 inline static constexpr result_type min() {
46 inline static constexpr result_type max() {
47 return std::numeric_limits<result_type>::max();
53 void seed(result_type val);
57 result_type operator()();
70 #endif // XGBOOST_CUSTOMIZE_GLOBAL_PRNG 88 std::shared_ptr<HostDeviceVector<int>> feature_set_tree_;
89 std::map<int, std::shared_ptr<HostDeviceVector<int>>> feature_set_level_;
90 float colsample_bylevel_{1.0f};
91 float colsample_bytree_{1.0f};
92 float colsample_bynode_{1.0f};
93 GlobalRandomEngine rng_;
95 std::shared_ptr<HostDeviceVector<int>> ColSample(
97 if (colsample == 1.0f)
return p_features;
98 const auto& features = p_features->HostVector();
99 CHECK_GT(features.size(), 0);
100 int n = std::max(1, static_cast<int>(colsample * features.size()));
101 auto p_new_features = std::make_shared<HostDeviceVector<int>>();
102 auto& new_features = *p_new_features;
103 new_features.Resize(features.size());
104 std::copy(features.begin(), features.end(),
105 new_features.HostVector().begin());
106 std::shuffle(new_features.HostVector().begin(),
107 new_features.HostVector().end(), rng_);
108 new_features.Resize(n);
109 std::sort(new_features.HostVector().begin(),
110 new_features.HostVector().end());
112 return p_new_features;
130 rabit::Broadcast(&seed,
sizeof(seed), 0);
143 void Init(int64_t num_col,
float colsample_bynode,
float colsample_bylevel,
144 float colsample_bytree,
bool skip_index_0 =
false) {
145 colsample_bylevel_ = colsample_bylevel;
146 colsample_bytree_ = colsample_bytree;
147 colsample_bynode_ = colsample_bynode;
149 if (feature_set_tree_ ==
nullptr) {
150 feature_set_tree_ = std::make_shared<HostDeviceVector<int>>();
154 int begin_idx = skip_index_0 ? 1 : 0;
155 feature_set_tree_->Resize(num_col - begin_idx);
156 std::iota(feature_set_tree_->HostVector().begin(),
157 feature_set_tree_->HostVector().end(), begin_idx);
159 feature_set_tree_ = ColSample(feature_set_tree_, colsample_bytree_);
166 feature_set_tree_->Resize(0);
167 feature_set_level_.clear();
182 if (colsample_bylevel_ == 1.0f && colsample_bynode_ == 1.0f) {
183 return feature_set_tree_;
186 if (feature_set_level_.count(depth) == 0) {
188 feature_set_level_[depth] = ColSample(feature_set_tree_, colsample_bylevel_);
190 if (colsample_bynode_ == 1.0f) {
192 return feature_set_level_[depth];
195 return ColSample(feature_set_level_[depth], colsample_bynode_);
201 #endif // XGBOOST_COMMON_RANDOM_H_ Definition: host_device_vector.h:200
void Reset()
Resets this object.
Definition: random.h:165
general stream interface for serialization, I/O
A device-and-host vector abstraction layer.
ColumnSampler()
Column sampler constructor.
Definition: random.h:128
ColumnSampler(uint32_t seed)
Column sampler constructor.
Definition: random.h:120
GlobalRandomEngine & GlobalRandom()
global singleton of a random engine. This random engine is thread-local and only visible to current t...
RandomEngine GlobalRandomEngine
global random engine
Definition: random.h:69
namespace of xgboost
Definition: base.h:79
std::shared_ptr< HostDeviceVector< int > > GetFeatureSet(int depth)
Samples a feature set.
Definition: random.h:181
void Init(int64_t num_col, float colsample_bynode, float colsample_bylevel, float colsample_bytree, bool skip_index_0=false)
Initialise this object before use.
Definition: random.h:143
Handles selection of columns due to colsample_bytree, colsample_bylevel and colsample_bynode paramete...
Definition: random.h:87
std::mt19937 RandomEngine
Define mt19937 as default type Random Engine.
Definition: random.h:28