score_updater.hpp 2.51 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef LIGHTGBM_BOOSTING_SCORE_UPDATER_HPP_
#define LIGHTGBM_BOOSTING_SCORE_UPDATER_HPP_

#include <LightGBM/meta.h>
#include <LightGBM/dataset.h>
#include <LightGBM/tree.h>
#include <LightGBM/tree_learner.h>

#include <cstring>

namespace LightGBM {
/*!
* \brief Used to store and update score for data
*/
class ScoreUpdater {
public:
  /*!
  * \brief Constructor, will pass a const pointer of dataset
  * \param data This class will bind with this data set
  */
  explicit ScoreUpdater(const Dataset* data)
    :data_(data) {
    num_data_ = data->num_data();
    score_ = new score_t[num_data_];
    // default start score is zero
    std::memset(score_, 0, sizeof(score_t)*num_data_);
    const score_t* init_score = data->metadata().init_score();
    // if exists initial score, will start from it
    if (init_score != nullptr) {
      for (data_size_t i = 0; i < num_data_; ++i) {
        score_[i] = init_score[i];
      }
    }
  }
  /*! \brief Destructor */
  ~ScoreUpdater() {
    delete[] score_;
  }
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
40
41
  * \brief Using tree model to get prediction number, then adding to scores for all data
  *        Note: this function generally will be used on validation data too.
Guolin Ke's avatar
Guolin Ke committed
42
43
44
45
46
47
  * \param tree Trained tree model
  */
  inline void AddScore(const Tree* tree) {
    tree->AddPredictionToScore(data_, num_data_, score_);
  }
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
48
49
50
  * \brief Adding prediction score, only used for training data.
  *        The training data is partitioned into tree leaves after training
  *        Based on which We can get prediction quckily.
Guolin Ke's avatar
Guolin Ke committed
51
52
53
54
55
56
  * \param tree_learner
  */
  inline void AddScore(const TreeLearner* tree_learner) {
    tree_learner->AddPredictionToScore(score_);
  }
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
57
58
  * \brief Using tree model to get prediction number, then adding to scores for parts of data
  *        Used for prediction of training out-of-bag data
Guolin Ke's avatar
Guolin Ke committed
59
  * \param tree Trained tree model
Hui Xue's avatar
Hui Xue committed
60
61
  * \param data_indices Indices of data that will be proccessed
  * \param data_cnt Number of data that will be proccessed
Guolin Ke's avatar
Guolin Ke committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  */
  inline void AddScore(const Tree* tree, const data_size_t* data_indices,
                                                  data_size_t data_cnt) {
    tree->AddPredictionToScore(data_, data_indices, data_cnt, score_);
  }
  /*! \brief Pointer of score */
  inline const score_t * score() { return score_; }

private:
  /*! \brief Number of total data */
  data_size_t num_data_;
  /*! \brief Pointer of data set */
  const Dataset* data_;
  /*! \brief scores for data set */
  score_t* score_;
};

}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
80
#endif   // LightGBM_BOOSTING_SCORE_UPDATER_HPP_