コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="lossFunction">The custom loss.</param>
        /// <param name="weights">The optional example weights.</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="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="AveragedPerceptronTrainer"/>.
        public static (Scalar <float> score, Scalar <bool> predictedLabel) AveragedPerceptron(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            IClassificationLoss lossFunction,
            Scalar <bool> label, Vector <float> features, Scalar <float> weights = null,
            float learningRate                   = AveragedLinearArguments.AveragedDefaultArgs.LearningRate,
            bool decreaseLearningRate            = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate,
            float l2RegularizerWeight            = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight,
            int numIterations                    = AveragedLinearArguments.AveragedDefaultArgs.NumIterations,
            Action <LinearBinaryPredictor> onFit = null
            )
        {
            OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit);

            bool hasProbs = lossFunction is HingeLoss;

            var args = new AveragedPerceptronTrainer.Arguments()
            {
                LearningRate         = learningRate,
                DecreaseLearningRate = decreaseLearningRate,
                L2RegularizerWeight  = l2RegularizerWeight,
                NumIterations        = numIterations
            };

            if (lossFunction != null)
            {
                args.LossFunction = new TrivialClassificationLossFactory(lossFunction);
            }

            var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration(
                (env, labelName, featuresName, weightsName) =>
            {
                args.FeatureColumn  = featuresName;
                args.LabelColumn    = labelName;
                args.InitialWeights = weightsName;

                var trainer = new AveragedPerceptronTrainer(env, args);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, label, features, weights, hasProbs);

            return(rec.Output);
        }