tree_learner.h 3.05 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>
Guolin Ke's avatar
Guolin Ke committed
10

11
#include <string>
Guolin Ke's avatar
Guolin Ke committed
12
13
#include <vector>

14
15
#include <LightGBM/json11.hpp>

16
17
using namespace json11;

Guolin Ke's avatar
Guolin Ke committed
18
19
20
21
22
namespace LightGBM {

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

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

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

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

Guolin Ke's avatar
Guolin Ke committed
42
43
  /*!
  * \brief Reset tree configs
Guolin Ke's avatar
Guolin Ke committed
44
  * \param config config of tree
Guolin Ke's avatar
Guolin Ke committed
45
  */
Guolin Ke's avatar
Guolin Ke committed
46
  virtual void ResetConfig(const Config* config) = 0;
Guolin Ke's avatar
Guolin Ke committed
47

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

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

63
64
65
  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
66
  /*!
Guolin Ke's avatar
Guolin Ke committed
67
  * \brief Set bagging data
Guolin Ke's avatar
Guolin Ke committed
68
69
70
71
72
73
74
  * \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
75
  * \brief Using last trained tree to predict score then adding to out_score;
Guolin Ke's avatar
Guolin Ke committed
76
77
  * \param out_score output score
  */
Guolin Ke's avatar
Guolin Ke committed
78
  virtual void AddPredictionToScore(const Tree* tree, double* out_score) const = 0;
Guolin Ke's avatar
Guolin Ke committed
79

80
  virtual void RenewTreeOutput(Tree* tree, const ObjectiveFunction* obj, std::function<double(const label_t*, int)> residual_getter,
81
82
                               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
83
84
85
86
87
88
  TreeLearner() = default;
  /*! \brief Disable copy */
  TreeLearner& operator=(const TreeLearner&) = delete;
  /*! \brief Disable copy */
  TreeLearner(const TreeLearner&) = delete;

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

}  // namespace LightGBM

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