本文整理汇总了C#中VowpalWabbit.Learn方法的典型用法代码示例。如果您正苦于以下问题:C# VowpalWabbit.Learn方法的具体用法?C# VowpalWabbit.Learn怎么用?C# VowpalWabbit.Learn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VowpalWabbit
的用法示例。
在下文中一共展示了VowpalWabbit.Learn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: TestDynamic
public void TestDynamic()
{
// TODO: look into friend assemblies and how to figure if one is a friend
using (var vw = new VowpalWabbit("--cb_adf --rank_all"))
using (var vwDynamic = new VowpalWabbitDynamic(new VowpalWabbitSettings("--cb_adf --rank_all") { TypeInspector = JsonTypeInspector.Default }))
{
var expected = vw.Learn(new[] { "| q:1", "2:-3:0.9 | q:2", "| q:3" }, VowpalWabbitPredictionType.ActionScore);
var actual = vwDynamic.Learn(
new
{
_multi = new[]
{
new { q = 1 },
new { q = 2 },
new { q = 3 }
}
},
VowpalWabbitPredictionType.ActionScore,
new ContextualBanditLabel(0, -3, 0.9f),
1);
AssertAreEqual(expected, actual);
expected = vw.Learn(new[] { "| q:1", "2:-5:0.9 | q:2", "| q:3" }, VowpalWabbitPredictionType.ActionScore);
actual = vwDynamic.Learn(
new
{
_multi = new[]
{
new { q = 1 },
new { q = 2 },
new { q = 3 }
}
},
VowpalWabbitPredictionType.ActionScore,
new ContextualBanditLabel(0, -5, 0.9f),
1);
AssertAreEqual(expected, actual);
expected = vw.Learn(new[] { "| q:1", "| q:2", "3:-2:0.8 | q:3" }, VowpalWabbitPredictionType.ActionScore);
actual = vwDynamic.Learn(
new
{
_multi = new[]
{
new { q = 1 },
new { q = 2 },
new { q = 3 }
},
_labelIndex = 2,
_label_Action = 3,
_label_Cost = -2,
_label_Probability = 0.8
},
VowpalWabbitPredictionType.ActionScore);
AssertAreEqual(expected, actual);
}
}
示例3: SetupVW
private VowpalWabbit SetupVW()
{
var vw = new VowpalWabbit(" --probabilities --loss_function=logistic --oaa 3");
vw.Learn("1 | a b");
vw.Learn("2 | a c");
vw.Learn("3 | c b e");
return vw;
}
示例4: TestExampleCacheForLearning
public void TestExampleCacheForLearning()
{
using (var vw = new VowpalWabbit<CachedData>(string.Empty))
{
vw.Learn(new CachedData(), new SimpleLabel());
}
}
示例5: 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);
}
}
示例6: TestExampleCacheDisabledForLearning
public void TestExampleCacheDisabledForLearning()
{
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings(enableExampleCaching: false)))
{
vw.Learn(new CachedData(), new SimpleLabel());
}
}
示例7: Ingest
private static void Ingest(VowpalWabbit vw, IEnumerable<List<string>> blocks)
{
foreach (var block in blocks)
{
vw.Learn(block);
}
vw.EndOfPass();
}
示例8: 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");
}
}
示例9: ProfilePerformanceWithStringData
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);
}
示例10: TestExampleCacheForLearning
public void TestExampleCacheForLearning()
{
try
{
using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings(string.Empty, enableExampleCaching: true)))
{
vw.Learn(new CachedData(), new SimpleLabel());
}
Assert.Fail("Expect NullReferenceException");
}
catch (NullReferenceException)
{
}
}
示例11: 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);
}
}
示例12: TestConsole
public void TestConsole()
{
var arrayModelPath = Path.GetTempFileName();
var newlineModelPath = Path.GetTempFileName();
var nativeModelPath = Path.GetTempFileName();
// Note: deployment item is not working on build server
cs_vw.Program.Main(new[] { @"..\cs\unittest\json\test_array.json", "-f", arrayModelPath });
cs_vw.Program.Main(new[] { @"..\cs\unittest\json\test_newline.json", "-f", newlineModelPath });
// compare model
using (var vw = new VowpalWabbit("-f " + nativeModelPath))
{
vw.Learn("1 | f:1");
vw.Learn("0 | f:2");
}
var arrayModel = File.ReadAllBytes(arrayModelPath);
var newlineModel = File.ReadAllBytes(newlineModelPath);
var nativeModel = File.ReadAllBytes(nativeModelPath);
CollectionAssert.AreEqual(nativeModel, arrayModel);
CollectionAssert.AreEqual(newlineModel, arrayModel);
}
示例13: TestMemoryLeak
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);
}
}
示例14: TestNull3
public void TestNull3()
{
using (var vw = new VowpalWabbit<Context, ADF>("--cb_adf --rank_all --interact ac"))
{
var ctx = new Context()
{
ID = 25,
Vector = new float[] { 3 },
VectorC = new float[] { 2, 2, 3 },
ActionDependentFeatures = new[] {
new ADF {
ADFID = "23",
}
}.ToList()
};
var label = new ContextualBanditLabel() {
Action = 1,
Cost= 1,
Probability = 0.2f
};
vw.Learn(ctx, ctx.ActionDependentFeatures, 0, label);
ctx.Vector = null;
vw.Learn(ctx, ctx.ActionDependentFeatures, 0, label);
ctx.Vector = new float[] { 2 };
ctx.VectorC = null;
vw.Learn(ctx, ctx.ActionDependentFeatures, 0, label);
ctx.Vector = null;
vw.Learn(ctx, ctx.ActionDependentFeatures, 0, label);
}
}
示例15: 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);
}
}