Esempio n. 1
0
        public static void TestMemoryLeak()
        {
            string outModelFile = "cb_adf_mem_leak.model";

            using (var vw = new VowpalWabbit <DataString, DataStringADF>("--cb_adf --rank_all"))
            {
                DataString[] sampleData = CreateStringCbAdfData(1000);
                foreach (DataString example in sampleData)
                {
                    vw.Learn(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
                }
                vw.Native.SaveModel(outModelFile);
            }

            var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings(string.Format("--quiet -t -i {0}", outModelFile))
            {
                MaxExampleCacheSize = 1024
            });
            var pool = new VowpalWabbitThreadedPrediction <DataString, DataStringADF>(vwModel);

            while (true)
            {
                vwModel = new VowpalWabbitModel(new VowpalWabbitSettings(string.Format("--quiet -t -i {0}", outModelFile))
                {
                    MaxExampleCacheSize = 1024
                });
                pool.UpdateModel(vwModel);
            }
        }
Esempio n. 2
0
        public void ThreadPoolNull()
        {
            using (var pool = new VowpalWabbitThreadedPrediction())
            {
                using (var vw = pool.GetOrCreate())
                {
                    Assert.IsNull(vw.Value);
                }

                pool.UpdateModel(new VowpalWabbitModel(string.Empty));

                using (var vw = pool.GetOrCreate())
                {
                    Assert.IsNotNull(vw.Value);
                }
            }
        }
Esempio n. 3
0
        public static void MultiThreadedPrediction()
        {
            var example = new MyExample { Income = 40, Age = 25 };

            var vwModel = new VowpalWabbitModel("-t -i m1.model");
            using (var pool = new VowpalWabbitThreadedPrediction<MyExample>(vwModel))
            {
                // thread-safe
                using (var vw = pool.GetOrCreate())
                {
                    // vw.Value is not thread-safe
                    vw.Value.Predict(example);
                }

                // thread-safe
                pool.UpdateModel(new VowpalWabbitModel("-t -i m2.model"));
            }
        }
Esempio n. 4
0
        public void TestSaveLoadSkip()
        {
            using (var vw = new VowpalWabbit <SimpleData>("--binary -f saveload.model"))
            {
                for (int i = 0; i < 100; i++)
                {
                    vw.Learn(new SimpleData {
                        Value = 1
                    }, new SimpleLabel {
                        Label = 1
                    });
                    vw.Learn(new SimpleData {
                        Value = -1
                    }, new SimpleLabel {
                        Label = -1
                    });
                }

                Assert.AreEqual(1, vw.Predict(new SimpleData {
                    Value = 1
                }, VowpalWabbitPredictionType.Scalar));
                Assert.AreEqual(-1, vw.Predict(new SimpleData {
                    Value = -1
                }, VowpalWabbitPredictionType.Scalar));
            }

            using (var model = new VowpalWabbitModel(new VowpalWabbitSettings {
                Arguments = "--binary", ModelStream = File.Open("saveload.model", FileMode.Open)
            }))
                using (var pool = new VowpalWabbitThreadedPrediction <SimpleData>(new VowpalWabbitSettings {
                    Model = model
                }))
                {
                    using (var vw = pool.GetOrCreate())
                    {
                        Assert.AreEqual(-1, vw.Value.Predict(new SimpleData {
                            Value = -1
                        }, VowpalWabbitPredictionType.Scalar));
                    }
                }
        }
        public static void MultiThreadedPrediction()
        {
            var example = new MyExample {
                Income = 40, Age = 25
            };

            var vwModel = new VowpalWabbitModel("-t -i m1.model");

            using (var pool = new VowpalWabbitThreadedPrediction <MyExample>(vwModel))
            {
                // thread-safe
                using (var vw = pool.GetOrCreate())
                {
                    // vw.Value is not thread-safe
                    vw.Value.Predict(example);
                }

                // thread-safe
                pool.UpdateModel(new VowpalWabbitModel("-t -i m2.model"));
            }
        }
Esempio n. 6
0
        public static void TestMemoryLeak()
        {
            string outModelFile = "cb_adf_mem_leak.model";
            using (var vw = new VowpalWabbit<DataString, DataStringADF>("--cb_adf --rank_all"))
            {
                DataString[] sampleData = CreateStringCbAdfData(1000);
                foreach (DataString example in sampleData)
                {
                    vw.Learn(example, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
                }
                vw.Native.SaveModel(outModelFile);
            }

            var vwModel = new VowpalWabbitModel(new VowpalWabbitSettings(string.Format("--quiet -t -i {0}", outModelFile), maxExampleCacheSize: 1024));
            var pool = new VowpalWabbitThreadedPrediction<DataString, DataStringADF>(vwModel);

            while (true)
            {
                vwModel = new VowpalWabbitModel(new VowpalWabbitSettings(string.Format("--quiet -t -i {0}", outModelFile), maxExampleCacheSize: 1024));
                pool.UpdateModel(vwModel);
            }
        }
Esempio n. 7
0
        public void ThreadPoolNull()
        {
            using (var pool = new VowpalWabbitThreadedPrediction())
            {
                using (var vw = pool.GetOrCreate())
                {
                    Assert.IsNull(vw.Value);
                }

                pool.UpdateModel(new VowpalWabbitModel(string.Empty));

                using (var vw = pool.GetOrCreate())
                {
                    Assert.IsNotNull(vw.Value);
                }
            }
        }
Esempio n. 8
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, example.ActionDependentFeatures, example.SelectedActionIndex, example.Label);
                }
                vw.Native.SaveModel(cbadfModelFile);
            }

            // Get ground truth predictions
            var expectedPredictions = new List<DataStringADF[]>();
            using (var vw = new VowpalWabbit<DataString, DataStringADF>(string.Format("-t -i {0}", cbadfModelFile)))
            {
                foreach (DataString example in sampleData)
                {
                    expectedPredictions.Add(vw.Predict(example, example.ActionDependentFeatures));
                }
            }

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

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

                            Assert.AreEqual(expectedPredictions.Count, actualPredictions.Count);
                            for (int j = 0; j < expectedPredictions.Count; j++)
                            {
                                ReferenceEquals(expectedPredictions[j], actualPredictions[j]);
                            }
                        }
                    }
                );
            }
        }
Esempio n. 9
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]);
                            }
                        }
                    }
                    );
                }
        }
Esempio n. 10
0
        public void TestSaveLoadSkip()
        {
            using (var vw = new VowpalWabbit<SimpleData>("--binary -f saveload.model"))
            {
                for (int i = 0; i < 100; i++)
                {
                    vw.Learn(new SimpleData { Value = 1 }, new SimpleLabel { Label = 1 });
                    vw.Learn(new SimpleData { Value = -1 }, new SimpleLabel { Label = -1 });
                }

                Assert.AreEqual(1, vw.Predict(new SimpleData { Value = 1 }, VowpalWabbitPredictionType.Scalar));
                Assert.AreEqual(-1, vw.Predict(new SimpleData { Value = -1 }, VowpalWabbitPredictionType.Scalar));
            }

            using (var model = new VowpalWabbitModel(new VowpalWabbitSettings { Arguments = "--binary", ModelStream = File.Open("saveload.model", FileMode.Open) }))
            using (var pool = new VowpalWabbitThreadedPrediction<SimpleData>(new VowpalWabbitSettings { Model = model }))
            {
                using (var vw = pool.GetOrCreate())
                {
                    Assert.AreEqual(-1, vw.Value.Predict(new SimpleData { Value = -1 }, VowpalWabbitPredictionType.Scalar));
                }
            }
        }