/// <summary> /// The main. /// </summary> public static void Main(string[] args) { InitializeUI(); Outputter outputter = Outputter.GetOutputter(Contents.ChapterName); // To get results exactly like in the book set it to FullRun. ExperimentRunType runType = ExperimentRunType.FastRun; try { RunExperiments(outputter, runType); } catch (Exception e) { Console.WriteLine($"\nAn unhandled exception was thrown:\n{e}"); } finally { if (args.Length == 1) { Console.WriteLine("\n\nSaving outputs..."); outputter.SaveOutputAsProducedFlattening(args[0]); Console.WriteLine("Done saving."); } } }
/// <summary> /// Forces ModelRunner to run experiments, takes its results and show them via outputter. /// </summary> /// <param name="outputter">A container for experiments output.</param> /// <param name="experimentRunType"> /// When set to <see cref="ExperimentRunType.FullRun"/>, inference is run to convergence, which gives the metrics shown in the book. /// When set to <see cref="ExperimentRunType.FastRun"/>, the number of iterations in inference is reduced to improve execution time, while still achieving reasonable accuracy numbers, and some of the trait counts are omitted. /// When set to <see cref="ExperimentRunType.TestRun"/>, the number of iterations in inference is reduced still, and even more of the trait counts are omitted. /// </param> public static void RunExperiments(Outputter outputter, ExperimentRunType experimentRunType) { // List containing numbers of traits to use in experiments. A separate set of experiments will be run for each number in the list. var traitCounts = experimentRunType == ExperimentRunType.FullRun ? new int[] { 0, 1, 2, 4, 8, 16 } : experimentRunType == ExperimentRunType.FastRun ? new int[] { 0, 1, 2, 4 } : new int[] { 0, 4 }; // experimentRunType == ExperimentRunType.TestRun var movies = GetMovies(); var recommenderMappingFactory = new RecommenderMappingFactory(movies); var modelRunner = new ModelRunner(recommenderMappingFactory, RatingsPath) { IterationCount = experimentRunType == ExperimentRunType.FullRun ? 200 : 30 }; #region Section3 Console.WriteLine($"\n{Contents.S3TrainingOurRecommender.NumberedName}.\n"); var(ratings, ratingsToStarsDistribution, rankToRatingsDistributions) = PriorRatings(movies); outputter.Out(ratings, Contents.S3TrainingOurRecommender.NumberedName, "Ratings"); outputter.Out(ratingsToStarsDistribution, Contents.S3TrainingOurRecommender.NumberedName, "The number of ratings given for each possible number of stars"); #endregion #region Section4 Console.WriteLine($"\n{Contents.S4OurFirstRecommendations.NumberedName}.\n"); outputter.Out(modelRunner.GetGroundTruth(recommenderMappingFactory.GetBinaryMapping(true)), Contents.S4OurFirstRecommendations.NumberedName, "Ground truth"); var(predictions, metricsOfPredictionsOnBinary) = modelRunner.PredictionsOnBinaryData(traitCounts); outputter.Out(predictions, Contents.S4OurFirstRecommendations.NumberedName, "Predictions"); outputter.Out(metricsOfPredictionsOnBinary.CorrectFractions, Contents.S4OurFirstRecommendations.NumberedName, "Fraction of predictions correct"); outputter.Out(metricsOfPredictionsOnBinary.Ndcgs, Contents.S4OurFirstRecommendations.NumberedName, "Average NDCG@5"); #endregion #region Section5 Console.WriteLine($"\n{Contents.S5ModellingStarRatings.NumberedName}.\n"); outputter.Out(modelRunner.GetGroundTruth(recommenderMappingFactory.GetStarsMapping(true)), Contents.S5ModellingStarRatings.NumberedName, "Ground truth"); var(posteriorDistributionsOfThresholds, predictionsOnStars, metricsOfPredictionsWithStars) = modelRunner.PredictionsOnStarRatings(traitCounts); var ratingsNumToMaeStars = modelRunner.GetRatingsNumToMaeOnStarsPredictions(); outputter.Out(posteriorDistributionsOfThresholds, Contents.S5ModellingStarRatings.NumberedName, "Posterior distributions for star ratings thresholds"); outputter.Out(predictionsOnStars, Contents.S5ModellingStarRatings.NumberedName, "Predictions"); var traitsToCorrectFractionSection5 = new Dictionary <string, IDictionary <string, double> >() { { "Initial", metricsOfPredictionsOnBinary.CorrectFractions }, { "With stars", metricsOfPredictionsWithStars.CorrectFractions } }; var traitCountToMaeSection5 = new Dictionary <string, IDictionary <string, double> >() { { "Initial", metricsOfPredictionsOnBinary.Ndcgs }, { "With stars", metricsOfPredictionsWithStars.Ndcgs } }; outputter.Out(traitsToCorrectFractionSection5, Contents.S5ModellingStarRatings.NumberedName, "Fraction of predictions correct"); outputter.Out(traitCountToMaeSection5, Contents.S5ModellingStarRatings.NumberedName, "Average NDCG@5"); outputter.Out(metricsOfPredictionsWithStars.Maes, Contents.S5ModellingStarRatings.NumberedName, "Mean absolute error (MAE)"); #endregion #region Section6 Console.WriteLine($"\n{Contents.S6AnotherColdStartProblem.NumberedName}.\n"); outputter.Out(rankToRatingsDistributions, Contents.S6AnotherColdStartProblem.NumberedName, "The number of ratings given for each movie in the data set as a whole. "); var metricsOfPredictionsWithFeatures = modelRunner.PredictionsOnDataWithFeatures(traitCounts); var ratingsNumToMaeFeatures = modelRunner.GetRatingsToMaeOnFeaturePredictions(); outputter.Out(ratingsNumToMaeStars, Contents.S6AnotherColdStartProblem.NumberedName, "MAE for movies with different numbers of ratings."); var ratingsNumToMae = new Dictionary <string, Dictionary <string, double> > { { "With stars", ratingsNumToMaeStars }, { "With stars and features", ratingsNumToMaeFeatures } }; outputter.Out(ratingsNumToMae, Contents.S6AnotherColdStartProblem.NumberedName, "MAE for movies with different numbers of ratings. A model including feature information."); var traitCountToMae = new Dictionary <string, IDictionary <string, double> >() { { "With stars", metricsOfPredictionsWithStars.Maes }, { "With stars and features", metricsOfPredictionsWithFeatures.Maes }, }; outputter.Out(traitCountToMae, Contents.S6AnotherColdStartProblem.NumberedName, "Mean absolute error (MAE)"); var traitCountToNdcg = new Dictionary <string, IDictionary <string, double> >() { { "Initial", metricsOfPredictionsOnBinary.Ndcgs }, { "With stars", metricsOfPredictionsWithStars.Ndcgs }, { "With stars and features", metricsOfPredictionsWithFeatures.Ndcgs }, }; outputter.Out(traitCountToNdcg, Contents.S6AnotherColdStartProblem.NumberedName, "Average NDCG@5"); #endregion Console.WriteLine("\nCompleted all experiments."); }