52 for (int64_t d = 1; d <= k; ++d)
67 std::vector<std::vector<int64_t> >
C;
76 template <
bool is_learn>
81 float final_prediction = 0;
89 for (
int i = 0; i < o.
N; i++)
93 float k = floorf((
float)(o.
N - i - s) / 2);
95 if (o.
N - (i + 1) < 0)
97 else if (k > o.
N - (i + 1))
101 else if (o.
C[o.
N - (i + 1)][(int64_t)k] != -1)
102 c = o.
C[o.
N - (i + 1)][(int64_t)k];
105 c =
choose(o.
N - (i + 1), (int64_t)k);
106 o.
C[o.
N - (i + 1)][(int64_t)k] =
c;
109 float w = c * (float)pow((
double)(0.5 + o.
gamma), (
double)k) *
110 (
float)pow((
double)0.5 - o.
gamma, (
double)(o.
N - (i + 1) - k));
144 template <
bool is_learn>
149 float final_prediction = 0;
156 float eta = 4.f / sqrtf((
float)o.
t);
158 for (
int i = 0; i < o.
N; i++)
180 if (o.
alpha[i] < -2.)
202 template <
bool is_learn>
207 float final_prediction = 0, partial_prediction = 0;
210 float v_normalization = 0, v_partial_sum = 0;
215 float eta = 4.f / (float)sqrtf((
float)o.
t);
217 float stopping_point = o.
_random_state->get_and_update_random();
219 for (
int i = 0; i < o.
N; i++)
234 if (v_partial_sum <= stopping_point)
241 v_partial_sum += o.
v[i];
244 if (ld.
label * partial_prediction < 0)
248 v_normalization += o.
v[i];
254 if (o.
alpha[i] < -2.)
262 if (v_partial_sum <= stopping_point)
271 v_partial_sum += o.
v[i];
278 for (
int i = 0; i < o.
N; i++)
281 o.
v[i] /= v_normalization;
299 std::stringstream os;
300 os <<
"boosts " << o.
N << endl;
309 for (
int i = 0; i < o.
N; i++)
318 std::stringstream os2;
319 os2 <<
"alpha " << o.
alpha[i] << endl;
323 for (
int i = 0; i < o.
N; i++)
332 std::stringstream os2;
333 os2 <<
"v " << o.
v[i] << endl;
339 cerr <<
"Loading alpha and v: " << endl;
343 cerr <<
"Saving alpha and v, current weighted_examples = " 346 for (
int i = 0; i < o.
N; i++)
348 cerr << o.
alpha[i] <<
" " << o.
v[i] << endl;
363 std::stringstream os;
364 os <<
"boosts " << o.
N << endl;
370 for (
int i = 0; i < o.
N; i++)
379 std::stringstream os2;
380 os2 <<
"alpha " << o.
alpha[i] << endl;
387 cerr <<
"Loading alpha: " << endl;
390 for (
int i = 0; i < o.
N; i++) cerr << o.
alpha[i] <<
" " << endl;
400 new_options.add(
make_option(
"boosting", data->N).keep().help(
"Online boosting with <N> weak learners"))
403 .help(
"weak learner's edge (=0.1), used only by online BBM"))
407 .default_value(
"BBM")
408 .help(
"specify the boosting algorithm: BBM (default), logistic (AdaBoost.OL.W), adaptive (AdaBoost.OL)"));
422 cerr <<
"Number of weak learners = " << data->N << endl;
424 cerr <<
"Gamma = " << data->gamma << endl;
426 data->C = std::vector<std::vector<int64_t> >(data->N, std::vector<int64_t>(data->N, -1));
430 data->alpha = std::vector<float>(data->N, 0);
431 data->v = std::vector<float>(data->N, 1);
434 if (data->alg ==
"BBM")
435 l = &init_learner<boosting, example>(
437 else if (data->alg ==
"logistic")
439 l = &init_learner<boosting, example>(data,
as_singleline(
setup_base(options, all)), predict_or_learn_logistic<true>,
440 predict_or_learn_logistic<false>, data->N);
443 else if (data->alg ==
"adaptive")
445 l = &init_learner<boosting, example>(data,
as_singleline(
setup_base(options, all)), predict_or_learn_adaptive<true>,
446 predict_or_learn_adaptive<false>, data->N);
450 THROW(
"Unrecognized boosting algorithm: \'" << data->alg <<
"\' Bailing!");
void predict(E &ec, size_t i=0)
double weighted_unlabeled_examples
void output_and_account_example(vw &all, active &a, example &ec)
void predict_or_learn(boosting &o, LEARNER::single_learner &base, example &ec)
base_learner * make_base(learner< T, E > &base)
LEARNER::base_learner * boosting_setup(options_i &options, vw &all)
virtual void add_and_parse(const option_group_definition &group)=0
void set_save_load(void(*sl)(T &, io_buf &, bool, bool))
void predict_or_learn_logistic(boosting &o, LEARNER::single_learner &base, example &ec)
void save_load(boosting &o, io_buf &model_file, bool read, bool text)
std::shared_ptr< rand_state > get_random_state()
std::unique_ptr< T, free_fn > free_ptr
single_learner * as_singleline(learner< T, E > *l)
size_t bin_text_write_fixed(io_buf &io, char *data, size_t len, std::stringstream &msg, bool text)
size_t bin_read_fixed(char *data, size_t len, const char *read_message)
void return_example(vw &all, boosting &, example &ec)
void predict_or_learn_adaptive(boosting &o, LEARNER::single_learner &base, example &ec)
virtual bool was_supplied(const std::string &key)=0
std::vector< float > alpha
std::shared_ptr< rand_state > _random_state
std::vector< std::vector< int64_t > > C
void finish_example(vw &, example &)
int64_t choose(int64_t n, int64_t k)
int add(svm_params ¶ms, svm_example *fec)
void save_load_sampling(boosting &o, io_buf &model_file, bool read, bool text)
typed_option< T > make_option(std::string name, T &location)
double weighted_labeled_examples
LEARNER::base_learner * setup_base(options_i &options, vw &all)
void learn(E &ec, size_t i=0)
double weighted_examples()
size_t bin_text_read_write_fixed(io_buf &io, char *data, size_t len, const char *read_message, bool read, std::stringstream &msg, bool text)