Ejemplo n.º 1
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the <see cref="OnlineGradientDescentTrainer"/> trainer.
        /// </summary>
        /// <param name="catalog">The regression catalog trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="lossFunction">The custom loss. Defaults to <see cref="SquaredLoss"/> if not provided.</param>
        /// <param name="learningRate">The learning Rate.</param>
        /// <param name="decreaseLearningRate">Decrease learning rate as iterations progress.</param>
        /// <param name="l2Regularization">L2 regularization weight.</param>
        /// <param name="numIterations">Number of training iterations through the data.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the
        /// result in any way; it is only a way for the caller to be informed about what was learnt.</param>
        /// <returns>The set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), and the predicted label.</returns>
        /// <seealso cref="OnlineGradientDescentTrainer"/>.
        /// <returns>The predicted output.</returns>
        public static Scalar <float> OnlineGradientDescent(this RegressionCatalog.RegressionTrainers catalog,
                                                           Scalar <float> label,
                                                           Vector <float> features,
                                                           Scalar <float> weights       = null,
                                                           IRegressionLoss lossFunction = null,
                                                           float learningRate           = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.LearningRate,
                                                           bool decreaseLearningRate    = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.DecreaseLearningRate,
                                                           float l2Regularization       = OnlineGradientDescentTrainer.Options.OgdDefaultArgs.L2Regularization,
                                                           int numIterations            = OnlineLinearOptions.OnlineDefault.NumberOfIterations,
                                                           Action <LinearRegressionModelParameters> onFit = null)
        {
            OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2Regularization, numIterations, onFit);
            Contracts.CheckValueOrNull(lossFunction);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new OnlineGradientDescentTrainer(env, labelName, featuresName, learningRate,
                                                               decreaseLearningRate, l2Regularization, numIterations, lossFunction);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }

                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the <see cref="OnlineGradientDescentTrainer"/> trainer.
        /// </summary>
        /// <param name="catalog">The regression catalog trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="options">Advanced arguments to the algorithm.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the
        /// result in any way; it is only a way for the caller to be informed about what was learnt.</param>
        /// <returns>The set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), and the predicted label.</returns>
        /// <seealso cref="OnlineGradientDescentTrainer"/>.
        /// <returns>The predicted output.</returns>
        public static Scalar <float> OnlineGradientDescent(this RegressionCatalog.RegressionTrainers catalog,
                                                           Scalar <float> label,
                                                           Vector <float> features,
                                                           Scalar <float> weights,
                                                           OnlineGradientDescentTrainer.Options options,
                                                           Action <LinearRegressionModelParameters> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckValue(options, nameof(options));
            Contracts.CheckValueOrNull(onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                options.LabelColumnName   = labelName;
                options.FeatureColumnName = featuresName;

                var trainer = new OnlineGradientDescentTrainer(env, options);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }

                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Ejemplo n.º 3
0
        public void OnlineLinearWorkout()
        {
            var dataPath = GetDataPath("breast-cancer.txt");

            var data = TextLoader.CreateReader(Env, ctx => (Label: ctx.LoadFloat(0), Features: ctx.LoadFloat(1, 10)))
                       .Read(dataPath);

            var pipe = data.MakeNewEstimator()
                       .Append(r => (r.Label, Features: r.Features.Normalize()));

            var trainData = pipe.Fit(data).Transform(data).AsDynamic;

            var ogdTrainer = new OnlineGradientDescentTrainer(Env, "Label", "Features");

            TestEstimatorCore(ogdTrainer, trainData);
            var ogdModel = ogdTrainer.Fit(trainData);

            ogdTrainer.Train(trainData, ogdModel.Model);

            var apTrainer = new AveragedPerceptronTrainer(Env, "Label", "Features", lossFunction: new HingeLoss(), advancedSettings: s =>
            {
                s.LearningRate = 0.5f;
            });

            TestEstimatorCore(apTrainer, trainData);

            var apModel = apTrainer.Fit(trainData);

            apTrainer.Train(trainData, apModel.Model);

            Done();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the <see cref="Microsoft.ML.Runtime.Learners.OnlineGradientDescentTrainer"/> trainer.
        /// </summary>
        /// <param name="ctx">The regression context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="lossFunction">The custom loss. Defaults to <see cref="SquaredLoss"/> if not provided.</param>
        /// <param name="learningRate">The learning Rate.</param>
        /// <param name="decreaseLearningRate">Decrease learning rate as iterations progress.</param>
        /// <param name="l2RegularizerWeight">L2 regularization weight.</param>
        /// <param name="numIterations">Number of training iterations through the data.</param>
        /// <param name="advancedSettings">A delegate to supply more advanced arguments to the algorithm.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the
        /// result in any way; it is only a way for the caller to be informed about what was learnt.</param>
        /// <returns>The set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), and the predicted label.</returns>
        /// <seealso cref="OnlineGradientDescentTrainer"/>.
        /// <returns>The predicted output.</returns>
        public static Scalar <float> OnlineGradientDescent(this RegressionContext.RegressionTrainers ctx,
                                                           Scalar <float> label,
                                                           Vector <float> features,
                                                           Scalar <float> weights       = null,
                                                           IRegressionLoss lossFunction = null,
                                                           float learningRate           = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.LearningRate,
                                                           bool decreaseLearningRate    = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.DecreaseLearningRate,
                                                           float l2RegularizerWeight    = OnlineGradientDescentTrainer.Arguments.OgdDefaultArgs.L2RegularizerWeight,
                                                           int numIterations            = OnlineLinearArguments.OnlineDefaultArgs.NumIterations,
                                                           Action <AveragedLinearArguments> advancedSettings = null,
                                                           Action <LinearRegressionPredictor> onFit          = null)
        {
            OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit, advancedSettings);
            Contracts.CheckValueOrNull(lossFunction);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new OnlineGradientDescentTrainer(env, labelName, featuresName, learningRate,
                                                               decreaseLearningRate, l2RegularizerWeight, numIterations, weightsName, lossFunction, advancedSettings);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }

                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
 public Arguments()
 {
     BasePredictors = new[]
     {
         ComponentFactoryUtils.CreateFromFunction(
             env => {
             var trainerEstimator = new OnlineGradientDescentTrainer(env);
             return(TrainerUtils.MapTrainerEstimatorToTrainer <OnlineGradientDescentTrainer,
                                                               LinearRegressionModelParameters, LinearRegressionModelParameters>(env, trainerEstimator));
         })
     };
 }
Ejemplo n.º 6
0
        public void OnlineLinearWorkout()
        {
            var dataPath = GetDataPath("breast-cancer.txt");

            var regressionData = TextLoaderStatic.CreateReader(ML, ctx => (Label: ctx.LoadFloat(0), Features: ctx.LoadFloat(1, 10)))
                                 .Read(dataPath);

            var regressionPipe = regressionData.MakeNewEstimator()
                                 .Append(r => (r.Label, Features: r.Features.Normalize()));

            var regressionTrainData = regressionPipe.Fit(regressionData).Transform(regressionData).AsDynamic;

            var ogdTrainer = new OnlineGradientDescentTrainer(ML, "Label", "Features");

            TestEstimatorCore(ogdTrainer, regressionTrainData);
            var ogdModel = ogdTrainer.Fit(regressionTrainData);

            ogdTrainer.Train(regressionTrainData, ogdModel.Model);

            var binaryData = TextLoaderStatic.CreateReader(ML, ctx => (Label: ctx.LoadBool(0), Features: ctx.LoadFloat(1, 10)))
                             .Read(dataPath);

            var binaryPipe = binaryData.MakeNewEstimator()
                             .Append(r => (r.Label, Features: r.Features.Normalize()));

            var binaryTrainData = binaryPipe.Fit(binaryData).Transform(binaryData).AsDynamic;
            var apTrainer       = new AveragedPerceptronTrainer(ML, "Label", "Features", lossFunction: new HingeLoss(), advancedSettings: s =>
            {
                s.LearningRate = 0.5f;
            });

            TestEstimatorCore(apTrainer, binaryTrainData);

            var apModel = apTrainer.Fit(binaryTrainData);

            apTrainer.Train(binaryTrainData, apModel.Model);

            var svmTrainer = new LinearSvmTrainer(ML, "Label", "Features");

            TestEstimatorCore(svmTrainer, binaryTrainData);

            var svmModel = svmTrainer.Fit(binaryTrainData);

            svmTrainer.Train(binaryTrainData, apModel.Model);

            Done();
        }
Ejemplo n.º 7
0
        public void OnlineLinearWorkout()
        {
            var dataPath = GetDataPath("breast-cancer.txt");

            var data = TextLoader.CreateReader(Env, ctx => (Label: ctx.LoadFloat(0), Features: ctx.LoadFloat(1, 10)))
                       .Read(new MultiFileSource(dataPath));

            var pipe = data.MakeNewEstimator()
                       .Append(r => (r.Label, Features: r.Features.Normalize()));

            var trainData = pipe.Fit(data).Transform(data).AsDynamic;

            IEstimator <ITransformer> est = new OnlineGradientDescentTrainer(Env, new OnlineGradientDescentTrainer.Arguments());

            TestEstimatorCore(est, trainData);

            est = new AveragedPerceptronTrainer(Env, new AveragedPerceptronTrainer.Arguments());
            TestEstimatorCore(est, trainData);

            Done();
        }