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