/// <summary> /// Priors from posteriors. /// </summary> /// <param name="posteriors">The posteriors.</param> /// <param name="noiseVariance">The noise variance.</param> /// <returns> /// The <see cref="Priors" /> /// </returns> internal static Priors FromPosteriors(Posteriors posteriors, double noiseVariance) { return(new Priors { Weights = posteriors.Weights, Threshold = posteriors.Threshold, NoiseVariance = noiseVariance }); }
/// <summary> /// Priors from posteriors with possible missing features. /// </summary> /// <param name="posteriors">The posteriors.</param> /// <param name="featureSet">The feature set.</param> /// <param name="noiseVariance">The noise variance.</param> /// <returns>The <see cref="Priors" /></returns> internal static Priors FromPosteriors(Posteriors posteriors, FeatureSet featureSet, double noiseVariance) { return(new Priors { Weights = featureSet.FeatureBuckets.ToDictionary( ia => ia, ia => posteriors.Weights.ContainsKey(ia) ? posteriors.Weights[ia] : Gaussian.FromMeanAndVariance(0.0, noiseVariance)), Threshold = posteriors.Threshold, NoiseVariance = noiseVariance, }); }
/// <summary> /// Runs the experiment. /// </summary> /// <param name="trainModel">The train model.</param> /// <param name="testModel">The test model.</param> /// <param name="inputs">The inputs.</param> /// <param name="priors">The priors.</param> /// <param name="thresholdAndNoiseVariance">The threshold and noise variance.</param> /// <param name="limit">The limit.</param> /// <param name="trainMode">The train mode.</param> /// <param name="testMode">The test mode.</param> public void Run( ReplyToModelBase trainModel, ReplyToModelBase testModel, Inputs inputs, Priors priors, double thresholdAndNoiseVariance, int limit = -1, InputMode trainMode = InputMode.Training, InputMode testMode = InputMode.Validation | InputMode.Testing) { this.ModelName = trainModel.Name; this.FeatureSetName = inputs.FeatureSet.ToString(); trainModel.ConstructModel(); testModel.ConstructModel(); int numTrain = trainMode == InputMode.Training ? inputs.Train.Count : inputs.TrainAndValidation.Count; if (limit > 0) { numTrain = Math.Min(numTrain, limit); } if (this.BatchSize == -1) { this.BatchSize = numTrain; } int numBatches = 1; if (this.Mode == ExperimentMode.Incremental || this.Mode == ExperimentMode.Online) { numBatches = numTrain / this.BatchSize; } for (int index = 0; index < numBatches; index++) { var batchResults = new Results(); var batch = this.GetBatch(inputs, index, trainMode); if (numBatches > 1) { Console.WriteLine(@"{0} batch {1}/{2}, batch size {3}", this.UserName, index + 1, numBatches, batch.Count); } var stopwatch = new Stopwatch(); stopwatch.Start(); trainModel.SetObservedValues(batch, inputs.FeatureSet, InputMode.Training, priors); try { trainModel.DoInference(inputs.FeatureSet, ref batchResults); } catch (Exception e) { Console.WriteLine(e.Message); return; } stopwatch.Stop(); this.TrainTimings.Add(stopwatch.Elapsed); // Save the last set of posteriors this.Posteriors = batchResults.Posteriors; // Set the priors to be the posteriors from the previous run var newPriors = Priors.FromPosteriors(this.Posteriors, inputs.FeatureSet, thresholdAndNoiseVariance); if (this.Mode == ExperimentMode.Online || this.Mode == ExperimentMode.Incremental) { if (this.Mode == ExperimentMode.Online) { priors = newPriors; } // clear posteriors to save on memory batchResults.Posteriors = null; } stopwatch.Restart(); // Validation and Test only testModel.Apply(inputs, inputs.FeatureSet, newPriors, ref batchResults, testMode); stopwatch.Stop(); this.TestTimings.Add(stopwatch.Elapsed); this.Results.Add(batchResults); this.Metrics.Add(new Metrics(inputs, batchResults, this.Mode)); this.Count++; } }