-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TestExampleCache.cs
132 lines (116 loc) · 4.23 KB
/
TestExampleCache.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
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VW;
using VW.Labels;
using VW.Serializer;
using VW.Serializer.Attributes;
namespace cs_unittest
{
[TestClass]
public class TestExampleCacheCases : TestBase
{
#if DEBUG
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestExampleCacheForLearning()
{
try
{
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings { EnableExampleCaching = true }))
{
vw.Learn(new CachedData(), new SimpleLabel());
}
Assert.Fail("Expect NotSupportedException");
}
catch (NotSupportedException)
{
}
}
#else
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestExampleCacheForLearning()
{
try
{
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings { EnableExampleCaching = true }))
{
vw.Learn(new CachedData(), new SimpleLabel());
}
Assert.Fail("Expect NullReferenceException");
}
catch (NullReferenceException)
{
}
}
#endif
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestExampleCacheDisabledForLearning()
{
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings { EnableExampleCaching = false }))
{
vw.Learn(new CachedData(), new SimpleLabel());
}
}
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestExampleCache()
{
var random = new Random(123);
var examples = new List<CachedData>();
for (int i = 0; i < 1000; i++)
{
examples.Add(new CachedData
{
Label = new SimpleLabel { Label = 1 },
Feature = random.NextDouble()
});
var cachedData = new CachedData
{
Label = new SimpleLabel { Label = 2 },
Feature = 10 + random.NextDouble()
};
examples.Add(cachedData);
examples.Add(cachedData);
}
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings("-k -c --passes 10") { EnableExampleCaching = false }))
{
foreach (var example in examples)
{
var pred = vw.Learn(example, example.Label, VowpalWabbitPredictionType.Scalar);
//Console.WriteLine($"feature {example.Label.Label} <- {example.Feature}");
//Console.WriteLine($" pred {pred}");
}
vw.Native.RunMultiPass();
vw.Native.SaveModel("models/model1");
}
using (var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings("-t") { ModelStream = File.OpenRead("models/model1") }))
using (var vwCached = new VowpalWabbit<CachedData>(new VowpalWabbitSettings { Model = vwModel, EnableExampleCaching = true, MaxExampleCacheSize = 5 }))
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings { Model = vwModel, EnableExampleCaching = false }))
{
foreach (var example in examples)
{
var cachedPrediction = vwCached.Predict(example, VowpalWabbitPredictionType.Scalar);
var prediction = vw.Predict(example, VowpalWabbitPredictionType.Scalar);
Assert.AreEqual(prediction, cachedPrediction);
//Console.WriteLine($"{example.Label.Label} to {prediction} to {cachedPrediction} {example.Feature}");
Assert.AreEqual(example.Label.Label, Math.Round(prediction));
}
}
}
}
[Cacheable]
public class CachedData
{
[Feature]
public double Feature { get; set; }
public SimpleLabel Label
{
get;
set;
}
}
}