/// <summary> /// Runs the personalisation. /// </summary> /// <param name="trainModel">The train model.</param> /// <param name="testModel">The test model.</param> /// <param name="inputs">The personalisation inputs.</param> /// <param name="priors">The priors.</param> /// <param name="precisionShape">The precision shape.</param> /// <param name="precisionScale">The precision scale.</param> /// <param name="thresholdAndNoiseVariance">The threshold and noise variance.</param> /// <param name="limit">The limit.</param> /// <param name="testMode">The test mode.</param> public void RunPersonalisation( CommunityModelBase trainModel, CommunityModelBase testModel, Inputs inputs, CommunityPriors priors, double precisionShape, double precisionScale, double thresholdAndNoiseVariance, int limit = -1, InputMode testMode = InputMode.Validation | InputMode.Testing) { RunPersonalisation( trainModel, testModel, inputs, priors, posteriors => CommunityPriors.FromPosteriors( posteriors, inputs.FeatureSet, precisionShape, precisionScale, thresholdAndNoiseVariance, new[] { inputs.UserName }), limit, testMode); }
/// <summary> /// Runs the community experiment. /// </summary> /// <param name="trainModel">The train model.</param> /// <param name="seedInputs">The seed inputs.</param> /// <param name="communityFeatureSet">The community feature set.</param> /// <param name="priors">The priors.</param> /// <param name="thresholdAndNoiseVariance">The threshold and noise variance.</param> /// <returns>The <see cref="CommunityPosteriors" />.</returns> public CommunityPosteriors RunCommunityTraining( CommunityModelBase trainModel, IList <Inputs> seedInputs, FeatureSet communityFeatureSet, CommunityPriors priors, double thresholdAndNoiseVariance) { this.ModelName = trainModel.Name; // Assume all users have the same feature set type! this.FeatureSetName = communityFeatureSet.ToString(); trainModel.ConstructModel(); // First train the seeds in offline mode trainModel.SetObservedValues( seedInputs.Select(ia => ia.Train.Instances).ToList(), communityFeatureSet, InputMode.CommunityTraining, priors); var communityResults = new Results(); // Now infer the community posteriors trainModel.DoInference(communityFeatureSet, seedInputs.Select(ia => ia.UserName), ref communityResults); this.CommunityResults.Add(communityResults); return(communityResults.CommunityPosteriors); }
/// <summary> /// Runs the personalisation. /// </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="point">The point.</param> /// <param name="thresholdAndNoiseVariance">The threshold and noise variance.</param> /// <param name="limit">The limit.</param> /// <param name="testMode">The test mode.</param> private void RunPersonalisation( CommunityModelBase trainModel, CommunityModelBase testModel, Inputs inputs, CommunityPriors priors, Func <CommunityPosteriors, CommunityPriors> priorsFromPosteriors, int limit = -1, InputMode testMode = InputMode.Validation | InputMode.Testing) { trainModel.ConstructModel(); testModel.ConstructModel(); int numTrain = inputs.Train.Count; if (limit > 0) { numTrain = Math.Min(numTrain, limit); } if (this.BatchSize == -1) { this.BatchSize = numTrain; } int numBatches = numTrain / this.BatchSize; for (int index = 0; index < numBatches; index++) { var batchResults = new Results(); var batch = this.GetBatch(inputs, index, InputMode.Training); if (numBatches > 1) { Console.WriteLine(@"{0} batch {1}/{2}, batch size {3}", this.UserName, index + 1, numBatches, batch.Count); } trainModel.SetObservedValues(new[] { batch }, inputs.FeatureSet, InputMode.Training, priors); try { trainModel.DoInference(inputs.FeatureSet, new[] { inputs.UserName }, ref batchResults); } catch (Exception e) { Console.WriteLine(e.Message); return; } // Set the priors to be the posteriors from the previous run priors = priorsFromPosteriors(batchResults.CommunityPosteriors); if (this.Mode == ExperimentMode.Online) { // clear posteriors to save memory batchResults.Posteriors = null; } // Validation and Test only testModel.Apply(inputs, inputs.FeatureSet, priors, ref batchResults, testMode); this.CommunityResults.Add(batchResults); this.Metrics.Add(new Metrics(inputs, batchResults, ExperimentMode.Online)); this.Count++; } }