예제 #1
0
        public void OVAWithAllConstructorArgs()
        {
            var(pipeline, data) = GetMultiClassPipeline();
            var calibrator        = new PlattCalibratorTrainer(Env);
            var averagePerceptron = new AveragedPerceptronTrainer(Env, "Label", "Features", advancedSettings: s =>
            {
                s.Shuffle    = true;
                s.Calibrator = null;
            });

            pipeline = pipeline.Append(new Ova(Env, averagePerceptron, "Label", true, calibrator: calibrator, 10000, true))
                       .Append(new KeyToValueMappingEstimator(Env, "PredictedLabel"));

            TestEstimatorCore(pipeline, data);
            Done();
        }
예제 #2
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();
        }
        CalibratorTestData GetCalibratorTestData()
        {
            var(pipeline, data) = GetBinaryClassificationPipeline();
            var binaryTrainer = new AveragedPerceptronTrainer(Env);

            pipeline = pipeline.Append(binaryTrainer);

            var transformer       = pipeline.Fit(data);
            var scoredData        = transformer.Transform(data);
            var scoredDataPreview = scoredData.Preview();

            Assert.True(scoredDataPreview.ColumnView.Length == 5);

            return(new CalibratorTestData
            {
                data = data,
                scoredData = scoredData,
                pipeline = pipeline,
                transformer = ((TransformerChain <BinaryPredictionTransformer <LinearBinaryModelParameters> >)transformer).LastTransformer as BinaryPredictionTransformer <LinearBinaryModelParameters>,
            });
        }
예제 #4
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();
        }
예제 #5
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </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="lossFunction">The custom loss.</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="AveragedPerceptronTrainer"/>.
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[AveragedPerceptron](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/AveragedPerceptronBinaryClassification.cs)]
        /// ]]></format>
        /// </example>
        public static (Scalar <float> score, Scalar <bool> predictedLabel) AveragedPerceptron(
            this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
            Scalar <bool> label,
            Vector <float> features,
            Scalar <float> weights,
            IClassificationLoss lossFunction,
            AveragedPerceptronTrainer.Options options,
            Action <LinearBinaryModelParameters> onFit = null
            )
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckValueOrNull(options);
            Contracts.CheckValueOrNull(onFit);

            bool hasProbs = lossFunction is LogLoss;

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

                var trainer = new AveragedPerceptronTrainer(env, options);

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

            return(rec.Output);
        }
예제 #6
0
        public void OVAWithAllConstructorArgs()
        {
            var    dataPath = GetDataPath(IrisDataPath);
            string featNam  = "Features";
            string labNam   = "Label";

            using (var env = new TlcEnvironment())
            {
                var calibrator = new FixedPlattCalibratorTrainer(env, new FixedPlattCalibratorTrainer.Arguments());

                var data = new TextLoader(env, GetIrisLoaderArgs()).Read(new MultiFileSource(dataPath));

                var averagePerceptron = new AveragedPerceptronTrainer(env, new AveragedPerceptronTrainer.Arguments {
                    FeatureColumn = featNam, LabelColumn = labNam, Shuffle = true, Calibrator = null
                });
                var pipeline = new TermEstimator(env, labNam)
                               .Append(new Ova(env, averagePerceptron, labNam, true, calibrator: calibrator, 10000, true))
                               .Append(new KeyToValueEstimator(env, "PredictedLabel"));

                TestEstimatorCore(pipeline, data);
            }
        }
        public void New_TrainWithInitialPredictor()
        {
            using (var env = new LocalEnvironment(seed: 1, conc: 1))
            {
                var data = new TextLoader(env, MakeSentimentTextLoaderArgs()).Read(new MultiFileSource(GetDataPath(TestDatasets.Sentiment.trainFilename)));

                // Pipeline.
                var pipeline = new TextTransform(env, "SentimentText", "Features");

                // Train the pipeline, prepare train set.
                var trainData = pipeline.FitAndTransform(data);

                // Train the first predictor.
                var trainer    = new LinearClassificationTrainer(env, "Features", "Label", advancedSettings: (s) => s.NumThreads = 1);
                var firstModel = trainer.Fit(trainData);

                // Train the second predictor on the same data.
                var secondTrainer = new AveragedPerceptronTrainer(env, "Label", "Features");

                var trainRoles = new RoleMappedData(trainData, label: "Label", feature: "Features");
                var finalModel = secondTrainer.Train(new TrainContext(trainRoles, initialPredictor: firstModel.Model));
            }
        }
        public void TrainWithInitialPredictor()
        {
            using (var env = new LocalEnvironment(seed: 1, conc: 1))
            {
                // Pipeline
                var loader = TextLoader.ReadFile(env, MakeSentimentTextLoaderArgs(), new MultiFileSource(GetDataPath(TestDatasets.Sentiment.trainFilename)));

                var trans     = TextTransform.Create(env, MakeSentimentTextTransformArgs(), loader);
                var trainData = trans;

                var cachedTrain = new CacheDataView(env, trainData, prefetch: null);
                // Train the first predictor.
                var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments
                {
                    NumThreads = 1
                });
                var trainRoles = new RoleMappedData(cachedTrain, label: "Label", feature: "Features");
                var predictor  = trainer.Train(new Runtime.TrainContext(trainRoles));

                // Train the second predictor on the same data.
                var secondTrainer  = new AveragedPerceptronTrainer(env, "Label", "Features");
                var finalPredictor = secondTrainer.Train(new TrainContext(trainRoles, initialPredictor: predictor));
            }
        }
        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;

            IEstimator <ITransformer> est = new OnlineGradientDescentTrainer(Env, "Label", "Features");

            TestEstimatorCore(est, trainData);

            est = new AveragedPerceptronTrainer(Env, "Label", "Features", lossFunction: new HingeLoss.Arguments(), advancedSettings: s =>
            {
                s.LearningRate = 0.5f;
            });
            TestEstimatorCore(est, trainData);

            Done();
        }
예제 #10
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="advancedSettings">A delegate to supply more avdanced 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="AveragedPerceptronTrainer"/>.
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[AveragedPerceptron](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/AveragedPerceptronBinaryClassification.cs)]
        /// ]]></format>
        /// </example>
        public static (Scalar <float> score, Scalar <bool> predictedLabel) AveragedPerceptron(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            Scalar <bool> label,
            Vector <float> features,
            Scalar <float> weights           = null,
            IClassificationLoss lossFunction = null,
            float learningRate        = AveragedLinearArguments.AveragedDefaultArgs.LearningRate,
            bool decreaseLearningRate = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate,
            float l2RegularizerWeight = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight,
            int numIterations         = AveragedLinearArguments.AveragedDefaultArgs.NumIterations,
            Action <AveragedPerceptronTrainer.Arguments> advancedSettings = null,
            Action <LinearBinaryModelParameters> onFit = null
            )
        {
            OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit, advancedSettings);

            bool hasProbs = lossFunction is LogLoss;

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

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

            return(rec.Output);
        }
예제 #11
0
 public MyAveragedPerceptron(IHostEnvironment env, AveragedPerceptronTrainer.Arguments args, string featureCol, string labelCol)
     : base(env, new TrainerInfo(caching: false), featureCol, labelCol)
 {
     _trainer = new AveragedPerceptronTrainer(env, args);
 }
예제 #12
0
        private void MixMatch(string dataPath)
        {
            // Create a new environment for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var env = new LocalEnvironment();

            // Read the data as an IDataView.
            // First, we define the reader: specify the data columns and where to find them in the text file.
            var reader = TextLoader.CreateReader(env, ctx => (
                                                     // The four features of the Iris dataset.
                                                     SepalLength: ctx.LoadFloat(0),
                                                     SepalWidth: ctx.LoadFloat(1),
                                                     PetalLength: ctx.LoadFloat(2),
                                                     PetalWidth: ctx.LoadFloat(3),
                                                     // Label: kind of iris.
                                                     Label: ctx.LoadText(4)
                                                     ),
                                                 // Default separator is tab, but the dataset has comma.
                                                 separator: ',');

            // Read the data.
            var data = reader.Read(new MultiFileSource(dataPath));

            // Build the pre-processing pipeline.
            var learningPipeline = reader.MakeNewEstimator()
                                   .Append(r => (
                                               // Convert string label to a key.
                                               Label: r.Label.ToKey(),
                                               // Concatenate all the features together into one column 'Features'.
                                               Features: r.SepalLength.ConcatWith(r.SepalWidth, r.PetalLength, r.PetalWidth)));

            // Now, at the time of writing, there is no static pipeline for OVA (one-versus-all). So, let's
            // append the OVA learner to the dynamic pipeline.
            IEstimator <ITransformer> dynamicPipe = learningPipeline.AsDynamic;

            // Create a binary classification trainer.
            var binaryTrainer = new AveragedPerceptronTrainer(env, "Label", "Features");

            // Append the OVA learner to the pipeline.
            dynamicPipe = dynamicPipe.Append(new Ova(env, binaryTrainer));

            // At this point, we have a choice. We could continue working with the dynamically-typed pipeline, and
            // ultimately call dynamicPipe.Fit(data.AsDynamic) to get the model, or we could go back into the static world.
            // Here's how we go back to the static pipeline:
            var staticFinalPipe = dynamicPipe.AssertStatic(env,
                                                           // Declare the shape of the input. As you can see, it's identical to the shape of the reader:
                                                           // four float features and a string label.
                                                           c => (
                                                               SepalLength: c.R4.Scalar,
                                                               SepalWidth: c.R4.Scalar,
                                                               PetalLength: c.R4.Scalar,
                                                               PetalWidth: c.R4.Scalar,
                                                               Label: c.Text.Scalar),
                                                           // Declare the shape of the output (or a relevant subset of it).
                                                           // In our case, we care only about the predicted label column (a key type), and scores (vector of floats).
                                                           c => (
                                                               Score: c.R4.Vector,
                                                               // Predicted label is a key backed by uint, with text values (since original labels are text).
                                                               PredictedLabel: c.KeyU4.TextValues.Scalar))
                                  // Convert the predicted label from key back to the original string value.
                                  .Append(r => r.PredictedLabel.ToValue());

            // Train the model in a statically typed way.
            var model = staticFinalPipe.Fit(data);

            // And here is how we could've stayed in the dynamic pipeline and train that way.
            dynamicPipe = dynamicPipe.Append(new KeyToValueEstimator(env, "PredictedLabel"));
            var dynamicModel = dynamicPipe.Fit(data.AsDynamic);

            // Now 'dynamicModel', and 'model.AsDynamic' are equivalent.
            var rs = model.Transform(data).GetColumn(x => x).ToArray();
        }