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
17
18
19
20
21
22
  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';
    }
23
    if (!boosting->LoadModelFromString(str_buf.str()))
24
      return false;
25
  }
26
27

  return true;
28
29
}

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

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

}  // namespace LightGBM