예제 #1
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);
        }
예제 #2
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();
        }
예제 #3
0
        private static int RunPredict(PredictOptions opts)
        {
            string resultFileName = Path.Combine(opts.Output, "ml.result");
            string inputFileName  = opts.Input;
            string modelFileName  = opts.Model;

            var problem = IOHelper.LoadDataFromTextFile(inputFileName);
            var model   = IOHelper.LoadModelFromFile(modelFileName);

            var predictor = new MfPredictor(model);

            using (var predictionWriter = File.CreateText(resultFileName))
            {
                foreach (var mfNode in problem.R)
                {
                    predictionWriter.WriteLine(Math.Round(predictor.Predict(mfNode.U, mfNode.V), 1));
                }
            }

            return(0);
        }