Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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]);
            }
        }
Exemplo n.º 3
0
        public void TestArguments()
        {
            using (var vw = new VowpalWabbit(new VowpalWabbitSettings("--cb_explore_adf --epsilon 0.3 --interact ud") { Verbose = true }))
            {
                // --cb_explore_adf --epsilon 0.3 --interact ud --cb_adf--csoaa_ldf multiline --csoaa_rank
                Console.WriteLine(vw.Arguments.CommandLine);
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--cb_explore_adf"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--epsilon 0.3"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--interact ud"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--csoaa_ldf multiline"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--csoaa_rank"));
                vw.SaveModel("args.model");
            }

            using (var vw = new VowpalWabbit(new VowpalWabbitSettings { ModelStream = File.Open("args.model", FileMode.Open) }))
            {
                Console.WriteLine(vw.Arguments.CommandLine);
                // --no_stdin--bit_precision 18--cb_explore_adf--epsilon 0.300000--cb_adf--cb_type ips --csoaa_ldf multiline--csoaa_rank--interact ud

                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--no_stdin"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--bit_precision 18"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--cb_explore_adf"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--epsilon 0.3"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--interact ud"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--csoaa_ldf multiline"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--csoaa_rank"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--cb_type ips"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--csoaa_ldf multiline"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--interact ud"));
                Assert.IsTrue(vw.Arguments.CommandLine.Contains("--csoaa_rank"));
            }
        }
Exemplo n.º 4
0
        public void TestID()
        {
            using (var vw = new VowpalWabbit("--id abc"))
            {
                Assert.AreEqual("abc", vw.ID);

                vw.SaveModel("model");

                vw.ID = "def";
                vw.SaveModel("model.1");
            }

            using (var vw = new VowpalWabbit("-i model"))
            {
                Assert.AreEqual("abc", vw.ID);
            }

            using (var vw = new VowpalWabbit("-i model.1"))
            {
                Assert.AreEqual("def", vw.ID);
            }

            using (var vwm = new VowpalWabbitModel("-i model.1"))
            {
                Assert.AreEqual("def", vwm.ID);
                using (var vw = new VowpalWabbit(new VowpalWabbitSettings(model: vwm)))
                {
                    Assert.AreEqual("def", vw.ID);
                    Assert.AreEqual(vwm.ID, vw.ID);
                }
            }
        }
Exemplo n.º 5
0
        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);
            }
        }
        public async Task<HttpResponseMessage> Post()
        {
            var header = this.Request.Headers.SingleOrDefault(x => x.Key == "Authorization");

            if (header.Value == null)
                throw new UnauthorizedAccessException("AuthorizationToken missing");

            var userToken = header.Value.First();

            if (string.IsNullOrWhiteSpace(userToken) || userToken != ConfigurationManager.AppSettings["UserToken"])
                return Request.CreateResponse(HttpStatusCode.Unauthorized);

            if (this.metaData == null || lastDownload + TimeSpan.FromMinutes(1) < DateTime.Now)
            {
                var url = ConfigurationManager.AppSettings["DecisionServiceSettingsUrl"];
                this.metaData = ApplicationMetadataUtil.DownloadMetadata<ApplicationClientMetadata>(url);
                lastDownload = DateTime.Now;
            }

            using (var vw = new VowpalWabbit(new VowpalWabbitSettings(metaData.TrainArguments)
            {
                EnableStringExampleGeneration = true,
                EnableStringFloatCompact = true
            }))
            using (var serializer = new VowpalWabbitJsonSerializer(vw))
            using (var example = serializer.ParseAndCreate(new JsonTextReader(new StreamReader(await Request.Content.ReadAsStreamAsync()))))
            {
                return Request.CreateResponse(HttpStatusCode.OK, example.VowpalWabbitString);
            }
        }
 public void TestExampleCacheForLearning()
 {
     using (var vw = new VowpalWabbit<CachedData>(string.Empty))
     {
         vw.Learn(new CachedData(), new SimpleLabel());
     }
 }
Exemplo n.º 8
0
        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);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="VowpalWabbitJson"/> class.
        /// </summary>
        /// <param name="vw">The VW native instance.</param>
        public VowpalWabbitJsonSerializer(VowpalWabbit vw)
        {
            Contract.Requires(vw != null);

            this.vw = vw;
            this.defaultMarshaller = new VowpalWabbitDefaultMarshaller();
            this.jsonSerializer = new JsonSerializer();
        }
 public DisclaimerPredict(string modelName, string dictionary, int dictionarySize, float cutoff)
 {
     //--quiet
     // _vw = new VowpalWabbit(string.Format("-t -i {0} --quiet", modelName));
     _vw = new VowpalWabbit(string.Format("-f {0} --loss_function logistic --passes 25 -c -l2", modelName));
     _dictionary = new WordDictionary(dictionary, dictionarySize);
     _cutoff = cutoff;
 }
Exemplo n.º 11
0
        public void TestExampleCacheDisabledForLearning()
        {
            using (var vw = new VowpalWabbit<CachedData>(new VowpalWabbitSettings(enableExampleCaching: false)))
            {
                vw.Learn(new CachedData(), new SimpleLabel());

            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="vwModel">Optional model to see multiworld testing</param>
        public VowpalWabbitMultiworldTesting(Stream vwModel = null)
        {
            var settings = vwModel == null ?
                new VowpalWabbitSettings("--multiworld_test f") :
                new VowpalWabbitSettings { ModelStream = vwModel };

            this.vw = new VowpalWabbit<LearnedVsConstantPolicy>(settings);
        }
 internal static void Validate(string line, VowpalWabbitExample ex, IVowpalWabbitLabelComparator comparator, string args = null)
 {
     using (var vw = new VowpalWabbit(args))
     using (var strExample = vw.ParseLine(line))
     {
         var diff = strExample.Diff(vw, ex, comparator);
         Assert.IsNull(diff, diff + " generated string: '" + ex.VowpalWabbitString + "'");
     }
 }
Exemplo n.º 14
0
        private static void Ingest(VowpalWabbit vw, IEnumerable<List<string>> blocks)
        {
            foreach (var block in blocks)
            {
                vw.Learn(block);
            }

            vw.EndOfPass();
        }
Exemplo n.º 15
0
        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);
            }
        }
Exemplo n.º 16
0
        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");
            }
        }
        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;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="VowpalWabbitJson"/> class.
        /// </summary>
        /// <param name="vw">The VW native instance.</param>
        public VowpalWabbitJsonSerializer(VowpalWabbit vw)
        {
            Contract.Requires(vw != null);

            this.vw = vw;
            this.defaultMarshaller = new VowpalWabbitDefaultMarshaller();
            this.jsonSerializer = new JsonSerializer();

            this.Context = new VowpalWabbitMarshalContext(this.vw);
            this.DefaultNamespaceContext = new VowpalWabbitMarshalContext(this.vw, this.Context.ExampleBuilder);
        }
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (this.vw != null)
         {
             this.vw.Dispose();
             this.vw = null;
         }
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="VowpalWabbitMarshalContext"/> class.
        /// </summary>
        /// <param name="vw">The VW instance the example will be imported to.</param>
        /// <param name="exampleBuilder">A shared example builder.</param>
        /// <param name="dictionary">Dictionary used for dictify operation.</param>
        /// <param name="fastDictionary">Dictionary used for dictify operation.</param>
        public VowpalWabbitMarshalContext(VowpalWabbit vw, VowpalWabbitExampleBuilder exampleBuilder, Dictionary<string, string> dictionary = null, Dictionary<object, string> fastDictionary = null)
        {
            this.VW = vw;
            this.ExampleBuilder = exampleBuilder;

            if (vw.Settings.EnableStringExampleGeneration)
            {
                this.StringExample = new StringBuilder();
                this.Dictionary = dictionary;
                this.FastDictionary = fastDictionary;
            }
        }
Exemplo n.º 21
0
            public MyListener(VowpalWabbit<Data> vw)
            {
                this.vw = vw;

                // re-use example object
                this.example = new Data()
                {
                    Features = new List<KeyValuePair<string, float>>()
                };

                this.stopwatch = new Stopwatch();
            }
Exemplo n.º 22
0
        public void TestJsonFeatureExtraction()
        {
            string json = "{\"ns1\":{\"location\":\"New York\", \"f2\":3.4}}";

            using (var vw = new VowpalWabbit("-b 3 --noconstant"))
            using (var serializer = new VowpalWabbitJsonSerializer(vw))
            using (var result = serializer.ParseAndCreate(json))
            {
                var singleExample = result as VowpalWabbitSingleLineExampleCollection;
                Assert.IsNotNull(singleExample);
                if (singleExample != null)
                {
                    foreach (var ns in singleExample.Example)
                    {
                        Console.WriteLine(ns.Index);

                        foreach (var feature in ns)
                        {
                            Console.WriteLine("{0}:{1}", feature.WeightIndex, feature.X);
                        }
                    }

                    var ns1 = singleExample.Example.ToArray();
                    Assert.AreEqual(1, ns1.Length);
                    Assert.AreEqual((byte)'n', ns1[0].Index);
                    CollectionAssert.AreEqual(
                            new[] {
                                new VowpalWabbitFeature(1, 12),
                                new VowpalWabbitFeature(3.4f, 28)
                            },
                            ns1[0].ToArray());
                }

                // for documentation purpose only
                var multiExample = result as VowpalWabbitMultiLineExampleCollection;
                Assert.IsNull(multiExample);
                if (multiExample != null)
                {
                    foreach (var example in multiExample.Examples)
                    {
                        foreach (var ns in example)
                        {
                            Console.WriteLine(ns.Index);

                            foreach (var feature in ns)
                            {
                                Console.WriteLine("{0}:{1}", feature.WeightIndex, feature.X);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 23
0
        public void TestJsonArray()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator())
            {
                validator.Validate("| :1 :2.3 :4", "{\"a\":[1,2.3,4]}");
                validator.Validate("|a :1 :2.3 :4", "{\"a\":{\"b\":[1,2.3,4]}}");
            }

            using (var vw = new VowpalWabbit("--json"))
            {
                AssertThrow(() => vw.ParseJson("{\"a\":{\"b\":[1,[1,2],4]}}"));
            }
        }
        /// <summary>
        /// Initializes a new instance of <see cref="VowpalWabbitJsonBuilder"/>.
        /// </summary>
        public VowpalWabbitJsonBuilder(IVowpalWabbitExamplePool vwPool, VowpalWabbitDefaultMarshaller defaultMarshaller, JsonSerializer jsonSerializer)
        {
            Contract.Requires(vw != null);
            Contract.Requires(defaultMarshaller != null);
            Contract.Requires(jsonSerializer != null);

            this.vw = vwPool.Native;
            this.defaultMarshaller = new VowpalWabbitDefaultMarshaller();
            this.jsonSerializer = new JsonSerializer();

            this.Context = new VowpalWabbitMarshalContext(vwPool);
            this.DefaultNamespaceContext = new VowpalWabbitMarshalContext(this.vw, this.Context.ExampleBuilder);
        }
Exemplo n.º 25
0
        public void TestEmptyID()
        {
            using (var vw = new VowpalWabbit("-l 1"))
            {
                Assert.AreEqual(string.Empty, vw.ID);

                vw.SaveModel("model");
            }

            using (var vw = new VowpalWabbit("-f model"))
            {
                Assert.AreEqual(string.Empty, vw.ID);
            }
        }
Exemplo n.º 26
0
 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);
 }
Exemplo n.º 27
0
        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)
            {
            }
        }
Exemplo n.º 28
0
        public void TestArguments()
        {
            using (var vw = new VowpalWabbit(new VowpalWabbitSettings("--cb_adf --rank_all --interact ud") { Verbose = true }))
            {
                Assert.AreEqual("--cb_adf --rank_all --interact ud --csoaa_ldf multiline --csoaa_rank", vw.Arguments.CommandLine);

                vw.SaveModel("args.model");
            }

            using (var vw = new VowpalWabbit(new VowpalWabbitSettings { ModelStream = File.Open("args.model", FileMode.Open) }))
            {
                Assert.AreEqual("--no_stdin --max_prediction 1 --bit_precision 18 --cb_adf --cb_type ips --rank_all --csoaa_ldf multiline --interact ud --csoaa_rank",
                    vw.Arguments.CommandLine);
            }
        }
Exemplo n.º 29
0
        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);
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Initializes a new <see cref="Namespace"/> instance.
        /// </summary>
        /// <param name="vw">VopwpalWabbit instance used for hashing.</param>
        /// <param name="name">The namespace name. First character is treated as feature group. Defaults to space.</param>
        public Namespace(VowpalWabbit vw, string name = null)
        {
            if (string.IsNullOrWhiteSpace(name))
                name = VowpalWabbitConstants.DefaultNamespace.ToString();

            if (name.Length > 1)
                this.Name = name.Substring(1);

            this.FeatureGroup = name[0];

            this.NamespaceHash = vw.HashSpace(name);

            if (vw.Settings.EnableStringExampleGeneration)
                this.NamespaceString = " |" + name;
        }
Exemplo n.º 31
0
        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);
                }
                vw.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));
                }
            }

            // Test synchronous VW instances using shared model
            using (var vwModel = new VowpalWabbitModel("-t", File.OpenRead(cbadfModelFile)))
                using (var vwShared1 = new VowpalWabbit <DataString, DataStringADF>(vwModel))
                    using (var vwShared2 = new VowpalWabbit <DataString, DataStringADF>(vwModel))
                    {
                        for (int i = 0; i < sampleData.Length; i++)
                        {
                            DataStringADF[] actualPrediction = vwShared1.Predict(sampleData[i]);
                            Assert.ReferenceEquals(expectedPredictions[i], actualPrediction);
                        }
                    }

            // Test concurrent VW instances using shared model and model pool
            using (var vwModel = new VowpalWabbitModel("-t", File.OpenRead(cbadfModelFile)))
                using (var vwPool = new ObjectPool <VowpalWabbit <DataString, DataStringADF> >(new VowpalWabbitFactory <DataString, DataStringADF>(vwModel)))
                {
                    Parallel.For
                    (
                        fromInclusive: 0,
                        toExclusive: 20,
                        parallelOptions: new ParallelOptions {
                        MaxDegreeOfParallelism = Environment.ProcessorCount * 2
                    },
                        body: i =>
                    {
                        using (PooledObject <VowpalWabbit <DataString, DataStringADF> > vwObject = vwPool.Get())
                        {
                            var actualPredictions = new List <DataStringADF[]>();
                            foreach (DataString example in sampleData)
                            {
                                actualPredictions.Add(vwObject.Value.Predict(example));
                            }

                            Assert.AreEqual(expectedPredictions.Count, actualPredictions.Count);
                            for (int j = 0; j < expectedPredictions.Count; j++)
                            {
                                Assert.ReferenceEquals(expectedPredictions[j], actualPredictions[j]);
                            }
                        }
                    }
                    );
                }
        }
Exemplo n.º 32
0
        internal VowpalWabbitSingleExampleSerializer(VowpalWabbitSingleExampleSerializerCompiler <TExample> compiler, VowpalWabbit vw)
        {
            if (compiler == null)
            {
                throw new ArgumentNullException("compiler");
            }
            Contract.Ensures(vw != null);
            Contract.EndContractBlock();

            this.vw       = vw;
            this.compiler = compiler;

            this.serializerFunc = compiler.Func(vw);

            var cacheableAttribute = (CacheableAttribute)typeof(TExample).GetCustomAttributes(typeof(CacheableAttribute), true).FirstOrDefault();

            if (cacheableAttribute == null)
            {
                return;
            }

            if (this.vw.Settings.EnableExampleCaching)
            {
                if (cacheableAttribute.EqualityComparer == null)
                {
                    this.exampleCache = new Dictionary <TExample, CacheEntry>();
                }
                else
                {
                    if (!typeof(IEqualityComparer <TExample>).IsAssignableFrom(cacheableAttribute.EqualityComparer))
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      CultureInfo.InvariantCulture,
                                      "EqualityComparer ({1}) specified in [Cachable] of {0} must implement IEqualityComparer<{0}>",
                                      typeof(TExample),
                                      cacheableAttribute.EqualityComparer));
                    }

                    var comparer = (IEqualityComparer <TExample>)Activator.CreateInstance(cacheableAttribute.EqualityComparer);
                    this.exampleCache = new Dictionary <TExample, CacheEntry>(comparer);
                }

#if DEBUG
                this.reverseLookup = new Dictionary <VowpalWabbitExample, CacheEntry>(new ReferenceEqualityComparer <VowpalWabbitExample>());
#endif
            }
        }
Exemplo n.º 33
0
        public static void Run(string ml_args, int tot_iter, int mod_iter, int rnd_seed = 0, int numContexts = 10, int numActions = 10, float minP = 0.03f, float maxP = 0.04f, float noClickCost = 0.0f, float clickCost = -1.0f, int pStrategy = 0)
        {
            // byte buffer outside so one can change the example and keep the memory around
            var exampleBuffer = new byte[32 * 1024];

            var randGen = new Random(rnd_seed);

            var simExamples = Enumerable.Range(0, numContexts)
                              .Select(i => new SimulatorExample(numActions, i, minP, maxP))
                              .ToArray();

            var   scorerPdf            = new float[numActions];
            int   clicks               = 0;
            int   goodActions          = 0;
            int   goodActionsSinceLast = 0;
            float cost;

            using (var learner = new VowpalWabbit(ml_args + " --quiet"))
            {
                for (int i = 1; i <= tot_iter; i++)
                {
                    // sample uniformly among contexts
                    int contextIndex = randGen.Next(simExamples.Length);
                    var simExample   = simExamples[contextIndex];
                    var costPdf      = simExample.PDF;

                    using (var ex = simExample.CreateExample(learner))
                    {
                        var scores = ex.Predict(VowpalWabbitPredictionType.ActionProbabilities, learner);

                        var total = 0.0;
                        foreach (var actionScore in scores)
                        {
                            total += actionScore.Score;
                            scorerPdf[actionScore.Action] = actionScore.Score;
                        }

                        var  draw      = randGen.NextDouble() * total;
                        var  sum       = 0.0;
                        uint topAction = 0;
                        foreach (var actionScore in scores)
                        {
                            sum += actionScore.Score;
                            if (sum > draw)
                            {
                                topAction = actionScore.Action;
                                break;
                            }
                        }

                        if (topAction == contextIndex)
                        {
                            goodActions          += 1;
                            goodActionsSinceLast += 1;
                        }

                        // simulate click/noClick behavior
                        if (randGen.NextDouble() < costPdf[topAction])
                        {
                            cost    = clickCost;
                            clicks += 1;
                        }
                        else
                        {
                            cost = noClickCost;
                        }

                        float pReported = scorerPdf[topAction];
                        switch (pStrategy)
                        {
                        case 1:
                            pReported = 1.0f / numActions;
                            break;

                        case 2:
                            pReported = Math.Max(pReported, 0.5f);
                            break;

                        case 6:
                            pReported = Math.Max(pReported, 0.9f);
                            break;

                        case 7:
                            pReported = 0.9f;
                            break;

                        case 13:
                            pReported = 0.5f;
                            break;

                        case 14:
                            pReported = Math.Max(pReported, 0.1f);
                            break;
                        }

                        ex.Examples[topAction].Label = new ContextualBanditLabel(topAction, cost, pReported);

                        // invoke learning
                        var oneStepAheadScores = ex.Learn(VowpalWabbitPredictionType.ActionProbabilities, learner);

                        if (i % mod_iter == 0 || i == tot_iter)
                        {
                            Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", ml_args, numActions, numContexts, noClickCost, clickCost, pStrategy, rnd_seed, i, clicks / (float)i, goodActions, goodActionsSinceLast);

                            goodActionsSinceLast = 0;
                        }
                    }
                }
            }
        }
Exemplo n.º 34
0
 public VowpalWabbitStreamWriter(Stream stream, Encoding encoding, string arguments) : base(stream, encoding)
 {
     vw = new VowpalWabbit(new VowpalWabbitSettings {
         Arguments = arguments, EnableStringExampleGeneration = true, EnableStringFloatCompact = true
     });
 }
 /// <summary>
 /// Creates a bound serializers.
 /// </summary>
 /// <param name="vw">The vw instance to bind to.</param>
 /// <returns></returns>
 IVowpalWabbitSerializer <TExample> IVowpalWabbitSerializerCompiler <TExample> .Create(VowpalWabbit vw)
 {
     return(this.Create(vw));
 }
Exemplo n.º 36
0
        // [TestMethod]
        // [TestCategory("Vowpal Wabbit")]
        public void TestJsonDictThreading()
        {
            var jsonResolver = new RefResolve();
            var settings     = new JsonSerializerSettings {
                ReferenceResolverProvider = () => jsonResolver
            };

            var rnd      = new Random(123);
            var examples = new List <Context>();

            var id = 0;

            // different reference objects
            for (int i = 0; i < 10; i++)
            {
                var data = Enumerable.Range(1, 5).Select(_ => (float)rnd.Next(10)).ToArray();

                // referencing the same data
                for (int j = 0; j < 5; j++)
                {
                    examples.Add(new Context(data, id++, settings));
                }
            }

            for (int i = 0; i < 4; i++)
            {
                Permute(examples, rnd);

                for (int maxDegreeOfParallelism = 1; maxDegreeOfParallelism < 4; maxDegreeOfParallelism++)
                {
                    var examplesFound = 0;
                    using (var vw = new VowpalWabbit(new VowpalWabbitSettings {
                        EnableStringExampleGeneration = true, EnableThreadSafeExamplePooling = true
                    }))
                        using (var resolver = new VowpalWabbitJsonReferenceResolver(serializer =>
                        {
                            using (var example = serializer.CreateExamples())
                            {
                                ValidateExample(example, (Context)serializer.UserContext);
                            }

                            serializer.Dispose();

                            Interlocked.Increment(ref examplesFound);
                        }))
                        {
                            Parallel.ForEach(
                                Partitioner.Create(0, examples.Count),
                                new ParallelOptions {
                                MaxDegreeOfParallelism = maxDegreeOfParallelism
                            },
                                range =>
                            {
                                for (int j = range.Item1; j < range.Item2; j++)
                                {
                                    var ctx        = examples[j];
                                    var serializer = new VowpalWabbitJsonSerializer(vw, resolver)
                                    {
                                        UserContext = ctx
                                    };

                                    var example = serializer.ParseAndCreate(ctx.JSON);

                                    // example not ready yet
                                    if (example == null)
                                    {
                                        continue;
                                    }

                                    ValidateExample(example, ctx);

                                    example.Dispose();
                                    serializer.Dispose();

                                    Interlocked.Increment(ref examplesFound);
                                }
                            });
                        }

                    Assert.AreEqual(examples.Count, examplesFound);
                }
            }
        }
Exemplo n.º 37
0
        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 --cache_file 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 --cache_file 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);
                                            }
        }
Exemplo n.º 38
0
        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]);
                            }
                        }
                    }
                    );
                }
        }
 /// <summary>
 /// Creates a bound serializers.
 /// </summary>
 /// <param name="vw">The vw instance to bind to.</param>
 /// <returns></returns>
 public VowpalWabbitSerializer <TExample> Create(VowpalWabbit vw)
 {
     return(new VowpalWabbitSerializer <TExample>(this, vw));
 }
Exemplo n.º 40
0
 internal static HandleRef getCPtr(VowpalWabbit obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }
Exemplo n.º 41
0
        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 <VowpalWabbitScalarPrediction>(dataLine).Value;
                                }
                                else
                                {
                                    actualValue = vw.Learn <VowpalWabbitScalarPrediction>(dataLine).Value;
                                }

                                //Assert.AreEqual(
                                //    expectedValue,
                                //    actualValue,
                                //    1e-5,
                                //    string.Format("Test {0}", nr));
                            }
                            else
                            {
                                vw.Learn(dataLine);
                            }
                        }

                        if (multiPass)
                        {
                            vw.RunMultiPass();
                        }

                        if (model != null)
                        {
                            vw.SaveModel(model);
                        }

                        VWTestHelper.AssertEqual(stderr, vw.PerformanceStatistics);
                    }

                    // reset
                    Reset();
                }
                else if ((match = Regex.Match(line, @"^# Test (?<nr>\d+):(?<comment>.*)?$")).Success)
                {
                    nr      = match.Groups["nr"].Value;
                    comment = match.Groups["comment"].Value;
                }
                else if ((match = Regex.Match(line, @"^\{VW\} (?<args>.*)$")).Success)
                {
                    args = match.Groups["args"].Value;
                }
                else if (line.EndsWith(".stdout"))
                {
                    stderr = line.Trim();
                }
                else if (line.EndsWith(".stderr"))
                {
                    stderr = line.Trim();
                }
                else if (line.EndsWith(".predict"))
                {
                    predict = line.Trim();
                }
                else if (line.StartsWith("#") && line.Contains("SkipC#"))
                {
                    skipTest = true;
                }
            }
        }
Exemplo n.º 42
0
        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>("-k -c --passes 10", new VowpalWabbitSerializerSettings
            {
                EnableExampleCaching = false
            }))
            {
                foreach (var example in examples)
                {
                    using (var vwExample = vw.ReadExample(example))
                    {
                        vwExample.Learn();
                    }
                }

                vw.RunMultiPass();
                vw.SaveModel("model1");
            }

            using (var vwModel = new VowpalWabbitModel("-t", File.OpenRead("model1")))
                using (var vwCached = new VowpalWabbit <CachedData>(vwModel, new VowpalWabbitSerializerSettings {
                    EnableExampleCaching = true, MaxExampleCacheSize = 5
                }))
                    using (var vw = new VowpalWabbit <CachedData>(vwModel, new VowpalWabbitSerializerSettings {
                        EnableExampleCaching = false
                    }))
                    {
                        foreach (var example in examples)
                        {
                            using (var vwCachedExample = vwCached.ReadExample(example))
                                using (var vwExample = vw.ReadExample(example))
                                {
                                    var cachedPrediction = vwCachedExample.Predict <VowpalWabbitScalarPrediction>();
                                    var prediction       = vwExample.Predict <VowpalWabbitScalarPrediction>();

                                    Assert.AreEqual(prediction.Value, cachedPrediction.Value);
                                    Assert.AreEqual(example.Label.Label, Math.Round(prediction.Value));
                                }
                        }
                    }
        }
Exemplo n.º 43
0
 public void TestPrivateClassException()
 {
     using (var vw = new VowpalWabbit <PrivateClass>(""))
     {
     }
 }
Exemplo n.º 44
0
        public void TrainNewVWModelWithMultiActionJsonDirectData()
        {
            int numLocations = 2; // user location

            string[] locations = new string[] { "HealthyTown", "LessHealthyTown" };

            int numActions            = 3; // food item
            int numExamplesPerActions = 10000;
            var recorder = new FoodRecorder();

            using (var vw = new VowpalWabbit <FoodContext>(
                       new VowpalWabbitSettings("--cb_explore_adf --epsilon 0.2 --cb_type dr -q ::")
            {
                TypeInspector = JsonTypeInspector.Default,
                EnableStringExampleGeneration = true,
                EnableStringFloatCompact = true
            }))
            {
                // Learn
                var rand = new Random(0);
                for (int iE = 0; iE < numExamplesPerActions * numLocations; iE++)
                {
                    DateTime timeStamp = DateTime.UtcNow;

                    int iL = rand.Next(0, numLocations);

                    var context = new FoodContext {
                        Actions = new int[] { 1, 2, 3 }, UserLocation = locations[iL]
                    };
                    string key = "fooditem " + Guid.NewGuid().ToString();

                    int action = iE % numActions + 1;
                    recorder.Record(null, null, new EpsilonGreedyState {
                        Probability = 1.0f / numActions
                    }, null, key);

                    float cost = 0;

                    var draw = rand.NextDouble();
                    if (context.UserLocation == "HealthyTown")
                    {
                        // for healthy town, buy burger 1 with probability 0.1, burger 2 with probability 0.15, salad with probability 0.6
                        if ((action == 1 && draw < 0.1) || (action == 2 && draw < 0.15) || (action == 3 && draw < 0.6))
                        {
                            cost = -10;
                        }
                    }
                    else
                    {
                        // for unhealthy town, buy burger 1 with probability 0.4, burger 2 with probability 0.6, salad with probability 0.2
                        if ((action == 1 && draw < 0.4) || (action == 2 && draw < 0.6) || (action == 3 && draw < 0.2))
                        {
                            cost = -10;
                        }
                    }
                    var label = new ContextualBanditLabel
                    {
                        Action      = (uint)action,
                        Cost        = cost,
                        Probability = recorder.GetProb(key)
                    };
                    vw.Learn(context, label, index: (int)label.Action - 1);
                }
                var expectedActions = new Dictionary <string, uint>();
                expectedActions.Add("HealthyTown", 3);
                expectedActions.Add("LessHealthyTown", 2);
                for (int iE = 0; iE < numExamplesPerActions; iE++)
                {
                    foreach (string location in locations)
                    {
                        DateTime timeStamp = DateTime.UtcNow;

                        var context = new FoodContext {
                            Actions = new int[] { 1, 2, 3 }, UserLocation = location
                        };
                        ActionScore[] predicts = vw.Predict(context, VowpalWabbitPredictionType.ActionProbabilities);
                        Assert.AreEqual(expectedActions[location], predicts[0].Action + 1);
                    }
                }
            }
        }
Exemplo n.º 45
0
        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))
                            {
                                object actualValue;
                                if (args.Contains("-t")) // test only
                                {
                                    actualValue = vw.Predict(dataLine, VowpalWabbitPredictionType.Dynamic);
                                }
                                else
                                {
                                    actualValue = vw.Learn(dataLine, VowpalWabbitPredictionType.Dynamic);
                                }
                            }
                            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);
                    }
                }
            }
        }
 internal VowpalWabbitExampleJsonValidator(VowpalWabbitSettings settings)
 {
     settings = (VowpalWabbitSettings)settings.Clone();
     settings.EnableStringExampleGeneration = true;
     this.vw = new VowpalWabbit(settings);
 }
Exemplo n.º 47
0
        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);
                    }
                }
            }
        }
Exemplo n.º 48
0
        public static void Run(string ml_args, int initial_random, int tot_iter, int mod_iter, int rewardSeed, ulong vwSeed, int exp_iter, int numContexts, int numActions, string ml_args_snips)
        {
            // byte buffer outside so one can change the example and keep the memory around
            var exampleBuffer = new byte[32 * 1024];

            var randGen = new Random(rewardSeed);
            var userGen = new Random();

            var simExamples = Enumerable.Range(0, numContexts)
                              .Select(i => new SimulatorExample(numActions, i))
                              .ToArray();

            var    scorerPdf = new float[numActions];
            var    histPred = new int[numActions, numContexts];
            var    histPred2 = new int[numActions, numContexts];
            var    histActions = new int[numActions, numContexts];
            var    histCost = new int[numActions, numContexts];
            var    histContext = new int[numContexts];
            int    clicks = 0;
            double snips_num = 0, snips_den = 0;

            using (var learner = new VowpalWabbit(ml_args))
                using (var learner2 = new VowpalWabbit(ml_args_snips))
                {
                    VowpalWabbit scorer = null;

                    scorer = new VowpalWabbit("--cb_explore_adf --epsilon 1 --quiet");
                    for (int i = 1; i <= tot_iter; i++)
                    {
                        // sample uniform among users
                        int userIndex  = userGen.Next(simExamples.Length);
                        var simExample = simExamples[userIndex];
                        var pdf        = simExample.PDF;

                        histContext[userIndex]++;

                        using (var ex = simExample.CreateExample(learner))
                        {
                            var scores = ex.Predict(VowpalWabbitPredictionType.ActionProbabilities, scorer);

                            var total = 0.0;

                            foreach (var actionScore in scores)
                            {
                                total += actionScore.Score;
                                scorerPdf[actionScore.Action] = actionScore.Score;
                            }

                            var  draw      = randGen.NextDouble() * total;
                            var  sum       = 0.0;
                            uint topAction = 0;
                            foreach (var actionScore in scores)
                            {
                                sum += actionScore.Score;
                                if (sum > draw)
                                {
                                    topAction = actionScore.Action;
                                    break;
                                }
                            }

                            int modelAction = (int)scores[0].Action;
                            if (i > initial_random)
                            {
                                histPred[modelAction, userIndex] += 1;
                            }
                            histActions[topAction, userIndex] += 1;

                            // simulate behavior
                            float cost = 0;
                            if (randGen.NextDouble() < pdf[topAction])
                            {
                                cost = -1;
                                histCost[topAction, userIndex] += 1;
                                clicks += 1;
                            }

                            ex.Examples[topAction].Label = new ContextualBanditLabel((uint)topAction, cost, scorerPdf[topAction]);

                            // simulate delay
                            if (i >= initial_random && (i % exp_iter == 0))
                            {
                                ExportScoringModel(learner, ref scorer);
                            }

                            // invoke learning
                            var oneStepAheadScores = ex.Learn(VowpalWabbitPredictionType.ActionProbabilities, learner);
                            histPred2[oneStepAheadScores[0].Action, userIndex] += 1;

                            var oneStepAheadScores2 = ex.Learn(VowpalWabbitPredictionType.ActionProbabilities, learner2);

                            // SNIPS
                            snips_num -= oneStepAheadScores2.First(f => f.Action == topAction).Score *cost / scorerPdf[topAction];
                            snips_den += oneStepAheadScores2.First(f => f.Action == topAction).Score / scorerPdf[topAction];

                            if (i % mod_iter == 0 || i == tot_iter)
                            {
                                Console.WriteLine(JsonConvert.SerializeObject(new
                                {
                                    Iter = i,
                                    clicks,
                                    CTR       = clicks / (float)i,
                                    aveLoss   = learner.PerformanceStatistics.AverageLoss,
                                    CTR_snips = snips_num / snips_den,
                                    CTR_ips   = snips_num / (float)i,
                                    aveLoss2  = learner2.PerformanceStatistics.AverageLoss,
                                    snips_num,
                                    snips_den,
                                    histActions,
                                    histPred,
                                    histCost,
                                    histContext,
                                    pdf
                                }));
                            }
                        }
                    }
                    Console.WriteLine("---------------------");
                    scorer?.Dispose();
                }
        }
Exemplo n.º 49
0
        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("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);
                            Assert.AreEqual(example.Label.Label, Math.Round(prediction));
                        }
                    }
        }
 /// <summary>
 /// Creates a new instance of <see cref="VowpalWabbit{TExample}"/>.
 /// </summary>
 /// <param name="vw">The wrapped vw instance.</param>
 protected override VowpalWabbitJson InternalCreate(VowpalWabbit vw)
 {
     return(new VowpalWabbitJson(vw));
 }
        public async Task TestAllReduce()
        {
            var data = Enumerable.Range(1, 1000).Select(_ => Generator.GenerateShared(10)).ToList();

            var stringSerializerCompiler = (VowpalWabbitSingleExampleSerializerCompiler <CbAdfShared>)
                                           VowpalWabbitSerializerFactory.CreateSerializer <CbAdfShared>(new VowpalWabbitSettings {
                EnableStringExampleGeneration = true
            });
            var stringSerializerAdfCompiler = (VowpalWabbitSingleExampleSerializerCompiler <CbAdfAction>)
                                              VowpalWabbitSerializerFactory.CreateSerializer <CbAdfAction>(new VowpalWabbitSettings {
                EnableStringExampleGeneration = true
            });

            var stringData = new List <List <string> >();

            VowpalWabbitPerformanceStatistics statsExpected;

            using (var spanningTree = new SpanningTreeClr())
            {
                spanningTree.Start();

                using (var vw1 = new VowpalWabbit(new VowpalWabbitSettings(@"--total 2 --node 1 --unique_id 0 --span_server localhost --cb_adf --rank_all --interact xy")
                {
                    EnableStringExampleGeneration = true
                }))
                    using (var vw2 = new VowpalWabbit(new VowpalWabbitSettings(@"--total 2 --node 0 --unique_id 0 --span_server localhost --cb_adf --rank_all --interact xy")
                    {
                        EnableStringExampleGeneration = true
                    }))
                    {
                        var stringSerializer    = stringSerializerCompiler.Func(vw1);
                        var stringSerializerAdf = stringSerializerAdfCompiler.Func(vw1);

                        // serialize
                        foreach (var d in data)
                        {
                            var block = new List <string>();

                            using (var context = new VowpalWabbitMarshalContext(vw1))
                            {
                                stringSerializer(context, d.Item1, SharedLabel.Instance);
                                block.Add(context.ToString());
                            }

                            block.AddRange(d.Item2.Select((a, i) =>
                            {
                                using (var context = new VowpalWabbitMarshalContext(vw1))
                                {
                                    stringSerializerAdf(context, a, i == d.Item3.Action ? d.Item3 : null);
                                    return(context.ToString());
                                }
                            }));

                            stringData.Add(block);
                        }

                        await Task.WhenAll(
                            Task.Factory.StartNew(() => Ingest(vw1, stringData.Take(500))),
                            Task.Factory.StartNew(() => Ingest(vw2, stringData.Skip(500))));

                        vw1.SaveModel("expected.1.model");
                        vw2.SaveModel("expected.2.model");

                        statsExpected = vw1.PerformanceStatistics;
                    }
            }

            // skip header
            var expected1Model = File.ReadAllBytes("expected.1.model").Skip(0x15).ToList();
            var expected2Model = File.ReadAllBytes("expected.2.model").Skip(0x15).ToList();

            var settings = new VowpalWabbitSettings("--cb_adf --rank_all --interact xy")
            {
                ParallelOptions = new ParallelOptions
                {
                    MaxDegreeOfParallelism = 2
                },
                ExampleCountPerRun  = 2000,
                ExampleDistribution = VowpalWabbitExampleDistribution.RoundRobin
            };

            using (var vw = new VowpalWabbitThreadedLearning(settings))
            {
                await Task.WhenAll(
                    Task.Factory.StartNew(() => Ingest(vw, stringData.Take(500))),
                    Task.Factory.StartNew(() => Ingest(vw, stringData.Skip(500))));

                // important to enqueue the request before Complete() is called
                var statsTask = vw.PerformanceStatistics;
                var modelSave = vw.SaveModel("actual.model");

                await vw.Complete();

                var statsActual = await statsTask;
                VWTestHelper.AssertEqual(statsExpected, statsActual);

                await modelSave;

                // skip header
                var actualModel = File.ReadAllBytes("actual.model").Skip(0x15).ToList();

                CollectionAssert.AreEqual(expected1Model, actualModel);
                CollectionAssert.AreEqual(expected2Model, actualModel);
            }

            using (var vw = new VowpalWabbitThreadedLearning(settings))
            {
                var vwManaged = vw.Create <CbAdfShared, CbAdfAction>();

                await Task.WhenAll(
                    Task.Factory.StartNew(() => Ingest(vwManaged, data.Take(500))),
                    Task.Factory.StartNew(() => Ingest(vwManaged, data.Skip(500))));

                // important to enqueue the request before Complete() is called
                var statsTask = vw.PerformanceStatistics;
                var modelSave = vw.SaveModel("actual.managed.model");

                await vw.Complete();

                var statsActual = await statsTask;
                VWTestHelper.AssertEqual(statsExpected, statsActual);

                await modelSave;

                // skip header
                var actualModel = File.ReadAllBytes("actual.managed.model").Skip(0x15).ToList();

                CollectionAssert.AreEqual(expected1Model, actualModel);
                CollectionAssert.AreEqual(expected2Model, actualModel);
            }
        }
Exemplo n.º 52
0
        public void Test_RunSequenceTask_WithoutBuiltinDriver()
        {
            const string input = "train-sets/sequencespan_data";

            const int bits   = 24;
            const int passes = 4;
            string    vwargs = $"-k -c -b {bits} --invariant --passes {passes} --search_rollout none --search_task sequencespan --search 7 --holdout_off";

            VowpalWabbit rawvw = new VowpalWabbit(vwargs);

            uint GetLabel(VowpalWabbitExample ex) => ex.GetPrediction(rawvw, VowpalWabbitPredictionType.Multiclass);

            void RunSearchPredict(Multiline multiline)
            {
                // Parse the examples explicitly, because there is no easy way to get multiple predictions spread
                // across multiple input examples in non-ADF/LDF mode

                // TPrediction rawvw.Predict(ml, TPredictionFactory); // << There does not exist an appropriate one for search
                // Mostly because the infrastructure is not there to pull out
                // predictions from multiple examples.

                List <VowpalWabbitExample> multiex = new List <VowpalWabbitExample>();

                try
                {
                    foreach (string line in multiline)
                    {
                        multiex.Add(rawvw.ParseLine(line));
                    }

                    rawvw.Predict(multiex);
                    uint[] labels = multiex.Select(GetLabel).ToArray();

                    CollectionAssert.AreEqual(new uint[] { 2, 1, 1, 2, 2, 1, 6, 7, 7, 7, 7, 1, 6, 4, 1 }, labels);
                }
                finally
                {
                    foreach (VowpalWabbitExample ex in multiex)
                    {
                        rawvw.ReturnExampleToPool(ex);
                    }
                }
            }

            using (FileDriver driver = new FileDriver(input))
            {
                int remainingPasses = passes;

                do
                {
                    driver.ForEachMultiline(rawvw.Learn);

                    rawvw.EndOfPass();
                    driver.Reset();
                } while (remainingPasses-- > 0);

                driver.Reset();

                driver.ForEachMultiline(RunSearchPredict);
            }

            rawvw.Dispose();
        }
Exemplo n.º 53
0
        //private class Event
        //{
        //    internal VowpalWabbitExampleCollection Example;

        //    internal string Line;

        //    internal int LineNr;

        //    internal ActionScore[] Prediction;
        //}

        /// <summary>
        /// Train VW on offline data.
        /// </summary>
        /// <param name="arguments">Base arguments.</param>
        /// <param name="inputFile">Path to input file.</param>
        /// <param name="predictionFile">Name of the output prediction file.</param>
        /// <param name="reloadInterval">The TimeSpan interval to reload model.</param>
        /// <param name="learningRate">
        /// Learning rate must be specified here otherwise on Reload it will be reset.
        /// </param>
        /// <param name="cacheFilePrefix">
        /// The prefix of the cache file name to use. For example: prefix = "test" => "test.vw.cache"
        /// If none or null, the input file name is used, e.g. "input.dataset" => "input.vw.cache"
        /// !!! IMPORTANT !!!: Always use a new cache name if a different dataset or reload interval is used.
        /// </param>
        /// <remarks>
        /// Both learning rates and cache file are added to initial training arguments as well as Reload arguments.
        /// </remarks>
        public static void Train(string arguments, string inputFile, string predictionFile = null, TimeSpan?reloadInterval = null, float?learningRate = null, string cacheFilePrefix = null)
        {
            var learningArgs = learningRate == null ? string.Empty : $" -l {learningRate}";

            int cacheIndex = 0;
            var cacheArgs  = (Func <int, string>)(i => $" --cache_file {cacheFilePrefix ?? Path.GetFileNameWithoutExtension(inputFile)}-{i}.vw.cache");

            using (var reader = new StreamReader(inputFile))
                using (var prediction = new StreamWriter(predictionFile ?? inputFile + ".prediction"))
                    using (var vw = new VowpalWabbit(new VowpalWabbitSettings(arguments + learningArgs + cacheArgs(cacheIndex++))
                    {
                        Verbose = true
                    }))
                    {
                        string   line;
                        int      lineNr          = 0;
                        int      invalidExamples = 0;
                        DateTime?lastTimestamp   = null;

                        while ((line = reader.ReadLine()) != null)
                        {
                            try
                            {
                                bool reload = false;
                                using (var jsonSerializer = new VowpalWabbitJsonSerializer(vw))
                                {
                                    if (reloadInterval != null)
                                    {
                                        jsonSerializer.RegisterExtension((state, property) =>
                                        {
                                            if (property.Equals("_timestamp", StringComparison.Ordinal))
                                            {
                                                var eventTimestamp = state.Reader.ReadAsDateTime();
                                                if (lastTimestamp == null)
                                                {
                                                    lastTimestamp = eventTimestamp;
                                                }
                                                else if (lastTimestamp + reloadInterval < eventTimestamp)
                                                {
                                                    reload        = true;
                                                    lastTimestamp = eventTimestamp;
                                                }

                                                return(true);
                                            }

                                            return(false);
                                        });
                                    }

                                    // var pred = vw.Learn(line, VowpalWabbitPredictionType.ActionProbabilities);
                                    using (var example = jsonSerializer.ParseAndCreate(line))
                                    {
                                        var pred = example.Learn(VowpalWabbitPredictionType.ActionProbabilities);

                                        prediction.WriteLine(JsonConvert.SerializeObject(
                                                                 new
                                        {
                                            nr  = lineNr,
                                            @as = pred.Select(x => x.Action),
                                            p   = pred.Select(x => x.Score)
                                        }));
                                    }

                                    if (reload)
                                    {
                                        vw.Reload(learningArgs + cacheArgs(cacheIndex++));
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                invalidExamples++;
                            }

                            lineNr++;
                        }
                    }

            // memory leak and not much gain below...
            //using (var vw = new VowpalWabbit(new VowpalWabbitSettings(arguments)
            //{
            //    Verbose = true,
            //    EnableThreadSafeExamplePooling = true,
            //    MaxExamples = 1024
            //}))
            //using (var reader = new StreamReader(inputFile))
            //using (var prediction = new StreamWriter(inputFile + ".prediction"))
            //{
            //    int invalidExamples = 0;

            //    var deserializeBlock = new TransformBlock<Event, Event>(
            //        evt =>
            //        {
            //            try
            //            {
            //                using (var vwJsonSerializer = new VowpalWabbitJsonSerializer(vw))
            //                {
            //                    evt.Example = vwJsonSerializer.ParseAndCreate(evt.Line);
            //                }
            //                // reclaim memory
            //                evt.Line = null;

            //                return evt;
            //            }
            //            catch (Exception)
            //            {
            //                Interlocked.Increment(ref invalidExamples);
            //                return null;
            //            }
            //        },
            //        new ExecutionDataflowBlockOptions
            //        {
            //            BoundedCapacity = 16,
            //            MaxDegreeOfParallelism = 8 // TODO: parameterize
            //        });

            //    var learnBlock = new TransformBlock<Event, Event>(
            //        evt =>
            //        {
            //            evt.Prediction = evt.Example.Learn(VowpalWabbitPredictionType.ActionProbabilities);
            //            evt.Example.Dispose();
            //            return evt;
            //        },
            //        new ExecutionDataflowBlockOptions
            //        {
            //            BoundedCapacity = 64,
            //            MaxDegreeOfParallelism = 1
            //        });

            //    var predictionBlock = new ActionBlock<Event>(
            //        evt => prediction.WriteLine(evt.LineNr + " " + string.Join(",", evt.Prediction.Select(a_s => $"{a_s.Action}:{a_s.Score}"))),
            //        new ExecutionDataflowBlockOptions
            //        {
            //            BoundedCapacity = 16,
            //            MaxDegreeOfParallelism = 1
            //        });

            //    var input = deserializeBlock.AsObserver();

            //    deserializeBlock.LinkTo(learnBlock, new DataflowLinkOptions { PropagateCompletion = true }, evt => evt != null);
            //    deserializeBlock.LinkTo(DataflowBlock.NullTarget<object>());

            //    learnBlock.LinkTo(predictionBlock, new DataflowLinkOptions { PropagateCompletion = true });

            //    string line;
            //    int lineNr = 0;

            //    while ((line = reader.ReadLine()) != null)
            //        input.OnNext(new Event { Line = line, LineNr = lineNr++ });
            //    input.OnCompleted();

            //    predictionBlock.Completion.Wait();

            //Console.WriteLine($"Examples {lineNr}. Invalid: {invalidExamples}");
            //}
        }