binary_objective.hpp 3.98 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
15
16
*/
class BinaryLogloss: public ObjectiveFunction {
public:
  explicit BinaryLogloss(const ObjectiveConfig& config) {
    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
26
27
28
29
30
  }
  ~BinaryLogloss() {}
  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
Guolin Ke's avatar
Guolin Ke committed
31
#pragma omp parallel for schedule(static) reduction(+:cnt_positive, cnt_negative)
Guolin Ke's avatar
Guolin Ke committed
32
    for (data_size_t i = 0; i < num_data_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
33
      if (label_[i] > 0) {
Guolin Ke's avatar
Guolin Ke committed
34
35
36
37
38
        ++cnt_positive;
      } else {
        ++cnt_negative;
      }
    }
ProtD's avatar
ProtD committed
39
    Log::Info("Number of positive: %d, number of negative: %d", cnt_positive, cnt_negative);
Guolin Ke's avatar
Guolin Ke committed
40
41
42
43
44
45
46
    // 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
47
    if (is_unbalance_ && cnt_positive > 0 && cnt_negative > 0) {
48
49
      if (cnt_positive > cnt_negative) {
        label_weights_[1] = 1.0f;
50
        label_weights_[0] = static_cast<double>(cnt_positive) / cnt_negative;
51
      } else {
52
        label_weights_[1] = static_cast<double>(cnt_negative) / cnt_positive;
53
54
        label_weights_[0] = 1.0f;
      }
Guolin Ke's avatar
Guolin Ke committed
55
    }
Guolin Ke's avatar
Guolin Ke committed
56
    label_weights_[1] *= scale_pos_weight_;
Guolin Ke's avatar
Guolin Ke committed
57
58
  }

59
  void GetGradients(const double* score, score_t* gradients, score_t* hessians) const override {
Guolin Ke's avatar
Guolin Ke committed
60
61
62
63
    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
64
65
66
        const int is_pos = label_[i] > 0;
        const int label = label_val_[is_pos];
        const double label_weight = label_weights_[is_pos];
Guolin Ke's avatar
Guolin Ke committed
67
        // calculate gradients and hessians
68
        const double response = -label * sigmoid_ / (1.0f + std::exp(label * sigmoid_ * score[i]));
69
70
        const double abs_response = fabs(response);
        gradients[i] = static_cast<score_t>(response * label_weight);
71
        hessians[i] = static_cast<score_t>(abs_response * (sigmoid_ - abs_response) * label_weight);
Guolin Ke's avatar
Guolin Ke committed
72
73
74
75
76
      }
    } 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
77
78
79
        const int is_pos = label_[i] > 0;
        const int label = label_val_[is_pos];
        const double label_weight = label_weights_[is_pos];
Guolin Ke's avatar
Guolin Ke committed
80
        // calculate gradients and hessians
81
        const double response = -label * sigmoid_ / (1.0f + std::exp(label * sigmoid_ * score[i]));
82
83
        const double abs_response = fabs(response);
        gradients[i] = static_cast<score_t>(response * label_weight  * weights_[i]);
84
        hessians[i] = static_cast<score_t>(abs_response * (sigmoid_ - abs_response) * label_weight * weights_[i]);
Guolin Ke's avatar
Guolin Ke committed
85
86
87
88
      }
    }
  }

Guolin Ke's avatar
Guolin Ke committed
89
90
  const char* GetName() const override {
    return "binary";
Guolin Ke's avatar
Guolin Ke committed
91
92
93
94
95
96
97
98
99
100
  }

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 */
101
  double sigmoid_;
Guolin Ke's avatar
Guolin Ke committed
102
103
104
  /*! \brief Values for positive and negative labels */
  int label_val_[2];
  /*! \brief Weights for positive and negative labels */
105
  double label_weights_[2];
Guolin Ke's avatar
Guolin Ke committed
106
107
  /*! \brief Weights for data */
  const float* weights_;
108
  double scale_pos_weight_;
Guolin Ke's avatar
Guolin Ke committed
109
110
111
};

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