binary_objective.hpp 6.94 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
#ifndef LIGHTGBM_OBJECTIVE_BINARY_OBJECTIVE_HPP_
#define LIGHTGBM_OBJECTIVE_BINARY_OBJECTIVE_HPP_

#include <LightGBM/objective_function.h>

6
7
#include <string>
#include <algorithm>
Guolin Ke's avatar
Guolin Ke committed
8
#include <cmath>
9
10
#include <cstring>
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
11
12
13

namespace LightGBM {
/*!
14
* \brief Objective function for binary classification
Guolin Ke's avatar
Guolin Ke committed
15
16
*/
class BinaryLogloss: public ObjectiveFunction {
Nikita Titov's avatar
Nikita Titov committed
17
 public:
Guolin Ke's avatar
Guolin Ke committed
18
  explicit BinaryLogloss(const Config& config, std::function<bool(label_t)> is_pos = nullptr) {
19
    sigmoid_ = static_cast<double>(config.sigmoid);
Guolin Ke's avatar
Guolin Ke committed
20
    if (sigmoid_ <= 0.0) {
21
      Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_);
Guolin Ke's avatar
Guolin Ke committed
22
    }
23
    is_unbalance_ = config.is_unbalance;
24
    scale_pos_weight_ = static_cast<double>(config.scale_pos_weight);
25
    if (is_unbalance_ && std::fabs(scale_pos_weight_ - 1.0f) > 1e-6) {
26
      Log::Fatal("Cannot set is_unbalance and scale_pos_weight at the same time");
27
    }
Guolin Ke's avatar
Guolin Ke committed
28
29
    is_pos_ = is_pos;
    if (is_pos_ == nullptr) {
30
      is_pos_ = [](label_t label) {return label > 0; };
Guolin Ke's avatar
Guolin Ke committed
31
    }
Guolin Ke's avatar
Guolin Ke committed
32
  }
Guolin Ke's avatar
Guolin Ke committed
33

34
35
36
  explicit BinaryLogloss(const std::vector<std::string>& strs) {
    sigmoid_ = -1;
    for (auto str : strs) {
Guolin Ke's avatar
Guolin Ke committed
37
      auto tokens = Common::Split(str.c_str(), ':');
38
39
40
41
42
43
44
45
46
47
48
      if (tokens.size() == 2) {
        if (tokens[0] == std::string("sigmoid")) {
          Common::Atof(tokens[1].c_str(), &sigmoid_);
        }
      }
    }
    if (sigmoid_ <= 0.0) {
      Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_);
    }
  }

Guolin Ke's avatar
Guolin Ke committed
49
  ~BinaryLogloss() {}
Guolin Ke's avatar
Guolin Ke committed
50

Guolin Ke's avatar
Guolin Ke committed
51
52
53
54
55
56
  void Init(const Metadata& metadata, data_size_t num_data) override {
    num_data_ = num_data;
    label_ = metadata.label();
    weights_ = metadata.weights();
    data_size_t cnt_positive = 0;
    data_size_t cnt_negative = 0;
57
    // REMOVEME: remove the warning after 2.4 version release
58
59
    Log::Warning("Starting from the 2.1.2 version, default value for "
                 "the \"boost_from_average\" parameter in \"binary\" objective is true.\n"
60
                 "This may cause significantly different results comparing to the previous versions of LightGBM.\n"
61
                 "Try to set boost_from_average=false, if your old models produce bad results");
Guolin Ke's avatar
Guolin Ke committed
62
    // count for positive and negative samples
63
    #pragma omp parallel for schedule(static) reduction(+:cnt_positive, cnt_negative)
Guolin Ke's avatar
Guolin Ke committed
64
    for (data_size_t i = 0; i < num_data_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
65
      if (is_pos_(label_[i])) {
Guolin Ke's avatar
Guolin Ke committed
66
67
68
69
70
        ++cnt_positive;
      } else {
        ++cnt_negative;
      }
    }
71
    need_train_ = true;
72
    if (cnt_negative == 0 || cnt_positive == 0) {
73
      Log::Warning("Contains only one class");
74
      // not need to boost.
75
      need_train_ = false;
76
    }
ProtD's avatar
ProtD committed
77
    Log::Info("Number of positive: %d, number of negative: %d", cnt_positive, cnt_negative);
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
82
83
84
    // use -1 for negative class, and 1 for positive class
    label_val_[0] = -1;
    label_val_[1] = 1;
    // weight for label
    label_weights_[0] = 1.0f;
    label_weights_[1] = 1.0f;
    // if using unbalance, change the labels weight
85
    if (is_unbalance_ && cnt_positive > 0 && cnt_negative > 0) {
86
87
      if (cnt_positive > cnt_negative) {
        label_weights_[1] = 1.0f;
88
        label_weights_[0] = static_cast<double>(cnt_positive) / cnt_negative;
89
      } else {
90
        label_weights_[1] = static_cast<double>(cnt_negative) / cnt_positive;
91
92
        label_weights_[0] = 1.0f;
      }
Guolin Ke's avatar
Guolin Ke committed
93
    }
Guolin Ke's avatar
Guolin Ke committed
94
    label_weights_[1] *= scale_pos_weight_;
Guolin Ke's avatar
Guolin Ke committed
95
96
  }

97
  void GetGradients(const double* score, score_t* gradients, score_t* hessians) const override {
98
99
100
    if (!need_train_) {
      return;
    }
Guolin Ke's avatar
Guolin Ke committed
101
102
103
104
    if (weights_ == nullptr) {
      #pragma omp parallel for schedule(static)
      for (data_size_t i = 0; i < num_data_; ++i) {
        // get label and label weights
Guolin Ke's avatar
Guolin Ke committed
105
        const int is_pos = is_pos_(label_[i]);
Guolin Ke's avatar
Guolin Ke committed
106
107
        const int label = label_val_[is_pos];
        const double label_weight = label_weights_[is_pos];
Guolin Ke's avatar
Guolin Ke committed
108
        // calculate gradients and hessians
109
        const double response = -label * sigmoid_ / (1.0f + std::exp(label * sigmoid_ * score[i]));
110
        const double abs_response = fabs(response);
111
112
        gradients[i] = static_cast<score_t>(response * label_weight);
        hessians[i] = static_cast<score_t>(abs_response * (sigmoid_ - abs_response) * label_weight);
Guolin Ke's avatar
Guolin Ke committed
113
114
115
116
117
      }
    } else {
      #pragma omp parallel for schedule(static)
      for (data_size_t i = 0; i < num_data_; ++i) {
        // get label and label weights
Guolin Ke's avatar
Guolin Ke committed
118
        const int is_pos = is_pos_(label_[i]);
Guolin Ke's avatar
Guolin Ke committed
119
120
        const int label = label_val_[is_pos];
        const double label_weight = label_weights_[is_pos];
Guolin Ke's avatar
Guolin Ke committed
121
        // calculate gradients and hessians
122
        const double response = -label * sigmoid_ / (1.0f + std::exp(label * sigmoid_ * score[i]));
123
        const double abs_response = fabs(response);
124
125
        gradients[i] = static_cast<score_t>(response * label_weight  * weights_[i]);
        hessians[i] = static_cast<score_t>(abs_response * (sigmoid_ - abs_response) * label_weight * weights_[i]);
Guolin Ke's avatar
Guolin Ke committed
126
127
128
      }
    }
  }
129

130
  // implement custom average to boost from (if enabled among options)
131
  double BoostFromScore(int) const override {
132
133
134
    double suml = 0.0f;
    double sumw = 0.0f;
    if (weights_ != nullptr) {
135
      #pragma omp parallel for schedule(static) reduction(+:suml, sumw)
136
      for (data_size_t i = 0; i < num_data_; ++i) {
137
        suml += is_pos_(label_[i]) * weights_[i];
138
139
140
141
142
143
        sumw += weights_[i];
      }
    } else {
      sumw = static_cast<double>(num_data_);
      #pragma omp parallel for schedule(static) reduction(+:suml)
      for (data_size_t i = 0; i < num_data_; ++i) {
144
        suml += is_pos_(label_[i]);
145
146
147
      }
    }
    double pavg = suml / sumw;
148
149
    pavg = std::min(pavg, 1.0 - kEpsilon);
    pavg = std::max<double>(pavg, kEpsilon);
150
151
152
153
    double initscore = std::log(pavg / (1.0f - pavg)) / sigmoid_;
    Log::Info("[%s:%s]: pavg=%f -> initscore=%f",  GetName(), __func__, pavg, initscore);
    return initscore;
  }
Guolin Ke's avatar
Guolin Ke committed
154

155
  bool ClassNeedTrain(int /*class_id*/) const override {
156
    return need_train_;
157
158
  }

Guolin Ke's avatar
Guolin Ke committed
159
160
  const char* GetName() const override {
    return "binary";
Guolin Ke's avatar
Guolin Ke committed
161
162
  }

Guolin Ke's avatar
Guolin Ke committed
163
164
  void ConvertOutput(const double* input, double* output) const override {
    output[0] = 1.0f / (1.0f + std::exp(-sigmoid_ * input[0]));
165
166
167
168
169
170
171
172
173
174
175
  }

  std::string ToString() const override {
    std::stringstream str_buf;
    str_buf << GetName() << " ";
    str_buf << "sigmoid:" << sigmoid_;
    return str_buf.str();
  }

  bool SkipEmptyClass() const override { return true; }

176
177
  bool NeedAccuratePrediction() const override { return false; }

Nikita Titov's avatar
Nikita Titov committed
178
 private:
Guolin Ke's avatar
Guolin Ke committed
179
180
181
  /*! \brief Number of data */
  data_size_t num_data_;
  /*! \brief Pointer of label */
182
  const label_t* label_;
Guolin Ke's avatar
Guolin Ke committed
183
184
185
  /*! \brief True if using unbalance training */
  bool is_unbalance_;
  /*! \brief Sigmoid parameter */
186
  double sigmoid_;
Guolin Ke's avatar
Guolin Ke committed
187
188
189
  /*! \brief Values for positive and negative labels */
  int label_val_[2];
  /*! \brief Weights for positive and negative labels */
190
  double label_weights_[2];
Guolin Ke's avatar
Guolin Ke committed
191
  /*! \brief Weights for data */
192
  const label_t* weights_;
193
  double scale_pos_weight_;
194
  std::function<bool(label_t)> is_pos_;
195
  bool need_train_;
Guolin Ke's avatar
Guolin Ke committed
196
197
198
};

}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
199
#endif   // LightGBM_OBJECTIVE_BINARY_OBJECTIVE_HPP_