Commit 1d7acf57 authored by Guolin Ke's avatar Guolin Ke
Browse files

change output model to double precision.

parent 487bd835
......@@ -13,6 +13,7 @@
#include <functional>
#include <memory>
#include <type_traits>
#include <iomanip>
namespace LightGBM {
......@@ -246,6 +247,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, char delimite
return std::string("");
}
std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1);
str_buf << arr[0];
for (size_t i = 1; i < arr.size(); ++i) {
str_buf << delimiter;
......@@ -260,6 +262,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, size_t n, cha
return std::string("");
}
std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1);
str_buf << arr[0];
for (size_t i = 1; i < std::min(n, arr.size()); ++i) {
str_buf << delimiter;
......@@ -308,13 +311,14 @@ inline static std::string Join(const std::vector<T>& strs, const char* delimiter
if (strs.empty()) {
return std::string("");
}
std::stringstream ss;
ss << strs[0];
std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1);
str_buf << strs[0];
for (size_t i = 1; i < strs.size(); ++i) {
ss << delimiter;
ss << strs[i];
str_buf << delimiter;
str_buf << strs[i];
}
return ss.str();
return str_buf.str();
}
template<typename T>
......@@ -324,13 +328,14 @@ inline static std::string Join(const std::vector<T>& strs, size_t start, size_t
}
start = std::min(start, static_cast<size_t>(strs.size()) - 1);
end = std::min(end, static_cast<size_t>(strs.size()));
std::stringstream ss;
ss << strs[start];
std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1);
str_buf << strs[start];
for (size_t i = start + 1; i < end; ++i) {
ss << delimiter;
ss << strs[i];
str_buf << delimiter;
str_buf << strs[i];
}
return ss.str();
return str_buf.str();
}
static inline int64_t Pow2RoundUp(int64_t x) {
......
......@@ -735,8 +735,8 @@ DllExport int LGBM_BoosterPredictForFile(BoosterHandle handle,
API_END();
}
int GetNumPredOneRow(const Booster* ref_booster, int predict_type, int64_t num_iteration) {
int num_preb_in_one_row = ref_booster->GetBoosting()->NumberOfClasses();
int64_t GetNumPredOneRow(const Booster* ref_booster, int predict_type, int64_t num_iteration) {
int64_t num_preb_in_one_row = ref_booster->GetBoosting()->NumberOfClasses();
if (predict_type == C_API_PREDICT_LEAF_INDEX) {
int64_t max_iteration = ref_booster->GetBoosting()->GetCurrentIteration();
if (num_iteration > 0) {
......@@ -776,7 +776,7 @@ DllExport int LGBM_BoosterPredictForCSR(BoosterHandle handle,
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
auto predictor = ref_booster->NewPredictor(static_cast<int>(num_iteration), predict_type);
auto get_row_fun = RowFunctionFromCSR(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
int num_preb_in_one_row = GetNumPredOneRow(ref_booster, predict_type, num_iteration);
int64_t num_preb_in_one_row = GetNumPredOneRow(ref_booster, predict_type, num_iteration);
int nrow = static_cast<int>(nindptr - 1);
#pragma omp parallel for schedule(guided)
for (int i = 0; i < nrow; ++i) {
......@@ -806,7 +806,7 @@ DllExport int LGBM_BoosterPredictForCSC(BoosterHandle handle,
API_BEGIN();
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
auto predictor = ref_booster->NewPredictor(static_cast<int>(num_iteration), predict_type);
int num_preb_in_one_row = GetNumPredOneRow(ref_booster, predict_type, num_iteration);
int64_t num_preb_in_one_row = GetNumPredOneRow(ref_booster, predict_type, num_iteration);
int ncol = static_cast<int>(ncol_ptr - 1);
Threading::For<int64_t>(0, num_row,
......@@ -849,7 +849,7 @@ DllExport int LGBM_BoosterPredictForMat(BoosterHandle handle,
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
auto predictor = ref_booster->NewPredictor(static_cast<int>(num_iteration), predict_type);
auto get_row_fun = RowPairFunctionFromDenseMatric(data, nrow, ncol, data_type, is_row_major);
int num_preb_in_one_row = GetNumPredOneRow(ref_booster, predict_type, num_iteration);
int64_t num_preb_in_one_row = GetNumPredOneRow(ref_booster, predict_type, num_iteration);
#pragma omp parallel for schedule(guided)
for (int i = 0; i < nrow; ++i) {
auto one_row = get_row_fun(i);
......
......@@ -12,6 +12,7 @@
#include <vector>
#include <string>
#include <memory>
#include <iomanip>
namespace LightGBM {
......@@ -121,72 +122,72 @@ void Tree::AddPredictionToScore(const Dataset* data, const data_size_t* used_dat
}
std::string Tree::ToString() {
std::stringstream ss;
ss << "num_leaves=" << num_leaves_ << std::endl;
ss << "split_feature="
std::stringstream str_buf;
str_buf << "num_leaves=" << num_leaves_ << std::endl;
str_buf << "split_feature="
<< Common::ArrayToString<int>(split_feature_real_, num_leaves_ - 1, ' ') << std::endl;
ss << "split_gain="
str_buf << "split_gain="
<< Common::ArrayToString<double>(split_gain_, num_leaves_ - 1, ' ') << std::endl;
ss << "threshold="
str_buf << "threshold="
<< Common::ArrayToString<double>(threshold_, num_leaves_ - 1, ' ') << std::endl;
ss << "decision_type="
str_buf << "decision_type="
<< Common::ArrayToString<int>(Common::ArrayCast<int8_t, int>(decision_type_), num_leaves_ - 1, ' ') << std::endl;
ss << "left_child="
str_buf << "left_child="
<< Common::ArrayToString<int>(left_child_, num_leaves_ - 1, ' ') << std::endl;
ss << "right_child="
str_buf << "right_child="
<< Common::ArrayToString<int>(right_child_, num_leaves_ - 1, ' ') << std::endl;
ss << "leaf_parent="
str_buf << "leaf_parent="
<< Common::ArrayToString<int>(leaf_parent_, num_leaves_, ' ') << std::endl;
ss << "leaf_value="
str_buf << "leaf_value="
<< Common::ArrayToString<double>(leaf_value_, num_leaves_, ' ') << std::endl;
ss << "leaf_count="
str_buf << "leaf_count="
<< Common::ArrayToString<data_size_t>(leaf_count_, num_leaves_, ' ') << std::endl;
ss << "internal_value="
str_buf << "internal_value="
<< Common::ArrayToString<double>(internal_value_, num_leaves_ - 1, ' ') << std::endl;
ss << "internal_count="
str_buf << "internal_count="
<< Common::ArrayToString<data_size_t>(internal_count_, num_leaves_ - 1, ' ') << std::endl;
ss << std::endl;
return ss.str();
str_buf << std::endl;
return str_buf.str();
}
std::string Tree::ToJSON() {
std::stringstream ss;
std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1);
str_buf << "\"num_leaves\":" << num_leaves_ << "," << std::endl;
ss << "\"num_leaves\":" << num_leaves_ << "," << std::endl;
str_buf << "\"tree_structure\":" << NodeToJSON(0) << std::endl;
ss << "\"tree_structure\":" << NodeToJSON(0) << std::endl;
return ss.str();
return str_buf.str();
}
std::string Tree::NodeToJSON(int index) {
std::stringstream ss;
std::stringstream str_buf;
str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 1);
if (index >= 0) {
// non-leaf
ss << "{" << std::endl;
ss << "\"split_index\":" << index << "," << std::endl;
ss << "\"split_feature\":" << split_feature_real_[index] << "," << std::endl;
ss << "\"split_gain\":" << split_gain_[index] << "," << std::endl;
ss << "\"threshold\":" << threshold_[index] << "," << std::endl;
ss << "\"decision_type\":\"" << Tree::GetDecisionTypeName(decision_type_[index]) << "\"," << std::endl;
ss << "\"internal_value\":" << internal_value_[index] << "," << std::endl;
ss << "\"internal_count\":" << internal_count_[index] << "," << std::endl;
ss << "\"left_child\":" << NodeToJSON(left_child_[index]) << "," << std::endl;
ss << "\"right_child\":" << NodeToJSON(right_child_[index]) << std::endl;
ss << "}";
str_buf << "{" << std::endl;
str_buf << "\"split_index\":" << index << "," << std::endl;
str_buf << "\"split_feature\":" << split_feature_real_[index] << "," << std::endl;
str_buf << "\"split_gain\":" << split_gain_[index] << "," << std::endl;
str_buf << "\"threshold\":" << threshold_[index] << "," << std::endl;
str_buf << "\"decision_type\":\"" << Tree::GetDecisionTypeName(decision_type_[index]) << "\"," << std::endl;
str_buf << "\"internal_value\":" << internal_value_[index] << "," << std::endl;
str_buf << "\"internal_count\":" << internal_count_[index] << "," << std::endl;
str_buf << "\"left_child\":" << NodeToJSON(left_child_[index]) << "," << std::endl;
str_buf << "\"right_child\":" << NodeToJSON(right_child_[index]) << std::endl;
str_buf << "}";
} else {
// leaf
index = ~index;
ss << "{" << std::endl;
ss << "\"leaf_index\":" << index << "," << std::endl;
ss << "\"leaf_parent\":" << leaf_parent_[index] << "," << std::endl;
ss << "\"leaf_value\":" << leaf_value_[index] << "," << std::endl;
ss << "\"leaf_count\":" << leaf_count_[index] << std::endl;
ss << "}";
str_buf << "{" << std::endl;
str_buf << "\"leaf_index\":" << index << "," << std::endl;
str_buf << "\"leaf_parent\":" << leaf_parent_[index] << "," << std::endl;
str_buf << "\"leaf_value\":" << leaf_value_[index] << "," << std::endl;
str_buf << "\"leaf_count\":" << leaf_count_[index] << std::endl;
str_buf << "}";
}
return ss.str();
return str_buf.str();
}
Tree::Tree(const std::string& str) {
......
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