"...tests/git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "eded794efb576da165e5dadb73f71ca77592bcd4"
boosting.cpp 1.54 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
#include <LightGBM/boosting.h>
#include "gbdt.h"

namespace LightGBM {

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
BoostingType GetBoostingTypeFromModelFile(const char* filename) {
  TextReader<size_t> model_reader(filename, true);
  std::string type = model_reader.first_line();
  if (type == std::string("gbdt")) {
    return BoostingType::kGBDT;
  }
  return BoostingType::kUnknow;
}

void LoadFileToBoosting(Boosting* boosting, const char* filename) {
  if (boosting != nullptr) {
    TextReader<size_t> model_reader(filename, true);
    model_reader.ReadAllLines();
    std::stringstream str_buf;
    for (auto& line : model_reader.Lines()) {
      str_buf << line << '\n';
    }
    boosting->ModelsFromString(str_buf.str());
  }
}

Boosting* Boosting::CreateBoosting(BoostingType type, const char* filename) {
  if (filename[0] == '\0') {
    if (type == BoostingType::kGBDT) {
      return new GBDT();
    } else {
      return nullptr;
    }
Guolin Ke's avatar
Guolin Ke committed
34
  } else {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    Boosting* ret = nullptr;
    auto type_in_file = GetBoostingTypeFromModelFile(filename);
    if (type_in_file == type) {
      if (type == BoostingType::kGBDT) {
        ret = new GBDT();
      }
      LoadFileToBoosting(ret, filename);
    } else {
      Log::Fatal("Boosting type in parameter is not same with the type in model file");
    }
    return ret;
  }
}

Boosting* Boosting::CreateBoosting(const char* filename) {
  auto type = GetBoostingTypeFromModelFile(filename);
  Boosting* ret = nullptr;
  if (type == BoostingType::kGBDT) {
    ret = new GBDT();
Guolin Ke's avatar
Guolin Ke committed
54
  }
55
56
  LoadFileToBoosting(ret, filename);
  return ret;
Guolin Ke's avatar
Guolin Ke committed
57
58
59
}

}  // namespace LightGBM