public void Train(ILabeledExampleCollection <LblT, ExT> dataset)
        {
            var binaryDataset = new LabeledDataset <LblT, ExT>(dataset.Select(le =>
                                                                              new LabeledExample <LblT, ExT>(le.Label.Equals(OneLabel) ? OneLabel : OtherLabel, le.Example)));

            mBinaryModel.Train(binaryDataset);
            IsTrained = true;
        }
        public override void Train(ILabeledExampleCollection <SentimentLabel, SparseVector <double> > dataset)
        {
            Preconditions.CheckNotNull(dataset);

            var ds = new LabeledDataset <SentimentLabel, SparseVector <double> >(dataset
                                                                                 .Select(le => new LabeledExample <SentimentLabel, SparseVector <double> >(le.Label, le.Example)));

            mPosModel = TrainModel(ds, SentimentLabel.Positive, SentimentLabel.Negative, SentimentLabel.Neutral);
            mNegModel = TrainModel(ds, SentimentLabel.Negative, SentimentLabel.Positive, SentimentLabel.Neutral);
            mNeuModel = TrainModel(ds, SentimentLabel.Neutral, SentimentLabel.Positive, SentimentLabel.Negative);

            IsTrained = true;
        }
Exemple #3
0
        public override void Train(ILabeledExampleCollection <SentimentLabel, SparseVector <double> > dataset)
        {
            Preconditions.CheckNotNull(dataset);

            var posDataset = new LabeledDataset <SentimentLabel, SparseVector <double> >(dataset.Select(le =>
                                                                                                        new LabeledExample <SentimentLabel, SparseVector <double> >(le.Label == SentimentLabel.Positive
                    ? SentimentLabel.Positive : SentimentLabel.Negative, le.Example)));

            mPosClassifier = CreateModel();
            mPosClassifier.Train(posDataset);

            var negDataset = new LabeledDataset <SentimentLabel, SparseVector <double> >(dataset.Select(le =>
                                                                                                        new LabeledExample <SentimentLabel, SparseVector <double> >(le.Label == SentimentLabel.Negative
                    ? SentimentLabel.Negative : SentimentLabel.Positive, le.Example)));

            mNegClassifier = CreateModel();
            mNegClassifier.Train(negDataset);

            if (PosBiasCalibration != null || NegBiasCalibration != null)
            {
                var    labeledDataset = new LabeledDataset <SentimentLabel, SparseVector <double> >(dataset);
                double?posBias        = Calibrate(true, labeledDataset);
                double?negBias        = Calibrate(false, labeledDataset);
                BiasToPosRate = posBias ?? BiasToPosRate;
                BiasToNegRate = negBias ?? BiasToNegRate;
            }

            mPosSortedScores = mNegSortedScores = null;
            mExampleScores   = dataset.Select(le =>
            {
                Prediction <SentimentLabel> posPrediction = mPosClassifier.Predict(le.Example);
                Prediction <SentimentLabel> negPrediction = mNegClassifier.Predict(le.Example);
                return(new ExampleScore
                {
                    Label = le.Label,
                    PosScore = posPrediction.BestClassLabel == SentimentLabel.Positive ? posPrediction.BestScore : -posPrediction.BestScore,
                    NegScore = negPrediction.BestClassLabel == SentimentLabel.Negative ? -negPrediction.BestScore : negPrediction.BestScore
                });
            }).ToArray();

            UpdateDistrTable();

            IsTrained = true;
        }
Exemple #4
0
        public void Train(ILabeledExampleCollection <LblT, string> dataset)
        {
            Preconditions.CheckState(!IsTrained);
            Preconditions.CheckNotNull(dataset);
            Preconditions.CheckNotNull(BowSpace);
            Preconditions.CheckNotNull(FeatureProcessor);
            Preconditions.CheckNotNull(Model);

            // preprocess the text
            foreach (LabeledExample <LblT, string> le in dataset)
            {
                le.Example = FeatureProcessor.Run(le.Example);
            }

            // bow vectors
            List <SparseVector <double> > bowData = BowSpace is DeltaBowSpace <LblT>
                                                    ?(BowSpace as DeltaBowSpace <LblT>).Initialize(dataset as ILabeledDataset <LblT, string> ?? new LabeledDataset <LblT, string>(dataset))
                                                        : BowSpace.Initialize(dataset.Select(d => d.Example));
            var bowDataset = new LabeledDataset <LblT, SparseVector <double> >();

            for (int i = 0; i < bowData.Count; i++)
            {
                bowDataset.Add(dataset[i].Label, bowData[i]);
            }

            // train
            if (OnTrainModel == null)
            {
                Model.Train(bowDataset);
            }
            else
            {
                OnTrainModel(this, bowDataset);
            }

            IsTrained = true;
        }