serial_tree_learner.h 5.48 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
#ifndef LIGHTGBM_TREELEARNER_SERIAL_TREE_LEARNER_H_
#define LIGHTGBM_TREELEARNER_SERIAL_TREE_LEARNER_H_

#include <LightGBM/utils/random.h>
#include <LightGBM/utils/array_args.h>

#include <LightGBM/tree_learner.h>
#include <LightGBM/dataset.h>
#include <LightGBM/tree.h>
Guolin Ke's avatar
Guolin Ke committed
10

Guolin Ke's avatar
Guolin Ke committed
11
12
#include "feature_histogram.hpp"
#include "split_info.hpp"
Guolin Ke's avatar
Guolin Ke committed
13
#include "data_partition.hpp"
Guolin Ke's avatar
Guolin Ke committed
14
15
16
17
18
19
#include "leaf_splits.hpp"

#include <cstdio>
#include <vector>
#include <random>
#include <cmath>
Guolin Ke's avatar
Guolin Ke committed
20
#include <memory>
Guolin Ke's avatar
Guolin Ke committed
21
22
23
24
25
26
27
28

namespace LightGBM {

/*!
* \brief Used for learning a tree by single machine
*/
class SerialTreeLearner: public TreeLearner {
public:
Guolin Ke's avatar
Guolin Ke committed
29
  explicit SerialTreeLearner(const TreeConfig* tree_config);
Guolin Ke's avatar
Guolin Ke committed
30
31
32
33
34

  ~SerialTreeLearner();

  void Init(const Dataset* train_data) override;

Guolin Ke's avatar
Guolin Ke committed
35
36
  void ResetTrainingData(const Dataset* train_data) override;

Guolin Ke's avatar
Guolin Ke committed
37
38
  void ResetConfig(const TreeConfig* tree_config) override;

39
  Tree* Train(const score_t* gradients, const score_t *hessians, bool is_constant_hessian) override;
Guolin Ke's avatar
Guolin Ke committed
40

Guolin Ke's avatar
Guolin Ke committed
41
42
  Tree* FitByExistingTree(const Tree* old_tree, const score_t* gradients, const score_t* hessians) const override;

Guolin Ke's avatar
Guolin Ke committed
43
44
45
46
  void SetBaggingData(const data_size_t* used_indices, data_size_t num_data) override {
    data_partition_->SetUsedDataIndices(used_indices, num_data);
  }

Guolin Ke's avatar
Guolin Ke committed
47
48
49
  void AddPredictionToScore(const Tree* tree, double* out_score) const override {
    if (tree->num_leaves() <= 1) { return; }
    CHECK(tree->num_leaves() <= data_partition_->num_leaves());
Guolin Ke's avatar
Guolin Ke committed
50
    #pragma omp parallel for schedule(static)
51
    for (int i = 0; i < tree->num_leaves(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
52
      double output = static_cast<double>(tree->LeafOutput(i));
Guolin Ke's avatar
Guolin Ke committed
53
54
      data_size_t cnt_leaf_data = 0;
      auto tmp_idx = data_partition_->GetIndexOnLeaf(i, &cnt_leaf_data);
Guolin Ke's avatar
Guolin Ke committed
55
      for (data_size_t j = 0; j < cnt_leaf_data; ++j) {
56
        out_score[tmp_idx[j]] += output;
Guolin Ke's avatar
Guolin Ke committed
57
58
59
60
61
62
63
64
65
66
67
68
69
      }
    }
  }

protected:
  /*!
  * \brief Some initial works before training
  */
  virtual void BeforeTrain();

  /*!
  * \brief Some initial works before FindBestSplit
  */
Guolin Ke's avatar
Guolin Ke committed
70
  virtual bool BeforeFindBestSplit(const Tree* tree, int left_leaf, int right_leaf);
Guolin Ke's avatar
Guolin Ke committed
71

72
  void ConstructHistograms(const std::vector<int8_t>& is_feature_used, bool use_subtract);
Guolin Ke's avatar
Guolin Ke committed
73
74
75
76
77
78
79
80
81
82
83
84

  /*!
  * \brief Find best thresholds for all features, using multi-threading.
  *  The result will be stored in smaller_leaf_splits_ and larger_leaf_splits_.
  *  This function will be called in FindBestSplit.
  */
  virtual void FindBestThresholds();

  /*!
  * \brief Find best features for leaves from smaller_leaf_splits_ and larger_leaf_splits_.
  *  This function will be called after FindBestThresholds.
  */
Guolin Ke's avatar
Guolin Ke committed
85
  virtual void FindBestSplitsForLeaves();
Guolin Ke's avatar
Guolin Ke committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

  /*!
  * \brief Partition tree and data according best split.
  * \param tree Current tree, will be splitted on this function.
  * \param best_leaf The index of leaf that will be splitted.
  * \param left_leaf The index of left leaf after splitted.
  * \param right_leaf The index of right leaf after splitted.
  */
  virtual void Split(Tree* tree, int best_leaf, int* left_leaf, int* right_leaf);

  /*!
  * \brief Get the number of data in a leaf
  * \param leaf_idx The index of leaf
  * \return The number of data in the leaf_idx leaf
  */
  inline virtual data_size_t GetGlobalDataCountInLeaf(int leaf_idx) const;
  /*! \brief number of data */
  data_size_t num_data_;
  /*! \brief number of features */
  int num_features_;
  /*! \brief training data */
  const Dataset* train_data_;
  /*! \brief gradients of current iteration */
  const score_t* gradients_;
  /*! \brief hessians of current iteration */
  const score_t* hessians_;
  /*! \brief training data partition on leaves */
Guolin Ke's avatar
Guolin Ke committed
113
  std::unique_ptr<DataPartition> data_partition_;
Guolin Ke's avatar
Guolin Ke committed
114
115
  /*! \brief used for generate used features */
  Random random_;
Hui Xue's avatar
Hui Xue committed
116
  /*! \brief used for sub feature training, is_feature_used_[i] = false means don't used feature i */
Guolin Ke's avatar
Guolin Ke committed
117
  std::vector<int8_t> is_feature_used_;
118
119
  /*! \brief pointer to histograms array of parent of current leaves */
  FeatureHistogram* parent_leaf_histogram_array_;
Guolin Ke's avatar
Guolin Ke committed
120
121
122
123
124
125
126
127
128
  /*! \brief pointer to histograms array of smaller leaf */
  FeatureHistogram* smaller_leaf_histogram_array_;
  /*! \brief pointer to histograms array of larger leaf */
  FeatureHistogram* larger_leaf_histogram_array_;

  /*! \brief store best split points for all leaves */
  std::vector<SplitInfo> best_split_per_leaf_;

  /*! \brief stores best thresholds for all feature for smaller leaf */
Guolin Ke's avatar
Guolin Ke committed
129
  std::unique_ptr<LeafSplits> smaller_leaf_splits_;
Guolin Ke's avatar
Guolin Ke committed
130
  /*! \brief stores best thresholds for all feature for larger leaf */
Guolin Ke's avatar
Guolin Ke committed
131
  std::unique_ptr<LeafSplits> larger_leaf_splits_;
Guolin Ke's avatar
Guolin Ke committed
132
133

  /*! \brief gradients of current iteration, ordered for cache optimized */
Guolin Ke's avatar
Guolin Ke committed
134
  std::vector<score_t> ordered_gradients_;
Guolin Ke's avatar
Guolin Ke committed
135
  /*! \brief hessians of current iteration, ordered for cache optimized */
Guolin Ke's avatar
Guolin Ke committed
136
  std::vector<score_t> ordered_hessians_;
Guolin Ke's avatar
Guolin Ke committed
137
138

  /*! \brief Store ordered bin */
Guolin Ke's avatar
Guolin Ke committed
139
  std::vector<std::unique_ptr<OrderedBin>> ordered_bins_;
Guolin Ke's avatar
Guolin Ke committed
140
141
142
  /*! \brief True if has ordered bin */
  bool has_ordered_bin_ = false;
  /*! \brief  is_data_in_leaf_[i] != 0 means i-th data is marked */
Guolin Ke's avatar
Guolin Ke committed
143
  std::vector<char> is_data_in_leaf_;
144
  /*! \brief used to cache historical histogram to speed up*/
Guolin Ke's avatar
Guolin Ke committed
145
  HistogramPool histogram_pool_;
Guolin Ke's avatar
Guolin Ke committed
146
  /*! \brief config of tree learner*/
Guolin Ke's avatar
Guolin Ke committed
147
  const TreeConfig* tree_config_;
Guolin Ke's avatar
Guolin Ke committed
148
  int num_threads_;
Guolin Ke's avatar
Guolin Ke committed
149
  std::vector<int> ordered_bin_indices_;
150
  bool is_constant_hessian_;
Guolin Ke's avatar
Guolin Ke committed
151
152
153
154
155
156
157
158
159
160
161
};

inline data_size_t SerialTreeLearner::GetGlobalDataCountInLeaf(int leafIdx) const {
  if (leafIdx >= 0) {
    return data_partition_->leaf_count(leafIdx);
  } else {
    return 0;
  }
}

}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
162
#endif   // LightGBM_TREELEARNER_SERIAL_TREE_LEARNER_H_