private static float[] GetLinearModelWeights(LinearRegressionModelParameters linearModel)
        {
            var weights = new VBuffer <float>();

            linearModel.GetFeatureWeights(ref weights);
            return(weights.GetValues().ToArray());
        }
Пример #2
0
        public static void SdcaRegression()
        {
            // Downloading a regression dataset from github.com/dotnet/machinelearning
            // this will create a housing.txt file in the filsystem this code will run
            // you can open the file to see the data.
            string dataFile = SamplesUtils.DatasetUtils.DownloadHousingRegressionDataset();

            // Creating the ML.Net IHostEnvironment object, needed for the pipeline
            var mlContext = new MLContext();

            // Creating a data reader, based on the format of the data
            var reader = TextLoader.CreateReader(mlContext, c => (
                                                     label: c.LoadFloat(0),
                                                     features: c.LoadFloat(1, 6)
                                                     ),
                                                 separator: '\t', hasHeader: true);

            // Read the data, and leave 10% out, so we can use them for testing
            var data = reader.Read(dataFile);

            var(trainData, testData) = mlContext.Regression.TrainTestSplit(data, testFraction: 0.1);

            // The predictor that gets produced out of training
            LinearRegressionModelParameters pred = null;

            // Create the estimator
            var learningPipeline = reader.MakeNewEstimator()
                                   .Append(r => (r.label, score: mlContext.Regression.Trainers.Sdca(
                                                     r.label,
                                                     r.features,
                                                     l1Threshold: 0f,
                                                     maxIterations: 100,
                                                     onFit: p => pred = p)
                                                 )
                                           );

            // Fit this pipeline to the training data
            var model = learningPipeline.Fit(trainData);

            // Check the weights that the model learned
            VBuffer <float> weights = default;

            pred.GetFeatureWeights(ref weights);

            var weightsValues = weights.GetValues();

            Console.WriteLine($"weight 0 - {weightsValues[0]}");
            Console.WriteLine($"weight 1 - {weightsValues[1]}");

            // Evaluate how the model is doing on the test data
            var dataWithPredictions = model.Transform(testData);
            var metrics             = mlContext.Regression.Evaluate(dataWithPredictions, r => r.label, r => r.score);

            Console.WriteLine($"L1 - {metrics.L1}");               // 3.7226085
            Console.WriteLine($"L2 - {metrics.L2}");               // 24.250636
            Console.WriteLine($"LossFunction - {metrics.LossFn}"); // 24.25063
            Console.WriteLine($"RMS - {metrics.Rms}");             // 4.924493
            Console.WriteLine($"RSquared - {metrics.RSquared}");   // 0.565467
        }
Пример #3
0
        private static void RetrainModel(MLContext mlContext)
        {
            // Define DataViewSchema of data prep pipeline and trained model
            DataViewSchema dataPrepPipelineSchema, modelSchema;

            // Load data preparation pipeline
            ITransformer dataPrepPipeline = mlContext.Model.Load(ModelParamsPath, out dataPrepPipelineSchema);

            // Load trained model
            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out modelSchema);

            // Extract trained model parameters
            LinearRegressionModelParameters originalModelParameters =
                ((ISingleFeaturePredictionTransformer <object>)trainedModel).Model as LinearRegressionModelParameters;

            //Load New Data
            IDataView newData = mlContext.Data.LoadFromEnumerable <TaxiTrip>(TrainingData);

            // Preprocess Data
            IDataView transformedNewData = dataPrepPipeline.Transform(newData);

            // Retrain model
            RegressionPredictionTransformer <LinearRegressionModelParameters> retrainedModel =
                mlContext.Regression.Trainers.OnlineGradientDescent()
                .Fit(transformedNewData, originalModelParameters);

            // Extract Model Parameters of re-trained model
            LinearRegressionModelParameters retrainedModelParameters = retrainedModel.Model as LinearRegressionModelParameters;

            // Inspect Change in Weights
            var weightDiffs =
                originalModelParameters.Weights.Zip(
                    retrainedModelParameters.Weights, (original, retrained) => original - retrained).ToArray();

            Console.WriteLine("Original | Retrained | Difference");
            for (int i = 0; i < weightDiffs.Count(); i++)
            {
                Console.WriteLine($"{originalModelParameters.Weights[i]} | {retrainedModelParameters.Weights[i]} | {weightDiffs[i]}");
            }

            // Define data preparation estimator
            IEstimator <ITransformer> dataPrepEstimator =
                mlContext.Transforms.Concatenate("Features", new string[] { "PassengerCount", "TripTime", "TripDistance", "FareAmount" })
                .Append(mlContext.Transforms.NormalizeMinMax("Features"));

            Console.WriteLine("The model has been retrained.");
            // Create data preparation transformer
            ITransformer dataPrepTransformer = dataPrepEstimator.Fit(newData);

            // Save Data Prep transformer
            mlContext.Model.Save(dataPrepTransformer, newData.Schema, ModelParamsPath);

            // Save Trained Model
            mlContext.Model.Save(retrainedModel, transformedNewData.Schema, ModelPath);
            Console.WriteLine("The retrained model is saved to {0}", ModelPath);
            TestSinglePrediction(mlContext);
            Console.WriteLine("-----------------------------------------------------------------------------------");
        }
Пример #4
0
        public static void RetrainModel(IEnumerable <CurrencyExchangeModelInput> newData)
        {
            DataViewSchema dataPrepPipelineSchema, modelSchema;

            ITransformer dataPrepPipeline = mlContext.Model.Load(GetAbsolutePath(preparationModelPath), out dataPrepPipelineSchema);
            ITransformer trainedModel     = mlContext.Model.Load(GetAbsolutePath(modelPath), out modelSchema);

            LinearRegressionModelParameters originalModelParameters =
                ((RegressionPredictionTransformer <object>)trainedModel).Model as LinearRegressionModelParameters;
        }
Пример #5
0
        static void Main(string[] args)
        {
            Helper.PrintLine("重新训练模型项目");
            MLContext mlContext = new MLContext();

            Helper.PrintLine("加载数据处理管道和神经网络模型...");
            ITransformer dataPrepPipeline = mlContext.Model.Load(DataPipelinePath, out DataViewSchema dataPrepPipelineSchema);
            ITransformer trainedModel     = mlContext.Model.Load(ModelPath, out DataViewSchema modelSchema);

            LinearRegressionModelParameters originalMP =
                ((ISingleFeaturePredictionTransformer <object>)trainedModel).Model as LinearRegressionModelParameters;

            Helper.PrintLine("重新训练神经网络...");
            HousingData[] housingData = new HousingData[]
            {
                new HousingData
                {
                    Size             = 850f,
                    HistoricalPrices = new float[] { 150000f, 175000f, 210000f },
                    CurrentPrice     = 205000f
                },
                new HousingData
                {
                    Size             = 900f,
                    HistoricalPrices = new float[] { 155000f, 190000f, 220000f },
                    CurrentPrice     = 210000f
                },
                new HousingData
                {
                    Size             = 550f,
                    HistoricalPrices = new float[] { 99000f, 98000f, 130000f },
                    CurrentPrice     = 180000f
                }
            };

            IDataView newData            = mlContext.Data.LoadFromEnumerable(housingData);
            IDataView transformedNewData = dataPrepPipeline.Transform(newData);

            RegressionPredictionTransformer <LinearRegressionModelParameters> retrainedModel =
                mlContext.Regression.Trainers.OnlineGradientDescent()
                .Fit(transformedNewData, originalMP);

            LinearRegressionModelParameters retrainedMP = retrainedModel.Model as LinearRegressionModelParameters;

            Helper.PrintLine($"比较模型参数变化:\n\t源模型参数\t|更新模型参数\t|变化\n\t{string.Join("\n\t", originalMP.Weights.Append(originalMP.Bias).Zip(retrainedMP.Weights.Append(retrainedMP.Bias)).Select(weights => $"{weights.First:F2}\t|{weights.Second:F2}\t|{weights.Second - weights.First:F2}"))}");

            Helper.Exit(0);
        }
Пример #6
0
        public void SdcaRegression()
        {
            var env        = new MLContext(seed: 0, conc: 1);
            var dataPath   = GetDataPath(TestDatasets.generatedRegressionDataset.trainFilename);
            var dataSource = new MultiFileSource(dataPath);

            var ctx = new RegressionContext(env);

            var reader = TextLoader.CreateReader(env,
                                                 c => (label: c.LoadFloat(11), features: c.LoadFloat(0, 10)),
                                                 separator: ';', hasHeader: true);

            LinearRegressionModelParameters pred = null;

            var est = reader.MakeNewEstimator()
                      .Append(r => (r.label, score: ctx.Trainers.Sdca(r.label, r.features, maxIterations: 2,
                                                                      onFit: p => pred = p, advancedSettings: s => s.NumThreads = 1)));

            var pipe = reader.Append(est);

            Assert.Null(pred);
            var model = pipe.Fit(dataSource);

            Assert.NotNull(pred);
            // 11 input features, so we ought to have 11 weights.
            Assert.Equal(11, pred.Weights.Count);

            var data = model.Read(dataSource);

            var metrics = ctx.Evaluate(data, r => r.label, r => r.score, new PoissonLoss());

            // Run a sanity check against a few of the metrics.
            Assert.InRange(metrics.L1, 0, double.PositiveInfinity);
            Assert.InRange(metrics.L2, 0, double.PositiveInfinity);
            Assert.InRange(metrics.Rms, 0, double.PositiveInfinity);
            Assert.Equal(metrics.Rms * metrics.Rms, metrics.L2, 5);
            Assert.InRange(metrics.LossFn, 0, double.PositiveInfinity);

            // Just output some data on the schema for fun.
            var schema = data.AsDynamic.Schema;

            for (int c = 0; c < schema.Count; ++c)
            {
                Console.WriteLine($"{schema[c].Name}, {schema[c].Type}");
            }
        }
Пример #7
0
        public void OnlineGradientDescent()
        {
            var env        = new MLContext(seed: 0);
            var dataPath   = GetDataPath(TestDatasets.generatedRegressionDataset.trainFilename);
            var dataSource = new MultiFileSource(dataPath);

            var ctx = new RegressionContext(env);

            var reader = TextLoader.CreateReader(env,
                                                 c => (label: c.LoadFloat(11), features: c.LoadFloat(0, 10)),
                                                 separator: ';', hasHeader: true);

            LinearRegressionModelParameters pred = null;

            var loss = new SquaredLoss();

            var est = reader.MakeNewEstimator()
                      .Append(r => (r.label, score: ctx.Trainers.OnlineGradientDescent(r.label, r.features,
                                                                                       lossFunction: loss,
                                                                                       onFit: (p) => { pred = p; })));

            var pipe = reader.Append(est);

            Assert.Null(pred);
            var model = pipe.Fit(dataSource);

            Assert.NotNull(pred);
            // 11 input features, so we ought to have 11 weights.
            VBuffer <float> weights = new VBuffer <float>();

            pred.GetFeatureWeights(ref weights);
            Assert.Equal(11, weights.Length);

            var data = model.Read(dataSource);

            var metrics = ctx.Evaluate(data, r => r.label, r => r.score, new PoissonLoss());

            // Run a sanity check against a few of the metrics.
            Assert.InRange(metrics.L1, 0, double.PositiveInfinity);
            Assert.InRange(metrics.L2, 0, double.PositiveInfinity);
            Assert.InRange(metrics.Rms, 0, double.PositiveInfinity);
            Assert.Equal(metrics.Rms * metrics.Rms, metrics.L2, 5);
            Assert.InRange(metrics.LossFn, 0, double.PositiveInfinity);
        }