Vowpal Wabbit
Classes | Functions
CSOAA Namespace Reference

Classes

struct  csoaa
 
struct  ldf
 

Functions

template<bool is_learn>
void inner_loop (single_learner &base, example &ec, uint32_t i, float cost, uint32_t &prediction, float &score, float &partial_prediction)
 
template<bool is_learn>
void predict_or_learn (csoaa &c, single_learner &base, example &ec)
 
void finish_example (vw &all, csoaa &, example &ec)
 
base_learnercsoaa_setup (options_i &options, vw &all)
 
bool ec_is_label_definition (example &ec)
 
bool ec_seq_is_label_definition (multi_ex &ec_seq)
 
bool ec_seq_has_label_definition (multi_ex &ec_seq)
 
bool cmp_wclass_ptr (const COST_SENSITIVE::wclass *a, const COST_SENSITIVE::wclass *b)
 
void compute_wap_values (std::vector< COST_SENSITIVE::wclass *> costs)
 
void subtract_feature (example &ec, float feature_value_x, uint64_t weight_index)
 
void subtract_example (vw &all, example *ec, example *ecsub)
 
void unsubtract_example (example *ec)
 
void make_single_prediction (ldf &data, single_learner &base, example &ec)
 
bool test_ldf_sequence (ldf &data, multi_ex &ec_seq)
 
void do_actual_learning_wap (ldf &data, single_learner &base, multi_ex &ec_seq)
 
void do_actual_learning_oaa (ldf &data, single_learner &base, multi_ex &ec_seq)
 
multi_ex process_labels (ldf &data, const multi_ex &ec_seq_all)
 
template<bool is_learn>
void do_actual_learning (ldf &data, single_learner &base, multi_ex &ec_seq_all)
 
void global_print_newline (vw &all)
 
void output_example (vw &all, example &ec, bool &hit_loss, multi_ex *ec_seq, ldf &data)
 
void output_rank_example (vw &all, example &head_ec, bool &hit_loss, multi_ex *ec_seq)
 
void output_example_seq (vw &all, ldf &data, multi_ex &ec_seq)
 
void end_pass (ldf &data)
 
void finish_multiline_example (vw &all, ldf &data, multi_ex &ec_seq)
 
void process_label (ldf &data, example *ec)
 
base_learnercsldf_setup (options_i &options, vw &all)
 

Function Documentation

◆ cmp_wclass_ptr()

bool CSOAA::cmp_wclass_ptr ( const COST_SENSITIVE::wclass a,
const COST_SENSITIVE::wclass b 
)
inline

Definition at line 197 of file csoaa.cc.

References COST_SENSITIVE::wclass::x.

Referenced by compute_wap_values().

197 { return a->x < b->x; }

◆ compute_wap_values()

void CSOAA::compute_wap_values ( std::vector< COST_SENSITIVE::wclass *>  costs)

Definition at line 199 of file csoaa.cc.

References cmp_wclass_ptr().

Referenced by do_actual_learning_wap().

200 {
201  std::sort(costs.begin(), costs.end(), cmp_wclass_ptr);
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;
205 }
bool cmp_wclass_ptr(const COST_SENSITIVE::wclass *a, const COST_SENSITIVE::wclass *b)
Definition: csoaa.cc:197

◆ csldf_setup()

LEARNER::base_learner * CSOAA::csldf_setup ( options_i options,
vw all 
)

Definition at line 787 of file csoaa.cc.

References prediction_type::action_scores, VW::config::option_group_definition::add(), VW::config::options_i::add_and_parse(), LEARNER::as_singleline(), vw::cost_sensitive, label_type::cs, COST_SENSITIVE::cs_label, ACTION_SCORE::delete_action_scores(), vw::delete_prediction, parser::emptylines_separate_examples, LEARNER::end_pass(), finish_multiline_example(), loss_function::getType(), LEARNER::init_learner(), vw::label_type, vw::loss, parser::lp, LEARNER::make_base(), VW::config::make_option(), prediction_type::multiclass, vw::options, vw::p, prediction_type::prob, shared_data::report_multiclass_log_loss, vw::sd, LEARNER::learner< T, E >::set_end_pass(), LEARNER::learner< T, E >::set_finish_example(), setup_base(), LabelDict::size_t_eq(), THROW, vw::trace_message, vw::training, and VW::config::options_i::was_supplied().

Referenced by parse_reductions().

788 {
789  auto ld = scoped_calloc_or_throw<ldf>();
790 
791  std::string csoaa_ldf;
792  std::string ldf_override;
793  std::string wap_ldf;
794 
795  option_group_definition csldf_outer_options("Cost Sensitive One Against All with Label Dependent Features");
796  csldf_outer_options.add(make_option("csoaa_ldf", csoaa_ldf)
797  .keep()
798  .help("Use one-against-all multiclass learning with label dependent features."));
799  csldf_outer_options.add(
800  make_option("ldf_override", ldf_override)
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"));
805 
806  option_group_definition csldf_inner_options("Cost Sensitive One Against All with Label Dependent Features");
807  csldf_inner_options.add(make_option("wap_ldf", wap_ldf)
808  .keep()
809  .help("Use weighted all-pairs multiclass learning with label dependent features. "
810  "Specify singleline or multiline."));
811 
812  options.add_and_parse(csldf_outer_options);
813  if (!options.was_supplied("csoaa_ldf"))
814  {
815  options.add_and_parse(csldf_inner_options);
816  if (!options.was_supplied("wap_ldf"))
817  {
818  return nullptr;
819  }
820  }
821 
822  ld->all = &all;
823  ld->first_pass = true;
824 
825  std::string ldf_arg;
826 
827  if (options.was_supplied("csoaa_ldf"))
828  ldf_arg = csoaa_ldf;
829  else
830  {
831  ldf_arg = wap_ldf;
832  ld->is_wap = true;
833  }
834  if (options.was_supplied("ldf_override"))
835  ldf_arg = ldf_override;
836  if (ld->rank)
838 
841 
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;
847  else
848  {
849  if (all.training)
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"))
852  THROW(
853  "ldf requires either m/multiline or mc/multiline-classifier. s/sc/singleline/singleline-classifier is no "
854  "longer supported");
855  }
856 
857  if (ld->is_probabilities)
858  {
859  all.sd->report_multiclass_log_loss = true;
860  auto loss_function_type = all.loss->getType();
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;
865  }
866 
867  all.p->emptylines_separate_examples = true; // TODO: check this to be sure!!! !ld->is_singleline;
868 
869  features fs;
870  ld->label_features.init(256, fs, LabelDict::size_t_eq);
871  ld->label_features.get(1, 94717244); // TODO: figure this out
873 
874  if (ld->rank)
875  pred_type = prediction_type::action_scores;
876  else if (ld->is_probabilities)
877  pred_type = prediction_type::prob;
878  else
879  pred_type = prediction_type::multiclass;
880 
881  ld->read_example_this_loop = 0;
882  learner<ldf, multi_ex>& l = init_learner(ld, as_singleline(setup_base(*all.options, all)), do_actual_learning<true>,
883  do_actual_learning<false>, 1, pred_type);
886  all.cost_sensitive = make_base(l);
887  return all.cost_sensitive;
888 }
bool report_multiclass_log_loss
Definition: global_data.h:166
loss_function * loss
Definition: global_data.h:523
LEARNER::base_learner * cost_sensitive
Definition: global_data.h:385
void(* delete_prediction)(void *)
Definition: global_data.h:485
VW::config::options_i * options
Definition: global_data.h:428
label_parser cs_label
void finish_multiline_example(vw &all, cbify &, multi_ex &ec_seq)
Definition: cbify.cc:373
label_type::label_type_t label_type
Definition: global_data.h:550
the core definition of a set of features.
base_learner * make_base(learner< T, E > &base)
Definition: learner.h:462
virtual void add_and_parse(const option_group_definition &group)=0
bool size_t_eq(const size_t &a, const size_t &b)
bool training
Definition: global_data.h:488
parser * p
Definition: global_data.h:377
single_learner * as_singleline(learner< T, E > *l)
Definition: learner.h:476
void end_pass(ldf &data)
Definition: csoaa.cc:725
void set_finish_example(void(*f)(vw &all, T &, E &))
Definition: learner.h:307
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)
Definition: learner.h:369
void delete_action_scores(void *v)
Definition: action_score.cc:29
shared_data * sd
Definition: global_data.h:375
vw_ostream trace_message
Definition: global_data.h:424
virtual bool was_supplied(const std::string &key)=0
virtual std::string getType()=0
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
void set_end_pass(void(*f)(T &))
Definition: learner.h:286
LEARNER::base_learner * setup_base(options_i &options, vw &all)
Definition: parse_args.cc:1222
#define THROW(args)
Definition: vw_exception.h:181
bool emptylines_separate_examples
Definition: parser.h:84
label_parser lp
Definition: parser.h:102

◆ csoaa_setup()

LEARNER::base_learner * CSOAA::csoaa_setup ( options_i options,
vw all 
)

Definition at line 117 of file csoaa.cc.

References VW::config::options_i::add_and_parse(), LEARNER::as_singleline(), c, vw::cost_sensitive, label_type::cs, COST_SENSITIVE::cs_label, finish_example(), LEARNER::init_learner(), vw::label_type, parser::lp, LEARNER::make_base(), VW::config::make_option(), prediction_type::multiclass, vw::options, vw::p, setup_base(), and VW::config::options_i::was_supplied().

Referenced by parse_reductions().

118 {
119  auto c = scoped_calloc_or_throw<csoaa>();
120  option_group_definition new_options("Cost Sensitive One Against All");
121  new_options.add(make_option("csoaa", c->num_classes).keep().help("One-against-all multiclass with <k> costs"));
122  options.add_and_parse(new_options);
123 
124  if (!options.was_supplied("csoaa"))
125  return nullptr;
126 
127  c->pred = calloc_or_throw<polyprediction>(c->num_classes);
128 
129  learner<csoaa, example>& l = init_learner(c, as_singleline(setup_base(*all.options, all)), predict_or_learn<true>,
130  predict_or_learn<false>, c->num_classes, prediction_type::multiclass);
131  all.p->lp = cs_label;
133 
134  l.set_finish_example(finish_example);
135  all.cost_sensitive = make_base(l);
136  return all.cost_sensitive;
137 }
LEARNER::base_learner * cost_sensitive
Definition: global_data.h:385
VW::config::options_i * options
Definition: global_data.h:428
label_parser cs_label
label_type::label_type_t label_type
Definition: global_data.h:550
base_learner * make_base(learner< T, E > &base)
Definition: learner.h:462
virtual void add_and_parse(const option_group_definition &group)=0
void finish_example(vw &all, csoaa &, example &ec)
Definition: csoaa.cc:115
parser * p
Definition: global_data.h:377
single_learner * as_singleline(learner< T, E > *l)
Definition: learner.h:476
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)
Definition: learner.h:369
virtual bool was_supplied(const std::string &key)=0
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
LEARNER::base_learner * setup_base(options_i &options, vw &all)
Definition: parse_args.cc:1222
constexpr uint64_t c
Definition: rand48.cc:12
label_parser lp
Definition: parser.h:102

◆ do_actual_learning()

template<bool is_learn>
void CSOAA::do_actual_learning ( ldf data,
single_learner base,
multi_ex ec_seq_all 
)

Definition at line 420 of file csoaa.cc.

References polyprediction::a_s, CSOAA::ldf::a_s, ACTION_SCORE::action_score::action, v_array< T >::begin(), v_array< T >::clear(), correctedExp, do_actual_learning_oaa(), do_actual_learning_wap(), ec_seq_has_label_definition(), CSOAA::ldf::ft_offset, CSOAA::ldf::is_probabilities, CSOAA::ldf::is_wap, make_single_prediction(), example::partial_prediction, example::pred, prediction_type::prob, polyprediction::prob, process_labels(), v_array< T >::push_back(), CSOAA::ldf::rank, ACTION_SCORE::action_score::score, ACTION_SCORE::score_comp(), v_array< T >::size(), CSOAA::ldf::stored_preds, test_ldf_sequence(), and THROW.

421 {
422  if (ec_seq_all.empty())
423  return; // nothing to do
424 
425  data.ft_offset = ec_seq_all[0]->ft_offset;
426 
427  // handle label definitions
428  auto ec_seq = process_labels(data, ec_seq_all);
429  if (ec_seq.empty())
430  return; // nothing more to do
431 
432  // Ensure there are no more labels
433  // (can be done in existing loops later but as a side effect learning
434  // will happen with bad example)
435  if (ec_seq_has_label_definition(ec_seq))
436  {
437  THROW("error: label definition encountered in data block");
438  }
439 
441  uint32_t K = (uint32_t)ec_seq.size();
442 
443  bool isTest = test_ldf_sequence(data, ec_seq);
445  uint32_t predicted_K = 0;
446  if (data.rank)
447  {
448  data.a_s.clear();
449  data.stored_preds.clear();
450  for (uint32_t k = 0; k < K; k++)
451  {
452  example* ec = ec_seq[k];
453  data.stored_preds.push_back(ec->pred.a_s);
454  make_single_prediction(data, base, *ec);
455  action_score s;
456  s.score = ec->partial_prediction;
457  s.action = k;
458  data.a_s.push_back(s);
459  }
460 
461  qsort((void*)data.a_s.begin(), data.a_s.size(), sizeof(action_score), score_comp);
462  }
463  else
464  {
465  float min_score = FLT_MAX;
466  for (uint32_t k = 0; k < K; k++)
467  {
468  example* ec = ec_seq[k];
469  make_single_prediction(data, base, *ec);
470  if (ec->partial_prediction < min_score)
471  {
472  min_score = ec->partial_prediction;
473  predicted_K = k;
474  }
475  }
476  }
477 
479  if (is_learn && !isTest)
480  {
481  if (data.is_wap)
482  do_actual_learning_wap(data, base, ec_seq);
483  else
484  do_actual_learning_oaa(data, base, ec_seq);
485  }
486 
487  if (data.rank)
488  {
489  data.stored_preds[0].clear();
490  for (size_t k = 0; k < K; k++)
491  {
492  ec_seq[k]->pred.a_s = data.stored_preds[k];
493  ec_seq[0]->pred.a_s.push_back(data.a_s[k]);
494  }
495  }
496  else
497  {
498  // Mark the predicted subexample with its class_index, all other with 0
499  for (size_t k = 0; k < K; k++)
500  {
501  if (k == predicted_K)
502  ec_seq[k]->pred.multiclass = ec_seq[k]->l.cs.costs[0].class_index;
503  else
504  ec_seq[k]->pred.multiclass = 0;
505  }
506  }
507 
509  if (data.is_probabilities)
510  {
511  float sum_prob = 0;
512  for (const auto& example : ec_seq)
513  {
514  // probability(correct_class) = 1 / (1+exp(-score)), where score is higher for better classes,
515  // but partial_prediction is lower for better classes (we are predicting the cost),
516  // so we need to take score = -partial_prediction,
517  // thus probability(correct_class) = 1 / (1+exp(-(-partial_prediction)))
518  float prob = 1.f / (1.f + correctedExp(example->partial_prediction));
519  example->pred.prob = prob;
520  sum_prob += prob;
521  }
522  // make sure that the probabilities sum up (exactly) to one
523  for (const auto& example : ec_seq)
524  {
525  example->pred.prob /= sum_prob;
526  }
527  }
528 }
void do_actual_learning_wap(ldf &data, single_learner &base, multi_ex &ec_seq)
Definition: csoaa.cc:292
#define correctedExp
Definition: correctedMath.h:27
ACTION_SCORE::action_scores a_s
Definition: example.h:47
void do_actual_learning_oaa(ldf &data, single_learner &base, multi_ex &ec_seq)
Definition: csoaa.cc:351
int score_comp(const void *p1, const void *p2)
Definition: action_score.h:56
float prob
Definition: example.h:51
float partial_prediction
Definition: example.h:68
bool test_ldf_sequence(ldf &data, multi_ex &ec_seq)
Definition: csoaa.cc:271
multi_ex process_labels(ldf &data, const multi_ex &ec_seq_all)
Definition: csoaa.cc:756
polyprediction pred
Definition: example.h:60
#define THROW(args)
Definition: vw_exception.h:181
bool ec_seq_has_label_definition(multi_ex &ec_seq)
Definition: csoaa.cc:192
void make_single_prediction(ldf &data, single_learner &base, example &ec)
Definition: csoaa.cc:251

◆ do_actual_learning_oaa()

void CSOAA::do_actual_learning_oaa ( ldf data,
single_learner base,
multi_ex ec_seq 
)

Definition at line 351 of file csoaa.cc.

References LabelDict::add_example_namespace_from_memory(), COST_SENSITIVE::label::costs, polylabel::cs, LabelDict::del_example_namespace_from_memory(), CSOAA::ldf::ft_offset, label_data::initial, example::l, label_data::label, CSOAA::ldf::label_features, LEARNER::learner< T, E >::learn(), process_labels(), simple_label, and CSOAA::ldf::treat_as_classifier.

Referenced by do_actual_learning().

352 {
353  float min_cost = FLT_MAX;
354  float max_cost = -FLT_MAX;
355 
356  for (const auto& example : ec_seq)
357  {
358  float ec_cost = example->l.cs.costs[0].x;
359  if (ec_cost < min_cost)
360  min_cost = ec_cost;
361  if (ec_cost > max_cost)
362  max_cost = ec_cost;
363  }
364 
365  for (const auto& ec : ec_seq)
366  {
367  // save original variables
368  label save_cs_label = ec->l.cs;
369  v_array<COST_SENSITIVE::wclass> costs = save_cs_label.costs;
370 
371  // build example for the base learner
373 
374  simple_label.initial = 0.;
375  float old_weight = ec->weight;
376  if (!data.treat_as_classifier) // treat like regression
377  simple_label.label = costs[0].x;
378  else // treat like classification
379  {
380  if (costs[0].x <= min_cost)
381  {
382  simple_label.label = -1.;
383  ec->weight = old_weight * (max_cost - min_cost);
384  }
385  else
386  {
387  simple_label.label = 1.;
388  ec->weight = old_weight * (costs[0].x - min_cost);
389  }
390  }
391  ec->l.simple = simple_label;
392 
393  // learn
394  LabelDict::add_example_namespace_from_memory(data.label_features, *ec, costs[0].class_index);
395  uint64_t old_offset = ec->ft_offset;
396  ec->ft_offset = data.ft_offset;
397  base.learn(*ec);
398  ec->ft_offset = old_offset;
399  LabelDict::del_example_namespace_from_memory(data.label_features, *ec, costs[0].class_index);
400  ec->weight = old_weight;
401 
402  // restore original cost-sensitive label, sum of importance weights and partial_prediction
403  ec->l.cs = save_cs_label;
404  ec->partial_prediction = costs[0].partial_prediction;
405  }
406 }
label_parser simple_label
float label
Definition: simple_label.h:14
void add_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
COST_SENSITIVE::label cs
Definition: example.h:30
void del_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
float initial
Definition: simple_label.h:16
polylabel l
Definition: example.h:57
void learn(E &ec, size_t i=0)
Definition: learner.h:160
v_array< wclass > costs

◆ do_actual_learning_wap()

void CSOAA::do_actual_learning_wap ( ldf data,
single_learner base,
multi_ex ec_seq 
)

Definition at line 292 of file csoaa.cc.

References LabelDict::add_example_namespace_from_memory(), CSOAA::ldf::all, compute_wap_values(), COST_SENSITIVE::label::costs, polylabel::cs, LabelDict::del_example_namespace_from_memory(), f, example_predict::ft_offset, CSOAA::ldf::ft_offset, label_data::initial, example::l, label_data::label, CSOAA::ldf::label_features, LEARNER::learner< T, E >::learn(), example::partial_prediction, polylabel::simple, simple_label, subtract_example(), unsubtract_example(), and example::weight.

Referenced by do_actual_learning().

293 {
294  size_t K = ec_seq.size();
295  std::vector<COST_SENSITIVE::wclass*> all_costs;
296  for (const auto& example : ec_seq) all_costs.push_back(&example->l.cs.costs[0]);
297  compute_wap_values(all_costs);
298 
299  for (size_t k1 = 0; k1 < K; k1++)
300  {
301  example* ec1 = ec_seq[k1];
302 
303  // save original variables
304  COST_SENSITIVE::label save_cs_label = ec1->l.cs;
306 
307  v_array<COST_SENSITIVE::wclass> costs1 = save_cs_label.costs;
308  if (costs1[0].class_index == (uint32_t)-1)
309  continue;
310 
311  LabelDict::add_example_namespace_from_memory(data.label_features, *ec1, costs1[0].class_index);
312 
313  for (size_t k2 = k1 + 1; k2 < K; k2++)
314  {
315  example* ec2 = ec_seq[k2];
317 
318  if (costs2[0].class_index == (uint32_t)-1)
319  continue;
320  float value_diff = fabs(costs2[0].wap_value - costs1[0].wap_value);
321  // float value_diff = fabs(costs2[0].x - costs1[0].x);
322  if (value_diff < 1e-6)
323  continue;
324 
325  LabelDict::add_example_namespace_from_memory(data.label_features, *ec2, costs2[0].class_index);
326 
327  // learn
328  simple_label.initial = 0.;
329  simple_label.label = (costs1[0].x < costs2[0].x) ? -1.0f : 1.0f;
330  float old_weight = ec1->weight;
331  ec1->weight = value_diff;
332  ec1->partial_prediction = 0.;
333  subtract_example(*data.all, ec1, ec2);
334  uint64_t old_offset = ec1->ft_offset;
335  ec1->ft_offset = data.ft_offset;
336  base.learn(*ec1);
337  ec1->ft_offset = old_offset;
338  ec1->weight = old_weight;
339  unsubtract_example(ec1);
340 
341  LabelDict::del_example_namespace_from_memory(data.label_features, *ec2, costs2[0].class_index);
342  }
343  LabelDict::del_example_namespace_from_memory(data.label_features, *ec1, costs1[0].class_index);
344 
345  // restore original cost-sensitive label, sum of importance weights
346  ec1->l.cs = save_cs_label;
347  // TODO: What about partial_prediction? See do_actual_learning_oaa.
348  }
349 }
void unsubtract_example(example *ec)
Definition: csoaa.cc:227
label_parser simple_label
void subtract_example(vw &all, example *ec, example *ecsub)
Definition: csoaa.cc:217
void compute_wap_values(std::vector< COST_SENSITIVE::wclass *> costs)
Definition: csoaa.cc:199
float partial_prediction
Definition: example.h:68
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
void add_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
COST_SENSITIVE::label cs
Definition: example.h:30
void del_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
float initial
Definition: simple_label.h:16
polylabel l
Definition: example.h:57
void learn(E &ec, size_t i=0)
Definition: learner.h:160
v_array< wclass > costs
float weight
Definition: example.h:62
float f
Definition: cache.cc:40

◆ ec_is_label_definition()

bool CSOAA::ec_is_label_definition ( example ec)

Definition at line 168 of file csoaa.cc.

References COST_SENSITIVE::label::costs, polylabel::cs, v_array< T >::empty(), example_predict::indices, and example::l.

Referenced by ec_seq_has_label_definition(), ec_seq_is_label_definition(), output_example(), output_rank_example(), and process_labels().

169 {
170  if (ec.indices.empty())
171  return false;
172  if (ec.indices[0] != 'l')
173  return false;
175  for (auto const& cost : costs)
176  if ((cost.class_index != 0) || (cost.x <= 0.))
177  return false;
178  return true;
179 }
v_array< namespace_index > indices
COST_SENSITIVE::label cs
Definition: example.h:30
polylabel l
Definition: example.h:57
bool empty() const
Definition: v_array.h:59
v_array< wclass > costs

◆ ec_seq_has_label_definition()

bool CSOAA::ec_seq_has_label_definition ( multi_ex ec_seq)

Definition at line 192 of file csoaa.cc.

References ec_is_label_definition().

Referenced by do_actual_learning().

193 {
194  return std::any_of(ec_seq.cbegin(), ec_seq.cend(), [](example* ec) { return ec_is_label_definition(*ec); });
195 }
bool ec_is_label_definition(example &ec)
Definition: csoaa.cc:168

◆ ec_seq_is_label_definition()

bool CSOAA::ec_seq_is_label_definition ( multi_ex ec_seq)

Definition at line 181 of file csoaa.cc.

References ec_is_label_definition(), and THROW.

Referenced by output_example_seq().

182 {
183  if (ec_seq.empty())
184  return false;
185  bool is_lab = ec_is_label_definition(*ec_seq[0]);
186  for (size_t i = 1; i < ec_seq.size(); i++)
187  if (is_lab != ec_is_label_definition(*ec_seq[i]))
188  THROW("error: mixed label definition and examples in ldf data!");
189  return is_lab;
190 }
bool ec_is_label_definition(example &ec)
Definition: csoaa.cc:168
#define THROW(args)
Definition: vw_exception.h:181

◆ end_pass()

void CSOAA::end_pass ( ldf data)

Definition at line 725 of file csoaa.cc.

References CSOAA::ldf::first_pass.

725 { data.first_pass = false; }

◆ finish_example()

void CSOAA::finish_example ( vw all,
csoaa ,
example ec 
)

Definition at line 115 of file csoaa.cc.

References COST_SENSITIVE::finish_example().

Referenced by csoaa_setup(), and finish_example().

115 { COST_SENSITIVE::finish_example(all, ec); }
void finish_example(vw &all, example &ec)

◆ finish_multiline_example()

void CSOAA::finish_multiline_example ( vw all,
ldf data,
multi_ex ec_seq 
)

Definition at line 727 of file csoaa.cc.

References VW::finish_example(), global_print_newline(), and output_example_seq().

728 {
729  if (!ec_seq.empty())
730  {
731  output_example_seq(all, data, ec_seq);
733  }
734  VW::finish_example(all, ec_seq);
735 }
void global_print_newline(vw &all)
Definition: csoaa.cc:530
void output_example_seq(vw &all, multi_ex &ec_seq)
Definition: cbify.cc:356
void finish_example(vw &, example &)
Definition: parser.cc:881

◆ global_print_newline()

void CSOAA::global_print_newline ( vw all)

Definition at line 530 of file csoaa.cc.

References f, vw::final_prediction_sink, and io_buf::write_file_or_socket().

Referenced by finish_multiline_example().

531 {
532  char temp[1];
533  temp[0] = '\n';
534  for (int f : all.final_prediction_sink)
535  {
536  ssize_t t;
537  t = io_buf::write_file_or_socket(f, temp, 1);
538  if (t != 1)
539  std::cerr << "write error: " << strerror(errno) << std::endl;
540  }
541 }
static ssize_t write_file_or_socket(int f, const void *buf, size_t nbytes)
Definition: io_buf.cc:140
v_array< int > final_prediction_sink
Definition: global_data.h:518
float f
Definition: cache.cc:40

◆ inner_loop()

template<bool is_learn>
void CSOAA::inner_loop ( single_learner base,
example ec,
uint32_t  i,
float  cost,
uint32_t &  prediction,
float &  score,
float &  partial_prediction 
)
inline

Definition at line 33 of file csoaa.cc.

References add_passthrough_feature, f, example::l, label_data::label, LEARNER::learner< T, E >::learn(), example::partial_prediction, LEARNER::learner< T, E >::predict(), polylabel::simple, and example::weight.

35 {
36  if (is_learn)
37  {
38  ec.weight = (cost == FLT_MAX) ? 0.f : 1.f;
39  ec.l.simple.label = cost;
40  base.learn(ec, i - 1);
41  }
42  else
43  base.predict(ec, i - 1);
44 
45  partial_prediction = ec.partial_prediction;
46  if (ec.partial_prediction < score || (ec.partial_prediction == score && i < prediction))
47  {
48  score = ec.partial_prediction;
49  prediction = i;
50  }
52 }
void predict(E &ec, size_t i=0)
Definition: learner.h:169
float partial_prediction
Definition: example.h:68
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
#define add_passthrough_feature(ec, i, x)
Definition: example.h:119
polylabel l
Definition: example.h:57
void learn(E &ec, size_t i=0)
Definition: learner.h:160
float weight
Definition: example.h:62
float f
Definition: cache.cc:40

◆ make_single_prediction()

void CSOAA::make_single_prediction ( ldf data,
single_learner base,
example ec 
)

Definition at line 251 of file csoaa.cc.

References LabelDict::add_example_namespace_from_memory(), COST_SENSITIVE::label::costs, polylabel::cs, LabelDict::del_example_namespace_from_memory(), example_predict::ft_offset, CSOAA::ldf::ft_offset, label_data::initial, example::l, label_data::label, CSOAA::ldf::label_features, example::partial_prediction, LEARNER::learner< T, E >::predict(), polylabel::simple, and simple_label.

Referenced by do_actual_learning().

252 {
253  COST_SENSITIVE::label ld = ec.l.cs;
255  simple_label.initial = 0.;
256  simple_label.label = FLT_MAX;
257 
258  LabelDict::add_example_namespace_from_memory(data.label_features, ec, ld.costs[0].class_index);
259 
260  ec.l.simple = simple_label;
261  uint64_t old_offset = ec.ft_offset;
262  ec.ft_offset = data.ft_offset;
263  base.predict(ec); // make a prediction
264  ec.ft_offset = old_offset;
265  ld.costs[0].partial_prediction = ec.partial_prediction;
266 
267  LabelDict::del_example_namespace_from_memory(data.label_features, ec, ld.costs[0].class_index);
268  ec.l.cs = ld;
269 }
label_parser simple_label
void predict(E &ec, size_t i=0)
Definition: learner.h:169
float partial_prediction
Definition: example.h:68
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
void add_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
COST_SENSITIVE::label cs
Definition: example.h:30
void del_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
float initial
Definition: simple_label.h:16
polylabel l
Definition: example.h:57
v_array< wclass > costs

◆ output_example()

void CSOAA::output_example ( vw all,
example ec,
bool &  hit_loss,
multi_ex ec_seq,
ldf data 
)

Definition at line 543 of file csoaa.cc.

References COST_SENSITIVE::label::costs, polylabel::cs, COST_SENSITIVE::cs_label, ec_is_label_definition(), example_is_newline(), vw::final_prediction_sink, CSOAA::ldf::is_probabilities, example::l, loss(), polyprediction::multiclass, example::num_features, example::partial_prediction, example::pred, vw::print, vw::print_text, COST_SENSITIVE::print_update(), polyprediction::prob, vw::raw_prediction, vw::sd, v_array< T >::size(), shared_data::sum_loss, shared_data::sum_loss_since_last_dump, example::tag, test_label(), and shared_data::total_features.

Referenced by output_example_seq().

544 {
545  label& ld = ec.l.cs;
547 
548  if (example_is_newline(ec))
549  return;
550  if (ec_is_label_definition(ec))
551  return;
552 
553  all.sd->total_features += ec.num_features;
554 
555  float loss = 0.;
556 
557  uint32_t predicted_class;
558  if (data.is_probabilities)
559  {
560  // predicted_K was already computed in do_actual_learning(),
561  // but we cannot store it in ec.pred union because we store ec.pred.prob there.
562  // So we must compute it again.
563  uint32_t predicted_K = 0;
564  float min_score = FLT_MAX;
565  for (size_t k = 0; k < ec_seq->size(); k++)
566  {
567  example* ec_k = (*ec_seq)[k];
568  if (ec_k->partial_prediction < min_score)
569  {
570  min_score = ec_k->partial_prediction;
571  predicted_K = (uint32_t)k;
572  }
573  }
574  predicted_class = (*ec_seq)[predicted_K]->l.cs.costs[0].class_index;
575  }
576  else
577  predicted_class = ec.pred.multiclass;
578 
580  {
581  for (auto const& cost : costs)
582  {
583  if (hit_loss)
584  break;
585  if (predicted_class == cost.class_index)
586  {
587  loss = cost.x;
588  hit_loss = true;
589  }
590  }
591 
592  all.sd->sum_loss += loss;
594  }
595 
596  for (int sink : all.final_prediction_sink)
597  all.print(sink, data.is_probabilities ? ec.pred.prob : (float)ec.pred.multiclass, 0, ec.tag);
598 
599  if (all.raw_prediction > 0)
600  {
601  std::string outputString;
602  std::stringstream outputStringStream(outputString);
603  for (size_t i = 0; i < costs.size(); i++)
604  {
605  if (i > 0)
606  outputStringStream << ' ';
607  outputStringStream << costs[i].class_index << ':' << costs[i].partial_prediction;
608  }
609  // outputStringStream << std::endl;
610  all.print_text(all.raw_prediction, outputStringStream.str(), ec.tag);
611  }
612 
613  COST_SENSITIVE::print_update(all, COST_SENSITIVE::cs_label.test_label(&ec.l), ec, ec_seq, false, predicted_class);
614 }
double sum_loss
Definition: global_data.h:145
v_array< char > tag
Definition: example.h:63
int raw_prediction
Definition: global_data.h:519
uint32_t multiclass
Definition: example.h:49
label_parser cs_label
float prob
Definition: example.h:51
v_array< int > final_prediction_sink
Definition: global_data.h:518
float partial_prediction
Definition: example.h:68
float loss(cbify &data, uint32_t label, uint32_t final_prediction)
Definition: cbify.cc:60
int example_is_newline(example const &ec)
Definition: example.h:104
bool ec_is_label_definition(example &ec)
Definition: csoaa.cc:168
double sum_loss_since_last_dump
Definition: global_data.h:146
COST_SENSITIVE::label cs
Definition: example.h:30
shared_data * sd
Definition: global_data.h:375
size_t num_features
Definition: example.h:67
void(* print_text)(int, std::string, v_array< char >)
Definition: global_data.h:522
polylabel l
Definition: example.h:57
bool test_label(void *v)
Definition: simple_label.cc:70
polyprediction pred
Definition: example.h:60
void print_update(vw &all, bool is_test, example &ec, multi_ex *ec_seq, bool action_scores, uint32_t prediction)
v_array< wclass > costs
void(* print)(int, float, float, v_array< char >)
Definition: global_data.h:521
uint64_t total_features
Definition: global_data.h:138

◆ output_example_seq()

void CSOAA::output_example_seq ( vw all,
ldf data,
multi_ex ec_seq 
)

Definition at line 669 of file csoaa.cc.

References ec_seq_is_label_definition(), shared_data::example_number, shared_data::holdout_multiclass_log_loss, vw::holdout_set_off, CSOAA::ldf::is_probabilities, shared_data::multiclass_log_loss, output_example(), output_rank_example(), vw::print_text, CSOAA::ldf::rank, vw::raw_prediction, vw::sd, test_ldf_sequence(), shared_data::weighted_labeled_examples, and shared_data::weighted_unlabeled_examples.

670 {
671  size_t K = ec_seq.size();
672  if ((K > 0) && !ec_seq_is_label_definition(ec_seq))
673  {
674  if (test_ldf_sequence(data, ec_seq))
675  all.sd->weighted_unlabeled_examples += ec_seq[0]->weight;
676  else
677  all.sd->weighted_labeled_examples += ec_seq[0]->weight;
678  all.sd->example_number++;
679 
680  bool hit_loss = false;
681  if (data.rank)
682  output_rank_example(all, **(ec_seq.begin()), hit_loss, &(ec_seq));
683  else
684  for (example* ec : ec_seq) output_example(all, *ec, hit_loss, &(ec_seq), data);
685 
686  if (all.raw_prediction > 0)
687  {
688  v_array<char> empty = {nullptr, nullptr, nullptr, 0};
689  all.print_text(all.raw_prediction, "", empty);
690  }
691 
692  if (data.is_probabilities)
693  {
694  float min_cost = FLT_MAX;
695  size_t correct_class_k = 0;
696 
697  for (size_t k = 0; k < K; k++)
698  {
699  float ec_cost = ec_seq[k]->l.cs.costs[0].x;
700  if (ec_cost < min_cost)
701  {
702  min_cost = ec_cost;
703  correct_class_k = k;
704  }
705  }
706 
707  float multiclass_log_loss = 999; // -log(0) = plus infinity
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);
711 
712  // TODO: How to detect if we should update holdout or normal loss?
713  // (ec.test_only) OR (COST_SENSITIVE::example_is_test(ec))
714  // What should be the "ec"? data.ec_seq[0]?
715  // Based on parse_args.cc (where "average multiclass log loss") is printed,
716  // I decided to try yet another way: (!all.holdout_set_off).
717  if (!all.holdout_set_off)
718  all.sd->holdout_multiclass_log_loss += multiclass_log_loss;
719  else
720  all.sd->multiclass_log_loss += multiclass_log_loss;
721  }
722  }
723 }
int raw_prediction
Definition: global_data.h:519
double weighted_unlabeled_examples
Definition: global_data.h:143
double holdout_multiclass_log_loss
Definition: global_data.h:168
bool holdout_set_off
Definition: global_data.h:499
double multiclass_log_loss
Definition: global_data.h:167
bool test_ldf_sequence(ldf &data, multi_ex &ec_seq)
Definition: csoaa.cc:271
shared_data * sd
Definition: global_data.h:375
void output_rank_example(vw &all, example &head_ec, bool &hit_loss, multi_ex *ec_seq)
Definition: csoaa.cc:616
void(* print_text)(int, std::string, v_array< char >)
Definition: global_data.h:522
uint64_t example_number
Definition: global_data.h:137
bool ec_seq_is_label_definition(multi_ex &ec_seq)
Definition: csoaa.cc:181
void output_example(vw &all, example &ec, bool &hit_loss, multi_ex *ec_seq, ldf &data)
Definition: csoaa.cc:543
double weighted_labeled_examples
Definition: global_data.h:141

◆ output_rank_example()

void CSOAA::output_rank_example ( vw all,
example head_ec,
bool &  hit_loss,
multi_ex ec_seq 
)

Definition at line 616 of file csoaa.cc.

References polyprediction::a_s, COST_SENSITIVE::label::costs, polylabel::cs, COST_SENSITIVE::cs_label, ec_is_label_definition(), example_is_newline(), vw::final_prediction_sink, example::l, loss(), example::num_features, example::pred, ACTION_SCORE::print_action_score(), vw::print_text, COST_SENSITIVE::print_update(), vw::raw_prediction, vw::sd, v_array< T >::size(), shared_data::sum_loss, shared_data::sum_loss_since_last_dump, example::tag, test_label(), and shared_data::total_features.

Referenced by output_example_seq().

617 {
618  label& ld = head_ec.l.cs;
620 
621  if (example_is_newline(head_ec))
622  return;
623  if (ec_is_label_definition(head_ec))
624  return;
625 
626  all.sd->total_features += head_ec.num_features;
627 
628  float loss = 0.;
629  v_array<action_score>& preds = head_ec.pred.a_s;
630 
631  if (!COST_SENSITIVE::cs_label.test_label(&head_ec.l))
632  {
633  size_t idx = 0;
634  for (example* ex : *ec_seq)
635  {
636  if (hit_loss)
637  break;
638  if (preds[0].action == idx)
639  {
640  loss = ex->l.cs.costs[0].x;
641  hit_loss = true;
642  }
643  idx++;
644  }
645  all.sd->sum_loss += loss;
647  assert(loss >= 0);
648  }
649 
650  for (int sink : all.final_prediction_sink) print_action_score(sink, head_ec.pred.a_s, head_ec.tag);
651 
652  if (all.raw_prediction > 0)
653  {
654  std::string outputString;
655  std::stringstream outputStringStream(outputString);
656  for (size_t i = 0; i < costs.size(); i++)
657  {
658  if (i > 0)
659  outputStringStream << ' ';
660  outputStringStream << costs[i].class_index << ':' << costs[i].partial_prediction;
661  }
662  // outputStringStream << std::endl;
663  all.print_text(all.raw_prediction, outputStringStream.str(), head_ec.tag);
664  }
665 
666  COST_SENSITIVE::print_update(all, COST_SENSITIVE::cs_label.test_label(&head_ec.l), head_ec, ec_seq, true, 0);
667 }
double sum_loss
Definition: global_data.h:145
v_array< char > tag
Definition: example.h:63
int raw_prediction
Definition: global_data.h:519
ACTION_SCORE::action_scores a_s
Definition: example.h:47
label_parser cs_label
v_array< int > final_prediction_sink
Definition: global_data.h:518
uint32_t action
Definition: search.h:19
float loss(cbify &data, uint32_t label, uint32_t final_prediction)
Definition: cbify.cc:60
int example_is_newline(example const &ec)
Definition: example.h:104
bool ec_is_label_definition(example &ec)
Definition: csoaa.cc:168
size_t size() const
Definition: v_array.h:68
double sum_loss_since_last_dump
Definition: global_data.h:146
COST_SENSITIVE::label cs
Definition: example.h:30
shared_data * sd
Definition: global_data.h:375
size_t num_features
Definition: example.h:67
void(* print_text)(int, std::string, v_array< char >)
Definition: global_data.h:522
polylabel l
Definition: example.h:57
bool test_label(void *v)
Definition: simple_label.cc:70
polyprediction pred
Definition: example.h:60
void print_update(vw &all, bool is_test, example &ec, multi_ex *ec_seq, bool action_scores, uint32_t prediction)
void print_action_score(int f, v_array< action_score > &a_s, v_array< char > &tag)
Definition: action_score.cc:8
v_array< wclass > costs
uint64_t total_features
Definition: global_data.h:138

◆ predict_or_learn()

template<bool is_learn>
void CSOAA::predict_or_learn ( csoaa c,
single_learner base,
example ec 
)

Definition at line 57 of file csoaa.cc.

References add_passthrough_feature, constant, COST_SENSITIVE::label::costs, polylabel::cs, DO_MULTIPREDICT, features::indicies, example::l, polyprediction::multiclass, LEARNER::learner< T, E >::multipredict(), CSOAA::csoaa::num_classes, example::partial_prediction, example::passthrough, CSOAA::csoaa::pred, example::pred, polyprediction::scalar, polylabel::simple, features::size(), and features::values.

58 {
59  // std::cerr << "------------- passthrough" << std::endl;
60  COST_SENSITIVE::label ld = ec.l.cs;
61  uint32_t prediction = 1;
62  float score = FLT_MAX;
63  size_t pt_start = ec.passthrough ? ec.passthrough->size() : 0;
64  ec.l.simple = {0., 0., 0.};
65  if (!ld.costs.empty())
66  {
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;
70  }
71  else if (DO_MULTIPREDICT && !is_learn)
72  {
73  ec.l.simple = {FLT_MAX, 0.f, 0.f};
74  base.multipredict(ec, 0, c.num_classes, c.pred, false);
75  for (uint32_t i = 1; i <= c.num_classes; i++)
76  {
77  add_passthrough_feature(ec, i, c.pred[i - 1].scalar);
78  if (c.pred[i - 1].scalar < c.pred[prediction - 1].scalar)
79  prediction = i;
80  }
81  ec.partial_prediction = c.pred[prediction - 1].scalar;
82  }
83  else
84  {
85  float temp;
86  for (uint32_t i = 1; i <= c.num_classes; i++) inner_loop<false>(base, ec, i, FLT_MAX, prediction, score, temp);
87  }
88  if (ec.passthrough)
89  {
90  uint64_t second_best = 0;
91  float second_best_cost = FLT_MAX;
92  for (size_t i = 0; i < ec.passthrough->size() - pt_start; i++)
93  {
94  float val = ec.passthrough->values[pt_start + i];
95  if ((val > ec.partial_prediction) && (val < second_best_cost))
96  {
97  second_best_cost = val;
98  second_best = ec.passthrough->indicies[pt_start + i];
99  }
100  }
101  if (second_best_cost < FLT_MAX)
102  {
103  float margin = second_best_cost - ec.partial_prediction;
104  add_passthrough_feature(ec, constant * 2, margin);
105  add_passthrough_feature(ec, constant * 2 + 1 + second_best, 1.);
106  }
107  else
108  add_passthrough_feature(ec, constant * 3, 1.);
109  }
110 
111  ec.pred.multiclass = prediction;
112  ec.l.cs = ld;
113 }
uint32_t multiclass
Definition: example.h:49
v_array< feature_index > indicies
float partial_prediction
Definition: example.h:68
v_array< feature_value > values
label_data simple
Definition: example.h:28
#define add_passthrough_feature(ec, i, x)
Definition: example.h:119
size_t size() const
COST_SENSITIVE::label cs
Definition: example.h:30
constexpr uint64_t constant
Definition: constant.h:11
#define DO_MULTIPREDICT
Definition: csoaa.cc:54
polylabel l
Definition: example.h:57
features * passthrough
Definition: example.h:74
void multipredict(E &ec, size_t lo, size_t count, polyprediction *pred, bool finalize_predictions)
Definition: learner.h:178
polyprediction pred
Definition: example.h:60
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ process_label()

void CSOAA::process_label ( ldf data,
example ec 
)
inline

Definition at line 741 of file csoaa.cc.

References COST_SENSITIVE::label::costs, polylabel::cs, example_predict::feature_space, example_predict::indices, example::l, CSOAA::ldf::label_features, and LabelDict::set_label_features().

Referenced by process_labels().

742 {
743  auto new_fs = ec->feature_space[ec->indices[0]];
744  auto& costs = ec->l.cs.costs;
745  for (auto const& cost : costs)
746  {
747  const auto lab = (size_t)cost.x;
748  LabelDict::set_label_features(data.label_features, lab, new_fs);
749  }
750 }
v_array< namespace_index > indices
void set_label_features(label_feature_map &lfm, size_t lab, features &fs)
std::array< features, NUM_NAMESPACES > feature_space
COST_SENSITIVE::label cs
Definition: example.h:30
polylabel l
Definition: example.h:57
v_array< wclass > costs

◆ process_labels()

multi_ex CSOAA::process_labels ( ldf data,
const multi_ex ec_seq_all 
)

Definition at line 756 of file csoaa.cc.

References ec_is_label_definition(), and process_label().

Referenced by do_actual_learning(), and do_actual_learning_oaa().

757 {
758  example* ec = ec_seq_all[0];
759 
760  // check the first element, if it's not a label, return
761  if (!ec_is_label_definition(*ec))
762  return ec_seq_all;
763 
764  // process the first element as a label
765  process_label(data, ec);
766 
767  multi_ex ret;
768  size_t i = 1;
769  // process the rest of the elements that are labels
770  for (; i < ec_seq_all.size(); i++)
771  {
772  ec = ec_seq_all[i];
773  if (!ec_is_label_definition(*ec))
774  {
775  for (size_t j = i; j < ec_seq_all.size(); j++) ret.push_back(ec_seq_all[j]);
776  // return index of the first element that is not a label
777  return ret;
778  }
779 
780  process_label(data, ec);
781  }
782 
783  // all examples were labels return size
784  return ret;
785 }
bool ec_is_label_definition(example &ec)
Definition: csoaa.cc:168
void process_label(ldf &data, example *ec)
Definition: csoaa.cc:741
std::vector< example * > multi_ex
Definition: example.h:122

◆ subtract_example()

void CSOAA::subtract_example ( vw all,
example ec,
example ecsub 
)

Definition at line 217 of file csoaa.cc.

References example_predict::feature_space, example_predict::indices, example::num_features, v_array< T >::push_back(), features::size(), features::sum_feat_sq, example::total_sum_feat_sq, and wap_ldf_namespace.

Referenced by do_actual_learning_wap().

218 {
220  wap_fs.sum_feat_sq = 0;
221  GD::foreach_feature<example&, uint64_t, subtract_feature>(all, *ecsub, *ec);
223  ec->num_features += wap_fs.size();
224  ec->total_sum_feat_sq += wap_fs.sum_feat_sq;
225 }
constexpr unsigned char wap_ldf_namespace
Definition: constant.h:20
v_array< namespace_index > indices
the core definition of a set of features.
std::array< features, NUM_NAMESPACES > feature_space
size_t size() const
void push_back(const T &new_ele)
Definition: v_array.h:107
size_t num_features
Definition: example.h:67
float total_sum_feat_sq
Definition: example.h:71
float sum_feat_sq

◆ subtract_feature()

void CSOAA::subtract_feature ( example ec,
float  feature_value_x,
uint64_t  weight_index 
)

Definition at line 211 of file csoaa.cc.

References example_predict::feature_space, and wap_ldf_namespace.

212 {
213  ec.feature_space[wap_ldf_namespace].push_back(-feature_value_x, weight_index);
214 }
constexpr unsigned char wap_ldf_namespace
Definition: constant.h:20
std::array< features, NUM_NAMESPACES > feature_space

◆ test_ldf_sequence()

bool CSOAA::test_ldf_sequence ( ldf data,
multi_ex ec_seq 
)

Definition at line 271 of file csoaa.cc.

References CSOAA::ldf::all, COST_SENSITIVE::cs_label, label_parser::test_label, test_label(), and vw::trace_message.

Referenced by do_actual_learning(), and output_example_seq().

272 {
273  bool isTest;
274  if (ec_seq.empty())
275  isTest = true;
276  else
277  isTest = COST_SENSITIVE::cs_label.test_label(&ec_seq[0]->l);
278  for (const auto& ec : ec_seq)
279  {
280  // Each sub-example must have just one cost
281  assert(ec->l.cs.costs.size() == 1);
282 
283  if (COST_SENSITIVE::cs_label.test_label(&ec->l) != isTest)
284  {
285  isTest = true;
286  data.all->trace_message << "warning: ldf example has mix of train/test data; assuming test" << std::endl;
287  }
288  }
289  return isTest;
290 }
label_parser cs_label
bool(* test_label)(void *)
Definition: label_parser.h:22
bool test_label(void *v)
Definition: simple_label.cc:70

◆ unsubtract_example()

void CSOAA::unsubtract_example ( example ec)

Definition at line 227 of file csoaa.cc.

References features::clear(), v_array< T >::decr(), v_array< T >::empty(), example_predict::feature_space, example_predict::indices, v_array< T >::last(), example::num_features, features::size(), features::sum_feat_sq, example::total_sum_feat_sq, and wap_ldf_namespace.

Referenced by do_actual_learning_wap().

228 {
229  if (ec->indices.empty())
230  {
231  std::cerr << "internal error (bug): trying to unsubtract_example, but there are no namespaces!" << std::endl;
232  return;
233  }
234 
235  if (ec->indices.last() != wap_ldf_namespace)
236  {
237  std::cerr
238  << "internal error (bug): trying to unsubtract_example, but either it wasn't added, or something was added "
239  "after and not removed!"
240  << std::endl;
241  return;
242  }
243 
245  ec->num_features -= fs.size();
246  ec->total_sum_feat_sq -= fs.sum_feat_sq;
247  fs.clear();
248  ec->indices.decr();
249 }
constexpr unsigned char wap_ldf_namespace
Definition: constant.h:20
v_array< namespace_index > indices
the core definition of a set of features.
std::array< features, NUM_NAMESPACES > feature_space
size_t size() const
size_t num_features
Definition: example.h:67
void clear()
float total_sum_feat_sq
Definition: example.h:71
float sum_feat_sq
bool empty() const
Definition: v_array.h:59
T last() const
Definition: v_array.h:57
void decr()
Definition: v_array.h:60