Skip to content

Commit

Permalink
removed parallelization of group-wise metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreFCruz committed Jul 4, 2022
1 parent 34f1103 commit cbf19a1
Showing 1 changed file with 14 additions and 66 deletions.
80 changes: 14 additions & 66 deletions include/LightGBM/objective_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeFPR(const double *score, double probabilities_threshold, std::unordered_map<constraint_group_t, double> &group_fpr) const
{
std::mutex fp_mutex, ln_mutex;
std::unordered_map<int, int> false_positives;
std::unordered_map<int, int> label_negatives;

Expand All @@ -634,14 +633,10 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction

if (label_[i] == 0)
{
{
std::lock_guard<std::mutex> lock(ln_mutex);
label_negatives[group] += 1;
}
label_negatives[group] += 1;

const double z = 1.0f / (1.0f + std::exp(-score[i]));
if (z >= probabilities_threshold) {
std::lock_guard<std::mutex> lock(fp_mutex);
false_positives[group] += 1;
}
}
Expand Down Expand Up @@ -693,7 +688,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeHingeFPR(const double *score, std::unordered_map<constraint_group_t, double> &group_fpr) const
{
std::mutex fp_mutex, ln_mutex;
std::unordered_map<constraint_group_t, double> false_positives; // map of group index to the respective hinge-proxy FPs
std::unordered_map<constraint_group_t, int> label_negatives; // map of group index to the respective number of LNs

Expand All @@ -705,17 +699,11 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
// HingeFPR uses only label negatives
if (label_[i] == 0)
{
{
std::lock_guard<std::mutex> lock(ln_mutex);
label_negatives[group] += 1;
}
label_negatives[group] += 1;

// proxy_margin_ is the line intercept value
const double hinge_score = proxy_margin_ + score[i];
{
std::lock_guard<std::mutex> lock(fp_mutex);
false_positives[group] += std::max(0.0, hinge_score);
}
false_positives[group] += std::max(0.0, hinge_score);
}
}

Expand All @@ -741,7 +729,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeQuadraticLossFPR(const double *score, std::unordered_map<constraint_group_t, double> &group_fpr) const
{
std::mutex fp_mutex, ln_mutex;
std::unordered_map<constraint_group_t, double> false_positives; // map of group index to the respective proxy FPs
std::unordered_map<constraint_group_t, int> label_negatives; // map of group index to the respective number of LNs

Expand All @@ -753,18 +740,12 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
// FPR uses only label NEGATIVES
if (label_[i] == 0 and score[i] > -proxy_margin_)
{ // Conditions for non-zero proxy-FPR value
{
std::lock_guard<std::mutex> lock(ln_mutex);
label_negatives[group] += 1;
}
label_negatives[group] += 1;

// proxy_margin_ corresponds to the symmetric of the function's zero point; f(-proxy_margin_)=0
const double quadratic_score = (1. / 2.) * std::pow(score[i] + proxy_margin_, 2);
assert(quadratic_score >= 0.);
{
std::lock_guard<std::mutex> lock(fp_mutex);
false_positives[group] += quadratic_score;
}
false_positives[group] += quadratic_score;
}
}

Expand All @@ -790,7 +771,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeXEntropyLossFPR(const double *score, std::unordered_map<constraint_group_t, double> &group_fpr) const
{
std::mutex fp_mutex, ln_mutex;
std::unordered_map<constraint_group_t, double> false_positives; // map of group index to the respective proxy FPs
std::unordered_map<constraint_group_t, int> label_negatives; // map of group index to the respective number of LNs
const double xent_horizontal_shift = log(exp(proxy_margin_) - 1);
Expand All @@ -803,18 +783,12 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
// FPR uses only label NEGATIVES
if (label_[i] == 0)
{
{
std::lock_guard<std::mutex> lock(ln_mutex);
label_negatives[group] += 1;
}
label_negatives[group] += 1;

// proxy_margin_ corresponds to the vertical margin at x=0; l(0) = proxy_margin_
const double xent_score = log(1 + exp(score[i] + xent_horizontal_shift));
assert(xent_score >= 0.);
{
std::lock_guard<std::mutex> lock(fp_mutex);
false_positives[group] += xent_score;
}
false_positives[group] += xent_score;
}
}

Expand All @@ -838,7 +812,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeFNR(const double *score, double probabilities_threshold, std::unordered_map<constraint_group_t, double> &group_fnr) const
{
std::mutex fn_mutex, lp_mutex;
std::unordered_map<constraint_group_t, int> false_negatives;
std::unordered_map<constraint_group_t, int> label_positives;

Expand All @@ -849,14 +822,10 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction

if (label_[i] == 1)
{
{
std::lock_guard<std::mutex> lock(lp_mutex);
label_positives[group] += 1;
}
label_positives[group] += 1;

const double z = 1.0f / (1.0f + std::exp(-score[i]));
if (z < probabilities_threshold) {
std::lock_guard<std::mutex> lock(fn_mutex);
false_negatives[group] += 1;
}
}
Expand Down Expand Up @@ -907,7 +876,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeHingeLossFNR(const double *score, std::unordered_map<constraint_group_t, double> &group_fnr) const
{
std::mutex fn_mutex, lp_mutex;
std::unordered_map<constraint_group_t, double> false_negatives; // map of group index to the respective hinge-proxy FNs
std::unordered_map<constraint_group_t, int> label_positives;

Expand All @@ -918,16 +886,10 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction

if (label_[i] == 1)
{
{
std::lock_guard<std::mutex> lock(lp_mutex);
label_positives[group] += 1;
}
label_positives[group] += 1;

const double hinge_score = proxy_margin_ - score[i];
{
std::lock_guard<std::mutex> lock(fn_mutex);
false_negatives[group] += std::max(0.0, hinge_score);
}
false_negatives[group] += std::max(0.0, hinge_score);
}
}

Expand All @@ -952,7 +914,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeQuadraticLossFNR(const double *score, std::unordered_map<constraint_group_t, double> &group_fnr) const
{
std::mutex fn_mutex, lp_mutex;
std::unordered_map<constraint_group_t, double> false_negatives; // map of group index to the respective proxy FPs
std::unordered_map<constraint_group_t, int> label_positives; // map of group index to the respective number of LNs

Expand All @@ -964,18 +925,12 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
// FNR uses only label POSITIVES
if (label_[i] == 1 and score[i] < proxy_margin_)
{ // Conditions for non-zero proxy-FNR value
{
std::lock_guard<std::mutex> lock(lp_mutex);
label_positives[group] += 1;
}
label_positives[group] += 1;

// proxy_margin_ corresponds to the function's zero point; f(proxy_margin_)=0
const double quadratic_score = (1. / 2.) * std::pow(score[i] - proxy_margin_, 2);
assert(quadratic_score >= 0.);
{
std::lock_guard<std::mutex> lock(fn_mutex);
false_negatives[group] += quadratic_score;
}
false_negatives[group] += quadratic_score;
}
}

Expand All @@ -1001,7 +956,6 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
*/
void ComputeXEntropyLossFNR(const double *score, std::unordered_map<constraint_group_t, double> &group_fnr) const
{
std::mutex fn_mutex, lp_mutex;
std::unordered_map<constraint_group_t, double> false_negatives; // map of group index to the respective proxy FPs
std::unordered_map<constraint_group_t, int> label_positives; // map of group index to the respective number of LNs
const double xent_horizontal_shift = log(exp(proxy_margin_) - 1);
Expand All @@ -1014,18 +968,12 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
// FNR uses only label POSITIVES
if (label_[i] == 1)
{
{
std::lock_guard<std::mutex> lock(lp_mutex);
label_positives[group] += 1;
}
label_positives[group] += 1;

// proxy_margin_ corresponds to the vertical margin at x=0; l(0) = proxy_margin_
const double xent_score = log(1 + exp(xent_horizontal_shift - score[i]));
assert(xent_score >= 0.);
{
std::lock_guard<std::mutex> lock(fn_mutex);
false_negatives[group] += xent_score;
}
false_negatives[group] += xent_score;
}
}

Expand Down

0 comments on commit cbf19a1

Please sign in to comment.