-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
boosting.cc
437 lines (360 loc) · 12.4 KB
/
boosting.cc
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD (revised)
// license as described in the file LICENSE.
/*
* Implementation of online boosting algorithms from
* Beygelzimer, Kale, Luo: Optimal and adaptive algorithms for online boosting,
* ICML-2015.
*/
#include "vw/core/reductions/boosting.h"
#include "vw/common/random.h"
#include "vw/config/options.h"
#include "vw/core/correctedMath.h"
#include "vw/core/learner.h"
#include "vw/core/setup_base.h"
#include "vw/core/shared_data.h"
#include "vw/core/vw_math.h"
#include "vw/io/logger.h"
#include <fmt/core.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace VW::LEARNER;
using namespace VW::config;
using namespace VW::reductions;
using std::endl;
namespace
{
class boosting
{
public:
int N = 0; // NOLINT
float gamma = 0.f;
std::string alg = "";
VW::workspace* all = nullptr;
std::shared_ptr<VW::rand_state> random_state;
std::vector<std::vector<int64_t> > C; // NOLINT
std::vector<float> alpha;
std::vector<float> v;
int t = 0;
VW::io::logger logger;
explicit boosting(VW::io::logger logger) : logger(std::move(logger)) {}
};
//---------------------------------------------------
// Online Boost-by-Majority (BBM)
// --------------------------------------------------
template <bool is_learn>
void predict_or_learn(boosting& o, VW::LEARNER::learner& base, VW::example& ec)
{
auto& ld = ec.l.simple;
float final_prediction = 0;
float s = 0;
float u = ec.weight;
if (is_learn) { o.t++; }
for (int i = 0; i < o.N; i++)
{
if (is_learn)
{
float k = floorf((o.N - i - s) / 2);
int64_t c;
if (o.N - (i + 1) < 0) { c = 0; }
else if (k > o.N - (i + 1)) { c = 0; }
else if (k < 0) { c = 0; }
else if (o.C[o.N - (i + 1)][static_cast<int64_t>(k)] != -1) { c = o.C[o.N - (i + 1)][static_cast<int64_t>(k)]; }
else
{
c = VW::math::choose(o.N - (i + 1), static_cast<int64_t>(k));
o.C[o.N - (i + 1)][static_cast<int64_t>(k)] = c;
}
float w = c * static_cast<float>(pow((0.5 + o.gamma), static_cast<double>(k))) *
static_cast<float>(pow((double)0.5 - o.gamma, static_cast<double>(o.N - (i + 1) - k)));
// update ec.weight, weight for learner i (starting from 0)
ec.weight = u * w;
base.predict(ec, i);
// ec.pred.scalar is now the i-th learner prediction on this example
s += ld.label * ec.pred.scalar;
final_prediction += ec.pred.scalar;
base.learn(ec, i);
}
else
{
base.predict(ec, i);
final_prediction += ec.pred.scalar;
}
}
ec.weight = u;
ec.partial_prediction = final_prediction;
ec.pred.scalar = VW::math::sign(final_prediction);
if (ld.label == ec.pred.scalar) { ec.loss = 0.; }
else { ec.loss = ec.weight; }
}
//-----------------------------------------------------------------
// Logistic boost
//-----------------------------------------------------------------
template <bool is_learn>
void predict_or_learn_logistic(boosting& o, VW::LEARNER::learner& base, VW::example& ec)
{
auto& ld = ec.l.simple;
float final_prediction = 0;
float s = 0;
float u = ec.weight;
if (is_learn) { o.t++; }
float eta = 4.f / sqrtf(static_cast<float>(o.t));
for (int i = 0; i < o.N; i++)
{
if (is_learn)
{
float w = 1 / (1 + VW::details::correctedExp(s));
ec.weight = u * w;
base.predict(ec, i);
float z;
z = ld.label * ec.pred.scalar;
s += z * o.alpha[i];
// if ld.label * ec.pred.scalar < 0, learner i made a mistake
final_prediction += ec.pred.scalar * o.alpha[i];
// update alpha
o.alpha[i] += eta * z / (1 + VW::details::correctedExp(s));
if (o.alpha[i] > 2.) { o.alpha[i] = 2; }
if (o.alpha[i] < -2.) { o.alpha[i] = -2; }
base.learn(ec, i);
}
else
{
base.predict(ec, i);
final_prediction += ec.pred.scalar * o.alpha[i];
}
}
ec.weight = u;
ec.partial_prediction = final_prediction;
ec.pred.scalar = VW::math::sign(final_prediction);
if (ld.label == ec.pred.scalar) { ec.loss = 0.; }
else { ec.loss = ec.weight; }
}
template <bool is_learn>
void predict_or_learn_adaptive(boosting& o, VW::LEARNER::learner& base, VW::example& ec)
{
auto& ld = ec.l.simple;
float final_prediction = 0, partial_prediction = 0;
float s = 0;
float v_normalization = 0, v_partial_sum = 0;
float u = ec.weight;
if (is_learn) { o.t++; }
float eta = 4.f / sqrtf(static_cast<float>(o.t));
float stopping_point = o.random_state->get_and_update_random();
for (int i = 0; i < o.N; i++)
{
if (is_learn)
{
float w = 1 / (1 + VW::details::correctedExp(s));
ec.weight = u * w;
base.predict(ec, i);
float z;
z = ld.label * ec.pred.scalar;
s += z * o.alpha[i];
if (v_partial_sum <= stopping_point) { final_prediction += ec.pred.scalar * o.alpha[i]; }
partial_prediction += ec.pred.scalar * o.alpha[i];
v_partial_sum += o.v[i];
// update v, exp(-1) = 0.36788
if (ld.label * partial_prediction < 0) { o.v[i] *= 0.36788f; }
v_normalization += o.v[i];
// update alpha
o.alpha[i] += eta * z / (1 + VW::details::correctedExp(s));
if (o.alpha[i] > 2.) { o.alpha[i] = 2; }
if (o.alpha[i] < -2.) { o.alpha[i] = -2; }
base.learn(ec, i);
}
else
{
base.predict(ec, i);
if (v_partial_sum <= stopping_point) { final_prediction += ec.pred.scalar * o.alpha[i]; }
else
{
// stopping at learner i
break;
}
v_partial_sum += o.v[i];
}
}
// normalize v vector in training
if (is_learn)
{
for (int i = 0; i < o.N; i++)
{
if (v_normalization) { o.v[i] /= v_normalization; }
}
}
ec.weight = u;
ec.partial_prediction = final_prediction;
ec.pred.scalar = VW::math::sign(final_prediction);
if (ld.label == ec.pred.scalar) { ec.loss = 0.; }
else { ec.loss = ec.weight; }
}
void save_load_sampling(boosting& o, VW::io_buf& model_file, bool read, bool text)
{
if (model_file.num_files() == 0) { return; }
std::stringstream os;
os << "boosts " << o.N << endl;
VW::details::bin_text_read_write_fixed(model_file, reinterpret_cast<char*>(&(o.N)), sizeof(o.N), read, os, text);
if (read)
{
o.alpha.resize(o.N);
o.v.resize(o.N);
}
for (int i = 0; i < o.N; i++)
{
if (read)
{
float f;
model_file.bin_read_fixed(reinterpret_cast<char*>(&f), sizeof(f));
o.alpha[i] = f;
}
else
{
std::stringstream os2;
os2 << "alpha " << o.alpha[i] << endl;
VW::details::bin_text_write_fixed(
model_file, reinterpret_cast<char*>(&(o.alpha[i])), sizeof(o.alpha[i]), os2, text);
}
}
for (int i = 0; i < o.N; i++)
{
if (read)
{
float f;
model_file.bin_read_fixed(reinterpret_cast<char*>(&f), sizeof(f));
o.v[i] = f;
}
else
{
std::stringstream os2;
os2 << "v " << o.v[i] << endl;
VW::details::bin_text_write_fixed(model_file, reinterpret_cast<char*>(&(o.v[i])), sizeof(o.v[i]), os2, text);
}
}
// avoid making syscalls multiple times
fmt::memory_buffer buffer;
if (read) { fmt::format_to(std::back_inserter(buffer), "Loading alpha and v: \n"); }
else
{
fmt::format_to(std::back_inserter(buffer), "Saving alpha and v, current weighted_examples = {}\n",
o.all->sd->weighted_labeled_examples + o.all->sd->weighted_unlabeled_examples);
}
for (int i = 0; i < o.N; i++) { fmt::format_to(std::back_inserter(buffer), "{0} {1}\n", o.alpha[i], o.v[i]); }
o.logger.err_info("{}", fmt::to_string(buffer));
}
void save_load(boosting& o, VW::io_buf& model_file, bool read, bool text)
{
if (model_file.num_files() == 0) { return; }
std::stringstream os;
os << "boosts " << o.N << endl;
VW::details::bin_text_read_write_fixed(model_file, reinterpret_cast<char*>(&(o.N)), sizeof(o.N), read, os, text);
if (read) { o.alpha.resize(o.N); }
for (int i = 0; i < o.N; i++)
{
if (read)
{
float f;
model_file.bin_read_fixed(reinterpret_cast<char*>(&f), sizeof(f));
o.alpha[i] = f;
}
else
{
std::stringstream os2;
os2 << "alpha " << o.alpha[i] << endl;
VW::details::bin_text_write_fixed(
model_file, reinterpret_cast<char*>(&(o.alpha[i])), sizeof(o.alpha[i]), os2, text);
}
}
if (!o.all->output_config.quiet)
{
// avoid making syscalls multiple times
fmt::memory_buffer buffer;
if (read) { fmt::format_to(std::back_inserter(buffer), "Loading alpha: \n"); }
else
{
fmt::format_to(
std::back_inserter(buffer), "Saving alpha, current weighted_examples = {}\n", o.all->sd->weighted_examples());
}
for (int i = 0; i < o.N; i++) { fmt::format_to(std::back_inserter(buffer), "{}\n", o.alpha[i]); }
o.logger.err_info("{}", fmt::to_string(buffer));
}
}
void save_load_boosting_noop(boosting&, VW::io_buf&, bool, bool) {}
} // namespace
std::shared_ptr<VW::LEARNER::learner> VW::reductions::boosting_setup(VW::setup_base_i& stack_builder)
{
options_i& options = *stack_builder.get_options();
VW::workspace& all = *stack_builder.get_all_pointer();
auto data = VW::make_unique<boosting>(all.logger);
option_group_definition new_options("[Reduction] Boosting");
new_options.add(make_option("boosting", data->N).keep().necessary().help("Online boosting with <N> weak learners"))
.add(make_option("gamma", data->gamma)
.default_value(0.1f)
.help("Weak learner's edge (=0.1), used only by online BBM"))
.add(
make_option("alg", data->alg)
.keep()
.default_value("BBM")
.one_of({"BBM", "logistic", "adaptive"})
.help("Specify the boosting algorithm: BBM (default), logistic (AdaBoost.OL.W), adaptive (AdaBoost.OL)"));
if (!options.add_parse_and_check_necessary(new_options)) { return nullptr; }
// Description of options:
// "BBM" implements online BBM (Algorithm 1 in BLK'15)
// "logistic" implements AdaBoost.OL.W (importance weighted version
// of Algorithm 2 in BLK'15)
// "adaptive" implements AdaBoost.OL (Algorithm 2 in BLK'15,
// using sampling rather than importance weighting)
all.logger.err_info("Number of weak learners = {}", data->N);
all.logger.err_info("Gamma = {}", data->gamma);
data->C = std::vector<std::vector<int64_t> >(data->N, std::vector<int64_t>(data->N, -1));
data->t = 0;
data->all = &all;
data->random_state = all.get_random_state();
data->alpha = std::vector<float>(data->N, 0);
data->v = std::vector<float>(data->N, 1);
size_t feature_width = data->N;
std::string name_addition;
void (*learn_ptr)(boosting&, VW::LEARNER::learner&, VW::example&);
void (*pred_ptr)(boosting&, VW::LEARNER::learner&, VW::example&);
void (*save_load_fn)(boosting&, io_buf&, bool, bool);
if (data->alg == "BBM")
{
name_addition = "";
learn_ptr = predict_or_learn<true>;
pred_ptr = predict_or_learn<false>;
save_load_fn = save_load_boosting_noop;
}
else if (data->alg == "logistic")
{
name_addition = "-logistic";
learn_ptr = predict_or_learn_logistic<true>;
pred_ptr = predict_or_learn_logistic<false>;
save_load_fn = save_load;
}
else if (data->alg == "adaptive")
{
name_addition = "-adaptive";
learn_ptr = predict_or_learn_adaptive<true>;
pred_ptr = predict_or_learn_adaptive<false>;
save_load_fn = save_load_sampling;
}
else { THROW("Unrecognized boosting algorithm: \'" << data->alg << "\'."); }
auto l = make_reduction_learner(std::move(data), require_singleline(stack_builder.setup_base_learner(feature_width)),
learn_ptr, pred_ptr, stack_builder.get_setupfn_name(boosting_setup) + name_addition)
.set_feature_width(feature_width)
.set_input_prediction_type(VW::prediction_type_t::SCALAR)
.set_output_prediction_type(VW::prediction_type_t::SCALAR)
.set_input_label_type(VW::label_type_t::SIMPLE)
.set_output_label_type(VW::label_type_t::SIMPLE)
.set_save_load(save_load_fn)
.set_output_example_prediction(VW::details::output_example_prediction_simple_label<boosting>)
.set_update_stats(VW::details::update_stats_simple_label<boosting>)
.set_print_update(VW::details::print_update_simple_label<boosting>)
.build();
return l;
}