示例#1
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));
                }
        }
示例#2
0
 public void ValidateExample(VowpalWabbitExampleCollection example, Context ctx)
 {
     using (var vw = new VowpalWabbit(new VowpalWabbitSettings {
         EnableStringExampleGeneration = true
     }))
         using (var validator = new VowpalWabbitExampleJsonValidator())
         {
             var singleExample = (VowpalWabbitSingleLineExampleCollection)example;
             validator.Validate(ctx.VW, example);
         }
 }
示例#3
0
文件: VWJson.cs 项目: yannstad/mwt-ds
        /// <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));
                }
        }
示例#4
0
文件: VWJson.cs 项目: yannstad/mwt-ds
        /// <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));
                }
        }
示例#5
0
 private void updateModelMaybe()
 {
     if (sinceLastUpdate >= ModelUpdateInterval)
     {
         // Locking at this level ensures a batch of events is processed completely before
         // the next batch (finer locking would allow interleaving, violating timeorder
         lock (this.vwLock)
         {
             // Exit gracefully if the object has been disposed
             if (vwDisposed)
             {
                 return;
             }
             foreach (var dp in log.FlushCompleteEvents())
             {
                 uint action = (uint)((int[])dp.InteractData.Value)[0];
                 var  label  = new ContextualBanditLabel(action, -dp.Reward, ((GenericTopSlotExplorerState)dp.InteractData.ExplorerState).Probabilities[0]);
                 // String (json) contexts need to be handled specially, since the C# interface
                 // does not currently handle the CB label properly
                 if (typeof(TContext) == typeof(string))
                 {
                     // Manually insert the CB label fields into the context
                     string labelStr = string.Format(CultureInfo.InvariantCulture, "\"_label_Action\":{0},\"_label_Cost\":{1},\"_label_Probability\":{2},\"_labelIndex\":{3},",
                                                     label.Action, label.Cost, label.Probability, label.Action - 1);
                     string context = ((string)dp.InteractData.Context).Insert(1, labelStr);
                     using (var vwSerializer = new VowpalWabbitJsonSerializer(vwJson.Native))
                         using (VowpalWabbitExampleCollection vwExample = vwSerializer.ParseAndCreate(context))
                         {
                             vwExample.Learn();
                         }
                 }
                 else
                 {
                     vw.Learn((TContext)dp.InteractData.Context, label, index: (int)label.Action - 1);
                 }
             }
             using (MemoryStream currModel = new MemoryStream())
             {
                 VowpalWabbit vwNative = (typeof(TContext) == typeof(string)) ? vwJson.Native : vw.Native;
                 vwNative.SaveModel(currModel);
                 currModel.Position = 0;
                 this.UpdateModel(currModel);
                 sinceLastUpdate = 0;
             }
         }
     }
 }
        public void Validate(string line, VowpalWabbitExampleCollection example, IVowpalWabbitLabelComparator labelComparator = null, ILabel label = null)
        {
            Assert.IsNotNull(example);

            var jsonExample = example as VowpalWabbitSingleLineExampleCollection;
            Assert.IsNotNull(jsonExample);

            using (var strExample = this.vw.ParseLine(line))
            using (var strJsonExample = this.vw.ParseLine(jsonExample.Example.VowpalWabbitString))
            {
                var diff = strExample.Diff(this.vw, jsonExample.Example, labelComparator);
                Assert.IsNull(diff, diff + " generated string: '" + jsonExample.VowpalWabbitString + "'");

                diff = strExample.Diff(this.vw, strJsonExample, labelComparator);
                Assert.IsNull(diff, diff);
            }
        }
示例#7
0
        public void Validate(string line, VowpalWabbitExampleCollection example, IVowpalWabbitLabelComparator labelComparator = null, ILabel label = null)
        {
            Assert.IsNotNull(example);

            var jsonExample = example as VowpalWabbitSingleLineExampleCollection;

            Assert.IsNotNull(jsonExample);

            using (var strExample = this.vw.ParseLine(line))
                using (var strJsonExample = this.vw.ParseLine(jsonExample.Example.VowpalWabbitString))
                {
                    var diff = strExample.Diff(this.vw, jsonExample.Example, labelComparator);
                    Assert.IsNull(diff, diff + " generated string: '" + jsonExample.VowpalWabbitString + "'");

                    diff = strExample.Diff(this.vw, strJsonExample, labelComparator);
                    Assert.IsNull(diff, diff);
                }
        }