本文整理汇总了C#中VowpalWabbit.Predict方法的典型用法代码示例。如果您正苦于以下问题:C# VowpalWabbit.Predict方法的具体用法?C# VowpalWabbit.Predict怎么用?C# VowpalWabbit.Predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VowpalWabbit
的用法示例。
在下文中一共展示了VowpalWabbit.Predict方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteTest
public static void ExecuteTest(int testCaseNr, string args, string input, string stderr, string predictFile)
{
using (var vw = new VowpalWabbit(args))
{
var multiline = IsMultilineData(input);
using (var streamReader = Open(input))
{
if (multiline)
{
var lines = new List<string>();
string dataLine;
while ((dataLine = streamReader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(dataLine))
{
if (lines.Count > 0)
{
if (args.Contains("-t")) // test only
vw.Predict(lines);
else
vw.Learn(lines);
}
lines.Clear();
continue;
}
lines.Add(dataLine);
}
}
else
{
string dataLine;
while ((dataLine = streamReader.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(predictFile) && File.Exists(predictFile))
{
float actualValue;
if (args.Contains("-t")) // test only
actualValue = vw.Predict(dataLine, VowpalWabbitPredictionType.Scalar);
else
actualValue = vw.Learn(dataLine, VowpalWabbitPredictionType.Scalar);
}
else
vw.Learn(dataLine);
}
}
if (vw.Arguments.NumPasses > 1)
vw.RunMultiPass();
else
vw.EndOfPass();
if (!string.IsNullOrWhiteSpace(stderr) && File.Exists(stderr))
VWTestHelper.AssertEqual(stderr, vw.PerformanceStatistics);
}
}
}
示例2: TestNull1
public void TestNull1()
{
using (var vw = new VowpalWabbit<Context, ADF>("--cb_adf --rank_all --interact ab"))
{
var ctx = new Context()
{
ID = 25,
Vector = null,
ActionDependentFeatures = new[] {
new ADF {
ADFID = "23"
}
}.ToList()
};
vw.Learn(ctx, ctx.ActionDependentFeatures, 0, new ContextualBanditLabel()
{
Action = 1,
Cost = 1,
Probability = 0.2f
});
var result = vw.Predict(ctx, ctx.ActionDependentFeatures);
Assert.AreEqual(1, result.Length);
}
}
示例3: Test87
public void Test87()
{
using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
{
var sampleData = CreateSampleCbAdfData();
var example = sampleData[0];
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];
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];
result = vw.Predict(example, example.ActionDependentFeatures);
ReferenceEquals(example.ActionDependentFeatures[0], result[1]);
ReferenceEquals(example.ActionDependentFeatures[1], result[0]);
}
}
示例4: BasicExample
public void BasicExample()
{
using (var vw = new VowpalWabbit("--quiet"))
{
vw.Learn("1 |f 13:3.9656971e-02 24:3.4781646e-02 69:4.6296168e-02");
var prediction = vw.Predict("|f 13:3.9656971e-02 24:3.4781646e-02 69:4.6296168e-02", VowpalWabbitPredictionType.Scalar);
vw.SaveModel("output.model");
}
}
示例5: AnnotationExample
public static void AnnotationExample()
{
using (var vw = new VowpalWabbit<MyExample>(new VowpalWabbitSettings { EnableStringExampleGeneration = true }))
{
var ex = new MyExample { Income = 40, Age = 25 };
var label = new SimpleLabel { Label = 1 };
var str = vw.Serializer.Create(vw.Native).SerializeToString(ex, label);
// 1 |p Income:4 | Age25
vw.Learn(ex, label);
var prediction = vw.Predict(ex, VowpalWabbitPredictionType.Scalar);
}
}
示例6: Test1and2
public void Test1and2()
{
var references = File.ReadAllLines(@"pred-sets\ref\0001.predict").Select(l => float.Parse(l, CultureInfo.InvariantCulture)).ToArray();
var input = new List<Test1>();
using (var vwStr = new VowpalWabbit(" -k -c test1and2.str --passes 8 -l 20 --power_t 1 --initial_t 128000 --ngram 3 --skips 1 --invariant --holdout_off"))
using (var vw = new VowpalWabbit<Test1>(new VowpalWabbitSettings(" -k -c test1and2 --passes 8 -l 20 --power_t 1 --initial_t 128000 --ngram 3 --skips 1 --invariant --holdout_off")
{ EnableExampleCaching = false }))
using (var vwValidate = new VowpalWabbitExampleValidator<Test1>("-l 20 --power_t 1 --initial_t 128000 --ngram 3 --skips 1 --invariant --holdout_off"))
{
var lineNr = 0;
VWTestHelper.ParseInput(
File.OpenRead(@"train-sets\0001.dat"),
new MyListener(data =>
{
input.Add(data);
vwValidate.Validate(data.Line, data, data.Label);
var expected = vwStr.Learn(data.Line, VowpalWabbitPredictionType.Dynamic);
Assert.IsInstanceOfType(expected, typeof(float));
var actual = vw.Learn(data, data.Label, VowpalWabbitPredictionType.Scalar);
Assert.AreEqual((float)expected, actual, 1e-6, "Learn output differs on line: " + lineNr);
lineNr++;
}));
vwStr.RunMultiPass();
vw.Native.RunMultiPass();
vwStr.SaveModel("models/str0001.model");
vw.Native.SaveModel("models/0001.model");
VWTestHelper.AssertEqual(@"train-sets\ref\0001.stderr", vwStr.PerformanceStatistics);
VWTestHelper.AssertEqual(@"train-sets\ref\0001.stderr", vw.Native.PerformanceStatistics);
}
Assert.AreEqual(input.Count, references.Length);
using (var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings("-k -t --invariant") { ModelStream = File.OpenRead("models/0001.model") }))
using (var vwInMemoryShared1 = new VowpalWabbit(new VowpalWabbitSettings { Model = vwModel }))
using (var vwInMemoryShared2 = new VowpalWabbit<Test1>(new VowpalWabbitSettings { Model = vwModel }))
using (var vwInMemory = new VowpalWabbit(new VowpalWabbitSettings("-k -t --invariant") { ModelStream = File.OpenRead("models/0001.model") }))
using (var vwStr = new VowpalWabbit("-k -t -i models/str0001.model --invariant"))
using (var vwNative = new VowpalWabbit("-k -t -i models/0001.model --invariant"))
using (var vw = new VowpalWabbit<Test1>("-k -t -i models/0001.model --invariant"))
using (var vwModel2 = new VowpalWabbitModel("-k -t --invariant -i models/0001.model"))
using (var vwInMemoryShared3 = new VowpalWabbit<Test1>(new VowpalWabbitSettings { Model = vwModel2 }))
{
for (var i = 0; i < input.Count; i++)
{
var actualStr = vwStr.Predict(input[i].Line, VowpalWabbitPredictionType.Scalar);
var actualNative = vwNative.Predict(input[i].Line, VowpalWabbitPredictionType.Scalar);
var actualInMemory = vwInMemory.Predict(input[i].Line, VowpalWabbitPredictionType.Scalar);
var actual = vw.Predict(input[i], VowpalWabbitPredictionType.Scalar, input[i].Label);
var actualShared1 = vwInMemoryShared1.Predict(input[i].Line, VowpalWabbitPredictionType.Scalar);
var actualShared2 = vwInMemoryShared2.Predict(input[i], VowpalWabbitPredictionType.Scalar, input[i].Label);
var actualShared3 = vwInMemoryShared3.Predict(input[i], VowpalWabbitPredictionType.Scalar, input[i].Label);
Assert.AreEqual(references[i], actualStr, 1e-5);
Assert.AreEqual(references[i], actualNative, 1e-5);
Assert.AreEqual(references[i], actualInMemory, 1e-5);
Assert.AreEqual(references[i], actual, 1e-5);
Assert.AreEqual(references[i], actualShared1, 1e-5);
Assert.AreEqual(references[i], actualShared2, 1e-5);
Assert.AreEqual(references[i], actualShared3, 1e-5);
}
// due to shared usage the counters don't match up
//VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwInMemoryShared2.Native.PerformanceStatistics);
//VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwInMemoryShared1.PerformanceStatistics);
VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwInMemory.PerformanceStatistics);
VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwStr.PerformanceStatistics);
VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vw.Native.PerformanceStatistics);
}
}
示例7: RunAllTest
public void RunAllTest()
{
var runTests = File.ReadAllLines("RunTests")
.SkipWhile(l => l != "__DATA__");
Match match;
foreach (var line in runTests)
{
if (line.Trim().Length == 0)
{
if (skipTest)
{
Reset();
continue;
}
// execute test case
var argsBuilder = new StringBuilder(args);
var dataFile = ExtractArgument(argsBuilder, @"-d\s+(\S+)");
var testing = false;
if (dataFile == null)
{
dataFile = ExtractArgument(argsBuilder, @"-t\s+(\S+)");
testing = dataFile != null;
}
if (dataFile == null)
{
dataFile = ExtractArgument(argsBuilder, @"(\S+)$");
}
if (dataFile == null)
{
Console.WriteLine("Skipping test " + nr);
Reset();
continue;
}
ExtractArgument(argsBuilder, @"-p\s+(\S+)");
var model = ExtractArgument(argsBuilder, @"-f\s+(\S+)");
var multiPass = args.Contains("--passes");
List<float> expectedPredictions = null;
if (File.Exists(predict))
{
expectedPredictions = File.ReadLines(predict)
.Select(l => float.Parse(l.Split(' ')[0], CultureInfo.InvariantCulture))
.ToList();
}
else
{
if (testing)
{
Console.WriteLine("Skipping inconsistent test -t without .predict file");
Reset();
continue;
}
}
Console.WriteLine("Running test {0}: {1} using {2}", nr, comment, argsBuilder);
var lineNr = 0;
// TODO: check for -p predict
// TODO: need to check which prediction label it will be
using (var vw = new VowpalWabbit(argsBuilder.ToString()))
{
foreach (var dataLine in File.ReadLines(dataFile))
{
if (expectedPredictions != null)
{
var expectedValue = expectedPredictions[lineNr++];
float actualValue;
if (testing)
{
actualValue = vw.Predict(dataLine, VowpalWabbitPredictionType.Scalar);
}
else
{
actualValue = vw.Learn(dataLine, VowpalWabbitPredictionType.Scalar);
}
//Assert.AreEqual(
// expectedValue,
// actualValue,
// 1e-5,
// string.Format("Test {0}", nr));
}
else
{
vw.Learn(dataLine);
}
}
if (multiPass)
{
//.........这里部分代码省略.........
示例8: TestExampleCache
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()
});
}
for (int i = 0; i < 1000; i++)
{
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)
{
vw.Learn(example, example.Label);
}
vw.Native.RunMultiPass();
vw.Native.SaveModel("model1");
}
using (var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings("-t", modelStream: File.OpenRead("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);
Assert.AreEqual(example.Label.Label, Math.Round(prediction));
}
}
}
示例9: TestSharedModel
public void TestSharedModel()
{
string cbadfModelFile = "models/cb_adf.model";
var sampleData = CreateSampleCbAdfData();
using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
{
foreach (DataString example in sampleData)
{
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)
{
expectedPredictions.Add(vw.Predict(example, example.ActionDependentFeatures));
}
}
// 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++)
{
DataStringADF[] actualPrediction = vwShared1.Predict(sampleData[i], sampleData[i].ActionDependentFeatures);
ReferenceEquals(expectedPredictions[i], actualPrediction);
}
}
// 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));
}
Assert.AreEqual(expectedPredictions.Count, actualPredictions.Count);
for (int j = 0; j < expectedPredictions.Count; j++)
{
ReferenceEquals(expectedPredictions[j], actualPredictions[j]);
}
}
}
);
}
}
示例10: ExecuteTest
public static void ExecuteTest(int testCaseNr, string args, string input, string stderr, string predictFile)
{
using (var vw = new VowpalWabbit(args))
{
var multiline = IsMultilineData(input);
using (var streamReader = Open(input))
{
if (multiline)
{
var lines = new List<string>();
string dataLine;
while ((dataLine = streamReader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(dataLine))
{
if (lines.Count > 0)
{
if (args.Contains("-t")) // test only
vw.Predict(lines);
else
vw.Learn(lines);
}
lines.Clear();
continue;
}
lines.Add(dataLine);
}
}
else
{
int lineNr = 0;
string[] predictions = null;
if (File.Exists(predictFile))
predictions = File.ReadAllLines(predictFile);
string dataLine;
while ((dataLine = streamReader.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(predictFile) && File.Exists(predictFile))
{
object actualValue;
if (args.Contains("-t")) // test only
actualValue = vw.Predict(dataLine, VowpalWabbitPredictionType.Dynamic);
else
actualValue = vw.Learn(dataLine, VowpalWabbitPredictionType.Dynamic);
if (predictions != null)
{
// validate predictions
var actualFloat = actualValue as float?;
if (actualFloat != null)
{
var expectedPrediction = float.Parse(predictions[lineNr].Split(' ').First(), CultureInfo.InvariantCulture);
VWTestHelper.FuzzyEqual(expectedPrediction, (float)actualFloat, 1e-4, "Prediction mismatch");
}
var actualScalar = actualValue as VowpalWabbitScalar?;
if (actualScalar != null)
{
var expectedPredictions = predictions[lineNr]
.Split(' ')
.Select(field => float.Parse(field, CultureInfo.InvariantCulture))
.ToArray();
Assert.AreEqual(2, expectedPredictions.Length);
VWTestHelper.FuzzyEqual(expectedPredictions[0], actualScalar.Value.Value, 1e-4, "Prediction value mismatch");
VWTestHelper.FuzzyEqual(expectedPredictions[1], actualScalar.Value.Confidence, 1e-4, "Prediction confidence mismatch");
}
}
}
else
vw.Learn(dataLine);
lineNr++;
}
}
if (vw.Arguments.NumPasses > 1)
vw.RunMultiPass();
else
vw.EndOfPass();
if (!string.IsNullOrWhiteSpace(stderr) && File.Exists(stderr))
VWTestHelper.AssertEqual(stderr, vw.PerformanceStatistics);
}
}
}
示例11: Test1and2
public void Test1and2()
{
var references = File.ReadAllLines(@"pred-sets\ref\0001.predict").Select(l => float.Parse(l, CultureInfo.InvariantCulture)).ToArray();
var input = new List<Test1>();
using (var vwStr = new VowpalWabbit(" -k -c test1and2.str --passes 8 -l 20 --power_t 1 --initial_t 128000 --ngram 3 --skips 1 --invariant --holdout_off"))
using (var vw = new VowpalWabbit<Test1>(" -k -c test1and2 --passes 8 -l 20 --power_t 1 --initial_t 128000 --ngram 3 --skips 1 --invariant --holdout_off"))
{
var lineNr = 0;
VWTestHelper.ParseInput(
File.OpenRead(@"train-sets\0001.dat"),
new MyListener(data =>
{
input.Add(data);
var expected = vwStr.Learn<VowpalWabbitScalarPrediction>(data.Line);
using (var example = vw.ReadExample(data))
{
var actual = example.LearnAndPredict<VowpalWabbitScalarPrediction>();
Assert.AreEqual(expected.Value, actual.Value, 1e-6, "Learn output differs on line: " + lineNr);
}
lineNr++;
}));
vwStr.RunMultiPass();
vw.RunMultiPass();
vwStr.SaveModel("models/str0001.model");
vw.SaveModel("models/0001.model");
VWTestHelper.AssertEqual(@"train-sets\ref\0001.stderr", vwStr.PerformanceStatistics);
VWTestHelper.AssertEqual(@"train-sets\ref\0001.stderr", vw.PerformanceStatistics);
}
Assert.AreEqual(input.Count, references.Length);
using (var vwModel = new VowpalWabbitModel("-k -t --invariant", File.OpenRead("models/0001.model")))
using (var vwInMemoryShared1 = new VowpalWabbit(vwModel))
using (var vwInMemoryShared2 = new VowpalWabbit<Test1>(vwModel))
using (var vwInMemory = new VowpalWabbit("-k -t --invariant", File.OpenRead("models/0001.model")))
using (var vwStr = new VowpalWabbit("-k -t -i models/str0001.model --invariant"))
using (var vw = new VowpalWabbit<Test1>("-k -t -i models/0001.model --invariant"))
{
for (var i = 0; i < input.Count; i++)
{
var actualStr = vwStr.Predict<VowpalWabbitScalarPrediction>(input[i].Line);
var actualShared1 = vwInMemoryShared1.Predict<VowpalWabbitScalarPrediction>(input[i].Line);
var actualInMemory = vwInMemory.Predict<VowpalWabbitScalarPrediction>(input[i].Line);
using (var example = vw.ReadExample(input[i]))
using (var exampleInMemory2 = vwInMemoryShared2.ReadExample(input[i]))
{
var actual = example.Predict<VowpalWabbitScalarPrediction>();
var actualShared2 = exampleInMemory2.Predict<VowpalWabbitScalarPrediction>();
Assert.AreEqual(references[i], actualStr.Value, 1e-5);
Assert.AreEqual(references[i], actualShared1.Value, 1e-5);
Assert.AreEqual(references[i], actualInMemory.Value, 1e-5);
Assert.AreEqual(references[i], actual.Value, 1e-5);
Assert.AreEqual(references[i], actualShared2.Value, 1e-5);
}
}
// VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwInMemoryShared2.PerformanceStatistics);
//VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwInMemoryShared1.PerformanceStatistics);
VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwInMemory.PerformanceStatistics);
VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vwStr.PerformanceStatistics);
VWTestHelper.AssertEqual(@"test-sets\ref\0001.stderr", vw.PerformanceStatistics);
}
}
示例12: TestSaveLoadSkip
public void TestSaveLoadSkip()
{
using (var vw = new VowpalWabbit<SimpleData>("--binary -f saveload.model"))
{
for (int i = 0; i < 100; i++)
{
vw.Learn(new SimpleData { Value = 1 }, new SimpleLabel { Label = 1 });
vw.Learn(new SimpleData { Value = -1 }, new SimpleLabel { Label = -1 });
}
Assert.AreEqual(1, vw.Predict(new SimpleData { Value = 1 }, VowpalWabbitPredictionType.Scalar));
Assert.AreEqual(-1, vw.Predict(new SimpleData { Value = -1 }, VowpalWabbitPredictionType.Scalar));
}
using (var model = new VowpalWabbitModel(new VowpalWabbitSettings { Arguments = "--binary", ModelStream = File.Open("saveload.model", FileMode.Open) }))
using (var pool = new VowpalWabbitThreadedPrediction<SimpleData>(new VowpalWabbitSettings { Model = model }))
{
using (var vw = pool.GetOrCreate())
{
Assert.AreEqual(-1, vw.Value.Predict(new SimpleData { Value = -1 }, VowpalWabbitPredictionType.Scalar));
}
}
}