Unverified Commit 52859744 authored by Guolin Ke's avatar Guolin Ke Committed by GitHub
Browse files

fix deterministic, part2 (#3578)

* fix deterministic, part2

* Apply suggestions from code review
parent d6f20e37
...@@ -20,7 +20,9 @@ namespace LightGBM { ...@@ -20,7 +20,9 @@ namespace LightGBM {
*/ */
class BinaryLogloss: public ObjectiveFunction { class BinaryLogloss: public ObjectiveFunction {
public: 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); sigmoid_ = static_cast<double>(config.sigmoid);
if (sigmoid_ <= 0.0) { if (sigmoid_ <= 0.0) {
Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_); Log::Fatal("Sigmoid parameter %f should be greater than zero", sigmoid_);
...@@ -36,7 +38,8 @@ class BinaryLogloss: public ObjectiveFunction { ...@@ -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; sigmoid_ = -1;
for (auto str : strs) { for (auto str : strs) {
auto tokens = Common::Split(str.c_str(), ':'); auto tokens = Common::Split(str.c_str(), ':');
...@@ -137,14 +140,14 @@ class BinaryLogloss: public ObjectiveFunction { ...@@ -137,14 +140,14 @@ class BinaryLogloss: public ObjectiveFunction {
double suml = 0.0f; double suml = 0.0f;
double sumw = 0.0f; double sumw = 0.0f;
if (weights_ != nullptr) { 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += is_pos_(label_[i]) * weights_[i]; suml += is_pos_(label_[i]) * weights_[i];
sumw += weights_[i]; sumw += weights_[i];
} }
} else { } else {
sumw = static_cast<double>(num_data_); 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += is_pos_(label_[i]); suml += is_pos_(label_[i]);
} }
...@@ -202,6 +205,7 @@ class BinaryLogloss: public ObjectiveFunction { ...@@ -202,6 +205,7 @@ class BinaryLogloss: public ObjectiveFunction {
double scale_pos_weight_; double scale_pos_weight_;
std::function<bool(label_t)> is_pos_; std::function<bool(label_t)> is_pos_;
bool need_train_; bool need_train_;
const bool deterministic_;
}; };
} // namespace LightGBM } // namespace LightGBM
......
...@@ -92,11 +92,13 @@ namespace LightGBM { ...@@ -92,11 +92,13 @@ namespace LightGBM {
*/ */
class RegressionL2loss: public ObjectiveFunction { class RegressionL2loss: public ObjectiveFunction {
public: public:
explicit RegressionL2loss(const Config& config) { explicit RegressionL2loss(const Config& config)
: deterministic_(config.deterministic) {
sqrt_ = config.reg_sqrt; sqrt_ = config.reg_sqrt;
} }
explicit RegressionL2loss(const std::vector<std::string>& strs) { explicit RegressionL2loss(const std::vector<std::string>& strs)
: deterministic_(false) {
sqrt_ = false; sqrt_ = false;
for (auto str : strs) { for (auto str : strs) {
if (str == std::string("sqrt")) { if (str == std::string("sqrt")) {
...@@ -172,14 +174,14 @@ class RegressionL2loss: public ObjectiveFunction { ...@@ -172,14 +174,14 @@ class RegressionL2loss: public ObjectiveFunction {
double suml = 0.0f; double suml = 0.0f;
double sumw = 0.0f; double sumw = 0.0f;
if (weights_ != nullptr) { 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i] * weights_[i]; suml += label_[i] * weights_[i];
sumw += weights_[i]; sumw += weights_[i];
} }
} else { } else {
sumw = static_cast<double>(num_data_); 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i]; suml += label_[i];
} }
...@@ -196,6 +198,7 @@ class RegressionL2loss: public ObjectiveFunction { ...@@ -196,6 +198,7 @@ class RegressionL2loss: public ObjectiveFunction {
/*! \brief Pointer of weights */ /*! \brief Pointer of weights */
const label_t* weights_; const label_t* weights_;
std::vector<label_t> trans_label_; std::vector<label_t> trans_label_;
const bool deterministic_;
}; };
/*! /*!
......
...@@ -43,10 +43,11 @@ namespace LightGBM { ...@@ -43,10 +43,11 @@ namespace LightGBM {
*/ */
class CrossEntropy: public ObjectiveFunction { class CrossEntropy: public ObjectiveFunction {
public: 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() {} ~CrossEntropy() {}
...@@ -113,14 +114,16 @@ class CrossEntropy: public ObjectiveFunction { ...@@ -113,14 +114,16 @@ class CrossEntropy: public ObjectiveFunction {
double suml = 0.0f; double suml = 0.0f;
double sumw = 0.0f; double sumw = 0.0f;
if (weights_ != nullptr) { 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i] * weights_[i]; suml += label_[i] * weights_[i];
sumw += weights_[i]; sumw += weights_[i];
} }
} else { } else {
sumw = static_cast<double>(num_data_); 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i]; suml += label_[i];
} }
...@@ -140,6 +143,7 @@ class CrossEntropy: public ObjectiveFunction { ...@@ -140,6 +143,7 @@ class CrossEntropy: public ObjectiveFunction {
const label_t* label_; const label_t* label_;
/*! \brief Weights for data */ /*! \brief Weights for data */
const label_t* weights_; const label_t* weights_;
const bool deterministic_;
}; };
/*! /*!
...@@ -147,12 +151,13 @@ class CrossEntropy: public ObjectiveFunction { ...@@ -147,12 +151,13 @@ class CrossEntropy: public ObjectiveFunction {
*/ */
class CrossEntropyLambda: public ObjectiveFunction { class CrossEntropyLambda: public ObjectiveFunction {
public: public:
explicit CrossEntropyLambda(const Config&) { explicit CrossEntropyLambda(const Config& config)
: deterministic_(config.deterministic) {
min_weight_ = max_weight_ = 0.0f; min_weight_ = max_weight_ = 0.0f;
} }
explicit CrossEntropyLambda(const std::vector<std::string>&) { explicit CrossEntropyLambda(const std::vector<std::string>&)
} : deterministic_(false) {}
~CrossEntropyLambda() {} ~CrossEntropyLambda() {}
...@@ -239,14 +244,16 @@ class CrossEntropyLambda: public ObjectiveFunction { ...@@ -239,14 +244,16 @@ class CrossEntropyLambda: public ObjectiveFunction {
double suml = 0.0f; double suml = 0.0f;
double sumw = 0.0f; double sumw = 0.0f;
if (weights_ != nullptr) { 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i] * weights_[i]; suml += label_[i] * weights_[i];
sumw += weights_[i]; sumw += weights_[i];
} }
} else { } else {
sumw = static_cast<double>(num_data_); 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) { for (data_size_t i = 0; i < num_data_; ++i) {
suml += label_[i]; suml += label_[i];
} }
...@@ -268,6 +275,7 @@ class CrossEntropyLambda: public ObjectiveFunction { ...@@ -268,6 +275,7 @@ class CrossEntropyLambda: public ObjectiveFunction {
label_t min_weight_; label_t min_weight_;
/*! \brief Maximum weight found during init */ /*! \brief Maximum weight found during init */
label_t max_weight_; label_t max_weight_;
const bool deterministic_;
}; };
} // end namespace LightGBM } // end namespace LightGBM
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment