Vowpal Wabbit
csoaa.cc
Go to the documentation of this file.
1 /*
2 Copyright (c) by respective owners including Yahoo!, Microsoft, and
3 individual contributors. All rights reserved. Released under a BSD (revised)
4 license as described in the file LICENSE.
5  */
6 #include <cfloat>
7 #include <cerrno>
8 
9 #include "correctedMath.h"
10 #include "reductions.h"
11 #include "v_hashmap.h"
12 #include "label_dictionary.h"
13 #include "vw.h"
14 #include "gd.h" // GD::foreach_feature() needed in subtract_example()
15 #include "vw_exception.h"
16 #include <algorithm>
17 #include "csoaa.h"
18 
19 using namespace LEARNER;
20 using namespace COST_SENSITIVE;
21 using namespace VW::config;
22 
23 namespace CSOAA
24 {
25 struct csoaa
26 {
27  uint32_t num_classes;
29  ~csoaa() { free(pred); }
30 };
31 
32 template <bool is_learn>
33 inline void inner_loop(single_learner& base, example& ec, uint32_t i, float cost, uint32_t& prediction, float& score,
34  float& partial_prediction)
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 }
53 
54 #define DO_MULTIPREDICT true
55 
56 template <bool is_learn>
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 }
114 
116 
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 }
138 
139 using namespace ACTION_SCORE;
140 
141 // TODO: passthrough for ldf
142 struct ldf
143 {
145 
147  bool is_wap;
152  vw* all;
153 
154  bool rank;
156  uint64_t ft_offset;
157 
159 
161  {
162  LabelDict::free_label_features(label_features);
163  a_s.delete_v();
164  stored_preds.delete_v();
165  }
166 };
167 
168 bool ec_is_label_definition(example& ec) // label defs look like "0:___" or just "label:___"
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 }
180 
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 }
191 
193 {
194  return std::any_of(ec_seq.cbegin(), ec_seq.cend(), [](example* ec) { return ec_is_label_definition(*ec); });
195 }
196 
197 inline bool cmp_wclass_ptr(const COST_SENSITIVE::wclass* a, const COST_SENSITIVE::wclass* b) { return a->x < b->x; }
198 
199 void compute_wap_values(std::vector<COST_SENSITIVE::wclass*> costs)
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 }
206 
207 // Substract a given feature from example ec.
208 // Rather than finding the corresponding namespace and feature in ec,
209 // add a new feature with opposite value (but same index) to ec to a special wap_ldf_namespace.
210 // This is faster and allows fast undo in unsubtract_example().
211 void subtract_feature(example& ec, float feature_value_x, uint64_t weight_index)
212 {
213  ec.feature_space[wap_ldf_namespace].push_back(-feature_value_x, weight_index);
214 }
215 
216 // Iterate over all features of ecsub including quadratic and cubic features and subtract them from ec.
217 void subtract_example(vw& all, example* ec, example* ecsub)
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 }
226 
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 }
250 
252 {
253  COST_SENSITIVE::label ld = ec.l.cs;
255  simple_label.initial = 0.;
256  simple_label.label = FLT_MAX;
257 
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 
268  ec.l.cs = ld;
269 }
270 
271 bool test_ldf_sequence(ldf& data, multi_ex& ec_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 }
291 
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 }
350 
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 }
407 
408 /*
409  * The begining of the multi_ex sequence may be labels. Process those
410  * and return the start index of the un-processed examples
411  */
412 multi_ex process_labels(ldf& data, const multi_ex& ec_seq_all);
413 
414 /*
415  * 1) process all labels at first
416  * 2) verify no labels in the middle of data
417  * 3) learn_or_predict(data) with rest
418  */
419 template <bool is_learn>
420 void do_actual_learning(ldf& data, single_learner& base, multi_ex& ec_seq_all)
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 }
529 
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 }
542 
543 void output_example(vw& all, example& ec, bool& hit_loss, multi_ex* ec_seq, ldf& data)
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 }
615 
616 void output_rank_example(vw& all, example& head_ec, bool& hit_loss, multi_ex* ec_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 }
668 
669 void output_example_seq(vw& all, ldf& data, multi_ex& ec_seq)
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 }
724 
725 void end_pass(ldf& data) { data.first_pass = false; }
726 
727 void finish_multiline_example(vw& all, ldf& data, multi_ex& ec_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 }
736 
737 /*
738  * Process a single example as a label.
739  * Note: example should already be confirmed as a label
740  */
741 void inline process_label(ldf& data, example* ec)
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;
749  }
750 }
751 
752 /*
753  * The begining of the multi_ex sequence may be labels. Process those
754  * and return the start index of the un-processed examples
755  */
756 multi_ex process_labels(ldf& data, const multi_ex& ec_seq_all)
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 }
786 
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 }
889 } // namespace CSOAA
void do_actual_learning_wap(ldf &data, single_learner &base, multi_ex &ec_seq)
Definition: csoaa.cc:292
bool report_multiclass_log_loss
Definition: global_data.h:166
double sum_loss
Definition: global_data.h:145
constexpr unsigned char wap_ldf_namespace
Definition: constant.h:20
v_array< char > tag
Definition: example.h:63
void unsubtract_example(example *ec)
Definition: csoaa.cc:227
int raw_prediction
Definition: global_data.h:519
v_array< namespace_index > indices
#define correctedExp
Definition: correctedMath.h:27
label_parser simple_label
uint32_t multiclass
Definition: example.h:49
ACTION_SCORE::action_scores a_s
Definition: example.h:47
loss_function * loss
Definition: global_data.h:523
void predict(E &ec, size_t i=0)
Definition: learner.h:169
LEARNER::base_learner * cost_sensitive
Definition: global_data.h:385
uint32_t num_classes
Definition: csoaa.cc:27
base_learner * csoaa_setup(options_i &options, vw &all)
Definition: csoaa.cc:117
void(* delete_prediction)(void *)
Definition: global_data.h:485
void subtract_example(vw &all, example *ec, example *ecsub)
Definition: csoaa.cc:217
void do_actual_learning_oaa(ldf &data, single_learner &base, multi_ex &ec_seq)
Definition: csoaa.cc:351
void global_print_newline(vw &all)
Definition: csoaa.cc:530
VW::config::options_i * options
Definition: global_data.h:428
float scalar
Definition: example.h:45
label_parser cs_label
int score_comp(const void *p1, const void *p2)
Definition: action_score.h:56
double weighted_unlabeled_examples
Definition: global_data.h:143
static ssize_t write_file_or_socket(int f, const void *buf, size_t nbytes)
Definition: io_buf.cc:140
float prob
Definition: example.h:51
void output_example_seq(vw &all, multi_ex &ec_seq)
Definition: cbify.cc:356
bool first_pass
Definition: csoaa.cc:148
void finish_multiline_example(vw &all, cbify &, multi_ex &ec_seq)
Definition: cbify.cc:373
void set_label_features(label_feature_map &lfm, size_t lab, features &fs)
v_array< feature_index > indicies
uint64_t ft_offset
Definition: csoaa.cc:156
label_type::label_type_t label_type
Definition: global_data.h:550
bool(* test_label)(void *)
Definition: label_parser.h:22
v_array< int > final_prediction_sink
Definition: global_data.h:518
the core definition of a set of features.
double holdout_multiclass_log_loss
Definition: global_data.h:168
size_t read_example_this_loop
Definition: csoaa.cc:146
base_learner * make_base(learner< T, E > &base)
Definition: learner.h:462
void compute_wap_values(std::vector< COST_SENSITIVE::wclass *> costs)
Definition: csoaa.cc:199
action_scores a_s
Definition: csoaa.cc:155
void predict_or_learn(csoaa &c, single_learner &base, example &ec)
Definition: csoaa.cc:57
uint32_t action
Definition: search.h:19
float partial_prediction
Definition: example.h:68
v_array< feature_value > values
virtual void add_and_parse(const option_group_definition &group)=0
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
float loss(cbify &data, uint32_t label, uint32_t final_prediction)
Definition: cbify.cc:60
void finish_example(vw &all, csoaa &, example &ec)
Definition: csoaa.cc:115
void add_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
#define add_passthrough_feature(ec, i, x)
Definition: example.h:119
bool holdout_set_off
Definition: global_data.h:499
int example_is_newline(example const &ec)
Definition: example.h:104
bool size_t_eq(const size_t &a, const size_t &b)
T *& begin()
Definition: v_array.h:42
bool training
Definition: global_data.h:488
bool ec_is_label_definition(example &ec)
Definition: csoaa.cc:168
size_t size() const
Definition: v_array.h:68
bool rank
Definition: csoaa.cc:154
double sum_loss_since_last_dump
Definition: global_data.h:146
parser * p
Definition: global_data.h:377
std::array< features, NUM_NAMESPACES > feature_space
single_learner * as_singleline(learner< T, E > *l)
Definition: learner.h:476
void do_actual_learning(ldf &data, single_learner &base, multi_ex &ec_seq_all)
Definition: csoaa.cc:420
double multiclass_log_loss
Definition: global_data.h:167
bool test_ldf_sequence(ldf &data, multi_ex &ec_seq)
Definition: csoaa.cc:271
size_t size() const
Definition: csoaa.cc:23
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
void push_back(const T &new_ele)
Definition: v_array.h:107
COST_SENSITIVE::label cs
Definition: example.h:30
shared_data * sd
Definition: global_data.h:375
void end_pass(example &ec, vw &all)
Definition: learner.cc:44
void clear()
Definition: v_array.h:88
void finish_example(vw &all, example &ec)
vw_ostream trace_message
Definition: global_data.h:424
void del_example_namespace_from_memory(label_feature_map &lfm, example &ec, size_t lab)
size_t num_features
Definition: example.h:67
virtual bool was_supplied(const std::string &key)=0
polyprediction * pred
Definition: csoaa.cc:28
base_learner * csldf_setup(options_i &options, vw &all)
Definition: csoaa.cc:787
virtual std::string getType()=0
float csoaa_example_t
Definition: csoaa.cc:151
constexpr uint64_t constant
Definition: constant.h:11
v_array< action_scores > stored_preds
Definition: csoaa.cc:158
void output_rank_example(vw &all, example &head_ec, bool &hit_loss, multi_ex *ec_seq)
Definition: csoaa.cc:616
void clear()
void(* print_text)(int, std::string, v_array< char >)
Definition: global_data.h:522
vw * all
Definition: csoaa.cc:152
float initial
Definition: simple_label.h:16
#define DO_MULTIPREDICT
Definition: csoaa.cc:54
void finish_example(vw &, example &)
Definition: parser.cc:881
void process_label(ldf &data, example *ec)
Definition: csoaa.cc:741
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)
Definition: cs_active.cc:90
option_group_definition & add(T &&op)
Definition: options.h:90
uint64_t example_number
Definition: global_data.h:137
std::vector< example * > multi_ex
Definition: example.h:122
bool cmp_wclass_ptr(const COST_SENSITIVE::wclass *a, const COST_SENSITIVE::wclass *b)
Definition: csoaa.cc:197
polylabel l
Definition: example.h:57
constexpr uint64_t a
Definition: rand48.cc:11
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
float total_sum_feat_sq
Definition: example.h:71
features * passthrough
Definition: example.h:74
float sum_feat_sq
multi_ex process_labels(ldf &data, const multi_ex &ec_seq_all)
Definition: csoaa.cc:756
bool ec_seq_is_label_definition(multi_ex &ec_seq)
Definition: csoaa.cc:181
void set_end_pass(void(*f)(T &))
Definition: learner.h:286
void output_example(vw &all, example &ec, bool &hit_loss, multi_ex *ec_seq, ldf &data)
Definition: csoaa.cc:543
bool empty() const
Definition: v_array.h:59
bool is_wap
Definition: csoaa.cc:147
LabelDict::label_feature_map label_features
Definition: csoaa.cc:144
void multipredict(E &ec, size_t lo, size_t count, polyprediction *pred, bool finalize_predictions)
Definition: learner.h:178
bool test_label(void *v)
Definition: simple_label.cc:70
double weighted_labeled_examples
Definition: global_data.h:141
LEARNER::base_learner * setup_base(options_i &options, vw &all)
Definition: parse_args.cc:1222
T last() const
Definition: v_array.h:57
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 delete_v()
Definition: v_array.h:98
bool is_probabilities
Definition: csoaa.cc:150
bool treat_as_classifier
Definition: csoaa.cc:149
void learn(E &ec, size_t i=0)
Definition: learner.h:160
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
float weight
Definition: example.h:62
void decr()
Definition: v_array.h:60
#define THROW(args)
Definition: vw_exception.h:181
constexpr uint64_t c
Definition: rand48.cc:12
bool emptylines_separate_examples
Definition: parser.h:84
void(* print)(int, float, float, v_array< char >)
Definition: global_data.h:521
float f
Definition: cache.cc:40
void subtract_feature(example &ec, float feature_value_x, uint64_t weight_index)
Definition: csoaa.cc:211
void free_label_features(label_feature_map &lfm)
uint64_t total_features
Definition: global_data.h:138
bool ec_seq_has_label_definition(multi_ex &ec_seq)
Definition: csoaa.cc:192
label_parser lp
Definition: parser.h:102
void make_single_prediction(ldf &data, single_learner &base, example &ec)
Definition: csoaa.cc:251