Skip to content

Commit

Permalink
fix deterministic, part2 (#3578)
Browse files Browse the repository at this point in the history
* fix deterministic, part2

* Apply suggestions from code review
  • Loading branch information
guolinke authored Nov 23, 2020
1 parent d6f20e3 commit 5285974
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
12 changes: 8 additions & 4 deletions src/objective/binary_objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace LightGBM {
*/
class BinaryLogloss: public ObjectiveFunction {
public:
explicit BinaryLogloss(const Config& config, std::function<bool(label_t)> is_pos = nullptr) {
explicit BinaryLogloss(const Config& config,
std::function<bool(label_t)> is_pos = nullptr)
: deterministic_(config.deterministic) {
sigmoid_ = static_cast<double>(config.sigmoid);
if (sigmoid_ <= 0.0) {
Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_);
Expand All @@ -36,7 +38,8 @@ class BinaryLogloss: public ObjectiveFunction {
}
}

explicit BinaryLogloss(const std::vector<std::string>& strs) {
explicit BinaryLogloss(const std::vector<std::string>& strs)
: deterministic_(false) {
sigmoid_ = -1;
for (auto str : strs) {
auto tokens = Common::Split(str.c_str(), ':');
Expand Down Expand Up @@ -137,14 +140,14 @@ class BinaryLogloss: public ObjectiveFunction {
double suml = 0.0f;
double sumw = 0.0f;
if (weights_ != nullptr) {
#pragma omp parallel for schedule(static) reduction(+:suml, sumw)
#pragma omp parallel for schedule(static) reduction(+:suml, sumw) if (!deterministic_)
for (data_size_t i = 0; i < num_data_; ++i) {
suml += is_pos_(label_[i]) * weights_[i];
sumw += weights_[i];
}
} else {
sumw = static_cast<double>(num_data_);
#pragma omp parallel for schedule(static) reduction(+:suml)
#pragma omp parallel for schedule(static) reduction(+:suml) if (!deterministic_)
for (data_size_t i = 0; i < num_data_; ++i) {
suml += is_pos_(label_[i]);
}
Expand Down Expand Up @@ -202,6 +205,7 @@ class BinaryLogloss: public ObjectiveFunction {
double scale_pos_weight_;
std::function<bool(label_t)> is_pos_;
bool need_train_;
const bool deterministic_;
};

} // namespace LightGBM
Expand Down
11 changes: 7 additions & 4 deletions src/objective/regression_objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ namespace LightGBM {
*/
class RegressionL2loss: public ObjectiveFunction {
public:
explicit RegressionL2loss(const Config& config) {
explicit RegressionL2loss(const Config& config)
: deterministic_(config.deterministic) {
sqrt_ = config.reg_sqrt;
}

explicit RegressionL2loss(const std::vector<std::string>& strs) {
explicit RegressionL2loss(const std::vector<std::string>& strs)
: deterministic_(false) {
sqrt_ = false;
for (auto str : strs) {
if (str == std::string("sqrt")) {
Expand Down Expand Up @@ -172,14 +174,14 @@ class RegressionL2loss: public ObjectiveFunction {
double suml = 0.0f;
double sumw = 0.0f;
if (weights_ != nullptr) {
#pragma omp parallel for schedule(static) reduction(+:suml, sumw)
#pragma omp parallel for schedule(static) reduction(+:suml, sumw) if (!deterministic_)
for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i] * weights_[i];
sumw += weights_[i];
}
} else {
sumw = static_cast<double>(num_data_);
#pragma omp parallel for schedule(static) reduction(+:suml)
#pragma omp parallel for schedule(static) reduction(+:suml) if (!deterministic_)
for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i];
}
Expand All @@ -196,6 +198,7 @@ class RegressionL2loss: public ObjectiveFunction {
/*! \brief Pointer of weights */
const label_t* weights_;
std::vector<label_t> trans_label_;
const bool deterministic_;
};

/*!
Expand Down
28 changes: 18 additions & 10 deletions src/objective/xentropy_objective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ namespace LightGBM {
*/
class CrossEntropy: public ObjectiveFunction {
public:
explicit CrossEntropy(const Config&) {
}
explicit CrossEntropy(const Config& config)
: deterministic_(config.deterministic) {}

explicit CrossEntropy(const std::vector<std::string>&) {
explicit CrossEntropy(const std::vector<std::string>&)
: deterministic_(false) {
}

~CrossEntropy() {}
Expand Down Expand Up @@ -113,14 +114,16 @@ class CrossEntropy: public ObjectiveFunction {
double suml = 0.0f;
double sumw = 0.0f;
if (weights_ != nullptr) {
#pragma omp parallel for schedule(static) reduction(+:suml, sumw)
#pragma omp parallel for schedule(static) reduction(+:suml, sumw) if (!deterministic_)

for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i] * weights_[i];
sumw += weights_[i];
}
} else {
sumw = static_cast<double>(num_data_);
#pragma omp parallel for schedule(static) reduction(+:suml)
#pragma omp parallel for schedule(static) reduction(+:suml) if (!deterministic_)

for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i];
}
Expand All @@ -140,19 +143,21 @@ class CrossEntropy: public ObjectiveFunction {
const label_t* label_;
/*! \brief Weights for data */
const label_t* weights_;
const bool deterministic_;
};

/*!
* \brief Objective function for alternative parameterization of cross-entropy (see top of file for explanation)
*/
class CrossEntropyLambda: public ObjectiveFunction {
public:
explicit CrossEntropyLambda(const Config&) {
explicit CrossEntropyLambda(const Config& config)
: deterministic_(config.deterministic) {
min_weight_ = max_weight_ = 0.0f;
}

explicit CrossEntropyLambda(const std::vector<std::string>&) {
}
explicit CrossEntropyLambda(const std::vector<std::string>&)
: deterministic_(false) {}

~CrossEntropyLambda() {}

Expand Down Expand Up @@ -239,14 +244,16 @@ class CrossEntropyLambda: public ObjectiveFunction {
double suml = 0.0f;
double sumw = 0.0f;
if (weights_ != nullptr) {
#pragma omp parallel for schedule(static) reduction(+:suml, sumw)
#pragma omp parallel for schedule(static) reduction(+:suml, sumw) if (!deterministic_)

for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i] * weights_[i];
sumw += weights_[i];
}
} else {
sumw = static_cast<double>(num_data_);
#pragma omp parallel for schedule(static) reduction(+:suml)
#pragma omp parallel for schedule(static) reduction(+:suml) if (!deterministic_)

for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i];
}
Expand All @@ -268,6 +275,7 @@ class CrossEntropyLambda: public ObjectiveFunction {
label_t min_weight_;
/*! \brief Maximum weight found during init */
label_t max_weight_;
const bool deterministic_;
};

} // end namespace LightGBM
Expand Down

0 comments on commit 5285974

Please sign in to comment.