Ejemplo n.º 1
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the SDCA trainer.
        /// </summary>
        /// <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="l2Const">The L2 regularization hyperparameter.</param>
        /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param>
        /// <param name="maxIterations">The maximum number of passes to perform over the data.</param>
        /// <param name="loss">The custom loss, if unspecified will be <see cref="SquaredLossSDCARegressionLossFunction"/>.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained.  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 predicted output.</returns>
        public static Scalar <float> PredictSdcaRegression(this Scalar <float> label, Vector <float> features, Scalar <float> weights = null,
                                                           float?l2Const     = null,
                                                           float?l1Threshold = null,
                                                           int?maxIterations = null,
                                                           ISupportSdcaRegressionLoss loss          = null,
                                                           Action <LinearRegressionPredictor> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(!(l2Const < 0), nameof(l2Const), "Must not be negative");
            Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative");
            Contracts.CheckParam(!(maxIterations < 1), nameof(maxIterations), "Must be positive if specified");
            Contracts.CheckValueOrNull(loss);
            Contracts.CheckValueOrNull(onFit);

            var args = new SdcaRegressionTrainer.Arguments()
            {
                L2Const       = l2Const,
                L1Threshold   = l1Threshold,
                MaxIterations = maxIterations
            };

            if (loss != null)
            {
                args.LossFunction = new TrivialRegressionLossFactory(loss);
            }

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new SdcaRegressionTrainer(env, args, featuresName, labelName, weightsName);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Ejemplo n.º 2
0
        public static CommonOutputs.RegressionOutput TrainRegression(IHostEnvironment env, SdcaRegressionTrainer.Arguments input)
        {
            Contracts.CheckValue(env, nameof(env));
            var host = env.Register("TrainSDCA");

            host.CheckValue(input, nameof(input));
            EntryPointUtils.CheckInputArgs(host, input);

            return(LearnerEntryPointsUtils.Train <SdcaRegressionTrainer.Arguments, CommonOutputs.RegressionOutput>(host, input,
                                                                                                                   () => new SdcaRegressionTrainer(host, input),
                                                                                                                   () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.LabelColumn)));
        }