Example #1
0
        protected override PolicyDecision <ActionProbability[]> MapContext(VowpalWabbit <TContext> vw, TContext context)
        {
            if (this.developmentMode)
            {
                using (var serializer = vw.Serializer.Create(vw.Native))
                {
                    Trace.TraceInformation("Example Context: {0}", serializer.SerializeToString(context));
                }
            }

            var vwPredictions = vw.Predict(context, VowpalWabbitPredictionType.ActionProbabilities);

            // VW multi-label predictions are 0-based
            var ap = vwPredictions
                     .Select(a =>
                             new ActionProbability
            {
                Action      = (int)(a.Action + 1),
                Probability = a.Score
            })
                     .ToArray();
            var state = new VWState {
                ModelId = vw.Native.ID
            };

            return(PolicyDecision.Create(ap, state));
        }
Example #2
0
        protected override PolicyDecision <ActionProbability[]> MapContext(VowpalWabbit vw, string context)
        {
            using (var vwJson = new VowpalWabbitJsonSerializer(vw))
                using (VowpalWabbitExampleCollection vwExample = vwJson.ParseAndCreate(context))
                {
                    if (this.developmentMode)
                    {
                        Trace.TraceInformation("Example Context: '{0}'", vwExample.VowpalWabbitString);
                    }

                    var vwPredictions = vwExample.Predict(VowpalWabbitPredictionType.ActionProbabilities);

                    // VW multi-label predictions are 0-based
                    var ap = vwPredictions
                             .Select(a =>
                                     new ActionProbability
                    {
                        Action      = (int)(a.Action + 1),
                        Probability = a.Score
                    })
                             .ToArray();
                    var state = new VWState {
                        ModelId = vw.Native.ID
                    };

                    return(PolicyDecision.Create(ap, state));
                }
        }
Example #3
0
        /// <summary>
        /// Determines the action to take for a given context.
        /// This implementation should be thread-safe if multithreading is needed.
        /// </summary>
        /// <param name="vw">The Vowpal Wabbit instance to use.</param>
        /// <param name="context">A user-defined context for the decision.</param>
        /// <returns>A decision tuple containing the index of the action to take (1-based), and the Id of the model or policy used to make the decision.
        /// Can be null if the Policy is not ready yet (e.g. model not loaded).</returns>
        protected override PolicyDecision <int> MapContext(VowpalWabbit vw, string context)
        {
            using (var vwJson = new VowpalWabbitJsonSerializer(vw))
                using (VowpalWabbitExampleCollection vwExample = vwJson.ParseAndCreate(context))
                {
                    var action = (int)vwExample.Predict(VowpalWabbitPredictionType.CostSensitive);
                    var state  = new VWState {
                        ModelId = vw.ID
                    };

                    return(PolicyDecision.Create(action, state));
                }
        }
Example #4
0
        /// <summary>
        /// Determines the action to take for a given context.
        /// This implementation should be thread-safe if multithreading is needed.
        /// </summary>
        /// <param name="vw">The Vowpal Wabbit instance to use.</param>
        /// <param name="context">A user-defined context for the decision.</param>
        /// <returns>A decision tuple containing the index of the action to take (1-based), and the Id of the model or policy used to make the decision.
        /// Can be null if the Policy is not ready yet (e.g. model not loaded).</returns>
        protected override PolicyDecision <int[]> MapContext(VowpalWabbit vw, string context)
        {
            using (var vwJson = new VowpalWabbitJsonSerializer(vw))
                using (VowpalWabbitExampleCollection vwExample = vwJson.ParseAndCreate(context))
                {
                    ActionScore[] vwMultilabelPredictions = vwExample.Predict(VowpalWabbitPredictionType.ActionProbabilities);

                    // VW multi-label predictions are 0-based
                    var actions = vwMultilabelPredictions.Select(a => (int)a.Action + 1).ToArray();
                    var state   = new VWState {
                        ModelId = vw.ID
                    };

                    return(PolicyDecision.Create(actions, state));
                }
        }
Example #5
0
        protected override PolicyDecision <int> MapContext(VowpalWabbit <TContext> vw, TContext context)
        {
            if (this.developmentMode)
            {
                using (var serializer = vw.Serializer.Create(vw.Native))
                {
                    Trace.TraceInformation("Example Context: {0}", serializer.SerializeToString(context));
                }
            }
            var action = (int)vw.Predict(context, VowpalWabbitPredictionType.CostSensitive);
            var state  = new VWState {
                ModelId = vw.Native.ID
            };

            return(PolicyDecision.Create(action, state));
        }
Example #6
0
        protected override PolicyDecision <int[]> MapContext(VowpalWabbit <TContext> vw, TContext context)
        {
            if (this.developmentMode)
            {
                using (var serializer = vw.Serializer.Create(vw.Native))
                {
                    Trace.TraceInformation("Example Context: {0}", serializer.SerializeToString(context));
                }
            }

            ActionScore[] vwMultilabelPredictions = vw.Predict(context, VowpalWabbitPredictionType.ActionScore);

            // VW multi-label predictions are 0-based
            var actions = vwMultilabelPredictions.Select(a => (int)a.Action + 1).ToArray();
            var state   = new VWState {
                ModelId = vw.Native.ID
            };

            return(PolicyDecision.Create(actions, state));
        }