예제 #1
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Trainers.LbfgsLogisticRegressionBinaryTrainer"/> trainer.
        /// </summary>
        /// <param name="catalog">The binary classification 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="enforceNonNegativity">Enforce non-negative weights.</param>
        /// <param name="l1Regularization">Weight of L1 regularization term.</param>
        /// <param name="l2Regularization">Weight of L2 regularization term.</param>
        /// <param name="historySize">Memory size for <see cref="Microsoft.ML.Trainers.LbfgsLogisticRegressionBinaryTrainer"/>. Low=faster, less accurate.</param>
        /// <param name="optimizationTolerance">Threshold for optimizer convergence.</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.  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> score, Scalar <float> probability, Scalar <bool> predictedLabel) LbfgsLogisticRegression(this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
                                                                                                                               Scalar <bool> label,
                                                                                                                               Vector <float> features,
                                                                                                                               Scalar <float> weights      = null,
                                                                                                                               float l1Regularization      = Options.Defaults.L1Regularization,
                                                                                                                               float l2Regularization      = Options.Defaults.L2Regularization,
                                                                                                                               float optimizationTolerance = Options.Defaults.OptimizationTolerance,
                                                                                                                               int historySize             = Options.Defaults.HistorySize,
                                                                                                                               bool enforceNonNegativity   = Options.Defaults.EnforceNonNegativity,
                                                                                                                               Action <CalibratedModelParametersBase <LinearBinaryModelParameters, PlattCalibrator> > onFit = null)
        {
            LbfgsStaticUtils.ValidateParams(label, features, weights, l1Regularization, l2Regularization, optimizationTolerance, historySize, enforceNonNegativity, onFit);

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new LbfgsLogisticRegressionBinaryTrainer(env, labelName, featuresName, weightsName,
                                                                       l1Regularization, l2Regularization, optimizationTolerance, historySize, enforceNonNegativity);

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

            return(rec.Output);
        }
예제 #2
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Trainers.LbfgsLogisticRegressionBinaryTrainer"/> trainer.
        /// </summary>
        /// <param name="catalog">The binary classification 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="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.  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>
        /// <param name="options">Advanced arguments to the algorithm.</param>
        /// <returns>The predicted output.</returns>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) LbfgsLogisticRegression(this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
                                                                                                                               Scalar <bool> label,
                                                                                                                               Vector <float> features,
                                                                                                                               Scalar <float> weights,
                                                                                                                               Options options,
                                                                                                                               Action <CalibratedModelParametersBase <LinearBinaryModelParameters, PlattCalibrator> > onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValue(options, nameof(options));
            Contracts.CheckValueOrNull(onFit);

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

                var trainer = new LbfgsLogisticRegressionBinaryTrainer(env, options);

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

            return(rec.Output);
        }
예제 #3
0
        public static IEstimator <ITransformer> BuildTrainingPipeline(MLContext mlContext)
        {
            // Data process configuration with pipeline data transformations
            var dataProcessPipeline = mlContext.Transforms.Categorical.OneHotEncoding(new[] { new InputOutputColumnPair("logged_in", "logged_in"), new InputOutputColumnPair("ns", "ns"), new InputOutputColumnPair("sample", "sample"), new InputOutputColumnPair("split", "split") })
                                      .Append(mlContext.Transforms.Text.FeaturizeText("comment_tf", "comment"))
                                      .Append(mlContext.Transforms.Concatenate("Features", new[] { "logged_in", "ns", "sample", "split", "comment_tf", "year" }))
                                      .Append(mlContext.Transforms.NormalizeMinMax("Features", "Features"))
                                      .AppendCacheCheckpoint(mlContext);

            // Set the training algorithm
            var trainer = mlContext.BinaryClassification.Trainers.LbfgsLogisticRegression(new LbfgsLogisticRegressionBinaryTrainer.Options()
            {
                L2Regularization = 0, 6806942f, L1Regularization = 0, 6276853f, OptimizationTolerance = 0, 0001f, HistorySize = 20, MaximumNumberOfIterations = 1294441415, InitialWeightsDiameter = 0, 8331606f, DenseOptimizer = true, LabelColumnName = "Label", FeatureColumnName = "Features"
            });