forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cb.cc
305 lines (258 loc) · 8.08 KB
/
cb.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright (c) by respective owners including Yahoo!, Microsoft, and
individual contributors. All rights reserved. Released under a BSD (revised)
license as described in the file LICENSE.
*/
#include <float.h>
#include "example.h"
#include "parse_primitives.h"
#include "vw.h"
using namespace LEARNER;
namespace CB
{
char* bufread_label(CB::label* ld, char* c, io_buf& cache)
{
size_t num = *(size_t *)c;
ld->costs.erase();
c += sizeof(size_t);
size_t total = sizeof(cb_class)*num;
if (buf_read(cache, c, total) < total)
{
cout << "error in demarshal of cost data" << endl;
return c;
}
for (size_t i = 0; i<num; i++)
{
cb_class temp = *(cb_class *)c;
c += sizeof(cb_class);
ld->costs.push_back(temp);
}
return c;
}
size_t read_cached_label(shared_data*, void* v, io_buf& cache)
{
CB::label* ld = (CB::label*) v;
ld->costs.erase();
char *c;
size_t total = sizeof(size_t);
if (buf_read(cache, c, total) < total)
return 0;
c = bufread_label(ld,c, cache);
return total;
}
float weight(void*)
{
return 1.;
}
char* bufcache_label(CB::label* ld, char* c)
{
*(size_t *)c = ld->costs.size();
c += sizeof(size_t);
for (size_t i = 0; i< ld->costs.size(); i++)
{
*(cb_class *)c = ld->costs[i];
c += sizeof(cb_class);
}
return c;
}
void cache_label(void* v, io_buf& cache)
{
char *c;
CB::label* ld = (CB::label*) v;
buf_write(cache, c, sizeof(size_t)+sizeof(cb_class)*ld->costs.size());
bufcache_label(ld,c);
}
void default_label(void* v)
{
CB::label* ld = (CB::label*) v;
ld->costs.erase();
}
void delete_label(void* v)
{
CB::label* ld = (CB::label*)v;
ld->costs.delete_v();
}
void copy_label(void*dst, void*src)
{
CB::label* ldD = (CB::label*)dst;
CB::label* ldS = (CB::label*)src;
copy_array(ldD->costs, ldS->costs);
}
bool substring_eq(substring ss, const char* str) {
size_t len_ss = ss.end - ss.begin;
size_t len_str = strlen(str);
if (len_ss != len_str) return false;
return (strncmp(ss.begin, str, len_ss) == 0);
}
void parse_label(parser* p, shared_data*, void* v, v_array<substring>& words)
{
CB::label* ld = (CB::label*)v;
for (size_t i = 0; i < words.size(); i++)
{
cb_class f;
tokenize(':', words[i], p->parse_name);
if( p->parse_name.size() < 1 || p->parse_name.size() > 3 )
{
cerr << "malformed cost specification!" << endl;
cerr << "terminating." << endl;
throw exception();
}
f.partial_prediction = 0.;
f.action = (uint32_t)hashstring(p->parse_name[0], 0);
f.cost = FLT_MAX;
if(p->parse_name.size() > 1)
f.cost = float_of_substring(p->parse_name[1]);
if ( nanpattern(f.cost))
{
cerr << "error NaN cost for action: ";
cerr.write(p->parse_name[0].begin, p->parse_name[0].end - p->parse_name[0].begin);
cerr << " terminating." << endl;
throw exception();
}
f.probability = .0;
if(p->parse_name.size() > 2)
f.probability = float_of_substring(p->parse_name[2]);
if ( nanpattern(f.probability))
{
cerr << "error NaN probability for action: ";
cerr.write(p->parse_name[0].begin, p->parse_name[0].end - p->parse_name[0].begin);
cerr << " terminating." << endl;
throw exception();
}
if( f.probability > 1.0 )
{
cerr << "invalid probability > 1 specified for an action, resetting to 1." << endl;
f.probability = 1.0;
}
if( f.probability < 0.0 )
{
cerr << "invalid probability < 0 specified for an action, resetting to 0." << endl;
f.probability = .0;
}
if (substring_eq(p->parse_name[0], "shared")) {
if (p->parse_name.size() == 1) {
f.probability = -1.f;
} else
cerr << "shared feature vectors should not have costs" << endl;
}
ld->costs.push_back(f);
}
}
label_parser cb_label = {default_label, parse_label,
cache_label, read_cached_label,
delete_label, weight,
copy_label,
sizeof(label)};
bool example_is_test(example& ec)
{
v_array<CB::cb_class> costs = ec.l.cb.costs;
if (costs.size() == 0) return true;
for (size_t j=0; j<costs.size(); j++)
if (costs[j].cost != FLT_MAX) return false;
return true;
}
bool ec_is_example_header(example& ec) // example headers look like "0:-1" or just "shared"
{
v_array<CB::cb_class> costs = ec.l.cb.costs;
if (costs.size() != 1) return false;
if (costs[0].action != 0) return false;
if (costs[0].cost >= 0) return false;
return true;
}
void print_update(vw& all, bool is_test, example& ec, const v_array<example*>* ec_seq, bool multilabel)
{
if (all.sd->weighted_examples >= all.sd->dump_interval && !all.quiet && !all.bfgs)
{
size_t num_current_features = ec.num_features;
// for csoaa_ldf we want features from the whole (multiline example),
// not only from one line (the first one) represented by ec
if (ec_seq != nullptr)
{
num_current_features = 0;
// If the first example is "shared", don't include its features.
// These should be already included in each example (TODO: including quadratic and cubic).
// TODO: code duplication csoaa.cc LabelDict::ec_is_example_header
example** ecc=ec_seq->begin;
example& first_ex = **ecc;
v_array<CB::cb_class> costs = first_ex.l.cb.costs;
if (costs.size() == 1 && costs[0].action == 0 && costs[0].cost < 0) ecc++;
for (; ecc!=ec_seq->end; ecc++)
num_current_features += (*ecc)->num_features;
}
std::string label_buf;
if (is_test)
label_buf = " unknown";
else
label_buf = " known";
if (multilabel) {
std::ostringstream pred_buf;
pred_buf << std::setw(all.sd->col_current_predict) << std::right << std::setfill(' ')
<< ec.pred.multilabels.label_v[0]<<".....";
all.sd->print_update(all.holdout_set_off, all.current_pass, label_buf, pred_buf.str(),
num_current_features, all.progress_add, all.progress_arg);;
}
else
all.sd->print_update(all.holdout_set_off, all.current_pass, label_buf, ec.pred.multiclass,
num_current_features, all.progress_add, all.progress_arg);
}
}
}
namespace CB_EVAL
{
size_t read_cached_label(shared_data*sd, void* v, io_buf& cache)
{
CB_EVAL::label* ld = (CB_EVAL::label*) v;
char* c;
size_t total = sizeof(uint32_t);
if (buf_read(cache, c, total) < total)
return 0;
ld->action = *(uint32_t*)c;
c += sizeof(uint32_t);
return total + CB::read_cached_label(sd, &(ld->event), cache);
}
void cache_label(void* v, io_buf& cache)
{
char *c;
CB_EVAL::label* ld = (CB_EVAL::label*) v;
buf_write(cache, c, sizeof(uint32_t));
*(uint32_t *)c = ld->action;
c+= sizeof(uint32_t);
CB::cache_label(&(ld->event), cache);
}
void default_label(void* v)
{
CB_EVAL::label* ld = (CB_EVAL::label*) v;
CB::default_label(&(ld->event));
ld->action = 0;
}
void delete_label(void* v)
{
CB_EVAL::label* ld = (CB_EVAL::label*)v;
CB::delete_label(&(ld->event));
}
void copy_label(void*dst, void*src)
{
CB_EVAL::label* ldD = (CB_EVAL::label*)dst;
CB_EVAL::label* ldS = (CB_EVAL::label*)src;
CB::copy_label(&(ldD->event), &(ldS)->event);
ldD->action = ldS->action;
}
void parse_label(parser* p, shared_data* sd, void* v, v_array<substring>& words)
{
CB_EVAL::label* ld = (CB_EVAL::label*)v;
if (words.size() < 2)
{
cout << "Evaluation can not happen without an action and an exploration" << endl;
throw exception();
}
ld->action = (uint32_t)hashstring(words[0], 0);
words.begin++;
CB::parse_label(p, sd, &(ld->event), words);
words.begin--;
}
label_parser cb_eval = {default_label, parse_label,
cache_label, read_cached_label,
delete_label, CB::weight,
copy_label,
sizeof(CB_EVAL::label)};
}