boosting.cpp 2.18 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
Guolin Ke's avatar
Guolin Ke committed
5
#include <LightGBM/boosting.h>
6

Guolin Ke's avatar
Guolin Ke committed
7
#include "dart.hpp"
8
#include "gbdt.h"
Guolin Ke's avatar
Guolin Ke committed
9
#include "goss.hpp"
Guolin Ke's avatar
Guolin Ke committed
10
#include "rf.hpp"
Guolin Ke's avatar
Guolin Ke committed
11
12
13

namespace LightGBM {

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

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

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

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