binary_objective.hpp 5.21 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
#ifndef LIGHTGBM_OBJECTIVE_BINARY_OBJECTIVE_HPP_
#define LIGHTGBM_OBJECTIVE_BINARY_OBJECTIVE_HPP_

#include <LightGBM/objective_function.h>

#include <cstring>
#include <cmath>

namespace LightGBM {
/*!
11
* \brief Objective function for binary classification
Guolin Ke's avatar
Guolin Ke committed
12
13
14
*/
class BinaryLogloss: public ObjectiveFunction {
public:
Guolin Ke's avatar
Guolin Ke committed
15
  explicit BinaryLogloss(const ObjectiveConfig& config, std::function<bool(float)> is_pos = nullptr) {
Guolin Ke's avatar
Guolin Ke committed
16
    is_unbalance_ = config.is_unbalance;
17
    sigmoid_ = static_cast<double>(config.sigmoid);
Guolin Ke's avatar
Guolin Ke committed
18
    if (sigmoid_ <= 0.0) {
19
      Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_);
Guolin Ke's avatar
Guolin Ke committed
20
    }
21
    scale_pos_weight_ = static_cast<double>(config.scale_pos_weight);
Guolin Ke's avatar
Guolin Ke committed
22
23
24
25
    is_pos_ = is_pos;
    if (is_pos_ == nullptr) {
      is_pos_ = [](float label) {return label > 0; };
    }
Guolin Ke's avatar
Guolin Ke committed
26
  }
Guolin Ke's avatar
Guolin Ke committed
27

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  explicit BinaryLogloss(const std::vector<std::string>& strs) {
    sigmoid_ = -1;
    for (auto str : strs) {
      auto tokens = Common::Split(str.c_str(), ":");
      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
43
  ~BinaryLogloss() {}
Guolin Ke's avatar
Guolin Ke committed
44

Guolin Ke's avatar
Guolin Ke committed
45
46
47
48
49
50
51
  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;
    // count for positive and negative samples
52
    #pragma omp parallel for schedule(static) reduction(+:cnt_positive, cnt_negative)
Guolin Ke's avatar
Guolin Ke committed
53
    for (data_size_t i = 0; i < num_data_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
54
      if (is_pos_(label_[i])) {
Guolin Ke's avatar
Guolin Ke committed
55
56
57
58
59
        ++cnt_positive;
      } else {
        ++cnt_negative;
      }
    }
60
61
62
63
64
    if (cnt_negative == 0 || cnt_positive == 0) {
      Log::Warning("Only contain one class.");
      // not need to boost.
      num_data_ = 0;
    }
ProtD's avatar
ProtD committed
65
    Log::Info("Number of positive: %d, number of negative: %d", cnt_positive, cnt_negative);
Guolin Ke's avatar
Guolin Ke committed
66
67
68
69
70
71
72
    // 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
73
    if (is_unbalance_ && cnt_positive > 0 && cnt_negative > 0) {
74
75
      if (cnt_positive > cnt_negative) {
        label_weights_[1] = 1.0f;
76
        label_weights_[0] = static_cast<double>(cnt_positive) / cnt_negative;
77
      } else {
78
        label_weights_[1] = static_cast<double>(cnt_negative) / cnt_positive;
79
80
        label_weights_[0] = 1.0f;
      }
Guolin Ke's avatar
Guolin Ke committed
81
    }
Guolin Ke's avatar
Guolin Ke committed
82
    label_weights_[1] *= scale_pos_weight_;
Guolin Ke's avatar
Guolin Ke committed
83
84
  }

85
  void GetGradients(const double* score, score_t* gradients, score_t* hessians) const override {
Guolin Ke's avatar
Guolin Ke committed
86
87
88
89
    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
90
        const int is_pos = is_pos_(label_[i]);
Guolin Ke's avatar
Guolin Ke committed
91
92
        const int label = label_val_[is_pos];
        const double label_weight = label_weights_[is_pos];
Guolin Ke's avatar
Guolin Ke committed
93
        // calculate gradients and hessians
94
        const double response = -label * sigmoid_ / (1.0f + std::exp(label * sigmoid_ * score[i]));
95
        const double abs_response = fabs(response);
96
97
        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
98
99
100
101
102
      }
    } 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
103
        const int is_pos = is_pos_(label_[i]);
Guolin Ke's avatar
Guolin Ke committed
104
105
        const int label = label_val_[is_pos];
        const double label_weight = label_weights_[is_pos];
Guolin Ke's avatar
Guolin Ke committed
106
        // calculate gradients and hessians
107
        const double response = -label * sigmoid_ / (1.0f + std::exp(label * sigmoid_ * score[i]));
108
        const double abs_response = fabs(response);
109
110
        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
111
112
113
114
      }
    }
  }

Guolin Ke's avatar
Guolin Ke committed
115
116
  const char* GetName() const override {
    return "binary";
Guolin Ke's avatar
Guolin Ke committed
117
118
  }

Guolin Ke's avatar
Guolin Ke committed
119
120
  void ConvertOutput(const double* input, double* output) const override {
    output[0] = 1.0f / (1.0f + std::exp(-sigmoid_ * input[0]));
121
122
123
124
125
126
127
128
129
130
131
  }

  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; }

132
133
  bool NeedAccuratePrediction() const override { return false; }

Guolin Ke's avatar
Guolin Ke committed
134
135
136
137
138
139
140
141
private:
  /*! \brief Number of data */
  data_size_t num_data_;
  /*! \brief Pointer of label */
  const float* label_;
  /*! \brief True if using unbalance training */
  bool is_unbalance_;
  /*! \brief Sigmoid parameter */
142
  double sigmoid_;
Guolin Ke's avatar
Guolin Ke committed
143
144
145
  /*! \brief Values for positive and negative labels */
  int label_val_[2];
  /*! \brief Weights for positive and negative labels */
146
  double label_weights_[2];
Guolin Ke's avatar
Guolin Ke committed
147
148
  /*! \brief Weights for data */
  const float* weights_;
149
  double scale_pos_weight_;
Guolin Ke's avatar
Guolin Ke committed
150
  std::function<bool(float)> is_pos_;
Guolin Ke's avatar
Guolin Ke committed
151
152
153
};

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