Vowpal Wabbit
Classes | Functions
CB_ALGS Namespace Reference

Classes

struct  cb
 

Functions

bool know_all_cost_example (CB::label &ld)
 
template<bool is_learn>
void predict_or_learn (cb &data, single_learner &base, example &ec)
 
void predict_eval (cb &, single_learner &, example &)
 
void learn_eval (cb &data, single_learner &, example &ec)
 
void output_example (vw &all, cb &data, example &ec, CB::label &ld)
 
void finish_example (vw &all, cb &c, example &ec)
 
void eval_finish_example (vw &all, cb &c, example &ec)
 
template<bool is_learn>
float get_cost_pred (LEARNER::single_learner *scorer, CB::cb_class *known_cost, example &ec, uint32_t index, uint32_t base)
 
float get_cost_estimate (CB::cb_class *observation, uint32_t action, float offset=0.)
 
float get_cost_estimate (CB::cb_class *observation, COST_SENSITIVE::label &scores, uint32_t action)
 
float get_cost_estimate (ACTION_SCORE::action_score &a_s, float cost, uint32_t action, float offset=0.)
 
bool example_is_newline_not_header (example const &ec)
 

Function Documentation

◆ eval_finish_example()

void CB_ALGS::eval_finish_example ( vw all,
cb c,
example ec 
)

Definition at line 125 of file cb_algs.cc.

References polylabel::cb_eval, CB_EVAL::label::event, VW::finish_example(), example::l, and output_example().

Referenced by cb_algs_setup().

126 {
127  output_example(all, c, ec, ec.l.cb_eval.event);
128  VW::finish_example(all, ec);
129 }
CB_EVAL::label cb_eval
Definition: example.h:33
CB::label event
Definition: cb.h:42
void finish_example(vw &, example &)
Definition: parser.cc:881
polylabel l
Definition: example.h:57
void output_example(vw &all, cb &data, example &ec, CB::label &ld)
Definition: cb_algs.cc:91

◆ example_is_newline_not_header()

bool CB_ALGS::example_is_newline_not_header ( example const &  ec)
inline

Definition at line 80 of file cb_algs.h.

References CB::ec_is_example_header(), and example_is_newline().

Referenced by copy_example_to_adf(), and GEN_CS::gen_cs_example_dr().

81 {
82  return (example_is_newline(ec) && !CB::ec_is_example_header(ec));
83 }
bool ec_is_example_header(example const &ec)
Definition: cb.cc:170
int example_is_newline(example const &ec)
Definition: example.h:104

◆ finish_example()

void CB_ALGS::finish_example ( vw all,
cb c,
example ec 
)

Definition at line 119 of file cb_algs.cc.

References polylabel::cb, VW::finish_example(), example::l, and output_example().

Referenced by cb_algs_setup().

120 {
121  output_example(all, c, ec, ec.l.cb);
122  VW::finish_example(all, ec);
123 }
CB::label cb
Definition: example.h:31
void finish_example(vw &, example &)
Definition: parser.cc:881
polylabel l
Definition: example.h:57
void output_example(vw &all, cb &data, example &ec, CB::label &ld)
Definition: cb_algs.cc:91

◆ get_cost_estimate() [1/3]

float CB_ALGS::get_cost_estimate ( CB::cb_class observation,
uint32_t  action,
float  offset = 0. 
)
inline

◆ get_cost_estimate() [2/3]

float CB_ALGS::get_cost_estimate ( CB::cb_class observation,
COST_SENSITIVE::label scores,
uint32_t  action 
)
inline

Definition at line 65 of file cb_algs.h.

References COST_SENSITIVE::label::costs, and get_cost_estimate().

66 {
67  for (auto& cl : scores.costs)
68  if (cl.class_index == action)
69  return get_cost_estimate(observation, action, cl.x) + cl.x;
70  return get_cost_estimate(observation, action);
71 }
uint32_t action
Definition: search.h:19
float get_cost_estimate(ACTION_SCORE::action_score &a_s, float cost, uint32_t action, float offset=0.)
Definition: cb_algs.h:73
v_array< wclass > costs

◆ get_cost_estimate() [3/3]

float CB_ALGS::get_cost_estimate ( ACTION_SCORE::action_score a_s,
float  cost,
uint32_t  action,
float  offset = 0. 
)
inline

Definition at line 73 of file cb_algs.h.

References ACTION_SCORE::action_score::action, and ACTION_SCORE::action_score::score.

74 {
75  if (action == a_s.action)
76  return (cost - offset) / a_s.score;
77  return 0.;
78 }
uint32_t action
Definition: search.h:19

◆ get_cost_pred()

template<bool is_learn>
float CB_ALGS::get_cost_pred ( LEARNER::single_learner scorer,
CB::cb_class known_cost,
example ec,
uint32_t  index,
uint32_t  base 
)

Definition at line 22 of file cb_algs.h.

References CB::cb_class::action, BASELINE::baseline_enabled(), polylabel::cb, CB::cb_class::cost, label_data::initial, example::l, label_data::label, LEARNER::learner< T, E >::learn(), example::pred, LEARNER::learner< T, E >::predict(), CB::cb_class::probability, BASELINE::reset_baseline_disabled(), polyprediction::scalar, BASELINE::set_baseline_enabled(), polylabel::simple, and example::weight.

24 {
25  CB::label ld = ec.l.cb;
26 
27  label_data simple_temp;
28  simple_temp.initial = 0.;
29  if (known_cost != nullptr && index == known_cost->action)
30  simple_temp.label = known_cost->cost;
31  else
32  simple_temp.label = FLT_MAX;
33 
34  const bool baseline_enabled_old = BASELINE::baseline_enabled(&ec);
36  ec.l.simple = simple_temp;
37  polyprediction p = ec.pred;
38  if (is_learn && known_cost != nullptr && index == known_cost->action)
39  {
40  float old_weight = ec.weight;
41  ec.weight /= known_cost->probability;
42  scorer->learn(ec, index - 1 + base);
43  ec.weight = old_weight;
44  }
45  else
46  scorer->predict(ec, index - 1 + base);
47 
48  if (!baseline_enabled_old)
50  float pred = ec.pred.scalar;
51  ec.pred = p;
52 
53  ec.l.cb = ld;
54 
55  return pred;
56 }
void predict(E &ec, size_t i=0)
Definition: learner.h:169
float scalar
Definition: example.h:45
CB::label cb
Definition: example.h:31
void set_baseline_enabled(example *ec)
Definition: baseline.cc:23
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
uint32_t action
Definition: cb.h:18
float probability
Definition: cb.h:19
void reset_baseline_disabled(example *ec)
Definition: baseline.cc:38
float initial
Definition: simple_label.h:16
polylabel l
Definition: example.h:57
Definition: cb.h:25
float cost
Definition: cb.h:17
polyprediction pred
Definition: example.h:60
void learn(E &ec, size_t i=0)
Definition: learner.h:160
float weight
Definition: example.h:62
bool baseline_enabled(example *ec)
Definition: baseline.cc:51

◆ know_all_cost_example()

bool CB_ALGS::know_all_cost_example ( CB::label ld)

Definition at line 33 of file cb_algs.cc.

References CB::label::costs.

34 {
35  if (ld.costs.size() <= 1) // this means we specified an example where all actions are possible but only specified the
36  // cost for the observed action
37  return false;
38 
39  // if we specified more than 1 action for this example, i.e. either we have a limited set of possible actions, or all
40  // actions are specified than check if all actions have a specified cost
41  for (auto& cl : ld.costs)
42  if (cl.cost == FLT_MAX)
43  return false;
44 
45  return true;
46 }
v_array< cb_class > costs
Definition: cb.h:27

◆ learn_eval()

void CB_ALGS::learn_eval ( cb data,
single_learner ,
example ec 
)

Definition at line 77 of file cb_algs.cc.

References c, CB_ALGS::cb::cb_cs_ld, polylabel::cb_eval, CB_ALGS::cb::cbcs, CB::label::costs, COST_SENSITIVE::label::costs, CB_EVAL::label::event, CB_ADF::get_observed_cost(), GEN_CS::cb_to_cs::known_cost, and example::l.

Referenced by cb_algs_setup().

78 {
79  CB_EVAL::label ld = ec.l.cb_eval;
80 
81  cb_to_cs& c = data.cbcs;
83  gen_cs_example<true>(c, ec, ld.event, data.cb_cs_ld);
84 
85  for (size_t i = 0; i < ld.event.costs.size(); i++)
86  ld.event.costs[i].partial_prediction = data.cb_cs_ld.costs[i].partial_prediction;
87 
88  ec.pred.multiclass = ec.l.cb_eval.action;
89 }
COST_SENSITIVE::label cb_cs_ld
Definition: cb_algs.cc:24
v_array< cb_class > costs
Definition: cb.h:27
CB_EVAL::label cb_eval
Definition: example.h:33
CB::label event
Definition: cb.h:42
CB::cb_class get_observed_cost(multi_ex &examples)
Definition: cb_adf.cc:99
CB::cb_class * known_cost
polylabel l
Definition: example.h:57
cb_to_cs cbcs
Definition: cb_algs.cc:23
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ output_example()

void CB_ALGS::output_example ( vw all,
cb data,
example ec,
CB::label ld 
)

Definition at line 91 of file cb_algs.cc.

References CB::cb_class::action, c, CB::cb_label, CB_ALGS::cb::cbcs, CB::label::costs, vw::final_prediction_sink, get_cost_estimate(), GEN_CS::cb_to_cs::known_cost, loss(), polyprediction::multiclass, example::num_features, CB::cb_class::partial_prediction, example::pred, GEN_CS::cb_to_cs::pred_scores, vw::print, vw::print_text, CB::print_update(), vw::raw_prediction, vw::sd, example::tag, label_parser::test_label, test_label(), example::test_only, and shared_data::update().

Referenced by eval_finish_example(), and finish_example().

92 {
93  float loss = 0.;
94 
95  cb_to_cs& c = data.cbcs;
96  if (!CB::cb_label.test_label(&ld))
98 
99  all.sd->update(ec.test_only, !CB::cb_label.test_label(&ld), loss, 1.f, ec.num_features);
100 
101  for (int sink : all.final_prediction_sink) all.print(sink, (float)ec.pred.multiclass, 0, ec.tag);
102 
103  if (all.raw_prediction > 0)
104  {
105  std::stringstream outputStringStream;
106  for (unsigned int i = 0; i < ld.costs.size(); i++)
107  {
108  cb_class cl = ld.costs[i];
109  if (i > 0)
110  outputStringStream << ' ';
111  outputStringStream << cl.action << ':' << cl.partial_prediction;
112  }
113  all.print_text(all.raw_prediction, outputStringStream.str(), ec.tag);
114  }
115 
116  print_update(all, CB::cb_label.test_label(&ld), ec, nullptr, false);
117 }
v_array< char > tag
Definition: example.h:63
int raw_prediction
Definition: global_data.h:519
uint32_t multiclass
Definition: example.h:49
COST_SENSITIVE::label pred_scores
bool(* test_label)(void *)
Definition: label_parser.h:22
v_array< int > final_prediction_sink
Definition: global_data.h:518
v_array< cb_class > costs
Definition: cb.h:27
float loss(cbify &data, uint32_t label, uint32_t final_prediction)
Definition: cbify.cc:60
float get_cost_estimate(CB::cb_class *observation, uint32_t action, float offset=0.)
Definition: cb_algs.h:58
void print_update(vw &all, bool is_test, example &ec, multi_ex *ec_seq, bool action_scores)
Definition: cb.cc:180
CB::cb_class * known_cost
uint32_t action
Definition: cb.h:18
float partial_prediction
Definition: cb.h:21
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
void update(bool test_example, bool labeled_example, float loss, float weight, size_t num_features)
Definition: global_data.h:190
label_parser cb_label
Definition: cb.cc:167
bool test_label(void *v)
Definition: simple_label.cc:70
polyprediction pred
Definition: example.h:60
cb_to_cs cbcs
Definition: cb_algs.cc:23
constexpr uint64_t c
Definition: rand48.cc:12
void(* print)(int, float, float, v_array< char >)
Definition: global_data.h:521
bool test_only
Definition: example.h:76

◆ predict_eval()

void CB_ALGS::predict_eval ( cb ,
single_learner ,
example  
)

Definition at line 75 of file cb_algs.cc.

References THROW.

Referenced by cb_algs_setup().

75 { THROW("can not use a test label for evaluation"); }
#define THROW(args)
Definition: vw_exception.h:181

◆ predict_or_learn()

template<bool is_learn>
void CB_ALGS::predict_or_learn ( cb data,
single_learner base,
example ec 
)

Definition at line 49 of file cb_algs.cc.

References CB::cb_class::action, c, polylabel::cb, CB_ALGS::cb::cb_cs_ld, CB_TYPE_DM, CB_ALGS::cb::cbcs, COST_SENSITIVE::label::costs, CB_ADF::get_observed_cost(), GEN_CS::cb_to_cs::known_cost, example::l, LEARNER::learner< T, E >::learn(), GEN_CS::cb_to_cs::num_actions, and LEARNER::learner< T, E >::predict().

50 {
51  CB::label ld = ec.l.cb;
52  cb_to_cs& c = data.cbcs;
54  if (c.known_cost != nullptr && (c.known_cost->action < 1 || c.known_cost->action > c.num_actions))
55  std::cerr << "invalid action: " << c.known_cost->action << std::endl;
56 
57  // generate a cost-sensitive example to update classifiers
58  gen_cs_example<is_learn>(c, ec, ld, data.cb_cs_ld);
59 
60  if (c.cb_type != CB_TYPE_DM)
61  {
62  ec.l.cs = data.cb_cs_ld;
63 
64  if (is_learn)
65  base.learn(ec);
66  else
67  base.predict(ec);
68 
69  for (size_t i = 0; i < ld.costs.size(); i++)
70  ld.costs[i].partial_prediction = data.cb_cs_ld.costs[i].partial_prediction;
71  ec.l.cb = ld;
72  }
73 }
void predict(E &ec, size_t i=0)
Definition: learner.h:169
CB::label cb
Definition: example.h:31
COST_SENSITIVE::label cb_cs_ld
Definition: cb_algs.cc:24
#define CB_TYPE_DM
Definition: cb_algs.h:14
CB::cb_class get_observed_cost(multi_ex &examples)
Definition: cb_adf.cc:99
CB::cb_class * known_cost
uint32_t action
Definition: cb.h:18
uint32_t num_actions
polylabel l
Definition: example.h:57
Definition: cb.h:25
void learn(E &ec, size_t i=0)
Definition: learner.h:160
cb_to_cs cbcs
Definition: cb_algs.cc:23
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12