"git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "459609103583af6e1fc610650718c87d184e3c88"
boosting.cpp 2.01 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
#include <LightGBM/boosting.h>
2

Guolin Ke's avatar
Guolin Ke committed
3
#include "dart.hpp"
4
#include "gbdt.h"
Guolin Ke's avatar
Guolin Ke committed
5
#include "goss.hpp"
Guolin Ke's avatar
Guolin Ke committed
6
#include "rf.hpp"
Guolin Ke's avatar
Guolin Ke committed
7
8
9

namespace LightGBM {

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

16
17
bool Boosting::LoadFileToBoosting(Boosting* boosting, const char* filename) {
  auto start_time = std::chrono::steady_clock::now();
Guolin Ke's avatar
Guolin Ke committed
18
19
  if (boosting != nullptr) {
    TextReader<size_t> model_reader(filename, true);
20
21
    size_t buffer_len = 0;
    auto buffer = model_reader.ReadContent(&buffer_len);
Guolin Ke's avatar
Guolin Ke committed
22
23
24
25
    if (!boosting->LoadModelFromString(buffer.data(), buffer_len)) {
      return false;
    }
  }
26
  std::chrono::duration<double, std::milli> delta = (std::chrono::steady_clock::now() - start_time);
27
  Log::Debug("Time for loading model: %f seconds", 1e-3*delta);
Guolin Ke's avatar
Guolin Ke committed
28
  return true;
29
30
}

31
Boosting* Boosting::CreateBoosting(const std::string& type, const char* filename) {
Guolin Ke's avatar
Guolin Ke committed
32
  if (filename == nullptr || filename[0] == '\0') {
Guolin Ke's avatar
Guolin Ke committed
33
    if (type == std::string("gbdt")) {
34
      return new GBDT();
Guolin Ke's avatar
Guolin Ke committed
35
    } else if (type == std::string("dart")) {
36
      return new DART();
Guolin Ke's avatar
Guolin Ke committed
37
38
    } else if (type == std::string("goss")) {
      return new GOSS();
Guolin Ke's avatar
Guolin Ke committed
39
40
    } else if (type == std::string("rf")) {
      return new RF();
41
42
43
    } else {
      return nullptr;
    }
Guolin Ke's avatar
Guolin Ke committed
44
  } else {
Guolin Ke's avatar
Guolin Ke committed
45
    std::unique_ptr<Boosting> ret;
46
    if (GetBoostingTypeFromModelFile(filename) == std::string("tree")) {
Guolin Ke's avatar
Guolin Ke committed
47
      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 {
56
        Log::Fatal("Unknown boosting type %s", type.c_str());
57
      }
58
      LoadFileToBoosting(ret.get(), filename);
59
    } else {
60
      Log::Fatal("Unknown model format or submodel type in model file %s", filename);
61
    }
Guolin Ke's avatar
Guolin Ke committed
62
    return ret.release();
63
64
65
  }
}

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