dart.hpp 6.85 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
#ifndef LIGHTGBM_BOOSTING_DART_H_
#define LIGHTGBM_BOOSTING_DART_H_

#include <LightGBM/boosting.h>
#include "score_updater.hpp"
#include "gbdt.h"

#include <cstdio>
#include <vector>
#include <string>
#include <fstream>

namespace LightGBM {
/*!
* \brief DART algorithm implementation. including Training, prediction, bagging.
*/
class DART: public GBDT {
public:
  /*!
  * \brief Constructor
  */
22
  DART() : GBDT() { }
Guolin Ke's avatar
Guolin Ke committed
23
24
25
26
27
28
29
30
  /*!
  * \brief Destructor
  */
  ~DART() { }
  /*!
  * \brief Initialization logic
  * \param config Config for boosting
  * \param train_data Training data
31
  * \param objective_function Training objective function
Guolin Ke's avatar
Guolin Ke committed
32
33
34
  * \param training_metrics Training metrics
  * \param output_model_filename Filename of output model
  */
Guolin Ke's avatar
Guolin Ke committed
35
  void Init(const Config* config, const Dataset* train_data,
36
            const ObjectiveFunction* objective_function,
37
38
            const std::vector<const Metric*>& training_metrics) override {
    GBDT::Init(config, train_data, objective_function, training_metrics);
Guolin Ke's avatar
Guolin Ke committed
39
    random_for_drop_ = Random(config_->drop_seed);
40
    sum_weight_ = 0.0f;
Guolin Ke's avatar
Guolin Ke committed
41
  }
Guolin Ke's avatar
Guolin Ke committed
42

Guolin Ke's avatar
Guolin Ke committed
43
  void ResetConfig(const Config* config) override {
Guolin Ke's avatar
Guolin Ke committed
44
    GBDT::ResetConfig(config);
Guolin Ke's avatar
Guolin Ke committed
45
    random_for_drop_ = Random(config_->drop_seed);
Guolin Ke's avatar
Guolin Ke committed
46
47
48
    sum_weight_ = 0.0f;
  }

Guolin Ke's avatar
Guolin Ke committed
49
50
51
  /*!
  * \brief one training iteration
  */
Guolin Ke's avatar
Guolin Ke committed
52
  bool TrainOneIter(const score_t* gradient, const score_t* hessian) override {
Guolin Ke's avatar
Guolin Ke committed
53
    is_update_score_cur_iter_ = false;
Guolin Ke's avatar
Guolin Ke committed
54
55
56
57
    bool ret = GBDT::TrainOneIter(gradient, hessian);
    if (ret) {
      return ret;
    }
Guolin Ke's avatar
Guolin Ke committed
58
59
    // normalize
    Normalize();
Guolin Ke's avatar
Guolin Ke committed
60
    if (!config_->uniform_drop) {
61
62
63
      tree_weight_.push_back(shrinkage_rate_);
      sum_weight_ += shrinkage_rate_;
    }
Guolin Ke's avatar
Guolin Ke committed
64
    return false;
Guolin Ke's avatar
Guolin Ke committed
65
  }
Guolin Ke's avatar
Guolin Ke committed
66

Guolin Ke's avatar
Guolin Ke committed
67
68
69
70
71
  /*!
  * \brief Get current training score
  * \param out_len length of returned score
  * \return training score
  */
72
  const double* GetTrainingScore(int64_t* out_len) override {
Guolin Ke's avatar
Guolin Ke committed
73
74
75
76
77
    if (!is_update_score_cur_iter_) {
      // only drop one time in one iteration
      DroppingTrees();
      is_update_score_cur_iter_ = true;
    }
78
    *out_len = static_cast<int64_t>(train_score_updater_->num_data()) * num_class_;
Guolin Ke's avatar
Guolin Ke committed
79
80
    return train_score_updater_->score();
  }
Guolin Ke's avatar
Guolin Ke committed
81

Guolin Ke's avatar
Guolin Ke committed
82
83
84
85
86
87
private:
  /*!
  * \brief drop trees based on drop_rate
  */
  void DroppingTrees() {
    drop_index_.clear();
Guolin Ke's avatar
Guolin Ke committed
88
    bool is_skip = random_for_drop_.NextFloat() < config_->skip_drop;
zhangyafeikimi's avatar
zhangyafeikimi committed
89
    // select dropping tree indices based on drop_rate and tree weights
90
    if (!is_skip) {
Guolin Ke's avatar
Guolin Ke committed
91
92
      double drop_rate = config_->drop_rate;
      if (!config_->uniform_drop) {
93
        double inv_average_weight = static_cast<double>(tree_weight_.size()) / sum_weight_;
Guolin Ke's avatar
Guolin Ke committed
94
95
        if (config_->max_drop > 0) {
          drop_rate = std::min(drop_rate, config_->max_drop * inv_average_weight / sum_weight_);
96
97
        }
        for (int i = 0; i < iter_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
98
          if (random_for_drop_.NextFloat() < drop_rate * tree_weight_[i] * inv_average_weight) {
Guolin Ke's avatar
Guolin Ke committed
99
            drop_index_.push_back(num_init_iteration_ + i);
Guolin Ke's avatar
Guolin Ke committed
100
            if (drop_index_.size() >= static_cast<size_t>(config_->max_drop)) {
101
102
              break;
            }
103
104
105
          }
        }
      } else {
Guolin Ke's avatar
Guolin Ke committed
106
107
        if (config_->max_drop > 0) {
          drop_rate = std::min(drop_rate, config_->max_drop / static_cast<double>(iter_));
108
109
        }
        for (int i = 0; i < iter_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
110
          if (random_for_drop_.NextFloat() < drop_rate) {
Guolin Ke's avatar
Guolin Ke committed
111
            drop_index_.push_back(num_init_iteration_ + i);
Guolin Ke's avatar
Guolin Ke committed
112
            if (drop_index_.size() >= static_cast<size_t>(config_->max_drop)) {
113
114
              break;
            }
115
          }
Guolin Ke's avatar
Guolin Ke committed
116
117
118
119
120
        }
      }
    }
    // drop trees
    for (auto i : drop_index_) {
121
122
      for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
        auto curr_tree = i * num_tree_per_iteration_ + cur_tree_id;
Guolin Ke's avatar
Guolin Ke committed
123
        models_[curr_tree]->Shrinkage(-1.0);
124
        train_score_updater_->AddScore(models_[curr_tree].get(), cur_tree_id);
Guolin Ke's avatar
Guolin Ke committed
125
126
      }
    }
Guolin Ke's avatar
Guolin Ke committed
127
128
    if (!config_->xgboost_dart_mode) {
      shrinkage_rate_ = config_->learning_rate / (1.0f + static_cast<double>(drop_index_.size()));
129
130
    } else {
      if (drop_index_.empty()) {
Guolin Ke's avatar
Guolin Ke committed
131
        shrinkage_rate_ = config_->learning_rate;
132
      } else {
Guolin Ke's avatar
Guolin Ke committed
133
        shrinkage_rate_ = config_->learning_rate / (config_->learning_rate + static_cast<double>(drop_index_.size()));
134
135
      }
    }
Guolin Ke's avatar
Guolin Ke committed
136
137
138
  }
  /*!
  * \brief normalize dropped trees
139
  * NOTE: num_drop_tree(k), learning_rate(lr), shrinkage_rate_ = lr / (k + 1)
wxchan's avatar
wxchan committed
140
  *       step 1: shrink tree to -1 -> drop tree
141
  *       step 2: shrink tree to k / (k + 1) - 1 from -1, by 1/(k+1)
wxchan's avatar
wxchan committed
142
  *               -> normalize for valid data
143
  *       step 3: shrink tree to k / (k + 1) from k / (k + 1) - 1, by -k
wxchan's avatar
wxchan committed
144
  *               -> normalize for train data
145
  *       end with tree weight = (k / (k + 1)) * old_weight
Guolin Ke's avatar
Guolin Ke committed
146
147
148
  */
  void Normalize() {
    double k = static_cast<double>(drop_index_.size());
Guolin Ke's avatar
Guolin Ke committed
149
    if (!config_->xgboost_dart_mode) {
150
      for (auto i : drop_index_) {
151
152
        for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
          auto curr_tree = i * num_tree_per_iteration_ + cur_tree_id;
153
154
155
          // update validation score
          models_[curr_tree]->Shrinkage(1.0f / (k + 1.0f));
          for (auto& score_updater : valid_score_updater_) {
156
            score_updater->AddScore(models_[curr_tree].get(), cur_tree_id);
157
158
159
          }
          // update training score
          models_[curr_tree]->Shrinkage(-k);
160
          train_score_updater_->AddScore(models_[curr_tree].get(), cur_tree_id);
161
        }
Guolin Ke's avatar
Guolin Ke committed
162
        if (!config_->uniform_drop) {
163
164
165
166
167
168
          sum_weight_ -= tree_weight_[i] * (1.0f / (k + 1.0f));
          tree_weight_[i] *= (k / (k + 1.0f));
        }
      }
    } else {
      for (auto i : drop_index_) {
169
170
        for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
          auto curr_tree = i * num_tree_per_iteration_ + cur_tree_id;
171
172
173
          // update validation score
          models_[curr_tree]->Shrinkage(shrinkage_rate_);
          for (auto& score_updater : valid_score_updater_) {
174
            score_updater->AddScore(models_[curr_tree].get(), cur_tree_id);
175
176
          }
          // update training score
Guolin Ke's avatar
Guolin Ke committed
177
          models_[curr_tree]->Shrinkage(-k / config_->learning_rate);
178
          train_score_updater_->AddScore(models_[curr_tree].get(), cur_tree_id);
179
        }
Guolin Ke's avatar
Guolin Ke committed
180
181
182
        if (!config_->uniform_drop) {
          sum_weight_ -= tree_weight_[i] * (1.0f / (k + config_->learning_rate));;
          tree_weight_[i] *= (k / (k + config_->learning_rate));
Guolin Ke's avatar
Guolin Ke committed
183
184
185
186
        }
      }
    }
  }
187
188
189
190
  /*! \brief The weights of all trees, used to choose drop trees */
  std::vector<double> tree_weight_;
  /*! \brief sum weights of all trees */
  double sum_weight_;
zhangyafeikimi's avatar
zhangyafeikimi committed
191
  /*! \brief The indices of dropping trees */
192
  std::vector<int> drop_index_;
Guolin Ke's avatar
Guolin Ke committed
193
194
  /*! \brief Random generator, used to select dropping trees */
  Random random_for_drop_;
Guolin Ke's avatar
Guolin Ke committed
195
196
  /*! \brief Flag that the score is update on current iter or not*/
  bool is_update_score_cur_iter_;
Guolin Ke's avatar
Guolin Ke committed
197
198
199
200
};

}  // namespace LightGBM
#endif   // LightGBM_BOOSTING_DART_H_