"vscode:/vscode.git/clone" did not exist on "311017ae3dced667a17ef6a1a22198ba6141d2eb"
tree_learner.h 3.95 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
Guolin Ke's avatar
Guolin Ke committed
5
6
7
8
#ifndef LIGHTGBM_TREE_LEARNER_H_
#define LIGHTGBM_TREE_LEARNER_H_

#include <LightGBM/config.h>
9
#include <LightGBM/meta.h>
10
#include <LightGBM/utils/json11.h>
Guolin Ke's avatar
Guolin Ke committed
11

12
13
14
#include <string>
#include <vector>

Guolin Ke's avatar
Guolin Ke committed
15
16
namespace LightGBM {

17
using json11_internal_lightgbm::Json;
18

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

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

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

39
40
41
  /*! Initialise some temporary storage, only needed for the linear tree; needs to be a method of TreeLearner since we call it in GBDT::RefitTree */
  virtual void InitLinear(const Dataset* /*train_data*/, const int /*max_leaves*/) {}

42
43
44
45
  virtual void ResetIsConstantHessian(bool is_constant_hessian) = 0;

  virtual void ResetTrainingData(const Dataset* train_data,
                                 bool is_constant_hessian) = 0;
Guolin Ke's avatar
Guolin Ke committed
46

Guolin Ke's avatar
Guolin Ke committed
47
48
  /*!
  * \brief Reset tree configs
Guolin Ke's avatar
Guolin Ke committed
49
  * \param config config of tree
Guolin Ke's avatar
Guolin Ke committed
50
  */
Guolin Ke's avatar
Guolin Ke committed
51
  virtual void ResetConfig(const Config* config) = 0;
Guolin Ke's avatar
Guolin Ke committed
52

shiyu1994's avatar
shiyu1994 committed
53
54
55
56
57
58
  /*!
  * \brief Reset boosting_on_gpu_
  * \param boosting_on_gpu flag for boosting on GPU
  */
  virtual void ResetBoostingOnGPU(const bool /*boosting_on_gpu*/) {}

59
60
  virtual void SetForcedSplit(const Json* forced_split_json) = 0;

Guolin Ke's avatar
Guolin Ke committed
61
  /*!
62
  * \brief training tree model on dataset
Guolin Ke's avatar
Guolin Ke committed
63
64
  * \param gradients The first order gradients
  * \param hessians The second order gradients
65
  * \param is_first_tree If linear tree learning is enabled, first tree needs to be handled differently
Guolin Ke's avatar
Guolin Ke committed
66
67
  * \return A trained tree
  */
68
  virtual Tree* Train(const score_t* gradients, const score_t* hessians, bool is_first_tree) = 0;
Guolin Ke's avatar
Guolin Ke committed
69

Guolin Ke's avatar
Guolin Ke committed
70
  /*!
71
  * \brief use an existing tree to fit the new gradients and hessians.
Guolin Ke's avatar
Guolin Ke committed
72
  */
73
  virtual Tree* FitByExistingTree(const Tree* old_tree, const score_t* gradients, const score_t* hessians) const = 0;
Guolin Ke's avatar
Guolin Ke committed
74

75
  virtual Tree* FitByExistingTree(const Tree* old_tree, const std::vector<int>& leaf_pred,
76
                                  const score_t* gradients, const score_t* hessians) const = 0;
77

Guolin Ke's avatar
Guolin Ke committed
78
  /*!
Guolin Ke's avatar
Guolin Ke committed
79
  * \brief Set bagging data
80
  * \param subset subset of bagging
Guolin Ke's avatar
Guolin Ke committed
81
82
83
  * \param used_indices Used data indices
  * \param num_data Number of used data
  */
84
85
86
  virtual void SetBaggingData(const Dataset* subset,
                              const data_size_t* used_indices,
                              data_size_t num_data) = 0;
87

Guolin Ke's avatar
Guolin Ke committed
88
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
89
  * \brief Using last trained tree to predict score then adding to out_score;
Guolin Ke's avatar
Guolin Ke committed
90
91
  * \param out_score output score
  */
Guolin Ke's avatar
Guolin Ke committed
92
  virtual void AddPredictionToScore(const Tree* tree, double* out_score) const = 0;
Guolin Ke's avatar
Guolin Ke committed
93

94
  virtual void RenewTreeOutput(Tree* tree, const ObjectiveFunction* obj, std::function<double(const label_t*, int)> residual_getter,
95
                               data_size_t total_num_data, const data_size_t* bag_indices, data_size_t bag_cnt, const double* train_score) const = 0;
96

Guolin Ke's avatar
Guolin Ke committed
97
98
99
100
101
102
  TreeLearner() = default;
  /*! \brief Disable copy */
  TreeLearner& operator=(const TreeLearner&) = delete;
  /*! \brief Disable copy */
  TreeLearner(const TreeLearner&) = delete;

Guolin Ke's avatar
Guolin Ke committed
103
104
  /*!
  * \brief Create object of tree learner
105
106
  * \param learner_type Type of tree learner
  * \param device_type Type of tree learner
107
  * \param booster_type Type of boosting
Guolin Ke's avatar
Guolin Ke committed
108
  * \param config config of tree
Guolin Ke's avatar
Guolin Ke committed
109
  */
110
  static TreeLearner* CreateTreeLearner(const std::string& learner_type,
111
                                        const std::string& device_type,
112
113
                                        const Config* config,
                                        const bool boosting_on_cuda);
Guolin Ke's avatar
Guolin Ke committed
114
115
116
117
};

}  // namespace LightGBM

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