! * \brief Objective function for binary classification */
| 19 | * \brief Objective function for binary classification |
| 20 | */ |
| 21 | class BinaryLogloss: public ObjectiveFunction { |
| 22 | public: |
| 23 | explicit BinaryLogloss(const Config& config, std::function<bool(label_t)> is_pos = nullptr) { |
| 24 | sigmoid_ = static_cast<double>(config.sigmoid); |
| 25 | if (sigmoid_ <= 0.0) { |
| 26 | Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_); |
| 27 | } |
| 28 | is_unbalance_ = config.is_unbalance; |
| 29 | scale_pos_weight_ = static_cast<double>(config.scale_pos_weight); |
| 30 | if (is_unbalance_ && std::fabs(scale_pos_weight_ - 1.0f) > 1e-6) { |
| 31 | Log::Fatal("Cannot set is_unbalance and scale_pos_weight at the same time"); |
| 32 | } |
| 33 | is_pos_ = is_pos; |
| 34 | if (is_pos_ == nullptr) { |
| 35 | is_pos_ = [](label_t label) {return label > 0; }; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | explicit BinaryLogloss(const std::vector<std::string>& strs) { |
| 40 | sigmoid_ = -1; |
| 41 | for (auto str : strs) { |
| 42 | auto tokens = Common::Split(str.c_str(), ':'); |
| 43 | if (tokens.size() == 2) { |
| 44 | if (tokens[0] == std::string("sigmoid")) { |
| 45 | Common::Atof(tokens[1].c_str(), &sigmoid_); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | if (sigmoid_ <= 0.0) { |
| 50 | Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_); |
| 51 | } |
| 52 | } |
| 53 | |
| 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); |
nothing calls this directly
no outgoing calls
no test coverage detected