"vscode:/vscode.git/clone" did not exist on "92f2a570efbd9af898cbb4bf42dec1a595c6e9d7"
split_info.hpp 2.53 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef LIGHTGBM_TREELEARNER_SPLIT_INFO_HPP_
#define LIGHTGBM_TREELEARNER_SPLIT_INFO_HPP_

#include <LightGBM/meta.h>

#include <cmath>
#include <cstdint>
#include <cstring>

#include <functional>

namespace LightGBM {

/*!
* \brief Used to store some information for gain split point
*/
struct SplitInfo {
public:
  /*! \brief Feature index */
  int feature;
  /*! \brief Split threshold */
  unsigned int threshold;
  /*! \brief Left output after split */
Guolin Ke's avatar
Guolin Ke committed
24
  double left_output;
Guolin Ke's avatar
Guolin Ke committed
25
  /*! \brief Right output after split */
Guolin Ke's avatar
Guolin Ke committed
26
  double right_output;
Guolin Ke's avatar
Guolin Ke committed
27
  /*! \brief Split gain */
Guolin Ke's avatar
Guolin Ke committed
28
  double gain;
Guolin Ke's avatar
Guolin Ke committed
29
30
31
32
33
  /*! \brief Left number of data after split */
  data_size_t left_count;
  /*! \brief Right number of data after split */
  data_size_t right_count;
  /*! \brief Left sum gradient after split */
Guolin Ke's avatar
Guolin Ke committed
34
  double left_sum_gradient;
Guolin Ke's avatar
Guolin Ke committed
35
  /*! \brief Left sum hessian after split */
Guolin Ke's avatar
Guolin Ke committed
36
  double left_sum_hessian;
Guolin Ke's avatar
Guolin Ke committed
37
  /*! \brief Right sum gradient after split */
Guolin Ke's avatar
Guolin Ke committed
38
  double right_sum_gradient;
Guolin Ke's avatar
Guolin Ke committed
39
  /*! \brief Right sum hessian after split */
Guolin Ke's avatar
Guolin Ke committed
40
  double right_sum_hessian;
Guolin Ke's avatar
Guolin Ke committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

  SplitInfo() {
    // initilize with -1 and -inf gain
    feature = -1;
    gain = kMinScore;
  }

  inline void Reset() {
    // initilize with -1 and -inf gain
    feature = -1;
    gain = kMinScore;
  }

  inline bool operator > (const SplitInfo &si) const;

  inline static void MaxReducer(const char* src, char* dst, int len) {
    const int type_size = sizeof(SplitInfo);
    int used_size = 0;
    const SplitInfo* p1;
    SplitInfo* p2;
    while (used_size < len) {
      p1 = reinterpret_cast<const SplitInfo*>(src);
      p2 = reinterpret_cast<SplitInfo*>(dst);
      if (*p1 > *p2) {
        // copy
        std::memcpy(dst, src, type_size);
      }
      src += type_size;
      dst += type_size;
      used_size += type_size;
    }
  }
};



inline bool SplitInfo::operator > (const SplitInfo& si) const {
Guolin Ke's avatar
Guolin Ke committed
78
79
  double local_gain = this->gain;
  double other_gain = si.gain;
Guolin Ke's avatar
Guolin Ke committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  // replace nan with -inf
  if (local_gain == NAN) {
    local_gain = kMinScore;
  }
  // replace nan with -inf
  if (other_gain == NAN) {
    other_gain = kMinScore;
  }
  int local_feature = this->feature;
  int other_feature = si.feature;
  // replace -1 with max int
  if (local_feature == -1) {
    local_feature = INT32_MAX;
  }
  // replace -1 with max int
  if (other_feature == -1) {
    other_feature = INT32_MAX;
  }
  if (local_gain != other_gain) {
    return local_gain > other_gain;
  } else {
    // if same gain, use smaller feature
    return local_feature < other_feature;
  }
}

}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
107
#endif   // LightGBM_TREELEARNER_SPLIT_INFO_HPP_