Vowpal Wabbit
Classes | Functions | Variables
CB Namespace Reference

Classes

struct  cb_class
 
struct  label
 

Functions

char * bufread_label (CB::label *ld, char *c, io_buf &cache)
 
size_t read_cached_label (shared_data *, void *v, io_buf &cache)
 
float weight (void *v)
 
char * bufcache_label (CB::label *ld, char *c)
 
void cache_label (void *v, io_buf &cache)
 
void default_label (void *v)
 
bool test_label (void *v)
 
void delete_label (void *v)
 
void copy_label (void *dst, void *src)
 
void parse_label (parser *p, shared_data *, void *v, v_array< substring > &words)
 
bool ec_is_example_header (example const &ec)
 
void print_update (vw &all, bool is_test, example &ec, multi_ex *ec_seq, bool action_scores)
 

Variables

label_parser cb_label
 
struct CB::cb_class __attribute__
 

Function Documentation

◆ bufcache_label()

char* CB::bufcache_label ( CB::label ld,
char *  c 
)

Definition at line 58 of file cb.cc.

References c, CB::label::costs, and CB::label::weight.

Referenced by cache_label().

59 {
60  *(size_t*)c = ld->costs.size();
61  c += sizeof(size_t);
62  for (auto const& cost : ld->costs)
63  {
64  *(cb_class*)c = cost;
65  c += sizeof(cb_class);
66  }
67  memcpy(c, &ld->weight, sizeof(ld->weight));
68  c += sizeof(ld->weight);
69  return c;
70 }
float weight
Definition: cb.h:28
v_array< cb_class > costs
Definition: cb.h:27
constexpr uint64_t c
Definition: rand48.cc:12

◆ bufread_label()

char* CB::bufread_label ( CB::label ld,
char *  c,
io_buf cache 
)

Definition at line 17 of file cb.cc.

References io_buf::buf_read(), c, CB::label::costs, and CB::label::weight.

Referenced by read_cached_label().

18 {
19  size_t num = *(size_t*)c;
20  ld->costs.clear();
21  c += sizeof(size_t);
22  size_t total = sizeof(cb_class) * num + sizeof(ld->weight);
23  if (cache.buf_read(c, total) < total)
24  {
25  std::cout << "error in demarshal of cost data" << std::endl;
26  return c;
27  }
28  for (size_t i = 0; i < num; i++)
29  {
30  cb_class temp = *(cb_class*)c;
31  c += sizeof(cb_class);
32  ld->costs.push_back(temp);
33  }
34  memcpy(&ld->weight, c, sizeof(ld->weight));
35  c += sizeof(ld->weight);
36  return c;
37 }
float weight
Definition: cb.h:28
v_array< cb_class > costs
Definition: cb.h:27
constexpr uint64_t c
Definition: rand48.cc:12
size_t buf_read(char *&pointer, size_t n)
Definition: io_buf.cc:12

◆ cache_label()

void CB::cache_label ( void *  v,
io_buf cache 
)

Definition at line 72 of file cb.cc.

References io_buf::buf_write(), bufcache_label(), c, CB::label::costs, and CB::label::weight.

Referenced by CB_EVAL::cache_label().

73 {
74  char* c;
75  CB::label* ld = (CB::label*)v;
76  cache.buf_write(c, sizeof(size_t) + sizeof(cb_class) * ld->costs.size() + sizeof(ld->weight));
77  bufcache_label(ld, c);
78 }
char * bufcache_label(CB::label *ld, char *c)
Definition: cb.cc:58
float weight
Definition: cb.h:28
v_array< cb_class > costs
Definition: cb.h:27
void buf_write(char *&pointer, size_t n)
Definition: io_buf.cc:94
Definition: cb.h:25
constexpr uint64_t c
Definition: rand48.cc:12

◆ copy_label()

void CB::copy_label ( void *  dst,
void *  src 
)

Definition at line 104 of file cb.cc.

References copy_array(), CB::label::costs, and CB::label::weight.

Referenced by add_to_vali(), VW::copy_example_data(), VW::copy_example_label(), CB_EVAL::copy_label(), and Search::predictor::set_input_at().

105 {
106  CB::label* ldD = (CB::label*)dst;
107  CB::label* ldS = (CB::label*)src;
108  copy_array(ldD->costs, ldS->costs);
109  ldD->weight = ldS->weight;
110 }
float weight
Definition: cb.h:28
void copy_array(v_array< T > &dst, const v_array< T > &src)
Definition: v_array.h:185
v_array< cb_class > costs
Definition: cb.h:27
Definition: cb.h:25

◆ default_label()

void CB::default_label ( void *  v)

Definition at line 80 of file cb.cc.

References CB::label::costs, and CB::label::weight.

Referenced by CB_EVAL::default_label().

81 {
82  CB::label* ld = (CB::label*)v;
83  ld->costs.clear();
84  ld->weight = 1;
85 }
float weight
Definition: cb.h:28
v_array< cb_class > costs
Definition: cb.h:27
Definition: cb.h:25

◆ delete_label()

void CB::delete_label ( void *  v)

◆ ec_is_example_header()

bool CB::ec_is_example_header ( example const &  ec)

Definition at line 170 of file cb.cc.

References polylabel::cb, CB::label::costs, f, example::l, and v_array< T >::size().

Referenced by LEARNER::example_is_newline_not_header(), CB_ALGS::example_is_newline_not_header(), EXPLORE_EVAL::output_example(), VW::shared_feature_merger::predict_or_learn(), and print_update().

171 {
172  v_array<CB::cb_class> costs = ec.l.cb.costs;
173  if (costs.size() != 1)
174  return false;
175  if (costs[0].probability == -1.f)
176  return true;
177  return false;
178 }
size_t size() const
Definition: v_array.h:68
float f
Definition: cache.cc:40

◆ parse_label()

void CB::parse_label ( parser p,
shared_data ,
void *  v,
v_array< substring > &  words 
)

Definition at line 112 of file cb.cc.

References CB::cb_class::action, CB::cb_class::cost, CB::label::costs, v_array< T >::empty(), f, float_of_substring(), hashstring(), parser::parse_name, CB::cb_class::partial_prediction, CB::cb_class::probability, v_array< T >::size(), substring_equal(), THROW, tokenize(), and CB::label::weight.

Referenced by CB_EVAL::parse_label().

113 {
114  CB::label* ld = (CB::label*)v;
115  ld->costs.clear();
116  ld->weight = 1.0;
117 
118  for (auto const& word : words)
119  {
120  cb_class f;
121  tokenize(':', word, p->parse_name);
122 
123  if (p->parse_name.empty() || p->parse_name.size() > 3)
124  THROW("malformed cost specification: " << p->parse_name);
125 
126  f.partial_prediction = 0.;
127  f.action = (uint32_t)hashstring(p->parse_name[0], 0);
128  f.cost = FLT_MAX;
129 
130  if (p->parse_name.size() > 1)
131  f.cost = float_of_substring(p->parse_name[1]);
132 
133  if (std::isnan(f.cost))
134  THROW("error NaN cost (" << p->parse_name[1] << " for action: " << p->parse_name[0]);
135 
136  f.probability = .0;
137  if (p->parse_name.size() > 2)
138  f.probability = float_of_substring(p->parse_name[2]);
139 
140  if (std::isnan(f.probability))
141  THROW("error NaN probability (" << p->parse_name[2] << " for action: " << p->parse_name[0]);
142 
143  if (f.probability > 1.0)
144  {
145  std::cerr << "invalid probability > 1 specified for an action, resetting to 1." << std::endl;
146  f.probability = 1.0;
147  }
148  if (f.probability < 0.0)
149  {
150  std::cerr << "invalid probability < 0 specified for an action, resetting to 0." << std::endl;
151  f.probability = .0;
152  }
153  if (substring_equal(p->parse_name[0], "shared"))
154  {
155  if (p->parse_name.size() == 1)
156  {
157  f.probability = -1.f;
158  }
159  else
160  std::cerr << "shared feature vectors should not have costs" << std::endl;
161  }
162 
163  ld->costs.push_back(f);
164  }
165 }
float weight
Definition: cb.h:28
v_array< cb_class > costs
Definition: cb.h:27
v_array< substring > parse_name
Definition: parser.h:100
size_t size() const
Definition: v_array.h:68
float float_of_substring(substring s)
void tokenize(char delim, substring s, ContainerT &ret, bool allow_empty=false)
Definition: cb.h:25
VW_STD14_CONSTEXPR uint64_t hashstring(substring s, uint64_t h)
Definition: hashstring.h:18
bool empty() const
Definition: v_array.h:59
bool substring_equal(const substring &a, const substring &b)
#define THROW(args)
Definition: vw_exception.h:181
float f
Definition: cache.cc:40

◆ print_update()

void CB::print_update ( vw all,
bool  is_test,
example ec,
multi_ex ec_seq,
bool  action_scores 
)

Definition at line 180 of file cb.cc.

References polyprediction::a_s, vw::bfgs, shared_data::col_current_predict, vw::current_pass, shared_data::dump_interval, ec_is_example_header(), v_array< T >::empty(), vw::holdout_set_off, polyprediction::multiclass, example::num_features, example::pred, shared_data::print_update(), vw::progress_add, vw::progress_arg, vw::quiet, vw::sd, and shared_data::weighted_examples().

Referenced by MWT::finish_example(), output_and_account_confidence_example(), output_and_account_example(), EXPLORE_EVAL::output_example(), VW::cb_explore_adf::cb_explore_adf_base< ExploreType >::output_example(), CB_ALGS::output_example(), output_example(), CB_ADF::output_example(), CB_ADF::output_rank_example(), and shared_data::print_update().

181 {
182  if (all.sd->weighted_examples() >= all.sd->dump_interval && !all.quiet && !all.bfgs)
183  {
184  size_t num_features = ec.num_features;
185 
186  size_t pred = ec.pred.multiclass;
187  if (ec_seq != nullptr)
188  {
189  num_features = 0;
190  // TODO: code duplication csoaa.cc LabelDict::ec_is_example_header
191  for (size_t i = 0; i < (*ec_seq).size(); i++)
192  if (!CB::ec_is_example_header(*(*ec_seq)[i]))
193  num_features += (*ec_seq)[i]->num_features;
194  }
195  std::string label_buf;
196  if (is_test)
197  label_buf = " unknown";
198  else
199  label_buf = " known";
200 
201  if (action_scores)
202  {
203  std::ostringstream pred_buf;
204  pred_buf << std::setw(shared_data::col_current_predict) << std::right << std::setfill(' ');
205  if (!ec.pred.a_s.empty())
206  pred_buf << ec.pred.a_s[0].action << ":" << ec.pred.a_s[0].score << "...";
207  else
208  pred_buf << "no action";
209  all.sd->print_update(all.holdout_set_off, all.current_pass, label_buf, pred_buf.str(), num_features,
210  all.progress_add, all.progress_arg);
211  }
212  else
213  all.sd->print_update(all.holdout_set_off, all.current_pass, label_buf, (uint32_t)pred, num_features,
214  all.progress_add, all.progress_arg);
215  }
216 }
uint32_t multiclass
Definition: example.h:49
ACTION_SCORE::action_scores a_s
Definition: example.h:47
bool ec_is_example_header(example const &ec)
Definition: cb.cc:170
v_array< action_score > action_scores
Definition: action_score.h:10
bool quiet
Definition: global_data.h:487
bool holdout_set_off
Definition: global_data.h:499
bool progress_add
Definition: global_data.h:545
shared_data * sd
Definition: global_data.h:375
float progress_arg
Definition: global_data.h:546
void print_update(bool holdout_set_off, size_t current_pass, float label, float prediction, size_t num_features, bool progress_add, float progress_arg)
Definition: global_data.h:225
bool bfgs
Definition: global_data.h:412
size_t num_features
Definition: example.h:67
uint64_t current_pass
Definition: global_data.h:396
bool empty() const
Definition: v_array.h:59
polyprediction pred
Definition: example.h:60
static constexpr int col_current_predict
Definition: global_data.h:184
double weighted_examples()
Definition: global_data.h:188
float dump_interval
Definition: global_data.h:147

◆ read_cached_label()

size_t CB::read_cached_label ( shared_data ,
void *  v,
io_buf cache 
)

Definition at line 39 of file cb.cc.

References io_buf::buf_read(), bufread_label(), c, and CB::label::costs.

Referenced by CB_EVAL::read_cached_label().

40 {
41  CB::label* ld = (CB::label*)v;
42  ld->costs.clear();
43  char* c;
44  size_t total = sizeof(size_t);
45  if (cache.buf_read(c, total) < total)
46  return 0;
47  bufread_label(ld, c, cache);
48 
49  return total;
50 }
v_array< cb_class > costs
Definition: cb.h:27
Definition: cb.h:25
constexpr uint64_t c
Definition: rand48.cc:12
char * bufread_label(CB::label *ld, char *c, io_buf &cache)
Definition: cb.cc:17
size_t buf_read(char *&pointer, size_t n)
Definition: io_buf.cc:12

◆ test_label()

bool CB::test_label ( void *  v)

Definition at line 87 of file cb.cc.

References CB::label::costs.

Referenced by CB_EVAL::test_label().

88 {
89  CB::label* ld = (CB::label*)v;
90  if (ld->costs.empty())
91  return true;
92  for (auto const& cost : ld->costs)
93  if (FLT_MAX != cost.cost && cost.probability > 0.)
94  return false;
95  return true;
96 }
v_array< cb_class > costs
Definition: cb.h:27
Definition: cb.h:25

◆ weight()

float CB::weight ( void *  v)

Definition at line 52 of file cb.cc.

References CB::label::weight.

53 {
54  CB::label* ld = (CB::label*)v;
55  return ld->weight;
56 }
float weight
Definition: cb.h:28
Definition: cb.h:25

Variable Documentation

◆ __attribute__

struct CB::cb_class CB::__attribute__

◆ cb_label

label_parser CB::cb_label
Initial value:
test_label, sizeof(label)}
void parse_label(parser *p, shared_data *sd, void *v, v_array< substring > &words)
Definition: cb.cc:276
void copy_label(void *dst, void *src)
Definition: cb.cc:268
void cache_label(void *v, io_buf &cache)
Definition: cb.cc:239
size_t read_cached_label(shared_data *sd, void *v, io_buf &cache)
Definition: cb.cc:227
void default_label(void *v)
Definition: cb.cc:249
float weight(void *v)
Definition: cb.cc:221
bool test_label(void *v)
Definition: simple_label.cc:70
void delete_label(void *v)
Definition: cb.cc:262

Definition at line 167 of file cb.cc.

Referenced by cb_adf_setup(), cb_algs_setup(), copy_example_to_adf(), explore_eval_setup(), init_adf_data(), mwt_setup(), CB_ALGS::output_example(), CB_EXPLORE::output_example(), VW::cb_explore_adf::softmax::setup(), VW::cb_explore_adf::greedy::setup(), VW::cb_explore_adf::first::setup(), VW::cb_explore_adf::bag::setup(), VW::cb_explore_adf::cover::setup(), VW::cb_explore_adf::regcb::setup(), Search::setup(), cbify::~cbify(), and warm_cb::~warm_cb().