goss.hpp 4.56 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2017 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
#ifndef LIGHTGBM_BOOSTING_GOSS_H_
#define LIGHTGBM_BOOSTING_GOSS_H_

8
#include <LightGBM/boosting.h>
Guolin Ke's avatar
Guolin Ke committed
9
10
11
12
#include <LightGBM/utils/array_args.h>
#include <LightGBM/utils/log.h>

#include <string>
13
#include <algorithm>
14
15
16
17
18
19
20
#include <chrono>
#include <cstdio>
#include <fstream>
#include <vector>

#include "gbdt.h"
#include "score_updater.hpp"
Guolin Ke's avatar
Guolin Ke committed
21
22
23
24

namespace LightGBM {

class GOSS: public GBDT {
Nikita Titov's avatar
Nikita Titov committed
25
 public:
Guolin Ke's avatar
Guolin Ke committed
26
27
28
  /*!
  * \brief Constructor
  */
29
  GOSS() : GBDT() {
Guolin Ke's avatar
Guolin Ke committed
30
31
32
33
34
  }

  ~GOSS() {
  }

Guolin Ke's avatar
Guolin Ke committed
35
  void Init(const Config* config, const Dataset* train_data, const ObjectiveFunction* objective_function,
36
37
            const std::vector<const Metric*>& training_metrics) override {
    GBDT::Init(config, train_data, objective_function, training_metrics);
38
39
40
41
42
43
44
45
46
    ResetGoss();
  }

  void ResetTrainingData(const Dataset* train_data, const ObjectiveFunction* objective_function,
                         const std::vector<const Metric*>& training_metrics) override {
    GBDT::ResetTrainingData(train_data, objective_function, training_metrics);
    ResetGoss();
  }

Guolin Ke's avatar
Guolin Ke committed
47
  void ResetConfig(const Config* config) override {
48
49
50
51
52
    GBDT::ResetConfig(config);
    ResetGoss();
  }

  void ResetGoss() {
Nikita Titov's avatar
Nikita Titov committed
53
    CHECK_LE(config_->top_rate + config_->other_rate, 1.0f);
Guolin Ke's avatar
Guolin Ke committed
54
55
    CHECK(config_->top_rate > 0.0f && config_->other_rate > 0.0f);
    if (config_->bagging_freq > 0 && config_->bagging_fraction != 1.0f) {
56
      Log::Fatal("Cannot use bagging in GOSS");
Guolin Ke's avatar
Guolin Ke committed
57
    }
58
    Log::Info("Using GOSS");
59
    balanced_bagging_ = false;
Guolin Ke's avatar
Guolin Ke committed
60
    bag_data_indices_.resize(num_data_);
61
    bagging_runner_.ReSize(num_data_);
Guolin Ke's avatar
Guolin Ke committed
62
63

    is_use_subset_ = false;
Guolin Ke's avatar
Guolin Ke committed
64
65
    if (config_->top_rate + config_->other_rate <= 0.5) {
      auto bag_data_cnt = static_cast<data_size_t>((config_->top_rate + config_->other_rate) * num_data_);
66
      bag_data_cnt = std::max(1, bag_data_cnt);
Guolin Ke's avatar
Guolin Ke committed
67
68
69
70
71
72
73
74
      tmp_subset_.reset(new Dataset(bag_data_cnt));
      tmp_subset_->CopyFeatureMapperFrom(train_data_);
      is_use_subset_ = true;
    }
    // flag to not bagging first
    bag_data_cnt_ = num_data_;
  }

75
  data_size_t BaggingHelper(data_size_t start, data_size_t cnt, data_size_t* buffer) override {
76
    if (cnt <= 0) {
77
78
      return 0;
    }
79
    std::vector<score_t> tmp_gradients(cnt, 0.0f);
Guolin Ke's avatar
Guolin Ke committed
80
    for (data_size_t i = 0; i < cnt; ++i) {
81
      for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
82
        size_t idx = static_cast<size_t>(cur_tree_id) * num_data_ + start + i;
Guolin Ke's avatar
Guolin Ke committed
83
84
        tmp_gradients[i] += std::fabs(gradients_[idx] * hessians_[idx]);
      }
Guolin Ke's avatar
Guolin Ke committed
85
    }
Guolin Ke's avatar
Guolin Ke committed
86
87
    data_size_t top_k = static_cast<data_size_t>(cnt * config_->top_rate);
    data_size_t other_k = static_cast<data_size_t>(cnt * config_->other_rate);
Guolin Ke's avatar
Guolin Ke committed
88
    top_k = std::max(1, top_k);
Guolin Ke's avatar
Guolin Ke committed
89
    ArrayArgs<score_t>::ArgMaxAtK(&tmp_gradients, 0, static_cast<int>(tmp_gradients.size()), top_k - 1);
90
    score_t threshold = tmp_gradients[top_k - 1];
Guolin Ke's avatar
Guolin Ke committed
91

92
    score_t multiply = static_cast<score_t>(cnt - top_k) / other_k;
Guolin Ke's avatar
Guolin Ke committed
93
    data_size_t cur_left_cnt = 0;
94
    data_size_t cur_right_pos = cnt;
Guolin Ke's avatar
Guolin Ke committed
95
96
    data_size_t big_weight_cnt = 0;
    for (data_size_t i = 0; i < cnt; ++i) {
97
      auto cur_idx = start + i;
98
      score_t grad = 0.0f;
99
      for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
100
        size_t idx = static_cast<size_t>(cur_tree_id) * num_data_ + cur_idx;
Guolin Ke's avatar
Guolin Ke committed
101
102
103
        grad += std::fabs(gradients_[idx] * hessians_[idx]);
      }
      if (grad >= threshold) {
104
        buffer[cur_left_cnt++] = cur_idx;
Guolin Ke's avatar
Guolin Ke committed
105
106
107
108
109
110
        ++big_weight_cnt;
      } else {
        data_size_t sampled = cur_left_cnt - big_weight_cnt;
        data_size_t rest_need = other_k - sampled;
        data_size_t rest_all = (cnt - i) - (top_k - big_weight_cnt);
        double prob = (rest_need) / static_cast<double>(rest_all);
111
112
        if (bagging_rands_[cur_idx / bagging_rand_block_].NextFloat() < prob) {
          buffer[cur_left_cnt++] = cur_idx;
113
          for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
114
            size_t idx = static_cast<size_t>(cur_tree_id) * num_data_ + cur_idx;
Guolin Ke's avatar
Guolin Ke committed
115
116
117
            gradients_[idx] *= multiply;
            hessians_[idx] *= multiply;
          }
Guolin Ke's avatar
Guolin Ke committed
118
        } else {
119
          buffer[--cur_right_pos] = cur_idx;
Guolin Ke's avatar
Guolin Ke committed
120
121
122
123
124
125
126
127
128
        }
      }
    }
    return cur_left_cnt;
  }

  void Bagging(int iter) override {
    bag_data_cnt_ = num_data_;
    // not subsample for first iterations
Guolin Ke's avatar
Guolin Ke committed
129
    if (iter < static_cast<int>(1.0f / config_->learning_rate)) { return; }
Guolin Ke's avatar
Guolin Ke committed
130
    GBDT::Bagging(iter);
Guolin Ke's avatar
Guolin Ke committed
131
132
133
134
135
  }
};

}  // namespace LightGBM
#endif   // LIGHTGBM_BOOSTING_GOSS_H_