Пример #1
0
        public void TestWiki()
        {
            using (var vw = new VW.VowpalWabbit("-f rcv1.model"))
            {
                // 1 |f 13:3.9656971e-02 24:3.4781646e-02 69:4.6296168e-02
                using (var exampleBuilder = new VW.VowpalWabbitExampleBuilder(vw))
                {
                    // important to dispose the namespace builder at the end, as data is only added to the example
                    // if there is any feature added to the namespace
                    using (var ns = exampleBuilder.AddNamespace('f'))
                    {
                        var namespaceHash = vw.HashSpace("f");

                        var featureHash = vw.HashFeature("13", namespaceHash);
                        ns.AddFeature(featureHash, 8.5609287e-02f);

                        featureHash = vw.HashFeature("24", namespaceHash);
                        ns.AddFeature(featureHash, 3.4781646e-02f);

                        featureHash = vw.HashFeature("69", namespaceHash);
                        ns.AddFeature(featureHash, 4.6296168e-02f);
                    }

                    exampleBuilder.ApplyLabel(new SimpleLabel() { Label = 1 });

                    // hand over of memory management
                    using (var example = exampleBuilder.CreateExample())
                    {
                        VowpalWabbitExampleValidator.Validate("1 |f 13:8.5609287e-02 24:3.4781646e-02 69:4.6296168e-02", example, VowpalWabbitLabelComparator.Simple);

                        vw.Learn(example);
                    }
                }
            }
        }
        /// <summary>
        /// Simplify learning of examples with action dependent features.
        /// </summary>
        public static void Learn <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSerializer <TExample> serializer,
            VowpalWabbitSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IReadOnlyCollection <TActionDependentFeature> actionDependentFeatures,
            int index,
            ILabel label)
        {
            Contract.Requires(vw != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);
            Contract.Requires(index >= 0);
            Contract.Requires(label != null);

            Execute(
                vw,
                serializer,
                actionDependentFeatureSerializer,
                example,
                actionDependentFeatures,
                (examples, _, __) =>
            {
                foreach (var ex in examples)
                {
                    vw.Learn(ex);
                }
            },
                index,
                label);
        }
        /// <summary>
        /// Simplify learning of examples with action dependent features.
        /// </summary>
        /// <typeparam name="TExample">The type of the user example.</typeparam>
        /// <typeparam name="TActionDependentFeature">The type of the user action dependent features.</typeparam>
        /// <param name="vw">The vw instance.</param>
        /// <param name="serializer">The serializer for <typeparamref name="TExample"/>.</param>
        /// <param name="actionDependentFeatureSerializer">The serializer for <typeparamref name="TActionDependentFeature"/>.</param>
        /// <param name="example">The user example.</param>
        /// <param name="actionDependentFeatures">The action dependent features.</param>
        /// <param name="index">The index of action dependent feature to label.</param>
        /// <param name="label">The label for the selected action dependent feature.</param>
        /// <returns>An ranked subset of predicted actions.</returns>
        public static ActionDependentFeature <TActionDependentFeature>[] LearnAndPredict <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSerializer <TExample> serializer,
            VowpalWabbitSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IReadOnlyCollection <TActionDependentFeature> actionDependentFeatures,
            int index,
            ILabel label)
        {
            Contract.Requires(vw != null);
            Contract.Requires(serializer != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);
            Contract.Requires(index >= 0);
            Contract.Requires(label != null);

            ActionDependentFeature <TActionDependentFeature>[] predictions = null;

            Execute(
                vw,
                serializer,
                actionDependentFeatureSerializer,
                example,
                actionDependentFeatures,
                (examples, validActionDependentFeatures, emptyActionDependentFeatures) =>
            {
                foreach (var ex in examples)
                {
                    vw.Learn(ex);
                }

                predictions = VowpalWabbitMultiLine.GetPrediction(vw, examples, validActionDependentFeatures, emptyActionDependentFeatures);
            },
                index,
                label);

            // default to the input list
            return(predictions ?? actionDependentFeatures.Select((o, i) => new ActionDependentFeature <TActionDependentFeature>(i, o)).ToArray());
        }
Пример #4
0
        public void TestWiki()
        {
            using (var vw = new VW.VowpalWabbit("-f rcv1.model"))
            {
                // 1 |f 13:3.9656971e-02 24:3.4781646e-02 69:4.6296168e-02
                using (var exampleBuilder = new VW.VowpalWabbitExampleBuilder(vw))
                {
                    // important to dispose the namespace builder at the end, as data is only added to the example
                    // if there is any feature added to the namespace
                    using (var ns = exampleBuilder.AddNamespace('f'))
                    {
                        var namespaceHash = vw.HashSpace("f");

                        var featureHash = vw.HashFeature("13", namespaceHash);
                        ns.AddFeature(featureHash, 8.5609287e-02f);

                        featureHash = vw.HashFeature("24", namespaceHash);
                        ns.AddFeature(featureHash, 3.4781646e-02f);

                        featureHash = vw.HashFeature("69", namespaceHash);
                        ns.AddFeature(featureHash, 4.6296168e-02f);
                    }

                    exampleBuilder.ApplyLabel(new SimpleLabel()
                    {
                        Label = 1
                    });

                    // hand over of memory management
                    using (var example = exampleBuilder.CreateExample())
                    {
                        VowpalWabbitExampleValidator.Validate("1 |f 13:8.5609287e-02 24:3.4781646e-02 69:4.6296168e-02", example, VowpalWabbitLabelComparator.Simple);

                        vw.Learn(example);
                    }
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Learns from these examples.
 /// </summary>
 protected override void LearnInternal(VowpalWabbit vw)
 {
     // unfortunately can't specify <void>
     this.Execute <int>(vw, ex => vw.Learn(ex));
 }
Пример #6
0
 /// <summary>
 /// Learn from these examples and returns the current prediction for it.
 /// </summary>
 /// <typeparam name="TPrediction">The prediction type.</typeparam>
 /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
 /// <returns>The prediction for the this example.</returns>
 protected override TPrediction LearnInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw)
 {
     return(this.Execute(vw, ex => vw.Learn(ex), predictionFactory));
 }
 /// <summary>
 /// Learn from this example and returns the current prediction for it.
 /// </summary>
 /// <typeparam name="TPrediction">The prediction type.</typeparam>
 /// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
 /// <param name="vw">The VW native instance.</param>
 /// <returns>The prediction for the this example.</returns>
 protected override TPrediction LearnInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw)
 {
     return(vw.Learn <TPrediction>(this.Example, predictionFactory));
 }
 /// <summary>
 /// Learns from this example.
 /// </summary>
 protected override void LearnInternal(VowpalWabbit vw)
 {
     vw.Learn(this.Example);
 }
        /// <summary>
        /// Simplify learning of examples with action dependent features.
        /// </summary>
        public static void Learn <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSerializer <TExample> serializer,
            VowpalWabbitSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IEnumerable <TActionDependentFeature> actionDependentFeatures,
            int index,
            ILabel label)
        {
            Contract.Requires(vw != null);
            Contract.Requires(serializer != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);
            Contract.Requires(index >= 0);
            Contract.Requires(label != null);

#if DEBUG
            // only in debug, since it's a hot path
            if (actionDependentFeatureSerializer.CachesExamples)
            {
                throw new NotSupportedException("Cached examples cannot be used for learning");
            }
#endif

            var examples = new List <VowpalWabbitExample>();

            try
            {
                // contains prediction results
                var sharedExample = serializer.Serialize(vw, example, SharedLabel.Instance);
                // check if we have shared features
                if (sharedExample != null)
                {
                    examples.Add(sharedExample);
                    vw.Learn(sharedExample);
                }

                var i = 0;
                foreach (var actionDependentFeature in actionDependentFeatures)
                {
                    var adfExample = actionDependentFeatureSerializer.Serialize(vw, actionDependentFeature, i == index ? label : null);
                    Contract.Assert(adfExample != null);

                    examples.Add(adfExample);

                    vw.Learn(adfExample);

                    i++;
                }

                // signal we're finished using an empty example
                var empty = vw.GetOrCreateEmptyExample();
                examples.Add(empty);
                vw.Learn(empty);

                // Dump input file for command line learning
                //File.AppendAllLines(@"c:\temp\msn.txt",
                //    examples.OfType<VowpalWabbitDebugExample>()
                //        .Select(e => e.VowpalWabbitString)
                //        .Union(new[] { "" }));
            }
            finally
            {
                // dispose examples
                // Note: must not dispose examples before final example
                // as the learning algorithm (such as cbf) keeps a reference
                // to the example
                foreach (var e in examples)
                {
                    e.Dispose();
                }
            }
        }
        /// <summary>
        /// Simplify learning of examples with action dependent features.
        /// </summary>
        /// <typeparam name="TExample">The type of the user example.</typeparam>
        /// <typeparam name="TActionDependentFeature">The type of the user action dependent features.</typeparam>
        /// <param name="vw">The vw instance.</param>
        /// <param name="serializer">The serializer for <typeparamref name="TExample"/>.</param>
        /// <param name="actionDependentFeatureSerializer">The serializer for <typeparamref name="TActionDependentFeature"/>.</param>
        /// <param name="example">The user example.</param>
        /// <param name="actionDependentFeatures">The action dependent features.</param>
        /// <param name="index">The index of action dependent feature to label.</param>
        /// <param name="label">The label for the selected action dependent feature.</param>
        /// <returns>An ranked subset of predicted action indexes.</returns>
        public static int[] LearnAndPredictIndex <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSerializer <TExample> serializer,
            VowpalWabbitSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IEnumerable <TActionDependentFeature> actionDependentFeatures,
            int index,
            ILabel label)
        {
            Contract.Requires(vw != null);
            Contract.Requires(serializer != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);
            Contract.Requires(index >= 0);
            Contract.Requires(label != null);

#if DEBUG
            // only in debug, since it's a hot path
            if (actionDependentFeatureSerializer.CachesExamples)
            {
                throw new NotSupportedException("Cached examples cannot be used for learning");
            }
#endif

            var examples = new List <VowpalWabbitExample>();

            try
            {
                // contains prediction results
                var sharedExample = serializer.Serialize(vw, example);
                // check if we have shared features
                if (sharedExample != null)
                {
                    examples.Add(sharedExample);
                    vw.Learn(sharedExample);
                }

                // leave as loop (vs. linq) so if the serializer throws an exception, anything allocated so far can be free'd
                var i = 0;
                foreach (var actionDependentFeature in actionDependentFeatures)
                {
                    var adfExample = actionDependentFeatureSerializer.Serialize(vw, actionDependentFeature, i == index ? label : null);
                    Contract.Assert(adfExample != null);

                    examples.Add(adfExample);

                    vw.Learn(adfExample);
                    i++;
                }

                // signal we're finished using an empty example
                var empty = vw.GetOrCreateEmptyExample();
                examples.Add(empty);
                vw.Learn(empty);

                // Nasty workaround. Since the prediction result is stored in the first example
                // and we'll have to get an actual VowpalWabbitExampt
                var firstExample = examples.FirstOrDefault();
                if (firstExample == null)
                {
                    return(null);
                }

                return(firstExample.GetPrediction(vw, VowpalWabbitPredictionType.Multilabel));
            }
            finally
            {
                // dispose examples
                // Note: must not dispose examples before final example
                // as the learning algorithm (such as cbf) keeps a reference
                // to the example
                foreach (var e in examples)
                {
                    e.Dispose();
                }
            }
        }