Exemple #1
0
        /// <summary>
        /// Simplify prediction 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>[] Predict <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSingleExampleSerializer <TExample> serializer,
            VowpalWabbitSingleExampleSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IReadOnlyCollection <TActionDependentFeature> actionDependentFeatures,
            int?index    = null,
            ILabel label = null)
        {
            Contract.Requires(vw != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);

            ActionDependentFeature <TActionDependentFeature>[] predictions = null;

            Execute(
                vw,
                serializer,
                actionDependentFeatureSerializer,
                example,
                actionDependentFeatures,
                (examples, validActionDependentFeatures, emptyActionDependentFeatures) =>
            {
                var ex_col = examples.ToList();
                vw.Predict(ex_col);
                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());
        }
Exemple #2
0
        /// <summary>
        /// Simplify learning of examples with action dependent features.
        /// </summary>
        public static void Learn <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSingleExampleSerializer <TExample> serializer,
            VowpalWabbitSingleExampleSerializer <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, _, __) =>
            {
                vw.Learn(examples.ToList());
            },
                index,
                label);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="VowpalWabbit{TExample}"/> class.
        /// </summary>
        /// <param name="vw">The native instance to wrap.</param>
        /// <remarks>This instance takes ownership of <paramref name="vw"/> instance and disposes it.</remarks>
        public VowpalWabbit(VowpalWabbit vw)
        {
            if (vw == null)
            {
                throw new ArgumentNullException("vw");
            }
            Contract.Ensures(this.serializer != null);
            Contract.EndContractBlock();

            this.vw = vw;
            this.compiledSerializer = VowpalWabbitSerializerFactory.CreateSerializer <TExample>(vw.Settings);

            if (this.compiledSerializer == null)
            {
                throw new ArgumentException("No features found for " + typeof(TExample));
            }

            this.serializer = this.compiledSerializer.Create(vw);

            // have a 2nd member to throw NullReferenceException in release instead of silently producing wrong results.
            this.learnSerializer = this.serializer.CachesExamples ? null : this.serializer;

            // have a 3rd member to avoid cast everytime...
            this.singleLineSerializer = this.serializer as VowpalWabbitSingleExampleSerializer <TExample>;
        }
        /// <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,
            VowpalWabbitSingleExampleSerializer <TExample> serializer,
            VowpalWabbitSingleExampleSerializer <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);

            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());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="VowpalWabbit{TExample}"/> class.
        /// </summary>
        /// <param name="vw">The native instance to wrap.</param>
        /// <param name="compiledSerializer">The per-compiled serializer.</param>
        /// <remarks>This instance takes ownership of <paramref name="vw"/> instance and disposes it.</remarks>
        public VowpalWabbit(VowpalWabbit vw, IVowpalWabbitSerializerCompiler <TExample> compiledSerializer)
        {
            if (vw == null)
            {
                throw new ArgumentNullException(nameof(vw));
            }
            if (compiledSerializer == null)
            {
                throw new ArgumentNullException(nameof(compiledSerializer));
            }
            Contract.Ensures(this.serializer != null);
            Contract.EndContractBlock();

            this.vw = vw;
            this.compiledSerializer = compiledSerializer;

            this.serializer = this.compiledSerializer.Create(vw);

            // have a 2nd member to throw NullReferenceException in release instead of silently producing wrong results.
            this.learnSerializer = this.serializer.CachesExamples ? null : this.serializer;

            // have a 3rd member to avoid cast everytime...
            this.singleLineSerializer = this.serializer as VowpalWabbitSingleExampleSerializer <TExample>;
        }
Exemple #6
0
        /// <summary>
        /// Simplify learning of examples with action dependent features.
        /// </summary>
        /// <typeparam name="TExample">User example type.</typeparam>
        /// <typeparam name="TActionDependentFeature">Action dependent feature type.</typeparam>
        /// <param name="vw">The VowpalWabbit instances.</param>
        /// <param name="serializer">The example serializer.</param>
        /// <param name="actionDependentFeatureSerializer">The action dependent feature serializer.</param>
        /// <param name="example">The example.</param>
        /// <param name="actionDependentFeatures">The action dependent features.</param>
        /// <param name="predictOrLearn">An action executed once the set of valid examples is determined. </param>
        /// <param name="index">The optional index of the action dependent feature this label belongs too.</param>
        /// <param name="label">The optional label to be used for learning or evaluation.</param>
        public static void Execute <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSingleExampleSerializer <TExample> serializer,
            VowpalWabbitSingleExampleSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IReadOnlyCollection <TActionDependentFeature> actionDependentFeatures,
            LearnOrPredictAction <TActionDependentFeature> predictOrLearn,
            int?index    = null,
            ILabel label = null)
        {
            Contract.Requires(vw != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);

            var examples      = new List <VowpalWabbitExample>(actionDependentFeatures.Count + 1);
            var validExamples = new List <VowpalWabbitExample>(actionDependentFeatures.Count + 1);
            var validActionDependentFeatures = new List <ActionDependentFeature <TActionDependentFeature> >(actionDependentFeatures.Count + 1);
            var emptyActionDependentFeatures = new List <ActionDependentFeature <TActionDependentFeature> >(actionDependentFeatures.Count + 1);

            VowpalWabbitExample emptyExample = null;

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

                        if (!sharedExample.IsNewLine)
                        {
                            validExamples.Add(sharedExample);
                        }
                    }
                }

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

                    examples.Add(adfExample);

                    if (!adfExample.IsNewLine)
                    {
                        validExamples.Add(adfExample);
                        validActionDependentFeatures.Add(new ActionDependentFeature <TActionDependentFeature>(i, actionDependentFeature));
                    }
                    else
                    {
                        emptyActionDependentFeatures.Add(new ActionDependentFeature <TActionDependentFeature>(i, actionDependentFeature));
                    }

                    i++;
                }

                if (validActionDependentFeatures.Count == 0)
                {
                    return;
                }

                // signal we're finished using an empty example
                emptyExample = vw.GetOrCreateNativeExample();
                emptyExample.MakeEmpty(vw);

                predictOrLearn(validExamples, validActionDependentFeatures, emptyActionDependentFeatures);
            }
            finally
            {
                if (emptyExample != null)
                {
                    emptyExample.Dispose();
                }

                // 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();
                }
            }
        }