Vowpal Wabbit
Classes | Functions
GEN_CS Namespace Reference

Classes

struct  cb_to_cs
 
struct  cb_to_cs_adf
 

Functions

constexpr bool observed_cost (cb_class *cl)
 
cb_classget_observed_cost (CB::label &ld)
 
float safe_probability (float prob)
 
void gen_cs_example_ips (multi_ex &examples, COST_SENSITIVE::label &cs_labels, float clip_p)
 
void gen_cs_example_dm (multi_ex &examples, COST_SENSITIVE::label &cs_labels)
 
void gen_cs_test_example (multi_ex &examples, COST_SENSITIVE::label &cs_labels)
 
void gen_cs_example_ips (cb_to_cs &c, CB::label &ld, COST_SENSITIVE::label &cs_ld, float clip_p)
 
void gen_cs_example_mtr (cb_to_cs_adf &c, multi_ex &ec_seq, COST_SENSITIVE::label &cs_labels)
 
void gen_cs_example_sm (multi_ex &, uint32_t chosen_action, float sign_offset, ACTION_SCORE::action_scores action_vals, COST_SENSITIVE::label &cs_labels)
 
template<bool is_learn>
void gen_cs_example_dm (cb_to_cs &c, example &ec, COST_SENSITIVE::label &cs_ld)
 
template<bool is_learn>
void gen_cs_label (cb_to_cs &c, example &ec, COST_SENSITIVE::label &cs_ld, uint32_t action, float clip_p=0.f)
 
template<bool is_learn>
void gen_cs_example_dr (cb_to_cs &c, example &ec, CB::label &ld, COST_SENSITIVE::label &cs_ld, float=0.f)
 
template<bool is_learn>
void gen_cs_example (cb_to_cs &c, example &ec, CB::label &ld, COST_SENSITIVE::label &cs_ld)
 
template<bool is_learn>
void gen_cs_example_dr (cb_to_cs_adf &c, multi_ex &examples, COST_SENSITIVE::label &cs_labels, float clip_p=0.f)
 
template<bool is_learn>
void gen_cs_example (cb_to_cs_adf &c, multi_ex &ec_seq, COST_SENSITIVE::label &cs_labels)
 
template<bool is_learn>
void call_cs_ldf (LEARNER::multi_learner &base, multi_ex &examples, v_array< CB::label > &cb_labels, COST_SENSITIVE::label &cs_labels, v_array< COST_SENSITIVE::label > &prepped_cs_labels, uint64_t offset, size_t id=0)
 

Function Documentation

◆ call_cs_ldf()

template<bool is_learn>
void GEN_CS::call_cs_ldf ( LEARNER::multi_learner base,
multi_ex examples,
v_array< CB::label > &  cb_labels,
COST_SENSITIVE::label cs_labels,
v_array< COST_SENSITIVE::label > &  prepped_cs_labels,
uint64_t  offset,
size_t  id = 0 
)

Definition at line 251 of file gen_cs_example.h.

References v_array< T >::clear(), COST_SENSITIVE::label::costs, v_array< T >::end(), v_array< T >::end_array, LEARNER::learner< T, E >::learn(), LEARNER::learner< T, E >::predict(), v_array< T >::push_back(), v_array< T >::resize(), and v_array< T >::size().

253 {
254  cb_labels.clear();
255  if (prepped_cs_labels.size() < cs_labels.costs.size() + 1)
256  {
257  prepped_cs_labels.resize(cs_labels.costs.size() + 1);
258  prepped_cs_labels.end() = prepped_cs_labels.end_array;
259  }
260 
261  // 1st: save cb_label (into mydata) and store cs_label for each example, which will be passed into base.learn.
262  // also save offsets
263  uint64_t saved_offset = examples[0]->ft_offset;
264  size_t index = 0;
265  for (auto ec : examples)
266  {
267  cb_labels.push_back(ec->l.cb);
268  prepped_cs_labels[index].costs.clear();
269  prepped_cs_labels[index].costs.push_back(cs_labels.costs[index]);
270  ec->l.cs = prepped_cs_labels[index++];
271  ec->ft_offset = offset;
272  }
273 
274  // 2nd: predict for each ex
275  // // call base.predict for all examples
276  if (is_learn)
277  base.learn(examples, (int32_t)id);
278  else
279  base.predict(examples, (int32_t)id);
280 
281  // 3rd: restore cb_label for each example
282  // (**ec).l.cb = array.element.
283  // and restore offsets
284  for (size_t i = 0; i < examples.size(); ++i)
285  {
286  examples[i]->l.cb = cb_labels[i];
287  examples[i]->ft_offset = saved_offset;
288  }
289 }
void resize(size_t length)
Definition: v_array.h:69
void predict(E &ec, size_t i=0)
Definition: learner.h:169
size_t size() const
Definition: v_array.h:68
void push_back(const T &new_ele)
Definition: v_array.h:107
void clear()
Definition: v_array.h:88
T *& end()
Definition: v_array.h:43
void learn(E &ec, size_t i=0)
Definition: learner.h:160
v_array< wclass > costs
T * end_array
Definition: v_array.h:38

◆ gen_cs_example() [1/2]

template<bool is_learn>
void GEN_CS::gen_cs_example ( cb_to_cs c,
example ec,
CB::label ld,
COST_SENSITIVE::label cs_ld 
)

Definition at line 167 of file gen_cs_example.h.

References c, GEN_CS::cb_to_cs::cb_type, CB_TYPE_DM, CB_TYPE_DR, CB_TYPE_IPS, f, gen_cs_example_dm(), gen_cs_example_ips(), gen_cs_example_mtr(), gen_cs_example_sm(), gen_cs_test_example(), and THROW.

168 {
169  switch (c.cb_type)
170  {
171  case CB_TYPE_IPS:
172  gen_cs_example_ips(c, ld, cs_ld);
173  break;
174  case CB_TYPE_DM:
175  gen_cs_example_dm<is_learn>(c, ec, cs_ld);
176  break;
177  case CB_TYPE_DR:
178  gen_cs_example_dr<is_learn>(c, ec, ld, cs_ld);
179  break;
180  default:
181  THROW("Unknown cb_type specified for contextual bandit learning: " << c.cb_type);
182  }
183 }
#define CB_TYPE_IPS
Definition: cb_algs.h:15
#define CB_TYPE_DM
Definition: cb_algs.h:14
#define CB_TYPE_DR
Definition: cb_algs.h:13
void gen_cs_example_ips(multi_ex &examples, COST_SENSITIVE::label &cs_labels, float clip_p)
#define THROW(args)
Definition: vw_exception.h:181
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_example() [2/2]

template<bool is_learn>
void GEN_CS::gen_cs_example ( cb_to_cs_adf c,
multi_ex ec_seq,
COST_SENSITIVE::label cs_labels 
)

Definition at line 232 of file gen_cs_example.h.

References c, GEN_CS::cb_to_cs_adf::cb_type, CB_TYPE_DR, CB_TYPE_IPS, CB_TYPE_MTR, gen_cs_example_ips(), gen_cs_example_mtr(), and THROW.

233 {
234  switch (c.cb_type)
235  {
236  case CB_TYPE_IPS:
237  gen_cs_example_ips(ec_seq, cs_labels);
238  break;
239  case CB_TYPE_DR:
240  gen_cs_example_dr<is_learn>(c, ec_seq, cs_labels);
241  break;
242  case CB_TYPE_MTR:
243  gen_cs_example_mtr(c, ec_seq, cs_labels);
244  break;
245  default:
246  THROW("Unknown cb_type specified for contextual bandit learning: " << c.cb_type);
247  }
248 }
#define CB_TYPE_IPS
Definition: cb_algs.h:15
#define CB_TYPE_DR
Definition: cb_algs.h:13
void gen_cs_example_ips(multi_ex &examples, COST_SENSITIVE::label &cs_labels, float clip_p)
void gen_cs_example_mtr(cb_to_cs_adf &c, multi_ex &ec_seq, COST_SENSITIVE::label &cs_labels)
#define THROW(args)
Definition: vw_exception.h:181
constexpr uint64_t c
Definition: rand48.cc:12
#define CB_TYPE_MTR
Definition: cb_algs.h:16

◆ gen_cs_example_dm() [1/2]

template<bool is_learn>
void GEN_CS::gen_cs_example_dm ( cb_to_cs c,
example ec,
COST_SENSITIVE::label cs_ld 
)

Definition at line 53 of file gen_cs_example.h.

References CB::cb_class::action, GEN_CS::cb_to_cs::avg_loss_regressors, polylabel::cb, CB::cb_class::cost, CB::label::costs, COST_SENSITIVE::label::costs, GEN_CS::cb_to_cs::known_cost, example::l, GEN_CS::cb_to_cs::last_correct_cost, GEN_CS::cb_to_cs::last_pred_reg, polyprediction::multiclass, GEN_CS::cb_to_cs::nb_ex_regressors, GEN_CS::cb_to_cs::num_actions, example::pred, GEN_CS::cb_to_cs::pred_scores, GEN_CS::cb_to_cs::scorer, and COST_SENSITIVE::wclass::x.

54 { // this implements the direct estimation method, where costs are directly specified by the learned regressor.
55  CB::label ld = ec.l.cb;
56 
57  float min = FLT_MAX;
58  uint32_t argmin = 1;
59  // generate cost sensitive example
60  cs_ld.costs.clear();
61  c.pred_scores.costs.clear();
62 
63  if (ld.costs.size() == 0 ||
64  (ld.costs.size() == 1 &&
65  ld.costs[0].cost != FLT_MAX)) // this is a typical example where we can perform all actions
66  { // in this case generate cost-sensitive example with all actions
67  for (uint32_t i = 1; i <= c.num_actions; i++)
68  {
69  COST_SENSITIVE::wclass wc = {0., i, 0., 0.};
70  // get cost prediction for this action
71  wc.x = CB_ALGS::get_cost_pred<is_learn>(c.scorer, c.known_cost, ec, i, 0);
72  if (wc.x < min)
73  {
74  min = wc.x;
75  argmin = i;
76  }
77 
78  c.pred_scores.costs.push_back(wc);
79 
80  if (c.known_cost != nullptr && c.known_cost->action == i)
81  {
82  c.nb_ex_regressors++;
83  c.avg_loss_regressors += (1.0f / c.nb_ex_regressors) *
84  ((c.known_cost->cost - wc.x) * (c.known_cost->cost - wc.x) - c.avg_loss_regressors);
85  c.last_pred_reg = wc.x;
86  c.last_correct_cost = c.known_cost->cost;
87  }
88 
89  cs_ld.costs.push_back(wc);
90  }
91  }
92  else // this is an example where we can only perform a subset of the actions
93  { // in this case generate cost-sensitive example with only allowed actions
94  for (auto& cl : ld.costs)
95  {
96  COST_SENSITIVE::wclass wc = {0., cl.action, 0., 0.};
97 
98  // get cost prediction for this action
99  wc.x = CB_ALGS::get_cost_pred<is_learn>(c.scorer, c.known_cost, ec, cl.action, 0);
100  if (wc.x < min || (wc.x == min && cl.action < argmin))
101  {
102  min = wc.x;
103  argmin = cl.action;
104  }
105  c.pred_scores.costs.push_back(wc);
106 
107  if (c.known_cost != nullptr && c.known_cost->action == cl.action)
108  {
109  c.nb_ex_regressors++;
110  c.avg_loss_regressors += (1.0f / c.nb_ex_regressors) *
111  ((c.known_cost->cost - wc.x) * (c.known_cost->cost - wc.x) - c.avg_loss_regressors);
112  c.last_pred_reg = wc.x;
113  c.last_correct_cost = c.known_cost->cost;
114  }
115 
116  cs_ld.costs.push_back(wc);
117  }
118  }
119 
120  ec.pred.multiclass = argmin;
121 }
uint32_t multiclass
Definition: example.h:49
CB::label cb
Definition: example.h:31
v_array< cb_class > costs
Definition: cb.h:27
polylabel l
Definition: example.h:57
Definition: cb.h:25
polyprediction pred
Definition: example.h:60
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_example_dm() [2/2]

void GEN_CS::gen_cs_example_dm ( multi_ex examples,
COST_SENSITIVE::label cs_labels 
)

Definition at line 60 of file gen_cs_example.cc.

References CB::label::costs, COST_SENSITIVE::label::costs, and COST_SENSITIVE::wclass::x.

Referenced by gen_cs_example(), and CB_ADF::cb_adf::learn_DM().

61 {
62  cs_labels.costs.clear();
63  for (uint32_t i = 0; i < examples.size(); i++)
64  {
65  CB::label ld = examples[i]->l.cb;
66 
67  COST_SENSITIVE::wclass wc = {0., i, 0., 0.};
68  if (ld.costs.size() == 1 && ld.costs[0].cost != FLT_MAX)
69  wc.x = ld.costs[0].cost;
70  cs_labels.costs.push_back(wc);
71  }
72 }
v_array< cb_class > costs
Definition: cb.h:27
Definition: cb.h:25
v_array< wclass > costs

◆ gen_cs_example_dr() [1/2]

template<bool is_learn>
void GEN_CS::gen_cs_example_dr ( cb_to_cs c,
example ec,
CB::label ld,
COST_SENSITIVE::label cs_ld,
float  = 0.f 
)

Definition at line 147 of file gen_cs_example.h.

References c, CB::label::costs, COST_SENSITIVE::label::costs, GEN_CS::cb_to_cs::num_actions, and GEN_CS::cb_to_cs::pred_scores.

148 { // this implements the doubly robust method
149  cs_ld.costs.clear();
150  c.pred_scores.costs.clear();
151  if (ld.costs.size() == 0) // a test example
152  for (uint32_t i = 1; i <= c.num_actions; i++)
153  { // Explicit declaration for a weak compiler.
154  COST_SENSITIVE::wclass temp = {FLT_MAX, i, 0., 0.};
155  cs_ld.costs.push_back(temp);
156  }
157  else if (ld.costs.size() == 0 || (ld.costs.size() == 1 && ld.costs[0].cost != FLT_MAX))
158  // this is a typical example where we can perform all actions
159  // in this case generate cost-sensitive example with all actions
160  for (uint32_t i = 1; i <= c.num_actions; i++) gen_cs_label<is_learn>(c, ec, cs_ld, i);
161  else // this is an example where we can only perform a subset of the actions
162  // in this case generate cost-sensitive example with only allowed actions
163  for (auto& cl : ld.costs) gen_cs_label<is_learn>(c, ec, cs_ld, cl.action);
164 }
v_array< cb_class > costs
Definition: cb.h:27
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_example_dr() [2/2]

template<bool is_learn>
void GEN_CS::gen_cs_example_dr ( cb_to_cs_adf c,
multi_ex examples,
COST_SENSITIVE::label cs_labels,
float  clip_p = 0.f 
)

Definition at line 197 of file gen_cs_example.h.

References CB::cb_class::action, CB::cb_class::cost, COST_SENSITIVE::label::costs, CB_ALGS::example_is_newline_not_header(), GEN_CS::cb_to_cs_adf::known_cost, GEN_CS::cb_to_cs_adf::pred_scores, CB::cb_class::probability, GEN_CS::cb_to_cs_adf::scorer, and COST_SENSITIVE::wclass::x.

198 { // size_t mysize = examples.size();
199  c.pred_scores.costs.clear();
200 
201  cs_labels.costs.clear();
202  for (size_t i = 0; i < examples.size(); i++)
203  {
204  if (CB_ALGS::example_is_newline_not_header(*examples[i]))
205  continue;
206 
207  COST_SENSITIVE::wclass wc = {0., (uint32_t)i, 0., 0.};
208 
209  if (c.known_cost.action == i)
210  {
211  int known_index = c.known_cost.action;
212  c.known_cost.action = 0;
213  // get cost prediction for this label
214  // num_actions should be 1 effectively.
215  // my get_cost_pred function will use 1 for 'index-1+base'
216  wc.x = CB_ALGS::get_cost_pred<is_learn>(c.scorer, &(c.known_cost), *(examples[i]), 0, 2);
217  c.known_cost.action = known_index;
218  }
219  else
220  wc.x = CB_ALGS::get_cost_pred<is_learn>(c.scorer, nullptr, *(examples[i]), 0, 2);
221 
222  c.pred_scores.costs.push_back(wc); // done
223 
224  // add correction if we observed cost for this action and regressor is wrong
225  if (c.known_cost.probability != -1 && c.known_cost.action == i)
226  wc.x += (c.known_cost.cost - wc.x) / std::max(c.known_cost.probability, clip_p);
227  cs_labels.costs.push_back(wc);
228  }
229 }
bool example_is_newline_not_header(example const &ec)
Definition: cb_algs.h:80
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_example_ips() [1/2]

void GEN_CS::gen_cs_example_ips ( multi_ex examples,
COST_SENSITIVE::label cs_labels,
float  clip_p 
)

Definition at line 45 of file gen_cs_example.cc.

References CB::label::costs, COST_SENSITIVE::label::costs, safe_probability(), and COST_SENSITIVE::wclass::x.

Referenced by gen_cs_example(), CB_ADF::cb_adf::learn_IPS(), CB_ADF::cb_adf::learn_MTR(), and VW::cb_explore_adf::cover::cb_explore_adf_cover::predict_or_learn_impl().

46 {
47  cs_labels.costs.clear();
48  for (uint32_t i = 0; i < examples.size(); i++)
49  {
50  CB::label ld = examples[i]->l.cb;
51 
52  COST_SENSITIVE::wclass wc = {0., i, 0., 0.};
53  if (ld.costs.size() == 1 && ld.costs[0].cost != FLT_MAX)
54  wc.x = ld.costs[0].cost / safe_probability(std::max(ld.costs[0].probability, clip_p));
55  cs_labels.costs.push_back(wc);
56  }
57 }
v_array< cb_class > costs
Definition: cb.h:27
float safe_probability(float prob)
Definition: cb.h:25
v_array< wclass > costs

◆ gen_cs_example_ips() [2/2]

void GEN_CS::gen_cs_example_ips ( cb_to_cs c,
CB::label ld,
COST_SENSITIVE::label cs_ld,
float  clip_p 
)

Definition at line 86 of file gen_cs_example.cc.

References CB::cb_class::action, GEN_CS::cb_to_cs::avg_loss_regressors, CB::cb_class::cost, CB::label::costs, COST_SENSITIVE::label::costs, GEN_CS::cb_to_cs::known_cost, GEN_CS::cb_to_cs::last_correct_cost, GEN_CS::cb_to_cs::last_pred_reg, GEN_CS::cb_to_cs::nb_ex_regressors, GEN_CS::cb_to_cs::num_actions, CB::cb_class::probability, safe_probability(), and COST_SENSITIVE::wclass::x.

87 {
88  // this implements the inverse propensity score method, where cost are importance weighted by the probability of the
89  // chosen action generate cost-sensitive example
90  cs_ld.costs.clear();
91  if (ld.costs.size() == 0 || (ld.costs.size() == 1 && ld.costs[0].cost != FLT_MAX))
92  // this is a typical example where we can perform all actions
93  {
94  // in this case generate cost-sensitive example with all actions
95  for (uint32_t i = 1; i <= c.num_actions; i++)
96  {
97  COST_SENSITIVE::wclass wc = {0., i, 0., 0.};
98  if (c.known_cost != nullptr && i == c.known_cost->action)
99  {
100  // use importance weighted cost for observed action, 0 otherwise
101  wc.x = c.known_cost->cost / safe_probability(std::max(c.known_cost->probability, clip_p));
102 
103  // ips can be thought as the doubly robust method with a fixed regressor that predicts 0 costs for everything
104  // update the loss of this regressor
105  c.nb_ex_regressors++;
106  c.avg_loss_regressors +=
107  (1.0f / c.nb_ex_regressors) * ((c.known_cost->cost) * (c.known_cost->cost) - c.avg_loss_regressors);
108  c.last_pred_reg = 0;
109  c.last_correct_cost = c.known_cost->cost;
110  }
111 
112  cs_ld.costs.push_back(wc);
113  }
114  }
115  else // this is an example where we can only perform a subset of the actions
116  {
117  // in this case generate cost-sensitive example with only allowed actions
118  for (auto& cl : ld.costs)
119  {
120  COST_SENSITIVE::wclass wc = {0., cl.action, 0., 0.};
121  if (c.known_cost != nullptr && cl.action == c.known_cost->action)
122  {
123  // use importance weighted cost for observed action, 0 otherwise
124  wc.x = c.known_cost->cost / safe_probability(std::max(c.known_cost->probability, clip_p));
125 
126  // ips can be thought as the doubly robust method with a fixed regressor that predicts 0 costs for everything
127  // update the loss of this regressor
128  c.nb_ex_regressors++;
129  c.avg_loss_regressors +=
130  (1.0f / c.nb_ex_regressors) * ((c.known_cost->cost) * (c.known_cost->cost) - c.avg_loss_regressors);
131  c.last_pred_reg = 0;
132  c.last_correct_cost = c.known_cost->cost;
133  }
134 
135  cs_ld.costs.push_back(wc);
136  }
137  }
138 }
v_array< cb_class > costs
Definition: cb.h:27
float safe_probability(float prob)
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_example_mtr()

void GEN_CS::gen_cs_example_mtr ( cb_to_cs_adf c,
multi_ex ec_seq,
COST_SENSITIVE::label cs_labels 
)

Definition at line 140 of file gen_cs_example.cc.

References GEN_CS::cb_to_cs_adf::action_sum, CB::label::costs, COST_SENSITIVE::label::costs, GEN_CS::cb_to_cs_adf::event_sum, GEN_CS::cb_to_cs_adf::mtr_ec_seq, GEN_CS::cb_to_cs_adf::mtr_example, and COST_SENSITIVE::wclass::x.

Referenced by gen_cs_example(), and CB_ADF::cb_adf::learn_MTR().

141 {
142  c.action_sum += ec_seq.size();
143  c.event_sum++;
144 
145  c.mtr_ec_seq.clear();
146  cs_labels.costs.clear();
147  for (size_t i = 0; i < ec_seq.size(); i++)
148  {
149  CB::label ld = ec_seq[i]->l.cb;
150 
151  COST_SENSITIVE::wclass wc = {0, 0, 0, 0};
152 
153  if (ld.costs.size() == 1 && ld.costs[0].cost != FLT_MAX)
154  {
155  wc.x = ld.costs[0].cost;
156  c.mtr_example = (uint32_t)i;
157  c.mtr_ec_seq.push_back(ec_seq[i]);
158  cs_labels.costs.push_back(wc);
159  }
160  }
161 }
v_array< cb_class > costs
Definition: cb.h:27
Definition: cb.h:25
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_example_sm()

void GEN_CS::gen_cs_example_sm ( multi_ex ,
uint32_t  chosen_action,
float  sign_offset,
ACTION_SCORE::action_scores  action_vals,
COST_SENSITIVE::label cs_labels 
)

Definition at line 163 of file gen_cs_example.cc.

References COST_SENSITIVE::label::costs, v_array< T >::size(), and COST_SENSITIVE::wclass::x.

Referenced by gen_cs_example(), and CB_ADF::cb_adf::learn_SM().

165 {
166  cs_labels.costs.clear();
167  for (uint32_t i = 0; i < action_vals.size(); i++)
168  {
169  uint32_t current_action = action_vals[i].action;
170  COST_SENSITIVE::wclass wc = {0., current_action, 0., 0.};
171 
172  if (current_action == chosen_action)
173  wc.x = action_vals[i].score + sign_offset;
174  else
175  wc.x = action_vals[i].score - sign_offset;
176 
177  // TODO: This clipping is conceptually unnecessary because the example weight for this instance should be close to
178  // 0.
179  if (wc.x > 100.)
180  wc.x = 100.0;
181  if (wc.x < -100.)
182  wc.x = -100.0;
183 
184  cs_labels.costs.push_back(wc);
185  }
186 }
size_t size() const
Definition: v_array.h:68
v_array< wclass > costs

◆ gen_cs_label()

template<bool is_learn>
void GEN_CS::gen_cs_label ( cb_to_cs c,
example ec,
COST_SENSITIVE::label cs_ld,
uint32_t  action,
float  clip_p = 0.f 
)

Definition at line 124 of file gen_cs_example.h.

References CB::cb_class::action, GEN_CS::cb_to_cs::avg_loss_regressors, CB::cb_class::cost, COST_SENSITIVE::label::costs, GEN_CS::cb_to_cs::known_cost, GEN_CS::cb_to_cs::last_correct_cost, GEN_CS::cb_to_cs::last_pred_reg, GEN_CS::cb_to_cs::nb_ex_regressors, GEN_CS::cb_to_cs::num_actions, GEN_CS::cb_to_cs::pred_scores, CB::cb_class::probability, GEN_CS::cb_to_cs::scorer, and COST_SENSITIVE::wclass::x.

125 {
126  COST_SENSITIVE::wclass wc = {0., action, 0., 0.};
127 
128  // get cost prediction for this action
129  wc.x = CB_ALGS::get_cost_pred<is_learn>(c.scorer, c.known_cost, ec, action, c.num_actions);
130 
131  c.pred_scores.costs.push_back(wc);
132  // add correction if we observed cost for this action and regressor is wrong
133  if (c.known_cost != nullptr && c.known_cost->action == action)
134  {
135  c.nb_ex_regressors++;
136  c.avg_loss_regressors += (1.0f / c.nb_ex_regressors) *
137  ((c.known_cost->cost - wc.x) * (c.known_cost->cost - wc.x) - c.avg_loss_regressors);
138  c.last_pred_reg = wc.x;
139  c.last_correct_cost = c.known_cost->cost;
140  wc.x += (c.known_cost->cost - wc.x) / std::max(c.known_cost->probability, clip_p);
141  }
142 
143  cs_ld.costs.push_back(wc);
144 }
uint32_t action
Definition: search.h:19
v_array< wclass > costs
constexpr uint64_t c
Definition: rand48.cc:12

◆ gen_cs_test_example()

void GEN_CS::gen_cs_test_example ( multi_ex examples,
COST_SENSITIVE::label cs_labels 
)

Definition at line 75 of file gen_cs_example.cc.

References COST_SENSITIVE::label::costs.

Referenced by CB_ADF::cb_adf::do_actual_learning(), gen_cs_example(), and CB_ADF::cb_adf::learn_SM().

76 {
77  cs_labels.costs.clear();
78  for (uint32_t i = 0; i < examples.size(); i++)
79  {
80  COST_SENSITIVE::wclass wc = {FLT_MAX, i, 0., 0.};
81  cs_labels.costs.push_back(wc);
82  }
83 }
v_array< wclass > costs

◆ get_observed_cost()

CB::cb_class * GEN_CS::get_observed_cost ( CB::label ld)

Definition at line 25 of file gen_cs_example.cc.

References CB::label::costs, and observed_cost().

26 {
27  for (auto& cl : ld.costs)
28  if (observed_cost(&cl))
29  return &cl;
30  return nullptr;
31 }
v_array< cb_class > costs
Definition: cb.h:27
constexpr bool observed_cost(cb_class *cl)

◆ observed_cost()

constexpr bool GEN_CS::observed_cost ( cb_class cl)
inline

Definition at line 19 of file gen_cs_example.cc.

References CB::cb_class::cost, and CB::cb_class::probability.

Referenced by get_observed_cost().

20 {
21  // cost observed for this action if it has non zero probability and cost != FLT_MAX
22  return (cl != nullptr && cl->cost != FLT_MAX && cl->probability > .0);
23 }
float probability
Definition: cb.h:19
float cost
Definition: cb.h:17

◆ safe_probability()

float GEN_CS::safe_probability ( float  prob)

Definition at line 33 of file gen_cs_example.cc.

References f, and prediction_type::prob.

Referenced by gen_cs_example_ips(), and CB_ADF::cb_adf::learn_SM().

34 {
35  if (prob <= 0.)
36  {
37  std::cout << "Probability " << prob << " is not possible, replacing with 1e-3. Fix your dataset. " << std::endl;
38  return 1e-3f;
39  }
40  else
41  return prob;
42 }
float f
Definition: cache.cc:40