tree_learner.h 3.03 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
#ifndef LIGHTGBM_TREE_LEARNER_H_
#define LIGHTGBM_TREE_LEARNER_H_

#include <LightGBM/config.h>
5
#include <LightGBM/meta.h>
Guolin Ke's avatar
Guolin Ke committed
6

7
#include <string>
Guolin Ke's avatar
Guolin Ke committed
8
9
#include <vector>

10
11
#include <LightGBM/json11.hpp>

12
13
using namespace json11;

Guolin Ke's avatar
Guolin Ke committed
14
15
16
17
18
namespace LightGBM {

/*! \brief forward declaration */
class Tree;
class Dataset;
19
class ObjectiveFunction;
Guolin Ke's avatar
Guolin Ke committed
20
21
22
23
24

/*!
* \brief Interface for tree learner
*/
class TreeLearner {
Nikita Titov's avatar
Nikita Titov committed
25
 public:
Guolin Ke's avatar
Guolin Ke committed
26
27
28
29
  /*! \brief virtual destructor */
  virtual ~TreeLearner() {}

  /*!
Guolin Ke's avatar
Guolin Ke committed
30
  * \brief Initialize tree learner with training dataset
Guolin Ke's avatar
Guolin Ke committed
31
  * \param train_data The used training data
32
  * \param is_constant_hessian True if all hessians share the same value
Guolin Ke's avatar
Guolin Ke committed
33
  */
34
  virtual void Init(const Dataset* train_data, bool is_constant_hessian) = 0;
Guolin Ke's avatar
Guolin Ke committed
35

Guolin Ke's avatar
Guolin Ke committed
36
37
  virtual void ResetTrainingData(const Dataset* train_data) = 0;

Guolin Ke's avatar
Guolin Ke committed
38
39
  /*!
  * \brief Reset tree configs
Guolin Ke's avatar
Guolin Ke committed
40
  * \param config config of tree
Guolin Ke's avatar
Guolin Ke committed
41
  */
Guolin Ke's avatar
Guolin Ke committed
42
  virtual void ResetConfig(const Config* config) = 0;
Guolin Ke's avatar
Guolin Ke committed
43

Guolin Ke's avatar
Guolin Ke committed
44
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
45
  * \brief training tree model on dataset 
Guolin Ke's avatar
Guolin Ke committed
46
47
  * \param gradients The first order gradients
  * \param hessians The second order gradients
48
  * \param is_constant_hessian True if all hessians share the same value
Guolin Ke's avatar
Guolin Ke committed
49
50
  * \return A trained tree
  */
51
  virtual Tree* Train(const score_t* gradients, const score_t* hessians, bool is_constant_hessian,
52
                      Json& forced_split_json) = 0;
Guolin Ke's avatar
Guolin Ke committed
53

Guolin Ke's avatar
Guolin Ke committed
54
55
56
  /*!
  * \brief use a existing tree to fit the new gradients and hessians.
  */
57
  virtual Tree* FitByExistingTree(const Tree* old_tree, const score_t* gradients, const score_t* hessians) const = 0;
Guolin Ke's avatar
Guolin Ke committed
58

59
60
61
  virtual Tree* FitByExistingTree(const Tree* old_tree, const std::vector<int>& leaf_pred,
                                  const score_t* gradients, const score_t* hessians) = 0;

Guolin Ke's avatar
Guolin Ke committed
62
  /*!
Guolin Ke's avatar
Guolin Ke committed
63
  * \brief Set bagging data
Guolin Ke's avatar
Guolin Ke committed
64
65
66
67
68
69
70
  * \param used_indices Used data indices
  * \param num_data Number of used data
  */
  virtual void SetBaggingData(const data_size_t* used_indices,
    data_size_t num_data) = 0;

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
71
  * \brief Using last trained tree to predict score then adding to out_score;
Guolin Ke's avatar
Guolin Ke committed
72
73
  * \param out_score output score
  */
Guolin Ke's avatar
Guolin Ke committed
74
  virtual void AddPredictionToScore(const Tree* tree, double* out_score) const = 0;
Guolin Ke's avatar
Guolin Ke committed
75

76
77
78
  virtual void RenewTreeOutput(Tree* tree, const ObjectiveFunction* obj, const double* prediction,
                               data_size_t total_num_data, const data_size_t* bag_indices, data_size_t bag_cnt) const = 0;

Guolin Ke's avatar
Guolin Ke committed
79
80
81
  virtual void RenewTreeOutput(Tree* tree, const ObjectiveFunction* obj, double prediction,
    data_size_t total_num_data, const data_size_t* bag_indices, data_size_t bag_cnt) const = 0;

Guolin Ke's avatar
Guolin Ke committed
82
83
84
85
86
87
  TreeLearner() = default;
  /*! \brief Disable copy */
  TreeLearner& operator=(const TreeLearner&) = delete;
  /*! \brief Disable copy */
  TreeLearner(const TreeLearner&) = delete;

Guolin Ke's avatar
Guolin Ke committed
88
89
  /*!
  * \brief Create object of tree learner
90
91
  * \param learner_type Type of tree learner
  * \param device_type Type of tree learner
Guolin Ke's avatar
Guolin Ke committed
92
  * \param config config of tree
Guolin Ke's avatar
Guolin Ke committed
93
  */
94
95
  static TreeLearner* CreateTreeLearner(const std::string& learner_type,
    const std::string& device_type,
Guolin Ke's avatar
Guolin Ke committed
96
    const Config* config);
Guolin Ke's avatar
Guolin Ke committed
97
98
99
100
};

}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
101
#endif   // LightGBM_TREE_LEARNER_H_