Exemplo n.º 1
0
        private static void MainMain()
        {
            var data         = "Data";
            var trainingPath = Path.Combine(data, "training.ratings");
            var testingPath  = Path.Combine(data, "test.ratings");

            MfProblem training = IOHelper.LoadDataFromTextFile(trainingPath);
            MfProblem testing  = IOHelper.LoadDataFromTextFile(testingPath);

            Console.WriteLine("Model training started.");

            var model = new MfTrainer(new ConsoleLogger()).Fit(training, testing, new MfTrainerOptions()
            {
                Verbose = true,
                LambdaRegularization = 0.2f,
                NumberOfThreads      = 16,
                NumberOfIterations   = 8
            });

            var predictor = new MfPredictor(model);

            Console.WriteLine("Prediction calculation started.");

            MfMetric metrics = predictor.Evaluate(testing);

            Console.WriteLine($"RMSE: {metrics.RootMeanSquaredError}");
            Console.WriteLine($"RSquared: {metrics.RSquared}");
            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        private static int RunTrain(TrainOptions opts)
        {
            var    trainingPath   = opts.Input;
            var    testingPath    = opts.Test;
            string resultFileName = Path.Combine(opts.Output, "ml.result");

            MfProblem training = IOHelper.LoadDataFromTextFile(trainingPath);
            MfProblem testing  = !string.IsNullOrEmpty(testingPath) ? IOHelper.LoadDataFromTextFile(testingPath) : null;

            MfModel model = new MfTrainer().Fit(training, testing, new MfTrainerOptions()
            {
                NumberOfThreads   = opts.Threads,
                ApproximationRank = opts.Factors,
                Eps = opts.Eps,
                LambdaRegularization           = opts.Lambda,
                NonNegativeMatrixFactorization = opts.NonNegativeMatrixFactorization,
                NumberOfIterations             = opts.NumberOfIterations,
                Verbose = opts.Verbose
            });

            var predictor = new MfPredictor(model);

            model.SaveModelToFile(resultFileName);

            if (testing != null)
            {
                MfMetric metrics = predictor.Evaluate(testing);
                Console.WriteLine($"RMSE: {metrics.RootMeanSquaredError}");
            }

            return(0);
        }