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>
 /// Predicts for 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 PredictInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw)
 {
     return(this.Execute(vw, ex => vw.Predict(ex), predictionFactory));
 }
Exemple #3
0
 /// <summary>
 /// Predicts for these examples.
 /// </summary>
 protected override void PredictInternal(VowpalWabbit vw)
 {
     // unfortunately can't specify <void>
     this.Execute <int>(vw, ex => vw.Predict(ex));
 }
 /// <summary>
 /// Predicts for 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 instance that should be used for prediction.</param>
 /// <returns>The prediction for the this example.</returns>
 protected override TPrediction PredictInternal <TPrediction>(IVowpalWabbitPredictionFactory <TPrediction> predictionFactory, VowpalWabbit vw)
 {
     return(vw.Predict <TPrediction>(this.Example, predictionFactory));
 }
 /// <summary>
 /// Predicts for this example.
 /// </summary>
 protected override void PredictInternal(VowpalWabbit vw)
 {
     vw.Predict(this.Example);
 }
        /// <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>
        /// <returns>An ranked subset of predicted action indexes.</returns>
        public static int[] PredictIndex <TExample, TActionDependentFeature>(
            VowpalWabbit vw,
            VowpalWabbitSerializer <TExample> serializer,
            VowpalWabbitSerializer <TActionDependentFeature> actionDependentFeatureSerializer,
            TExample example,
            IEnumerable <TActionDependentFeature> actionDependentFeatures)
        {
            Contract.Requires(vw != null);
            Contract.Requires(serializer != null);
            Contract.Requires(actionDependentFeatureSerializer != null);
            Contract.Requires(example != null);
            Contract.Requires(actionDependentFeatures != null);

            // shared |userlda :.1 |che a:.1
            // `doc1 |lda :.1 :.2 [1]
            // `doc2 |lda :.2 :.3 [2]
            // <new line>
            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.Predict(sharedExample);
                }

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

                    examples.Add(adfExample);

                    vw.Predict(adfExample);
                }

                // signal we're finished using an empty example
                var empty = vw.GetOrCreateEmptyExample();
                examples.Add(empty);
                vw.Predict(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();
                }
            }
        }