Commit 3d65d065 authored by Guolin Ke's avatar Guolin Ke
Browse files

fix some formats

parent ba5c7459
...@@ -121,8 +121,8 @@ public: ...@@ -121,8 +121,8 @@ public:
virtual void PredictRaw(const double* features, double* output, virtual void PredictRaw(const double* features, double* output,
const PredictionEarlyStopInstance* early_stop) const = 0; const PredictionEarlyStopInstance* early_stop) const = 0;
virtual void PredictRawByMap(const std::unordered_map<int, double>& features, double* output, virtual void PredictRawByMap(const std::unordered_map<int, double>& features, double* output,
const PredictionEarlyStopInstance* early_stop) const = 0; const PredictionEarlyStopInstance* early_stop) const = 0;
/*! /*!
...@@ -134,8 +134,8 @@ public: ...@@ -134,8 +134,8 @@ public:
virtual void Predict(const double* features, double* output, virtual void Predict(const double* features, double* output,
const PredictionEarlyStopInstance* early_stop) const = 0; const PredictionEarlyStopInstance* early_stop) const = 0;
virtual void PredictByMap(const std::unordered_map<int, double>& features, double* output, virtual void PredictByMap(const std::unordered_map<int, double>& features, double* output,
const PredictionEarlyStopInstance* early_stop) const = 0; const PredictionEarlyStopInstance* early_stop) const = 0;
/*! /*!
...@@ -210,7 +210,7 @@ public: ...@@ -210,7 +210,7 @@ public:
* \param filename Filename that want to save to * \param filename Filename that want to save to
*/ */
virtual void SaveModelToProto(int num_iteration, const char* filename) const = 0; virtual void SaveModelToProto(int num_iteration, const char* filename) const = 0;
/*! /*!
* \brief Restore from a serialized protobuf file * \brief Restore from a serialized protobuf file
* \param filename Filename that want to restore from * \param filename Filename that want to restore from
......
...@@ -478,8 +478,8 @@ inline void Tree::PredictContrib(const double* feature_values, int num_features, ...@@ -478,8 +478,8 @@ inline void Tree::PredictContrib(const double* feature_values, int num_features,
output[num_features] += ExpectedValue(); output[num_features] += ExpectedValue();
// Run the recursion with preallocated space for the unique path data // Run the recursion with preallocated space for the unique path data
if (num_leaves_ > 1) { if (num_leaves_ > 1) {
const int max_path_len = MaxDepth()+1; const int max_path_len = MaxDepth() + 1;
PathElement *unique_path_data = new PathElement[(max_path_len*(max_path_len+1))/2]; PathElement *unique_path_data = new PathElement[(max_path_len*(max_path_len + 1)) / 2];
TreeSHAP(feature_values, output, 0, 0, unique_path_data, 1, 1, -1); TreeSHAP(feature_values, output, 0, 0, unique_path_data, 1, 1, -1);
delete[] unique_path_data; delete[] unique_path_data;
} }
...@@ -490,8 +490,8 @@ inline void Tree::RecomputeLeafDepths(int node, int depth) { ...@@ -490,8 +490,8 @@ inline void Tree::RecomputeLeafDepths(int node, int depth) {
if (node < 0) { if (node < 0) {
leaf_depth_[~node] = depth; leaf_depth_[~node] = depth;
} else { } else {
RecomputeLeafDepths(left_child_[node], depth+1); RecomputeLeafDepths(left_child_[node], depth + 1);
RecomputeLeafDepths(right_child_[node], depth+1); RecomputeLeafDepths(right_child_[node], depth + 1);
} }
} }
......
...@@ -65,7 +65,7 @@ public: ...@@ -65,7 +65,7 @@ public:
if (is_predict_leaf_index) { if (is_predict_leaf_index) {
predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) { predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
int tid = omp_get_thread_num(); int tid = omp_get_thread_num();
if(num_feature_ > MAX_FEATURE && num_feature_/static_cast<int>(features.size()) > SPARSITY) { if (num_feature_ > MAX_FEATURE && num_feature_ / static_cast<int>(features.size()) > SPARSITY) {
CopyToPredictMap(tid, features); CopyToPredictMap(tid, features);
boosting_->PredictLeafIndexByMap(predict_buf_map_[tid], output); boosting_->PredictLeafIndexByMap(predict_buf_map_[tid], output);
ClearPredictMap(tid); ClearPredictMap(tid);
...@@ -88,7 +88,7 @@ public: ...@@ -88,7 +88,7 @@ public:
if (is_raw_score) { if (is_raw_score) {
predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) { predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
int tid = omp_get_thread_num(); int tid = omp_get_thread_num();
if(num_feature_ > MAX_FEATURE && num_feature_/static_cast<int>(features.size()) > SPARSITY) { if (num_feature_ > MAX_FEATURE && num_feature_ / static_cast<int>(features.size()) > SPARSITY) {
CopyToPredictMap(tid, features); CopyToPredictMap(tid, features);
boosting_->PredictRawByMap(predict_buf_map_[tid], output, &early_stop_); boosting_->PredictRawByMap(predict_buf_map_[tid], output, &early_stop_);
ClearPredictMap(tid); ClearPredictMap(tid);
...@@ -101,7 +101,7 @@ public: ...@@ -101,7 +101,7 @@ public:
} else { } else {
predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) { predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
int tid = omp_get_thread_num(); int tid = omp_get_thread_num();
if(num_feature_ > MAX_FEATURE && num_feature_/static_cast<int>(features.size()) > SPARSITY) { if (num_feature_ > MAX_FEATURE && num_feature_ / static_cast<int>(features.size()) > SPARSITY) {
CopyToPredictMap(tid, features); CopyToPredictMap(tid, features);
boosting_->PredictByMap(predict_buf_map_[tid], output, &early_stop_); boosting_->PredictByMap(predict_buf_map_[tid], output, &early_stop_);
ClearPredictMap(tid); ClearPredictMap(tid);
...@@ -151,20 +151,20 @@ public: ...@@ -151,20 +151,20 @@ public:
TextReader<data_size_t> predict_data_reader(data_filename, has_header); TextReader<data_size_t> predict_data_reader(data_filename, has_header);
std::unordered_map<int, int> feature_names_map_; std::unordered_map<int, int> feature_names_map_;
bool need_adjust = false; bool need_adjust = false;
if(has_header) { if (has_header) {
std::string first_line = predict_data_reader.first_line(); std::string first_line = predict_data_reader.first_line();
std::vector<std::string> header = Common::Split(first_line.c_str(), "\t,"); std::vector<std::string> header = Common::Split(first_line.c_str(), "\t,");
header.erase(header.begin() + boosting_->LabelIdx()); header.erase(header.begin() + boosting_->LabelIdx());
for(int i = 0; i < static_cast<int>(header.size()); ++i) { for (int i = 0; i < static_cast<int>(header.size()); ++i) {
for(int j = 0; j < static_cast<int>(boosting_->FeatureNames().size()); ++j) { for (int j = 0; j < static_cast<int>(boosting_->FeatureNames().size()); ++j) {
if(header[i] == boosting_->FeatureNames()[j]) { if (header[i] == boosting_->FeatureNames()[j]) {
feature_names_map_[i] = j; feature_names_map_[i] = j;
break; break;
} }
} }
} }
for(auto s:feature_names_map_) { for (auto s : feature_names_map_) {
if(s.first != s.second) { if (s.first != s.second) {
need_adjust = true; need_adjust = true;
break; break;
} }
...@@ -176,14 +176,13 @@ public: ...@@ -176,14 +176,13 @@ public:
parser_fun = [this, &parser, &tmp_label, &need_adjust, &feature_names_map_] parser_fun = [this, &parser, &tmp_label, &need_adjust, &feature_names_map_]
(const char* buffer, std::vector<std::pair<int, double>>* feature) { (const char* buffer, std::vector<std::pair<int, double>>* feature) {
parser->ParseOneLine(buffer, feature, &tmp_label); parser->ParseOneLine(buffer, feature, &tmp_label);
if(need_adjust) { if (need_adjust) {
int i = 0, j = static_cast<int>(feature->size()); int i = 0, j = static_cast<int>(feature->size());
while(i < j) { while (i < j) {
if(feature_names_map_.find((*feature)[i].first) != feature_names_map_.end()) { if (feature_names_map_.find((*feature)[i].first) != feature_names_map_.end()) {
(*feature)[i].first = feature_names_map_[(*feature)[i].first]; (*feature)[i].first = feature_names_map_[(*feature)[i].first];
++i; ++i;
} } else {
else {
//move the non-used features to the end of the feature vector //move the non-used features to the end of the feature vector
std::swap((*feature)[i], (*feature)[--j]); std::swap((*feature)[i], (*feature)[--j]);
} }
......
...@@ -30,16 +30,16 @@ std::chrono::duration<double, std::milli> tree_time; ...@@ -30,16 +30,16 @@ std::chrono::duration<double, std::milli> tree_time;
#endif // TIMETAG #endif // TIMETAG
GBDT::GBDT() : iter_(0), GBDT::GBDT() : iter_(0),
train_data_(nullptr), train_data_(nullptr),
objective_function_(nullptr), objective_function_(nullptr),
early_stopping_round_(0), early_stopping_round_(0),
max_feature_idx_(0), max_feature_idx_(0),
num_tree_per_iteration_(1), num_tree_per_iteration_(1),
num_class_(1), num_class_(1),
num_iteration_for_pred_(0), num_iteration_for_pred_(0),
shrinkage_rate_(0.1f), shrinkage_rate_(0.1f),
num_init_iteration_(0), num_init_iteration_(0),
need_re_bagging_(false) { need_re_bagging_(false) {
#pragma omp parallel #pragma omp parallel
#pragma omp master #pragma omp master
...@@ -224,7 +224,7 @@ data_size_t GBDT::BaggingHelper(Random& cur_rand, data_size_t start, data_size_t ...@@ -224,7 +224,7 @@ data_size_t GBDT::BaggingHelper(Random& cur_rand, data_size_t start, data_size_t
void GBDT::Bagging(int iter) { void GBDT::Bagging(int iter) {
// if need bagging // if need bagging
if ((bag_data_cnt_ < num_data_ && iter % gbdt_config_->bagging_freq == 0) if ((bag_data_cnt_ < num_data_ && iter % gbdt_config_->bagging_freq == 0)
|| need_re_bagging_) { || need_re_bagging_) {
need_re_bagging_ = false; need_re_bagging_ = false;
const data_size_t min_inner_size = 1000; const data_size_t min_inner_size = 1000;
......
...@@ -20,7 +20,7 @@ namespace LightGBM { ...@@ -20,7 +20,7 @@ namespace LightGBM {
/*! /*!
* \brief GBDT algorithm implementation. including Training, prediction, bagging. * \brief GBDT algorithm implementation. including Training, prediction, bagging.
*/ */
class GBDT: public GBDTBase { class GBDT : public GBDTBase {
public: public:
/*! /*!
...@@ -187,14 +187,14 @@ public: ...@@ -187,14 +187,14 @@ public:
void PredictRaw(const double* features, double* output, void PredictRaw(const double* features, double* output,
const PredictionEarlyStopInstance* earlyStop) const override; const PredictionEarlyStopInstance* earlyStop) const override;
void PredictRawByMap(const std::unordered_map<int, double>& features, double* output, void PredictRawByMap(const std::unordered_map<int, double>& features, double* output,
const PredictionEarlyStopInstance* early_stop) const override; const PredictionEarlyStopInstance* early_stop) const override;
void Predict(const double* features, double* output, void Predict(const double* features, double* output,
const PredictionEarlyStopInstance* earlyStop) const override; const PredictionEarlyStopInstance* earlyStop) const override;
void PredictByMap(const std::unordered_map<int, double>& features, double* output, void PredictByMap(const std::unordered_map<int, double>& features, double* output,
const PredictionEarlyStopInstance* early_stop) const override; const PredictionEarlyStopInstance* early_stop) const override;
void PredictLeafIndex(const double* features, double* output) const override; void PredictLeafIndex(const double* features, double* output) const override;
...@@ -252,7 +252,7 @@ public: ...@@ -252,7 +252,7 @@ public:
* \param filename Filename that want to save to * \param filename Filename that want to save to
*/ */
void SaveModelToProto(int num_iteration, const char* filename) const override; void SaveModelToProto(int num_iteration, const char* filename) const override;
/*! /*!
* \brief Restore from a serialized protobuf file * \brief Restore from a serialized protobuf file
* \param filename Filename that want to restore from * \param filename Filename that want to restore from
......
...@@ -104,10 +104,10 @@ std::string GBDT::ModelToIfElse(int num_iteration) const { ...@@ -104,10 +104,10 @@ std::string GBDT::ModelToIfElse(int num_iteration) const {
// PredictRawByMap // PredictRawByMap
str_buf << "double (*PredictTreeByMapPtr[])(const std::unordered_map<int, double>&) = { "; str_buf << "double (*PredictTreeByMapPtr[])(const std::unordered_map<int, double>&) = { ";
for (int i = 0; i < num_used_model; ++i) { for (int i = 0; i < num_used_model; ++i) {
if (i > 0) { if (i > 0) {
str_buf << " , "; str_buf << " , ";
} }
str_buf << "PredictTree" << i << "ByMap"; str_buf << "PredictTree" << i << "ByMap";
} }
str_buf << " };" << std::endl << std::endl; str_buf << " };" << std::endl << std::endl;
...@@ -185,10 +185,10 @@ std::string GBDT::ModelToIfElse(int num_iteration) const { ...@@ -185,10 +185,10 @@ std::string GBDT::ModelToIfElse(int num_iteration) const {
//PredictLeafIndexByMap //PredictLeafIndexByMap
str_buf << "double (*PredictTreeLeafByMapPtr[])(const std::unordered_map<int, double>&) = { "; str_buf << "double (*PredictTreeLeafByMapPtr[])(const std::unordered_map<int, double>&) = { ";
for (int i = 0; i < num_used_model; ++i) { for (int i = 0; i < num_used_model; ++i) {
if (i > 0) { if (i > 0) {
str_buf << " , "; str_buf << " , ";
} }
str_buf << "PredictTree" << i << "LeafByMap"; str_buf << "PredictTree" << i << "LeafByMap";
} }
str_buf << " };" << std::endl << std::endl; str_buf << " };" << std::endl << std::endl;
......
...@@ -381,30 +381,28 @@ std::string Tree::ToIfElse(int index, bool is_predict_leaf_index) const { ...@@ -381,30 +381,28 @@ std::string Tree::ToIfElse(int index, bool is_predict_leaf_index) const {
//Predict func by Map to ifelse //Predict func by Map to ifelse
str_buf << "double PredictTree" << index; str_buf << "double PredictTree" << index;
if (is_predict_leaf_index) { if (is_predict_leaf_index) {
str_buf << "LeafByMap"; str_buf << "LeafByMap";
} } else {
else { str_buf << "ByMap";
str_buf << "ByMap";
} }
str_buf << "(const std::unordered_map<int, double>& arr) { "; str_buf << "(const std::unordered_map<int, double>& arr) { ";
if (num_leaves_ <= 1) { if (num_leaves_ <= 1) {
str_buf << "return " << leaf_value_[0] << ";"; str_buf << "return " << leaf_value_[0] << ";";
} } else {
else { str_buf << "const std::vector<uint32_t> cat_threshold = {";
str_buf << "const std::vector<uint32_t> cat_threshold = {"; for (size_t i = 0; i < cat_threshold_.size(); ++i) {
for (size_t i = 0; i < cat_threshold_.size(); ++i) { if (i != 0) {
if (i != 0) { str_buf << ",";
str_buf << ","; }
} str_buf << cat_threshold_[i];
str_buf << cat_threshold_[i]; }
} str_buf << "};";
str_buf << "};"; // use this for the missing value conversion
// use this for the missing value conversion str_buf << "double fval = 0.0f; ";
str_buf << "double fval = 0.0f; "; if (num_cat_ > 0) {
if (num_cat_ > 0) { str_buf << "int int_fval = 0; ";
str_buf << "int int_fval = 0; "; }
} str_buf << NodeToIfElseByMap(0, is_predict_leaf_index);
str_buf << NodeToIfElseByMap(0, is_predict_leaf_index);
} }
str_buf << " }" << std::endl; str_buf << " }" << std::endl;
...@@ -696,7 +694,7 @@ double Tree::ExpectedValue() const { ...@@ -696,7 +694,7 @@ double Tree::ExpectedValue() const {
const double total_count = internal_count_[0]; const double total_count = internal_count_[0];
double exp_value = 0.0; double exp_value = 0.0;
for (int i = 0; i < num_leaves(); ++i) { for (int i = 0; i < num_leaves(); ++i) {
exp_value += (leaf_count_[i]/total_count)*LeafOutput(i); exp_value += (leaf_count_[i] / total_count)*LeafOutput(i);
} }
return exp_value; return exp_value;
} }
......
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