-
Notifications
You must be signed in to change notification settings - Fork 21
/
gbm.cpp
188 lines (152 loc) · 6.41 KB
/
gbm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <random>
#include <algorithm>
#include "classifier.hpp"
#include "gbm.hpp"
namespace gbm {
Boosting *train(const std::vector<std::string> &filenames,
double *startResolution,
const int numScales,
const int numTrees,
const int treeDepth,
const double radius,
const int maxSamples,
const std::vector<int> &classes) {
std::vector<float> gt;
std::vector< std::vector<double> > featureRows;
std::vector< std::vector<double> > featuresData;
std::vector< std::vector<int> > featuresIdx;
size_t numFeats;
int numClass;
getTrainingData(filenames, startResolution, numScales, radius, maxSamples, classes,
[&featureRows, &featuresData, &featuresIdx, >](const std::vector<Feature *> &features, const size_t idx, const int g) {
const size_t row = featureRows.size();
featureRows.emplace_back();
featureRows[row].resize(features.size(), 0);
for (std::size_t f = 0; f < features.size(); f++) {
featureRows[row][f] = features[f]->getValue(idx);
featuresData[f].push_back(featureRows[row][f]);
featuresIdx[f].push_back(row);
}
gt.push_back(g);
},
[&numFeats, &numClass, &featuresData, &featuresIdx](const size_t numFeatures, const int numClasses) {
numFeats = numFeatures;
numClass = numClasses;
featuresData.resize(numFeats);
featuresIdx.resize(numFeats);
});
const size_t numRows = gt.size();
std::cout << "Using " << numRows << " inliers" << std::endl;
LightGBM::Config ioconfig;
ioconfig.num_class = numClass;
ioconfig.max_bin = 255;
std::unique_ptr<LightGBM::Dataset> dset;
LightGBM::DatasetLoader loader(ioconfig, nullptr, numClass, nullptr);
dset.reset(loader.ConstructFromSampleData(LightGBM::Common::Vector2Ptr<double>(&featuresData).data(),
LightGBM::Common::Vector2Ptr<int>(&featuresIdx).data(),
numFeats,
LightGBM::Common::VectorSize<double>(featuresData).data(),
numRows,
numRows,
numRows));
#pragma omp parallel for schedule(static)
for (long long int i = 0; i < numRows; ++i) {
dset->PushOneRow(omp_get_thread_num(), i, featureRows[i]);
}
dset->FinishLoad();
/*
for(int j = 0; j < numFeats; j++){
const auto nbins = dset->FeatureBinMapper(j)->num_bin();
// std::cout << features[j]->getName() << std::endl;
std::cout << "Feat " << j << std::endl;
std::cout << " " << dset->FeatureBinMapper(j)->BinToValue(0) << " ";
std::cout << " " << dset->FeatureBinMapper(j)->BinToValue(nbins-2) << " ";
std::cout << std::endl;
}
*/
if (!dset->SetFloatField("label", gt.data(), numRows)) {
throw std::runtime_error("Error setting label");
}
LightGBM::Config boostConfig;
boostConfig.num_iterations = numTrees;
// boostConfig.bagging_freq = 1;
boostConfig.bagging_fraction = 0.5;
boostConfig.num_class = numClass;
boostConfig.max_depth = treeDepth;
// tree params
boostConfig.num_leaves = 16;
boostConfig.learning_rate = 0.2;
std::stringstream ss;
ss << *startResolution << " " << radius << " " << numScales;
boostConfig.data = ss.str();
LightGBM::Config objConfig;
objConfig.num_class = numClass;
auto *objFunc = LightGBM::ObjectiveFunction::CreateObjectiveFunction("multiclass", objConfig);
objFunc->Init(dset->metadata(), dset->num_data());
LightGBM::Config metricConfig;
metricConfig.num_class = numClass;
std::vector< std::unique_ptr<LightGBM::Metric> > trainMetrics;
auto metric = std::unique_ptr<LightGBM::Metric>(
LightGBM::Metric::CreateMetric("multi_logloss", metricConfig));
metric->Init(dset->metadata(), dset->num_data());
trainMetrics.push_back(std::move(metric));
auto *booster = LightGBM::Boosting::CreateBoosting("gbdt", nullptr);
booster->Init(&boostConfig, dset.get(), objFunc,
LightGBM::Common::ConstPtrInVectorWrapper<LightGBM::Metric>(trainMetrics));
// Add if you want to add validation data (eval)
// booster->AddValidDataset(dset.get(), LightGBM::Common::ConstPtrInVectorWrapper<LightGBM::Metric>(trainMetrics));
for (int i = 0; i < boostConfig.num_iterations; i++) {
auto scores = booster->GetEvalAt(0);
for (const auto &v : scores) std::cout << "Iteration " << (i + 1) << " score: " << v << std::endl;
if (booster->TrainOneIter(nullptr, nullptr)) {
std::cout << "Breaking.." << std::endl;
break;
}
}
return booster;
}
Boosting *loadBooster(const std::string &modelFilename) {
std::cout << "Loading " << modelFilename << std::endl;
auto *booster = LightGBM::Boosting::CreateBoosting("gbdt", nullptr);
if (!LightGBM::Boosting::LoadFileToBoosting(booster, modelFilename.c_str())) {
throw std::runtime_error("Cannot open " + modelFilename);
}
booster->InitPredict(0, 0, false);
return booster;
}
void saveBooster(Boosting *booster, const std::string &modelFilename) {
booster->SaveModelToFile(0, 0, 0, modelFilename.c_str());
std::cout << "Saved " << modelFilename << std::endl;
}
BoosterParams extractBoosterParams(Boosting *booster) {
json j = json::parse(booster->GetLoadedParam());
if (!j.contains("data") || j["data"].get<std::string>().empty()) throw std::runtime_error("Invalid booster model (data params missing?)");
std::stringstream ss(j["data"].get<std::string>());
BoosterParams p;
ss >> p.resolution;
ss >> p.radius;
ss >> p.numScales;
return p;
}
void classify(PointSet &pointSet,
Boosting *booster,
const std::vector<Feature *> &features,
const std::vector<Label> &labels,
const Regularization regularization,
const double regRadius,
const bool useColors,
const bool unclassifiedOnly,
const bool evaluate,
const std::vector<int> &skip,
const std::string &statsFile
) {
LightGBM::PredictionEarlyStopConfig early_stop_config;
auto earlyStop = LightGBM::CreatePredictionEarlyStopInstance("none", early_stop_config);
classifyData<double>(pointSet,
[&booster, &earlyStop](const double *ft, double *probs) {
booster->Predict(ft, probs, &earlyStop);
},
features, labels, regularization, regRadius, useColors, unclassifiedOnly, evaluate, skip, statsFile);
}
}