예제 #1
0
 /// <summary>
 /// Initializes a new instance of <see cref="SdcaRegressionTrainer"/>
 /// </summary>
 /// <param name="env">The environment to use.</param>
 /// <param name="labelColumn">The label, or dependent variable.</param>
 /// <param name="featureColumn">The features, or independent variables.</param>
 /// <param name="weights">The optional example weights.</param>
 /// <param name="loss">The custom loss.</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>
 internal SdcaRegressionTrainer(IHostEnvironment env,
                                string labelColumn              = DefaultColumnNames.Label,
                                string featureColumn            = DefaultColumnNames.Features,
                                string weights                  = null,
                                ISupportSdcaRegressionLoss loss = null,
                                float?l2Const     = null,
                                float?l1Threshold = null,
                                int?maxIterations = null)
     : base(env, featureColumn, TrainerUtils.MakeR4ScalarColumn(labelColumn), TrainerUtils.MakeR4ScalarWeightColumn(weights),
            l2Const, l1Threshold, maxIterations)
 {
     Host.CheckNonEmpty(featureColumn, nameof(featureColumn));
     Host.CheckNonEmpty(labelColumn, nameof(labelColumn));
     _loss = loss ?? SdcaTrainerOptions.LossFunction.CreateComponent(env);
     Loss  = _loss;
 }
 /// <summary>
 /// Initializes a new instance of <see cref="SdcaRegressionTrainer"/>
 /// </summary>
 /// <param name="env">The environment to use.</param>
 /// <param name="featureColumn">The features, or independent variables.</param>
 /// <param name="labelColumn">The label, or dependent variable.</param>
 /// <param name="loss">The custom loss.</param>
 /// <param name="weightColumn">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="advancedSettings">A delegate to set more settings.
 /// The settings here will override the ones provided in the direct method signature,
 /// if both are present and have different values.
 /// The columns names, however need to be provided directly, not through the <paramref name="advancedSettings"/>.</param>
 public SdcaRegressionTrainer(IHostEnvironment env,
                              string featureColumn,
                              string labelColumn,
                              string weightColumn             = null,
                              ISupportSdcaRegressionLoss loss = null,
                              float?l2Const     = null,
                              float?l1Threshold = null,
                              int?maxIterations = null,
                              Action <Arguments> advancedSettings = null)
     : base(env, featureColumn, TrainerUtils.MakeR4ScalarLabel(labelColumn), TrainerUtils.MakeR4ScalarWeightColumn(weightColumn), advancedSettings,
            l2Const, l1Threshold, maxIterations)
 {
     Host.CheckNonEmpty(featureColumn, nameof(featureColumn));
     Host.CheckNonEmpty(labelColumn, nameof(labelColumn));
     _loss = loss ?? Args.LossFunction.CreateComponent(env);
     Loss  = _loss;
 }
        /// <summary>
        /// Predict a target using a linear regression model trained with the SDCA 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="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> Sdca(this RegressionContext.RegressionTrainers ctx,
                                          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, if specified.");
            Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified.");
            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);
        }
 public TrivialRegressionLossFactory(ISupportSdcaRegressionLoss loss)
 {
     _loss = loss;
 }