Search code examples
c#.netml.net

How to use ML.net


I have this:

    class SuggestedFeedBackPredition
    {

        [LoadColumn(0), ColumnName("Label")]
        public bool IsGood { get; set; }
    }

    public class SuggestedFeedbackTrainingData
    {
        [LoadColumn(1), ColumnName("SuggestedFeedBackText")]
        public string? SuggestedFeedBackText { get; set; }

        [LoadColumn(0), ColumnName("Label")]
        public bool IsGood { get; set; }

        public Single Score { get; set; }
        public Single Probability { get; set; }
        public Single PredictedLabel { get; set; }
    }

I have this:

var mlContext = new MLContext();
        var mlData = mlContext.Data.LoadFromEnumerable<SuggestedFeedbackTrainingData>(suggestedFeedbackTrainingData());
        //var pipline = mlContext.Transforms.Text.FeaturizeText("Label", "Features");
        //var pipline = mlContext.Transforms.Text.FeaturizeText("Features", "SuggestedFeedbBackPridiction");
        //var pipline = mlContext.Transforms.CopyColumns(outputColumnName: "Features", inputColumnName: "SuggestedFeedBackText").Append(mlContext.Transforms.Concatenate("Label",
        //        "Label"));
        var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("SuggestedFeedBackText");
        ITransformer model = dataProcessPipeline.Fit(mlData);
        //var dataview1 = mlContext.Data.LoadFromEnumerable<SuggestedFeedbackTrainingData>(testFeedbackTrainingData());
        //var predictions = model.Transform(dataview1);
        //var metrics = mlContext.BinaryClassification.Evaluate(predictions, "IsGood", "Score");

        var predictionsObj = mlContext.Model.CreatePredictionEngine<SuggestedFeedbackTrainingData, SuggestedFeedBackPredition>(model);
        var ff = new SuggestedFeedbackTrainingData();
        ff.SuggestedFeedBackText = text.word;
        var fff = predictionsObj.Predict(ff);
        var isGood = fff.IsGood;

and in my training data:

public static List<SuggestedFeedbackTrainingData> suggestedFeedbackTrainingData()
    {
        List<SuggestedFeedbackTrainingData> sftd = new List<SuggestedFeedbackTrainingData>();

        sftd.Add(new SuggestedFeedbackTrainingData()
        {
            SuggestedFeedBackText = "great",
            IsGood = true
        });

        sftd.Add(new SuggestedFeedbackTrainingData()
        {
            SuggestedFeedBackText = "good",
            IsGood = true
        });

        sftd.Add(new SuggestedFeedbackTrainingData()
        {
            SuggestedFeedBackText = "bad",
            IsGood = false
        });

        sftd.Add(new SuggestedFeedbackTrainingData()
        {
            SuggestedFeedBackText = "nice",
            IsGood = true
        });

        return sftd;
    }

When my input is "this is good" i keep getting the value to property "IsGood" false should return "true"

am i missing something?


Solution

  • There are a variety of things that may impact the performance of your model. Here is some guidance on how to improve your model. Generally that involves using more (and representative data), trying different algorithms, using more columns, etc.

    https://learn.microsoft.com/dotnet/machine-learning/resources/improve-machine-learning-model-ml-net

    Since it sounds like you're just getting started, I would recommend using Model Builder in Visual Studio. This uses Automated Machine Learning (AutoML) to help find the "best" model for your data. I'll just note though that AutoML won't help you overcome not having enough representative data.

    https://dotnet.microsoft.com/learn/ml-dotnet/get-started-tutorial/data