32 template <
bool is_learn>
34 float& partial_prediction)
38 ec.
weight = (cost == FLT_MAX) ? 0.
f : 1.
f;
40 base.
learn(ec, i - 1);
54 #define DO_MULTIPREDICT true 56 template <
bool is_learn>
61 uint32_t prediction = 1;
62 float score = FLT_MAX;
65 if (!ld.
costs.empty())
67 for (
auto& cl : ld.
costs)
68 inner_loop<is_learn>(base, ec, cl.class_index, cl.x, prediction, score, cl.
partial_prediction);
69 ec.partial_prediction = score;
73 ec.
l.
simple = {FLT_MAX, 0.f, 0.f};
86 for (uint32_t i = 1; i <= c.
num_classes; i++) inner_loop<false>(base, ec, i, FLT_MAX, prediction, score, temp);
90 uint64_t second_best = 0;
91 float second_best_cost = FLT_MAX;
97 second_best_cost = val;
101 if (second_best_cost < FLT_MAX)
119 auto c = scoped_calloc_or_throw<csoaa>();
121 new_options.add(
make_option(
"csoaa",
c->num_classes).keep().help(
"One-against-all multiclass with <k> costs"));
127 c->pred = calloc_or_throw<polyprediction>(
c->num_classes);
175 for (
auto const& cost : costs)
176 if ((cost.class_index != 0) || (cost.x <= 0.))
186 for (
size_t i = 1; i < ec_seq.size(); i++)
188 THROW(
"error: mixed label definition and examples in ldf data!");
202 costs[0]->wap_value = 0.;
203 for (
size_t i = 1; i < costs.size(); i++)
204 costs[i]->wap_value = costs[i - 1]->wap_value + (costs[i]->x - costs[i - 1]->x) / (float)i;
221 GD::foreach_feature<example&, uint64_t, subtract_feature>(all, *ecsub, *ec);
231 std::cerr <<
"internal error (bug): trying to unsubtract_example, but there are no namespaces!" << std::endl;
238 <<
"internal error (bug): trying to unsubtract_example, but either it wasn't added, or something was added " 239 "after and not removed!" 256 simple_label.
label = FLT_MAX;
278 for (
const auto& ec : ec_seq)
281 assert(ec->l.cs.costs.size() == 1);
286 data.
all->
trace_message <<
"warning: ldf example has mix of train/test data; assuming test" << std::endl;
294 size_t K = ec_seq.size();
295 std::vector<COST_SENSITIVE::wclass*> all_costs;
299 for (
size_t k1 = 0; k1 < K; k1++)
308 if (costs1[0].class_index == (uint32_t)-1)
313 for (
size_t k2 = k1 + 1; k2 < K; k2++)
318 if (costs2[0].class_index == (uint32_t)-1)
320 float value_diff = fabs(costs2[0].wap_value - costs1[0].wap_value);
322 if (value_diff < 1e-6)
329 simple_label.
label = (costs1[0].x < costs2[0].x) ? -1.0
f : 1.0
f;
330 float old_weight = ec1->
weight;
346 ec1->
l.
cs = save_cs_label;
353 float min_cost = FLT_MAX;
354 float max_cost = -FLT_MAX;
356 for (
const auto&
example : ec_seq)
359 if (ec_cost < min_cost)
361 if (ec_cost > max_cost)
365 for (
const auto& ec : ec_seq)
368 label save_cs_label = ec->l.cs;
375 float old_weight = ec->weight;
377 simple_label.
label = costs[0].x;
380 if (costs[0].x <= min_cost)
382 simple_label.
label = -1.;
383 ec->weight = old_weight * (max_cost - min_cost);
387 simple_label.
label = 1.;
388 ec->weight = old_weight * (costs[0].x - min_cost);
395 uint64_t old_offset = ec->ft_offset;
398 ec->ft_offset = old_offset;
400 ec->weight = old_weight;
403 ec->l.cs = save_cs_label;
404 ec->partial_prediction = costs[0].partial_prediction;
419 template <
bool is_learn>
422 if (ec_seq_all.empty())
425 data.
ft_offset = ec_seq_all[0]->ft_offset;
437 THROW(
"error: label definition encountered in data block");
441 uint32_t K = (uint32_t)ec_seq.size();
445 uint32_t predicted_K = 0;
450 for (uint32_t k = 0; k < K; k++)
465 float min_score = FLT_MAX;
466 for (uint32_t k = 0; k < K; k++)
479 if (is_learn && !isTest)
490 for (
size_t k = 0; k < K; k++)
493 ec_seq[0]->pred.a_s.push_back(data.
a_s[k]);
499 for (
size_t k = 0; k < K; k++)
501 if (k == predicted_K)
502 ec_seq[k]->pred.multiclass = ec_seq[k]->l.cs.costs[0].class_index;
504 ec_seq[k]->pred.multiclass = 0;
512 for (
const auto&
example : ec_seq)
523 for (
const auto&
example : ec_seq)
539 std::cerr <<
"write error: " << strerror(errno) << std::endl;
557 uint32_t predicted_class;
563 uint32_t predicted_K = 0;
564 float min_score = FLT_MAX;
565 for (
size_t k = 0; k < ec_seq->size(); k++)
571 predicted_K = (uint32_t)k;
574 predicted_class = (*ec_seq)[predicted_K]->l.cs.costs[0].class_index;
581 for (
auto const& cost : costs)
585 if (predicted_class == cost.class_index)
601 std::string outputString;
602 std::stringstream outputStringStream(outputString);
603 for (
size_t i = 0; i < costs.
size(); i++)
606 outputStringStream <<
' ';
607 outputStringStream << costs[i].class_index <<
':' << costs[i].partial_prediction;
638 if (preds[0].
action == idx)
640 loss = ex->l.cs.costs[0].x;
654 std::string outputString;
655 std::stringstream outputStringStream(outputString);
656 for (
size_t i = 0; i < costs.
size(); i++)
659 outputStringStream <<
' ';
660 outputStringStream << costs[i].class_index <<
':' << costs[i].partial_prediction;
671 size_t K = ec_seq.size();
680 bool hit_loss =
false;
694 float min_cost = FLT_MAX;
695 size_t correct_class_k = 0;
697 for (
size_t k = 0; k < K; k++)
699 float ec_cost = ec_seq[k]->l.cs.costs[0].x;
700 if (ec_cost < min_cost)
707 float multiclass_log_loss = 999;
708 float correct_class_prob = ec_seq[correct_class_k]->pred.prob;
709 if (correct_class_prob > 0)
710 multiclass_log_loss = -log(correct_class_prob);
745 for (
auto const& cost : costs)
747 const auto lab = (size_t)cost.x;
770 for (; i < ec_seq_all.size(); i++)
775 for (
size_t j = i; j < ec_seq_all.size(); j++) ret.push_back(ec_seq_all[j]);
789 auto ld = scoped_calloc_or_throw<ldf>();
791 std::string csoaa_ldf;
792 std::string ldf_override;
798 .help(
"Use one-against-all multiclass learning with label dependent features."));
799 csldf_outer_options.
add(
801 .help(
"Override singleline or multiline from csoaa_ldf or wap_ldf, eg if stored in file"));
802 csldf_outer_options.
add(
make_option(
"csoaa_rank", ld->rank).keep().help(
"Return actions sorted by score order"));
803 csldf_outer_options.
add(
804 make_option(
"probabilities", ld->is_probabilities).keep().help(
"predict probabilites of all classes"));
809 .help(
"Use weighted all-pairs multiclass learning with label dependent features. " 810 "Specify singleline or multiline."));
823 ld->first_pass =
true;
835 ldf_arg = ldf_override;
842 ld->treat_as_classifier =
false;
843 if (ldf_arg ==
"multiline" || ldf_arg ==
"m")
844 ld->treat_as_classifier =
false;
845 else if (ldf_arg ==
"multiline-classifier" || ldf_arg ==
"mc")
846 ld->treat_as_classifier =
true;
850 THROW(
"ldf requires either m/multiline or mc/multiline-classifier");
851 if ((ldf_arg ==
"singleline" || ldf_arg ==
"s") || (ldf_arg ==
"singleline-classifier" || ldf_arg ==
"sc"))
853 "ldf requires either m/multiline or mc/multiline-classifier. s/sc/singleline/singleline-classifier is no " 857 if (ld->is_probabilities)
861 if (loss_function_type !=
"logistic")
862 all.
trace_message <<
"WARNING: --probabilities should be used only with --loss_function=logistic" << std::endl;
863 if (!ld->treat_as_classifier)
864 all.
trace_message <<
"WARNING: --probabilities should be used with --csoaa_ldf=mc (or --oaa)" << std::endl;
871 ld->label_features.get(1, 94717244);
876 else if (ld->is_probabilities)
881 ld->read_example_this_loop = 0;
883 do_actual_learning<false>, 1, pred_type);
void do_actual_learning_wap(ldf &data, single_learner &base, multi_ex &ec_seq)
bool report_multiclass_log_loss
constexpr unsigned char wap_ldf_namespace
void unsubtract_example(example *ec)
v_array< namespace_index > indices
label_parser simple_label
ACTION_SCORE::action_scores a_s
void predict(E &ec, size_t i=0)
LEARNER::base_learner * cost_sensitive
base_learner * csoaa_setup(options_i &options, vw &all)
void(* delete_prediction)(void *)
void subtract_example(vw &all, example *ec, example *ecsub)
void do_actual_learning_oaa(ldf &data, single_learner &base, multi_ex &ec_seq)
void global_print_newline(vw &all)
VW::config::options_i * options
int score_comp(const void *p1, const void *p2)
double weighted_unlabeled_examples
static ssize_t write_file_or_socket(int f, const void *buf, size_t nbytes)
void output_example_seq(vw &all, multi_ex &ec_seq)
void finish_multiline_example(vw &all, cbify &, multi_ex &ec_seq)
void set_label_features(label_feature_map &lfm, size_t lab, features &fs)
v_array< feature_index > indicies
label_type::label_type_t label_type
bool(* test_label)(void *)
v_array< int > final_prediction_sink
the core definition of a set of features.
double holdout_multiclass_log_loss
size_t read_example_this_loop
base_learner * make_base(learner< T, E > &base)
void compute_wap_values(std::vector< COST_SENSITIVE::wclass *> costs)
void predict_or_learn(csoaa &c, single_learner &base, example &ec)
v_array< feature_value > values
virtual void add_and_parse(const option_group_definition &group)=0
float loss(cbify &data, uint32_t label, uint32_t final_prediction)
void finish_example(vw &all, csoaa &, example &ec)
void add_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
#define add_passthrough_feature(ec, i, x)
int example_is_newline(example const &ec)
bool size_t_eq(const size_t &a, const size_t &b)
bool ec_is_label_definition(example &ec)
double sum_loss_since_last_dump
std::array< features, NUM_NAMESPACES > feature_space
single_learner * as_singleline(learner< T, E > *l)
void do_actual_learning(ldf &data, single_learner &base, multi_ex &ec_seq_all)
double multiclass_log_loss
bool test_ldf_sequence(ldf &data, multi_ex &ec_seq)
void set_finish_example(void(*f)(vw &all, T &, E &))
learner< T, E > & init_learner(free_ptr< T > &dat, L *base, void(*learn)(T &, L &, E &), void(*predict)(T &, L &, E &), size_t ws, prediction_type::prediction_type_t pred_type)
void delete_action_scores(void *v)
void push_back(const T &new_ele)
void end_pass(example &ec, vw &all)
void finish_example(vw &all, example &ec)
void del_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
virtual bool was_supplied(const std::string &key)=0
base_learner * csldf_setup(options_i &options, vw &all)
virtual std::string getType()=0
constexpr uint64_t constant
v_array< action_scores > stored_preds
void output_rank_example(vw &all, example &head_ec, bool &hit_loss, multi_ex *ec_seq)
void(* print_text)(int, std::string, v_array< char >)
void finish_example(vw &, example &)
void process_label(ldf &data, example *ec)
void inner_loop(cs_active &cs_a, single_learner &base, example &ec, uint32_t i, float cost, uint32_t &prediction, float &score, float &partial_prediction, bool query_this_label, bool &query_needed)
option_group_definition & add(T &&op)
std::vector< example * > multi_ex
bool cmp_wclass_ptr(const COST_SENSITIVE::wclass *a, const COST_SENSITIVE::wclass *b)
typed_option< T > make_option(std::string name, T &location)
multi_ex process_labels(ldf &data, const multi_ex &ec_seq_all)
bool ec_seq_is_label_definition(multi_ex &ec_seq)
void set_end_pass(void(*f)(T &))
void output_example(vw &all, example &ec, bool &hit_loss, multi_ex *ec_seq, ldf &data)
LabelDict::label_feature_map label_features
void multipredict(E &ec, size_t lo, size_t count, polyprediction *pred, bool finalize_predictions)
double weighted_labeled_examples
LEARNER::base_learner * setup_base(options_i &options, vw &all)
void print_update(vw &all, bool is_test, example &ec, multi_ex *ec_seq, bool action_scores, uint32_t prediction)
void learn(E &ec, size_t i=0)
void print_action_score(int f, v_array< action_score > &a_s, v_array< char > &tag)
bool emptylines_separate_examples
void(* print)(int, float, float, v_array< char >)
void subtract_feature(example &ec, float feature_value_x, uint64_t weight_index)
void free_label_features(label_feature_map &lfm)
bool ec_seq_has_label_definition(multi_ex &ec_seq)
void make_single_prediction(ldf &data, single_learner &base, example &ec)