"git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "fdf1f3260affc26fd060526b55a136016db5633d"
boosting.cpp 2.16 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 "rf.hpp"
Guolin Ke's avatar
Guolin Ke committed
10
11
12

namespace LightGBM {

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

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

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

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