tree_learner.h 2.05 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
24
#ifndef LIGHTGBM_TREE_LEARNER_H_
#define LIGHTGBM_TREE_LEARNER_H_


#include <LightGBM/meta.h>
#include <LightGBM/config.h>

#include <vector>

namespace LightGBM {

/*! \brief forward declaration */
class Tree;
class Dataset;

/*!
* \brief Interface for tree learner
*/
class TreeLearner {
public:
  /*! \brief virtual destructor */
  virtual ~TreeLearner() {}

  /*!
Guolin Ke's avatar
Guolin Ke committed
25
  * \brief Initialize tree learner with training dataset
Guolin Ke's avatar
Guolin Ke committed
26
27
28
29
  * \param train_data The used training data
  */
  virtual void Init(const Dataset* train_data) = 0;

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

Guolin Ke's avatar
Guolin Ke committed
32
33
34
35
36
37
  /*!
  * \brief Reset tree configs
  * \param tree_config config of tree
  */
  virtual void ResetConfig(const TreeConfig* tree_config) = 0;

Guolin Ke's avatar
Guolin Ke committed
38
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
39
  * \brief training tree model on dataset 
Guolin Ke's avatar
Guolin Ke committed
40
41
42
43
44
45
  * \param gradients The first order gradients
  * \param hessians The second order gradients
  * \return A trained tree
  */
  virtual Tree* Train(const score_t* gradients, const score_t* hessians) = 0;

Guolin Ke's avatar
Guolin Ke committed
46
47
48
49
50
  /*!
  * \brief use a existing tree to fit the new gradients and hessians.
  */
  virtual Tree* FitByExistingTree(const Tree* old_tree, const score_t* gradients, const score_t* hessians) const = 0;

Guolin Ke's avatar
Guolin Ke committed
51
  /*!
Guolin Ke's avatar
Guolin Ke committed
52
  * \brief Set bagging data
Guolin Ke's avatar
Guolin Ke committed
53
54
55
56
57
58
59
  * \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
60
  * \brief Using last trained tree to predict score then adding to out_score;
Guolin Ke's avatar
Guolin Ke committed
61
62
  * \param out_score output score
  */
Guolin Ke's avatar
Guolin Ke committed
63
  virtual void AddPredictionToScore(const Tree* tree, double* out_score) const = 0;
Guolin Ke's avatar
Guolin Ke committed
64

Guolin Ke's avatar
Guolin Ke committed
65
66
67
68
69
70
  TreeLearner() = default;
  /*! \brief Disable copy */
  TreeLearner& operator=(const TreeLearner&) = delete;
  /*! \brief Disable copy */
  TreeLearner(const TreeLearner&) = delete;

Guolin Ke's avatar
Guolin Ke committed
71
72
73
  /*!
  * \brief Create object of tree learner
  * \param type Type of tree learner
Guolin Ke's avatar
Guolin Ke committed
74
  * \param tree_config config of tree
Guolin Ke's avatar
Guolin Ke committed
75
  */
76
  static TreeLearner* CreateTreeLearner(const std::string& type,
Guolin Ke's avatar
Guolin Ke committed
77
    const TreeConfig* tree_config);
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
};

}  // namespace LightGBM

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