goss.hpp 4.6 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
13
#include <LightGBM/utils/array_args.h>
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/openmp_wrapper.h>

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

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

namespace LightGBM {

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

  ~GOSS() {
  }

Guolin Ke's avatar
Guolin Ke committed
36
  void Init(const Config* config, const Dataset* train_data, const ObjectiveFunction* objective_function,
37
38
            const std::vector<const Metric*>& training_metrics) override {
    GBDT::Init(config, train_data, objective_function, training_metrics);
39
40
41
42
43
44
45
46
47
    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
48
  void ResetConfig(const Config* config) override {
49
50
51
52
53
    GBDT::ResetConfig(config);
    ResetGoss();
  }

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

    is_use_subset_ = false;
Guolin Ke's avatar
Guolin Ke committed
65
66
    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_);
67
      bag_data_cnt = std::max(1, bag_data_cnt);
Guolin Ke's avatar
Guolin Ke committed
68
69
70
71
72
73
74
75
      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_;
  }

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

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

}  // namespace LightGBM
#endif   // LIGHTGBM_BOOSTING_GOSS_H_