-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
vw_label.h
387 lines (310 loc) · 8.03 KB
/
vw_label.h
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// 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.
#pragma once
#include "vw/core/best_constant.h"
#include "vw/core/cb.h"
#include "vw/core/constant.h"
#include "vw/core/multiclass.h"
#include "vw/core/vw.h"
#include "vw_clr.h"
namespace VW
{
namespace Labels
{
// The label classes are a replication of the parse_label function pointers provided by individual
// modules. Main reason for creation is thread-saftey. The C++ version use a shared v_array in parser
// and thus need to be synchronized.
// These label classes are thread-safe and even more efficient as they avoid marshalling.
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Globalization;
using namespace System::Text;
using namespace MULTICLASS;
using namespace Newtonsoft::Json;
public interface class ILabel
{
void UpdateExample(VW::workspace* vw, example* ex);
void ReadFromExample(example* ex);
};
public ref class ContextualBanditLabel sealed : ILabel
{
private:
uint32_t m_action;
float m_cost;
float m_probability;
public:
ContextualBanditLabel()
: m_action(0), m_cost(0), m_probability(0)
{ }
ContextualBanditLabel(uint32_t action, float cost, float probability)
: m_action(action), m_cost(cost), m_probability(0)
{ Probability = probability;
}
[JsonProperty]
property uint32_t Action
{ uint32_t get()
{ return m_action;
}
void set(uint32_t value)
{ m_action = value;
}
}
[JsonProperty]
property float Probability
{ float get()
{ return m_probability;
}
void set(float value)
{ if (value < 0 || value >1)
{
if (value > 1 && value - 1 < VW::details::PROBABILITY_TOLERANCE)
m_probability = 1.0f;
else
throw gcnew ArgumentOutOfRangeException("invalid probability: " + value);
}
else
m_probability = value;
}
}
[JsonProperty]
property float Cost
{ float get()
{ return m_cost;
}
void set(float value)
{ m_cost = value;
}
}
[JsonIgnore]
property bool IsShared
{ bool get()
{ return m_cost == FLT_MAX && m_probability == -1.f;
}
}
virtual void ReadFromExample(example* ex)
{
VW::cb_label* ld = &ex->l.cb;
if (ld->costs.size() > 0)
{
VW::cb_class& f = ld->costs[0];
m_action = f.action;
m_cost = f.cost;
m_probability = f.probability;
}
}
virtual void UpdateExample(VW::workspace* vw, example* ex)
{
VW::cb_label* ld = &ex->l.cb;
VW::cb_class f;
f.partial_prediction = 0.;
f.action = m_action;
f.cost = m_cost;
f.probability = m_probability;
ld->costs.push_back(f);
}
virtual String^ ToString() override
{ auto sb = gcnew StringBuilder;
sb->Append(m_action.ToString(CultureInfo::InvariantCulture));
sb->Append(L':');
sb->Append(m_cost.ToString(CultureInfo::InvariantCulture));
sb->Append(L':');
sb->Append(m_probability.ToString(CultureInfo::InvariantCulture));
return sb->ToString();
}
};
/// <summary>
/// In multi-line scenarios the first example can contain a set of shared features. This first example must be
/// marked using a 'shared' label.
/// </summary>
public ref class SharedLabel sealed : ILabel
{
private:
uint32_t m_action;
SharedLabel() : m_action((uint32_t)VW::uniform_hash("shared", 6, 0)) {}
public:
static SharedLabel^ Instance = gcnew SharedLabel;
virtual void UpdateExample(VW::workspace* vw, example* ex)
{
VW::cb_label* ld = &ex->l.cb;
VW::cb_class f;
f.partial_prediction = 0.;
f.action = m_action;
f.cost = FLT_MAX;
f.probability = -1.f;
ld->costs.push_back(f);
}
virtual String^ ToString() override
{ return "shared";
}
virtual void ReadFromExample(example* ex)
{
}
};
public ref class SimpleLabel sealed : ILabel
{
private:
float m_label;
Nullable<float> m_weight;
Nullable<float> m_initial;
public:
SimpleLabel() : m_label(0)
{ }
[JsonProperty]
property float Label
{ float get()
{ return m_label;
}
void set(float value)
{ m_label = value;
}
}
[JsonProperty(NullValueHandling = NullValueHandling::Ignore)]
property Nullable<float> Weight
{ Nullable<float> get()
{ return m_weight;
}
void set(Nullable<float> value)
{ m_weight = value;
}
}
[JsonProperty(NullValueHandling = NullValueHandling::Ignore)]
property Nullable<float> Initial
{ Nullable<float> get()
{ return m_initial;
}
void set(Nullable<float> value)
{ m_initial = value;
}
}
virtual void ReadFromExample(example* ex)
{
auto* ld = &ex->l.simple;
m_label = ld->label;
const auto& red_fts = ex->ex_reduction_features.template get<VW::simple_label_reduction_features>();
m_weight = red_fts.weight;
m_initial = red_fts.initial;
}
virtual void UpdateExample(VW::workspace* vw, example* ex)
{
auto* ld = &ex->l.simple;
ld->label = m_label;
if (m_weight.HasValue) ex->weight = m_weight.Value;
if (m_initial.HasValue)
{
auto& red_fts = ex->ex_reduction_features.template get<VW::simple_label_reduction_features>();
red_fts.initial = m_initial.Value;
}
VW::count_label(*vw->sd, ld->label);
}
virtual String^ ToString() override
{ auto sb = gcnew StringBuilder;
sb->Append(m_label.ToString(CultureInfo::InvariantCulture));
if (m_weight.HasValue)
{ sb->Append(L' ');
sb->Append(m_weight.Value.ToString(CultureInfo::InvariantCulture));
if (m_initial.HasValue)
{ sb->Append(L' ');
sb->Append(m_initial.Value.ToString(CultureInfo::InvariantCulture));
}
}
return sb->ToString();
}
};
public ref class MulticlassLabel sealed : ILabel
{
public:
ref class Label sealed
{
private:
uint32_t m_class;
Nullable<float> m_weight;
public:
property uint32_t Class
{ uint32_t get()
{ return m_class;
}
void set(uint32_t value)
{ m_class = value;
}
}
[JsonProperty(NullValueHandling = NullValueHandling::Ignore)]
property Nullable<float> Weight
{ Nullable<float> get()
{ return m_weight;
}
void set(Nullable<float> value)
{ m_weight = value;
}
}
};
private:
List<Label^>^ m_classes;
public:
[JsonProperty]
property List<Label^>^ Classes
{ List<Label^>^ get()
{ return m_classes;
}
void set(List<Label^>^ value)
{ m_classes = value;
}
}
virtual void ReadFromExample(example* ex)
{ throw gcnew NotImplementedException("to be done...");
}
virtual void UpdateExample(VW::workspace* vw, example* ex) { throw gcnew NotImplementedException("to be done..."); }
virtual String^ ToString() override
{ auto sb = gcnew StringBuilder;
for each (Label^ label in m_classes)
{ sb->Append(L' ');
sb->Append(label->Class.ToString(CultureInfo::InvariantCulture));
if (label->Weight.HasValue)
{ sb->Append(L' ');
sb->Append(label->Weight.Value.ToString(CultureInfo::InvariantCulture));
}
}
// strip first space
if (sb->Length > 0)
sb->Remove(0, 1);
return sb->ToString();
}
};
public ref class StringLabel sealed : ILabel
{
private:
String^ m_label;
public:
StringLabel()
{ }
StringLabel(String^ label) : m_label(label)
{ }
[JsonProperty]
property String^ Label
{ String^ get()
{ return m_label;
}
void set(String^ value)
{ m_label = value;
}
}
virtual void ReadFromExample(example* ex)
{ throw gcnew NotImplementedException("to be done...");
}
virtual void UpdateExample(VW::workspace* vw, example* ex)
{ auto bytes = System::Text::Encoding::UTF8->GetBytes(m_label);
auto valueHandle = GCHandle::Alloc(bytes, GCHandleType::Pinned);
try
{ VW::parse_example_label(*vw, *ex, reinterpret_cast<char*>(valueHandle.AddrOfPinnedObject().ToPointer()));
}
CATCHRETHROW
finally
{ valueHandle.Free();
}
}
virtual String^ ToString() override
{ return m_label;
}
};
}
}