예제 #1
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{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, 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);
        }
예제 #2
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);
 }