コード例 #1
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;
             }
         }
     }
 }
コード例 #2
0
 private void updateModelMaybe()
 {
     if (sinceLastUpdate >= ModelUpdateInterval)
     {
         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[action - 1]);
             vw.Learn((TContext)dp.InteractData.Context, label, index: (int)label.Action - 1);
         }
         using (MemoryStream currModel = new MemoryStream())
         {
             vw.Native.SaveModel(currModel);
             currModel.Position = 0;
             this.UpdateModel(currModel);
             sinceLastUpdate = 0;
         }
     }
 }