Medial Code Documentation
Loading...
Searching...
No Matches
split_info.hpp
1#ifndef LIGHTGBM_TREELEARNER_SPLIT_INFO_HPP_
2#define LIGHTGBM_TREELEARNER_SPLIT_INFO_HPP_
3
4#include <LightGBM/meta.h>
5
6#include <cmath>
7#include <cstdint>
8#include <cstring>
9#include <limits>
10#include <functional>
11
12namespace LightGBM {
13
17struct SplitInfo {
18public:
20 int feature = -1;
22 uint32_t threshold = 0;
27 int num_cat_threshold = 0;
29 double left_output = 0.0;
31 double right_output = 0.0;
33 double gain = kMinScore;
37 double left_sum_hessian = 0;
42 std::vector<uint32_t> cat_threshold;
44 bool default_left = true;
45 int8_t monotone_type = 0;
46 double min_constraint = -std::numeric_limits<double>::max();
47 double max_constraint = std::numeric_limits<double>::max();
48 inline static int Size(int max_cat_threshold) {
49 return 2 * sizeof(int) + sizeof(uint32_t) + sizeof(bool) + sizeof(double) * 9 + sizeof(data_size_t) * 2 + max_cat_threshold * sizeof(uint32_t) + sizeof(int8_t);
50 }
51
52 inline void CopyTo(char* buffer) const {
53 std::memcpy(buffer, &feature, sizeof(feature));
54 buffer += sizeof(feature);
55 std::memcpy(buffer, &left_count, sizeof(left_count));
56 buffer += sizeof(left_count);
57 std::memcpy(buffer, &right_count, sizeof(right_count));
58 buffer += sizeof(right_count);
59 std::memcpy(buffer, &gain, sizeof(gain));
60 buffer += sizeof(gain);
61 std::memcpy(buffer, &threshold, sizeof(threshold));
62 buffer += sizeof(threshold);
63 std::memcpy(buffer, &left_output, sizeof(left_output));
64 buffer += sizeof(left_output);
65 std::memcpy(buffer, &right_output, sizeof(right_output));
66 buffer += sizeof(right_output);
67 std::memcpy(buffer, &left_sum_gradient, sizeof(left_sum_gradient));
68 buffer += sizeof(left_sum_gradient);
69 std::memcpy(buffer, &left_sum_hessian, sizeof(left_sum_hessian));
70 buffer += sizeof(left_sum_hessian);
71 std::memcpy(buffer, &right_sum_gradient, sizeof(right_sum_gradient));
72 buffer += sizeof(right_sum_gradient);
73 std::memcpy(buffer, &right_sum_hessian, sizeof(right_sum_hessian));
74 buffer += sizeof(right_sum_hessian);
75 std::memcpy(buffer, &default_left, sizeof(default_left));
76 buffer += sizeof(default_left);
77 std::memcpy(buffer, &monotone_type, sizeof(monotone_type));
78 buffer += sizeof(monotone_type);
79 std::memcpy(buffer, &min_constraint, sizeof(min_constraint));
80 buffer += sizeof(min_constraint);
81 std::memcpy(buffer, &max_constraint, sizeof(max_constraint));
82 buffer += sizeof(max_constraint);
83 std::memcpy(buffer, &num_cat_threshold, sizeof(num_cat_threshold));
84 buffer += sizeof(num_cat_threshold);
85 std::memcpy(buffer, cat_threshold.data(), sizeof(uint32_t) * num_cat_threshold);
86 }
87
88 void CopyFrom(const char* buffer) {
89 std::memcpy(&feature, buffer, sizeof(feature));
90 buffer += sizeof(feature);
91 std::memcpy(&left_count, buffer, sizeof(left_count));
92 buffer += sizeof(left_count);
93 std::memcpy(&right_count, buffer, sizeof(right_count));
94 buffer += sizeof(right_count);
95 std::memcpy(&gain, buffer, sizeof(gain));
96 buffer += sizeof(gain);
97 std::memcpy(&threshold, buffer, sizeof(threshold));
98 buffer += sizeof(threshold);
99 std::memcpy(&left_output, buffer, sizeof(left_output));
100 buffer += sizeof(left_output);
101 std::memcpy(&right_output, buffer, sizeof(right_output));
102 buffer += sizeof(right_output);
103 std::memcpy(&left_sum_gradient, buffer, sizeof(left_sum_gradient));
104 buffer += sizeof(left_sum_gradient);
105 std::memcpy(&left_sum_hessian, buffer, sizeof(left_sum_hessian));
106 buffer += sizeof(left_sum_hessian);
107 std::memcpy(&right_sum_gradient, buffer, sizeof(right_sum_gradient));
108 buffer += sizeof(right_sum_gradient);
109 std::memcpy(&right_sum_hessian, buffer, sizeof(right_sum_hessian));
110 buffer += sizeof(right_sum_hessian);
111 std::memcpy(&default_left, buffer, sizeof(default_left));
112 buffer += sizeof(default_left);
113 std::memcpy(&monotone_type, buffer, sizeof(monotone_type));
114 buffer += sizeof(monotone_type);
115 std::memcpy(&min_constraint, buffer, sizeof(min_constraint));
116 buffer += sizeof(min_constraint);
117 std::memcpy(&max_constraint, buffer, sizeof(max_constraint));
118 buffer += sizeof(max_constraint);
119 std::memcpy(&num_cat_threshold, buffer, sizeof(num_cat_threshold));
120 buffer += sizeof(num_cat_threshold);
121 cat_threshold.resize(num_cat_threshold);
122 std::memcpy(cat_threshold.data(), buffer, sizeof(uint32_t) * num_cat_threshold);
123 }
124
125 inline void Reset() {
126 // initialize with -1 and -inf gain
127 feature = -1;
128 gain = kMinScore;
129 }
130
131 inline bool operator > (const SplitInfo& si) const {
132 double local_gain = this->gain;
133 double other_gain = si.gain;
134 // replace nan with -inf
135 if (local_gain == NAN) {
136 local_gain = kMinScore;
137 }
138 // replace nan with -inf
139 if (other_gain == NAN) {
140 other_gain = kMinScore;
141 }
142 int local_feature = this->feature;
143 int other_feature = si.feature;
144 // replace -1 with max int
145 if (local_feature == -1) {
146 local_feature = INT32_MAX;
147 }
148 // replace -1 with max int
149 if (other_feature == -1) {
150 other_feature = INT32_MAX;
151 }
152 if (local_gain != other_gain) {
153 return local_gain > other_gain;
154 } else {
155 // if same gain, use smaller feature
156 return local_feature < other_feature;
157 }
158 }
159
160 inline bool operator == (const SplitInfo& si) const {
161 double local_gain = this->gain;
162 double other_gain = si.gain;
163 // replace nan with -inf
164 if (local_gain == NAN) {
165 local_gain = kMinScore;
166 }
167 // replace nan with -inf
168 if (other_gain == NAN) {
169 other_gain = kMinScore;
170 }
171 int local_feature = this->feature;
172 int other_feature = si.feature;
173 // replace -1 with max int
174 if (local_feature == -1) {
175 local_feature = INT32_MAX;
176 }
177 // replace -1 with max int
178 if (other_feature == -1) {
179 other_feature = INT32_MAX;
180 }
181 if (local_gain != other_gain) {
182 return local_gain == other_gain;
183 } else {
184 // if same gain, use smaller feature
185 return local_feature == other_feature;
186 }
187 }
188};
189
191public:
193 int feature = -1;
195 double gain = kMinScore;
200
201 inline void Reset() {
202 // initialize with -1 and -inf gain
203 feature = -1;
204 gain = kMinScore;
205 }
206
207 void CopyFrom(const SplitInfo& other) {
208 feature = other.feature;
209 gain = other.gain;
210 left_count = other.left_count;
211 right_count = other.right_count;
212 }
213
214 void CopyFrom(const char* buffer) {
215 std::memcpy(&feature, buffer, sizeof(feature));
216 buffer += sizeof(feature);
217 std::memcpy(&left_count, buffer, sizeof(left_count));
218 buffer += sizeof(left_count);
219 std::memcpy(&right_count, buffer, sizeof(right_count));
220 buffer += sizeof(right_count);
221 std::memcpy(&gain, buffer, sizeof(gain));
222 buffer += sizeof(gain);
223 }
224
225 inline bool operator > (const LightSplitInfo& si) const {
226 double local_gain = this->gain;
227 double other_gain = si.gain;
228 // replace nan with -inf
229 if (local_gain == NAN) {
230 local_gain = kMinScore;
231 }
232 // replace nan with -inf
233 if (other_gain == NAN) {
234 other_gain = kMinScore;
235 }
236 int local_feature = this->feature;
237 int other_feature = si.feature;
238 // replace -1 with max int
239 if (local_feature == -1) {
240 local_feature = INT32_MAX;
241 }
242 // replace -1 with max int
243 if (other_feature == -1) {
244 other_feature = INT32_MAX;
245 }
246 if (local_gain != other_gain) {
247 return local_gain > other_gain;
248 } else {
249 // if same gain, use smaller feature
250 return local_feature < other_feature;
251 }
252 }
253
254 inline bool operator == (const LightSplitInfo& si) const {
255 double local_gain = this->gain;
256 double other_gain = si.gain;
257 // replace nan with -inf
258 if (local_gain == NAN) {
259 local_gain = kMinScore;
260 }
261 // replace nan with -inf
262 if (other_gain == NAN) {
263 other_gain = kMinScore;
264 }
265 int local_feature = this->feature;
266 int other_feature = si.feature;
267 // replace -1 with max int
268 if (local_feature == -1) {
269 local_feature = INT32_MAX;
270 }
271 // replace -1 with max int
272 if (other_feature == -1) {
273 other_feature = INT32_MAX;
274 }
275 if (local_gain != other_gain) {
276 return local_gain == other_gain;
277 } else {
278 // if same gain, use smaller feature
279 return local_feature == other_feature;
280 }
281 }
282};
283
284} // namespace LightGBM
285#endif // LightGBM_TREELEARNER_SPLIT_INFO_HPP_
desc and descl2 fields must be written in reStructuredText format
Definition application.h:10
int32_t data_size_t
Type of data size, it is better to use signed type.
Definition meta.h:14
Definition split_info.hpp:190
int feature
Feature index.
Definition split_info.hpp:193
data_size_t right_count
Right number of data after split.
Definition split_info.hpp:199
double gain
Split gain.
Definition split_info.hpp:195
data_size_t left_count
Left number of data after split.
Definition split_info.hpp:197
Used to store some information for gain split point.
Definition split_info.hpp:17
double gain
Split gain.
Definition split_info.hpp:33
data_size_t left_count
Left number of data after split.
Definition split_info.hpp:24
double left_sum_hessian
Left sum hessian after split.
Definition split_info.hpp:37
uint32_t threshold
Split threshold.
Definition split_info.hpp:22
data_size_t right_count
Right number of data after split.
Definition split_info.hpp:26
int feature
Feature index.
Definition split_info.hpp:20
double left_sum_gradient
Left sum gradient after split.
Definition split_info.hpp:35
double right_sum_gradient
Right sum gradient after split.
Definition split_info.hpp:39
bool default_left
True if default split is left.
Definition split_info.hpp:44
double right_sum_hessian
Right sum hessian after split.
Definition split_info.hpp:41
double right_output
Right output after split.
Definition split_info.hpp:31
double left_output
Left output after split.
Definition split_info.hpp:29