-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TestCbAdf.cs
424 lines (362 loc) · 16 KB
/
TestCbAdf.cs
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VW;
using VW.Labels;
using VW.Serializer.Attributes;
namespace cs_unittest
{
[TestClass]
public class TestCbAdfClass : TestBase
{
public void ProfilePerformanceWithStringData()
{
string outModelFile = "profile_cb_adf.model";
using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
{
DataString[] sampleData = CreateStringCbAdfData(1000 * 1000);
foreach (DataString example in sampleData)
{
vw.Learn(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
}
vw.Native.SaveModel(outModelFile);
}
File.Delete(outModelFile);
}
public void ProfilePerformanceWithFloatData()
{
string outModelFile = "profile_cb_adf.model";
using (var vw = new VowpalWabbit<DataFloat, DataFloatADF>("--cb_adf --rank_all"))
{
DataFloat[] sampleData = CreateFloatCbAdfData(1000 * 1000);
foreach (DataFloat example in sampleData)
{
vw.Learn(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
}
vw.Native.SaveModel(outModelFile);
}
File.Delete(outModelFile);
}
private void Validate(VowpalWabbitExampleValidator<DataString> vwSharedValidation, VowpalWabbitExampleValidator<DataStringADF> vwADFValidation, DataString example)
{
vwSharedValidation.Validate(example.Line, example, SharedLabel.Instance);
for (int i = 0; i < example.ActionDependentFeatures.Count; i++)
{
var adf = example.ActionDependentFeatures[i];
vwADFValidation.Validate(adf.Line, adf, i == example.SelectedActionIndex ? example.Label : null);
}
}
public static void TestMemoryLeak()
{
string outModelFile = "cb_adf_mem_leak.model";
using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
{
DataString[] sampleData = CreateStringCbAdfData(1000);
foreach (DataString example in sampleData)
{
vw.Learn(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
}
vw.Native.SaveModel(outModelFile);
}
var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings(string.Format("--quiet -t -i {0}", outModelFile)) { MaxExampleCacheSize = 1024 });
var pool = new VowpalWabbitThreadedPrediction<DataString, DataStringADF>(vwModel);
while (true)
{
vwModel = new VowpalWabbitModel(new VowpalWabbitSettings(string.Format("--quiet -t -i {0}", outModelFile)) { MaxExampleCacheSize = 1024 });
pool.UpdateModel(vwModel);
}
}
[TestMethod]
[TestCategory("Command line through marshalling")]
public void Test87()
{
using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
using (var vwSharedValidation = new VowpalWabbitExampleValidator<DataString>("--cb_adf --rank_all"))
using (var vwADFValidation = new VowpalWabbitExampleValidator<DataStringADF>("--cb_adf --rank_all"))
{
var sampleData = CreateSampleCbAdfData();
var example = sampleData[0];
Validate(vwSharedValidation, vwADFValidation, example);
var result = vw.LearnAndPredict(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
ReferenceEquals(example.ActionDependentFeatures[0], result[0]);
ReferenceEquals(example.ActionDependentFeatures[1], result[1]);
ReferenceEquals(example.ActionDependentFeatures[2], result[2]);
example = sampleData[1];
Validate(vwSharedValidation, vwADFValidation, example);
result = vw.LearnAndPredict(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
ReferenceEquals(example.ActionDependentFeatures[0], result[1]);
ReferenceEquals(example.ActionDependentFeatures[1], result[0]);
example = sampleData[2];
Validate(vwSharedValidation, vwADFValidation, example);
result = vw.Predict(example, example.ActionDependentFeatures);
ReferenceEquals(example.ActionDependentFeatures[0], result[1]);
ReferenceEquals(example.ActionDependentFeatures[1], result[0]);
}
}
[TestMethod]
public void TestSharedModel()
{
string cbadfModelFile = "models/cb_adf.model";
var sampleData = CreateSampleCbAdfData();
using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
using (var vwSharedValidation = new VowpalWabbitExampleValidator<DataString>("--cb_adf --rank_all"))
using (var vwADFValidation = new VowpalWabbitExampleValidator<DataStringADF>("--cb_adf --rank_all"))
{
foreach (DataString example in sampleData)
{
Validate(vwSharedValidation, vwADFValidation, example);
vw.Learn(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
}
vw.Native.SaveModel(cbadfModelFile);
}
// Get ground truth predictions
var expectedPredictions = new List<DataStringADF[]>();
using (var vw = new VowpalWabbit<DataString, DataStringADF>(string.Format("-t -i {0}", cbadfModelFile)))
{
foreach (DataString example in sampleData)
{
var pred = vw.Predict(example, example.ActionDependentFeatures);
if (pred == null)
expectedPredictions.Add(null);
else
{
expectedPredictions.Add(pred.Select(p => p.Feature).ToArray());
}
}
}
// Test synchronous VW instances using shared model
using (var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings("-t") { ModelStream = File.OpenRead(cbadfModelFile) }))
using (var vwShared1 = new VowpalWabbit<DataString, DataStringADF>(new VowpalWabbitSettings{ Model = vwModel }))
using (var vwShared2 = new VowpalWabbit<DataString, DataStringADF>(new VowpalWabbitSettings{ Model = vwModel }))
{
for (int i = 0; i < sampleData.Length; i++)
{
var actualPrediction = vwShared1.Predict(sampleData[i], sampleData[i].ActionDependentFeatures);
if (actualPrediction == null)
ReferenceEquals(expectedPredictions[i], actualPrediction);
else
ReferenceEquals(expectedPredictions[i], actualPrediction.Select(p => p.Feature).ToArray());
}
}
// Test concurrent VW instances using shared model and model pool
using (var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings("-t") { ModelStream = File.OpenRead(cbadfModelFile) }))
using (var vwPool = new VowpalWabbitThreadedPrediction<DataString, DataStringADF>(vwModel))
{
Parallel.For
(
fromInclusive: 0,
toExclusive: 20,
parallelOptions: new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 2 },
body: i =>
{
using (var vwObject = vwPool.GetOrCreate())
{
var actualPredictions = new List<DataStringADF[]>();
foreach (DataString example in sampleData)
{
actualPredictions.Add(vwObject.Value.Predict(example, example.ActionDependentFeatures).Select(p => p.Feature).ToArray());
}
Assert.AreEqual(expectedPredictions.Count, actualPredictions.Count);
for (int j = 0; j < expectedPredictions.Count; j++)
{
ReferenceEquals(expectedPredictions[j], actualPredictions[j]);
}
}
}
);
}
}
private DataString[] CreateSampleCbAdfData()
{
var sampleData = new DataString[3];
//shared | s_1 s_2
//0:1.0:0.5 | a_1 b_1 c_1
//| a_2 b_2 c_2
//| a_3 b_3 c_3
//| b_1 c_1 d_1
//0:0.0:0.5 | b_2 c_2 d_2
//| a_1 b_1 c_1
//| a_3 b_3 c_3
sampleData[0] = new DataString
{
Line = "shared | s_1 s_2",
Shared = new[] { "s_1", "s_2" },
ActionDependentFeatures = new[] {
new DataStringADF
{
Line = "0:1.0:0.5 | a_1 b_1 c_1",
Features = new[] { "a_1", "b_1", "c_1" },
},
new DataStringADF
{
Line = "| a_2 b_2 c_2",
Features = new [] { "a_2","b_2","c_2" }
},
new DataStringADF
{
Line = "| a_3 b_3 c_3",
Features = new [] { "a_3","b_3","c_3" }
},
},
SelectedActionIndex = 0,
Label = new ContextualBanditLabel
{
Cost = 1f,
Probability = .5f
}
};
sampleData[1] = new DataString
{
Line = string.Empty,
ActionDependentFeatures = new[] {
new DataStringADF
{
Line = "| b_1 c_1 d_1",
Features = new [] { "b_1","c_1","d_1" }
},
new DataStringADF
{
Line = "0:0.0:0.5 | b_2 c_2 d_2",
Features = new [] { "b_2", "c_2", "d_2" }
},
},
SelectedActionIndex = 1,
Label = new ContextualBanditLabel
{
Cost = 0f,
Probability = .5f
}
};
sampleData[2] = new DataString
{
Line = string.Empty,
ActionDependentFeatures = new[] {
new DataStringADF
{
Line = "| a_1 b_1 c_1 ",
Features = new [] { "a_1","b_1","c_1" }
},
new DataStringADF
{
Line = "| a_3 b_3 c_3",
Features = new [] { "a_3","b_3","c_3" }
}
}
};
return sampleData;
}
private static DataString[] CreateStringCbAdfData(int numSamples, int randomSeed = 0)
{
var random = new Random(randomSeed);
var sampleData = new DataString[numSamples];
for (int i = 0; i < numSamples; i++)
{
int numActions = random.Next(2, 5);
int[] fIndex = Enumerable.Range(1, numActions).OrderBy(ind => random.Next()).Take(numActions).ToArray();
var features = new string[numActions][];
for (int j = 0; j < numActions; j++)
{
features[j] = new string[]
{
"a_" + fIndex[j],
"b_" + fIndex[j],
"c_" + fIndex[j],
"d_" + fIndex[j]
};
}
var adf = new DataStringADF[numActions];
for (int j = 0; j < numActions; j++)
{
adf[j] = new DataStringADF { Features = features[j] };
}
sampleData[i] = new DataString
{
ActionDependentFeatures = adf,
SelectedActionIndex = random.Next(-1, numActions),
Label = new ContextualBanditLabel
{
Cost = (float)random.NextDouble(),
Probability = (float)random.NextDouble()
}
};
}
return sampleData;
}
private DataFloat[] CreateFloatCbAdfData(int numSamples, int randomSeed = 0)
{
var random = new Random(randomSeed);
var sampleData = new DataFloat[numSamples];
for (int i = 0; i < numSamples; i++)
{
int numActions = random.Next(2, 5);
int[] fIndex = Enumerable.Range(1, numActions).OrderBy(ind => random.Next()).Take(numActions).ToArray();
var features = new float[numActions][];
for (int j = 0; j < numActions; j++)
{
features[j] = new float[]
{
(fIndex[j] + 0) / (float)numActions,
(fIndex[j] + 1) / (float)numActions,
(fIndex[j] + 2) / (float)numActions,
(fIndex[j] + 3) / (float)numActions
};
}
var adf = new DataFloatADF[numActions];
for (int j = 0; j < numActions; j++)
{
adf[j] = new DataFloatADF { Features = features[j] };
}
sampleData[i] = new DataFloat
{
ActionDependentFeatures = adf,
SelectedActionIndex = random.Next(-1, numActions),
Label = new ContextualBanditLabel
{
Cost = (float)random.NextDouble(),
Probability = (float)random.NextDouble()
}
};
}
return sampleData;
}
public class DataString
{
public string Line { get; set; }
[Feature]
public string[] Shared { get; set; }
public IReadOnlyList<DataStringADF> ActionDependentFeatures { get; set; }
public int SelectedActionIndex { get; set; }
public ILabel Label { get; set; }
}
public class DataFloat
{
[Feature]
public string[] Shared { get; set; }
public IReadOnlyList<DataFloatADF> ActionDependentFeatures { get; set; }
public int SelectedActionIndex { get; set; }
public ILabel Label { get; set; }
}
public class DataStringADF
{
public string Line { get; set; }
[Feature]
public string[] Features { get; set; }
public override string ToString()
{
return string.Join(" ", this.Features);
}
}
public class DataFloatADF
{
[Feature]
public float[] Features { get; set; }
public override string ToString()
{
return string.Join(" ", this.Features);
}
}
}
}