boosting.cpp 2.21 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
#include <LightGBM/boosting.h>
#include "gbdt.h"
Guolin Ke's avatar
Guolin Ke committed
3
#include "dart.hpp"
Guolin Ke's avatar
Guolin Ke committed
4
#include "goss.hpp"
Guolin Ke's avatar
Guolin Ke committed
5
#include "rf.hpp"
Guolin Ke's avatar
Guolin Ke committed
6
7
8

namespace LightGBM {

Guolin Ke's avatar
Guolin Ke committed
9
std::string GetBoostingTypeFromModelFile(const char* filename) {
10
11
  TextReader<size_t> model_reader(filename, true);
  std::string type = model_reader.first_line();
Guolin Ke's avatar
Guolin Ke committed
12
  return type;
13
14
}

15
bool Boosting::LoadFileToBoosting(Boosting* boosting, const char* filename) {
16
  if (boosting != nullptr) {
17
18
19
20
21
    TextReader<size_t> model_reader(filename, true);
    model_reader.ReadAllLines();
    std::stringstream str_buf;
    for (auto& line : model_reader.Lines()) {
      str_buf << line << '\n';
22
    }
23
24
    if (!boosting->LoadModelFromString(str_buf.str()))
      return false;
25
  }
26
  return true;
27
28
}

29
Boosting* Boosting::CreateBoosting(const std::string& type, const char* filename) {
Guolin Ke's avatar
Guolin Ke committed
30
  if (filename == nullptr || filename[0] == '\0') {
Guolin Ke's avatar
Guolin Ke committed
31
    if (type == std::string("gbdt")) {
32
      return new GBDT();
Guolin Ke's avatar
Guolin Ke committed
33
    } else if (type == std::string("dart")) {
34
      return new DART();
Guolin Ke's avatar
Guolin Ke committed
35
36
    } else if (type == std::string("goss")) {
      return new GOSS();
Guolin Ke's avatar
Guolin Ke committed
37
38
    } else if (type == std::string("rf")) {
      return new RF();
39
40
41
    } else {
      return nullptr;
    }
Guolin Ke's avatar
Guolin Ke committed
42
  } else {
Guolin Ke's avatar
Guolin Ke committed
43
    std::unique_ptr<Boosting> ret;
44
45
    auto type_in_file = GetBoostingTypeFromModelFile(filename);
    if (type_in_file == std::string("tree")) {
Guolin Ke's avatar
Guolin Ke committed
46
      if (type == std::string("gbdt")) {
Guolin Ke's avatar
Guolin Ke committed
47
        ret.reset(new GBDT());
Guolin Ke's avatar
Guolin Ke committed
48
      } else if (type == std::string("dart")) {
Guolin Ke's avatar
Guolin Ke committed
49
        ret.reset(new DART());
Guolin Ke's avatar
Guolin Ke committed
50
51
      } else if (type == std::string("goss")) {
        ret.reset(new GOSS());
Guolin Ke's avatar
Guolin Ke committed
52
53
      } else if (type == std::string("rf")) {
        return new RF();
Guolin Ke's avatar
Guolin Ke committed
54
      } else {
zhangyafeikimi's avatar
zhangyafeikimi committed
55
        Log::Fatal("unknown boosting type %s", type.c_str());
56
      }
57
      LoadFileToBoosting(ret.get(), filename);
58
    } else {
59
      Log::Fatal("unknown submodel type in model file %s", filename);
60
    }
Guolin Ke's avatar
Guolin Ke committed
61
    return ret.release();
62
63
64
  }
}

65
66
67
68
69
70
71
72
73
74
75
76
Boosting* Boosting::CreateBoosting(const char* filename) {
  auto type = GetBoostingTypeFromModelFile(filename);
  std::unique_ptr<Boosting> ret;
  if (type == std::string("tree")) {
    ret.reset(new GBDT());
  } else {
    Log::Fatal("unknown submodel type in model file %s", filename);
  }
  LoadFileToBoosting(ret.get(), filename);
  return ret.release();
}

Guolin Ke's avatar
Guolin Ke committed
77
}  // namespace LightGBM