-
Notifications
You must be signed in to change notification settings - Fork 3
/
compboost.cpp
251 lines (193 loc) · 8.83 KB
/
compboost.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// ========================================================================== //
// ___. __ //
// ____ ____ _____ ______\_ |__ ____ ____ _______/ |_ //
// _/ ___\/ _ \ / \\____ \| __ \ / _ \ / _ \/ ___/\ __\ //
// \ \__( <_> ) Y Y \ |_> > \_\ ( <_> | <_> )___ \ | | //
// \___ >____/|__|_| / __/|___ /\____/ \____/____ > |__| //
// \/ \/|__| \/ \/ //
// //
// ========================================================================== //
//
// Compboost is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Compboost is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Compboost. If not, see <http://www.gnu.org/licenses/>.
//
// This file contains:
// -------------------
//
// Implementation of the "Compboost" class.
//
// Written by:
// -----------
//
// Daniel Schalk
// Institut für Statistik
// Ludwig-Maximilians-Universität München
// Ludwigstraße 33
// D-80539 München
//
// https://www.compstat.statistik.uni-muenchen.de
//
// ========================================================================== //
#include "compboost.h"
namespace cboost {
// --------------------------------------------------------------------------- #
// Constructor:
// --------------------------------------------------------------------------- #
// todo: response as call by reference!
Compboost::Compboost () {}
Compboost::Compboost (arma::vec response, double learning_rate,
bool stop_if_all_stopper_fulfilled, optimizer::Optimizer* used_optimizer,
loss::Loss* used_loss, loggerlist::LoggerList* used_logger,
blearnerlist::BaselearnerList used_baselearner_list)
: response ( response ),
learning_rate ( learning_rate ),
stop_if_all_stopper_fulfilled ( stop_if_all_stopper_fulfilled ),
used_optimizer ( used_optimizer ),
used_loss ( used_loss ),
used_baselearner_list ( used_baselearner_list ),
used_logger ( used_logger )
{
blearner_track = blearnertrack::BaselearnerTrack(learning_rate);
}
// --------------------------------------------------------------------------- #
// Member functions:
// --------------------------------------------------------------------------- #
void Compboost::TrainCompboost (bool trace)
{
// Make sure, that the selected baselearner and logger data is empty:
blearner_track.ClearBaselearnerVector();
used_logger->ClearLoggerData();
// Initialize zero model and pseudo residuals:
initialization = used_loss->ConstantInitializer(response);
arma::vec pseudo_residuals_init (response.size());
// std::cout << "<<Compboost>> Initialize zero model and pseudo residuals" << std::endl;
// Initialize prediction and fill with zero model:
arma::vec prediction(response.size());
prediction.fill(initialization);
// std::cout << "<<Compboost>> Initialize prediction and fill with zero model" << std::endl;
// Initialize trace:
if (trace) {
std::cout << std::endl;
used_logger->InitializeLoggerPrinter();
}
// Declare variables to stop the algorithm:
bool stop_the_algorithm = false;
unsigned int k = 1;
// Main Algorithm. While the stop criteria isn't fullfilled, run the
// algorithm:
while (! stop_the_algorithm) {
// Define pseudo residuals as negative gradient:
pseudo_residuals = -used_loss->DefinedGradient(response, prediction);
// std::cout << "\n<<Compboost>> Define pseudo residuals as negative gradient" << std::endl;
// Cast integer k to string for baselearner identifier:
std::string temp_string = std::to_string(k);
blearner::Baselearner* selected_blearner = used_optimizer->FindBestBaselearner(temp_string, pseudo_residuals, used_baselearner_list.GetMap());
// std::cout << "<<Compboost>> Cast integer k to string for baselearner identifier" << std::endl;
// Insert new baselearner to vector of selected baselearner:
blearner_track.InsertBaselearner(selected_blearner);
// std::cout << "<<Compboost>> Insert new baselearner to vector of selected baselearner" << std::endl;
// Update model (prediction) and shrink by learning rate:
prediction += learning_rate * selected_blearner->predict();
// std::cout << "<<Compboost>> Update model (prediction) and shrink by learning rate" << std::endl;
// Log the current step:
// The last term has to be the prediction or anything like that. This is
// important to track the risk (inbag or oob)!!!!
used_logger->LogCurrent(k, 6);
// std::cout << "<<Compboost>> Log the current step" << std::endl;
// Get status of the algorithm (is stopping criteria reached):
stop_the_algorithm = ! used_logger->GetStopperStatus(stop_if_all_stopper_fulfilled);
// Print trace:
if (trace) {
used_logger->PrintLoggerStatus();
}
// Increment k:
k += 1;
}
if (trace) {
std::cout << std::endl;
std::cout << std::endl;
}
// Set model prediction:
model_prediction = prediction;
}
arma::vec Compboost::GetPrediction ()
{
return model_prediction;
}
std::map<std::string, arma::mat> Compboost::GetParameter ()
{
return blearner_track.GetParameterMap();
}
std::vector<std::string> Compboost::GetSelectedBaselearner ()
{
std::vector<std::string> selected_blearner;
// Issue: https://github.com/schalkdaniel/compboost/issues/62
// Doesn't work:
// for (std::vector<blearner::Baselearner*>::iterator it = blearner_track->GetBaselearnerVector().begin(); it != blearner_track->GetBaselearnerVector().end(); ++it) {
// selected_blearner.push_back((*it)->GetBaselearnerType());
// }
// Does work:
for (unsigned int i = 0; i < blearner_track.GetBaselearnerVector().size(); i++) {
selected_blearner.push_back(blearner_track.GetBaselearnerVector()[i]->GetDataIdentifier() + ": " + blearner_track.GetBaselearnerVector()[i]->GetBaselearnerType());
}
return selected_blearner;
}
std::map<std::string, arma::mat> Compboost::GetParameterOfIteration (unsigned int k)
{
return blearner_track.GetEstimatedParameterForIteration(k);
}
std::pair<std::vector<std::string>, arma::mat> Compboost::GetParameterMatrix ()
{
return blearner_track.GetParameterMatrix();
}
arma::vec Compboost::Predict (std::map<std::string, arma::mat> data_map)
{
// std::cout << "Get into Compboost::Predict" << std::endl;
std::map<std::string, arma::mat> parameter_map = blearner_track.GetParameterMap();
arma::vec pred(data_map.begin()->second.n_rows);
pred.fill(initialization);
// std::cout << "initialize pred vec" << std::endl;
for (auto& it : parameter_map) {
std::string sel_factory = it.first;
// std::cout << "Fatory id of parameter map: " << sel_factory << std::endl;
blearnerfactory::BaselearnerFactory* sel_factory_obj = used_baselearner_list.GetMap().find(sel_factory)->second;
// std::cout << "Data of selected factory: " << sel_factory_obj->GetDataIdentifier() << std::endl;
arma::mat data_trafo = sel_factory_obj->InstantiateData((data_map.find(sel_factory_obj->GetDataIdentifier())->second));
pred += data_trafo * it.second;
}
return pred;
}
arma::vec Compboost::PredictionOfIteration (std::map<std::string, arma::mat> data_map, unsigned int k)
{
// std::cout << "Get into Compboost::Predict" << std::endl;
std::map<std::string, arma::mat> parameter_map = blearner_track.GetEstimatedParameterForIteration(k);
arma::vec pred(data_map.begin()->second.n_rows);
pred.fill(initialization);
// std::cout << "initialize pred vec" << std::endl;
for (auto& it : parameter_map) {
std::string sel_factory = it.first;
// std::cout << "Fatory id of parameter map: " << sel_factory << std::endl;
blearnerfactory::BaselearnerFactory* sel_factory_obj = used_baselearner_list.GetMap().find(sel_factory)->second;
// std::cout << "Data of selected factory: " << sel_factory_obj->GetDataIdentifier() << std::endl;
arma::mat data_trafo = sel_factory_obj->InstantiateData((data_map.find(sel_factory_obj->GetDataIdentifier())->second));
pred += data_trafo * it.second;
}
return pred;
}
// Destructor:
Compboost::~Compboost ()
{
// std::cout << "Call Compboost Destructor" << std::endl;
// delete used_optimizer;
// delete used_loss;
// delete used_logger;
}
} // namespace cboost