private void TestSinglePrediction(MLContext mlContext, string sentiment)
        {
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = sentiment.ToString()
            };

            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out var modelInputSchema);

            // Create prediction engine related to the loaded trained model
            var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            //Score
            var resultprediction = predEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");

            //  MessageBox.Show($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");

            this.sentimentToxic     = $"{(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")}";
            this.sentimentToxicProb = $"{resultprediction.Probability}";



            //  MessageBox.Show(sentimentToxicProb);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Create MLContext to be shared across the model creation workflow objects
            // Set a random seed for repeatable/deterministic results across multiple trainings.
            var mlContext = new MLContext(seed: 1);

            if (IsTrainingMode)
            {
                // STEP 1: Common data loading configuration
                IDataView dataView = mlContext.Data.LoadFromTextFile <SentimentIssue>(DataPath, hasHeader: true);

                TrainTestData trainTestSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
                IDataView     trainingData   = trainTestSplit.TrainSet;
                IDataView     testData       = trainTestSplit.TestSet;

                // STEP 2: Common data process configuration with pipeline data transformations
                var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(
                    outputColumnName: "Features",
                    inputColumnName: nameof(SentimentIssue.Text));

                // STEP 3: Set the training algorithm, then create and config the modelBuilder
                var trainer          = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features");
                var trainingPipeline = dataProcessPipeline.Append(trainer);

                // STEP 4: Train the model fitting to the DataSet
                var trainedModel = trainingPipeline.Fit(trainingData);

                // STEP 5: Evaluate the model and show accuracy stats
                var predictions = trainedModel.Transform(testData);
                var metrics     = mlContext.BinaryClassification.Evaluate(data: predictions, labelColumnName: "Label", scoreColumnName: "Score");

                ConsoleHelper.PrintBinaryClassificationMetrics(trainer.ToString(), metrics);

                // STEP 6: Save/persist the trained model to a .ZIP file
                mlContext.Model.Save(trainedModel, trainingData.Schema, ModelPath);
                Console.WriteLine("The model is saved to {0}", ModelPath);
            }
            else
            {
                // TRY IT: Make a single test prediction, loading the model from .ZIP file
                DataViewSchema modelSchema;
                var            sentimentModel = mlContext.Model.Load(ModelPath, out modelSchema);

                SentimentIssue sampleStatement = new SentimentIssue {
                    Text = "Your service is crappy *0(*!!!!!!s****."
                };
                // SentimentIssue sampleStatement = new SentimentIssue { Text = "Not the best, imo" };

                // Create prediction engine related to the loaded trained model
                var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(sentimentModel);

                // Score
                var resultprediction = predEngine.Predict(sampleStatement);

                Console.WriteLine($"=============== Single Prediction  ===============");
                Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
                Console.WriteLine($"================End of Process.Hit any key to exit==================================");
                Console.ReadLine();
            }
        }
        // (OPTIONAL) Try/test a single prediction by loding the model from the file, first.
        private static void TestSinglePrediction(MLContext mlContext)
        {
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel;

            using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                trainedModel = mlContext.Model.Load(stream);
            }

            // Create prediction engine related to the loaded trained model
            var predFunction = trainedModel.MakePredictionFunction <SentimentIssue, SentimentPrediction>(mlContext);

            //Score
            var resultprediction = predFunction.Predict(sampleStatement);

            // Using a Model Scorer helper class --> 3 lines, including the object creation, and a single object to deal with
            // var modelScorer = new ModelScorer<SentimentIssue, SentimentPrediction>(mlContext);
            // modelScorer.LoadModelFromZipFile(ModelPath);
            // var resultprediction = modelScorer.PredictSingle(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Nice")} sentiment | Probability: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");
            //
        }
Esempio n. 4
0
        public IActionResult Smoke([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "smoke")] HttpRequest req, ILogger log)
        {
            try
            {
                var sentimentIssue = new SentimentIssue()
                {
                    SentimentText = "This was a great place!"
                };

                //Make Prediction
                var sentimentPrediction = _predictionEnginePool.Predict(modelName: Constants.ModelName, example: sentimentIssue);

                //Convert prediction to string
                string sentiment = Convert.ToBoolean(sentimentPrediction.Prediction) ? "Positive" : "Negative";

                //Get model uri
                var uri = Environment.GetEnvironmentVariable("ML_MODEL_URI") ?? string.Empty;

                //Return Prediction
                return(new OkObjectResult($"{sentiment}-{uri}"));
            }
            catch (Exception ex)
            {
                log.LogCritical(ex.ToString());
            }

            return(new BadRequestResult());
        }
        public override bool Execute(CheckInput input)
        {
            var mlContext = new MLContext(seed: 1);

            var modelPath = input.ModelPath.IsEmpty()
                ? TrainingCommand.GetModelPath(TrainingCommand.DefaultModelPath)
                : input.ModelPath.EndsWith(".zip")
                    ? input.ModelPath
                    : TrainingCommand.GetModelPath(input.ModelPath);

            var transformer = mlContext.Model.Load(modelPath, out _);
            var engine      =
                mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(transformer);

            var example = new SentimentIssue {
                Text = input.Text
            };
            var prediction = engine.Predict(example);
            var result     = prediction.Prediction ? "Toxic" : "Non Toxic";

            Console.WriteLine($"=============== Single Prediction  ===============");

            Console.WriteLine($"Text: {example.Text} |" +
                              $" Prediction: {result} sentiment | " +
                              $"Probability of being toxic: {prediction.Probability} ");
            return(true);
        }
Esempio n. 6
0
        public SentimentPrediction Predict(SentimentIssue issue)
        {
            var predictionEngine =
                context.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(BuildAndTrainModel.Value);

            return(predictionEngine.Predict(issue));
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            //Create MLContext to be shared across the model creation workflow objects
            //Set a random seed for repeatable/deterministic results across multiple trainings.
            var mlContext = new MLContext();

            // STEP 1: Common data loading configuration
            DataLoader dataLoader       = new DataLoader(mlContext);
            var        trainingDataView = dataLoader.GetDataView(TrainDataPath);
            var        testDataView     = dataLoader.GetDataView(TestDataPath);

            // STEP 2: Common data process configuration with pipeline data transformations
            var dataProcessor       = new DataProcessor(mlContext);
            var dataProcessPipeline = dataProcessor.DataProcessPipeline;

            // (OPTIONAL) Peek data (such as 2 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
            Common.ConsoleHelper.PeekDataViewInConsole <SentimentIssue>(mlContext, trainingDataView, dataProcessPipeline, 2);
            //Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, "Features", trainingDataView, dataProcessPipeline, 2);

            // STEP 3: Set the training algorithm, then create and config the modelBuilder
            var modelBuilder = new Common.ModelBuilder <SentimentIssue, SentimentPrediction>(mlContext, dataProcessPipeline);
            var trainer      = mlContext.BinaryClassification.Trainers.StochasticDualCoordinateAscent(label: "Label", features: "Features");

            //Other way: var trainer = new LinearClassificationTrainer(mlContext, "Features", "Label");
            modelBuilder.AddTrainer(trainer);

            // STEP 4: Train the model fitting to the DataSet
            Console.WriteLine("=============== Training the model ===============");
            modelBuilder.Train(trainingDataView);

            // STEP 5: Evaluate the model and show accuracy stats
            Console.WriteLine("===== Evaluating Model's accuracy with Test data =====");
            var metrics = modelBuilder.EvaluateBinaryClassificationModel(testDataView, "Label", "Score");

            Common.ConsoleHelper.PrintBinaryClassificationMetrics("StochasticDualCoordinateAscent", metrics);

            // STEP 6: Save/persist the trained model to a .ZIP file
            Console.WriteLine("=============== Saving the model to a file ===============");
            modelBuilder.SaveModelAsFile(ModelPath);

            // (OPTIONAL) Try/test a single prediction by loding the model from the file, first.
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };
            var modelScorer = new Common.ModelScorer <SentimentIssue, SentimentPrediction>(mlContext);

            modelScorer.LoadModelFromZipFile(ModelPath);
            var resultprediction = modelScorer.PredictSingle(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Nice")} sentiment | Probability: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");
            //

            Common.ConsoleHelper.ConsoleWriteHeader("=============== End of training process, hit any key to finish ===============");
            Console.ReadKey();
        }
        public void ClassifySentimentText(PredictionEngine <SentimentIssue, SentimentPrediction> predictor, string text)
        {
            var input = new SentimentIssue {
                Text = text
            };
            var prediction = predictor.Predict(input);

            _logger.LogInformation("The text '{0}' is {1} Probability of being toxic: {2}", input.Text, Convert.ToBoolean(prediction.Prediction) ? "Toxic" : "Non Toxic", prediction.Probability);
        }
        public SentimentPrediction Predict(string message)
        {
            var sampleStatement = new SentimentIssue {
                Text = message
            };
            var resultprediction = eng.Predict(sampleStatement);

            return(resultprediction);
        }
        public SentimentPrediction GetSentiment(SentimentIssue sentimentIssue)
        {
            // Create prediction engine related to the loaded trained model
            var predEngine = loadedModel.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(mlContext);

            //Score
            var resultprediction = predEngine.Predict(sentimentIssue);

            return(resultprediction);
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            // ML NET
            var mlContext = new MLContext(seed: 1);

            // Tipos de Dados
            IDataView dataView = mlContext.Data.LoadFromTextFile <SentimentIssue>(DataPath, hasHeader: true);

            TrainTestData trainTestSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
            IDataView     trainingData   = trainTestSplit.TrainSet;

            // PipeLine
            var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentIssue.Text));


            // Algoritmo que será aplicado.
            var trainer          = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features");
            var trainingPipeline = dataProcessPipeline.Append(trainer);

            // Treinar
            ITransformer trainedModel = trainingPipeline.Fit(trainingData);


            Console.WriteLine("The model is saved to {0}", ModelPath);

            // Frases para treinar
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very shit movie"
            };
            SentimentIssue sampleStatement1 = new SentimentIssue {
                Text = "This is a very good movie"
            };
            SentimentIssue sampleStatement2 = new SentimentIssue {
                Text = "awesome movie"
            };


            // Predizer as frases
            var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            // Score
            var resultprediction  = predEngine.Predict(sampleStatement);
            var resultprediction1 = predEngine.Predict(sampleStatement1);
            var resultprediction2 = predEngine.Predict(sampleStatement2);


            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text:  { sampleStatement.Text } | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
            Console.WriteLine($"Text: { sampleStatement1.Text } | Prediction: {(Convert.ToBoolean(resultprediction1.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction1.Probability} ");
            Console.WriteLine($"Text: { sampleStatement2.Text } | Prediction: {(Convert.ToBoolean(resultprediction2.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction2.Probability} ");
            Console.WriteLine($"================End of Process.Hit any key to exit==================================");
        }
Esempio n. 12
0
        public PredictionResult Predict(string sentence)
        {
            var sentimentIssue = new SentimentIssue {
                Text = sentence
            };
            var predEngine       = _mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(_trainedModel);
            var resultprediction = predEngine.Predict(sentimentIssue);

            return(new PredictionResult()
            {
                SentimentPrediction = resultprediction, Sentence = sentence
            });
        }
Esempio n. 13
0
        public void should_verify_prediction(string issue, bool expected)
        {
            // Act
            var sampleStatement = new SentimentIssue
            {
                Text = issue
            };

            //  Arrange
            var result = Sut.Predict(sampleStatement);

            //  Assert
            result.Prediction.Should().Be(expected);
        }
Esempio n. 14
0
        private static void TestSinglePrediction(MLContext mlContext)
        {
            // (OPTIONAL) Try/test a single prediction by loding the model from the file, first.
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };
            var modelScorer = new Common.ModelScorer <SentimentIssue, SentimentPrediction>(mlContext);

            modelScorer.LoadModelFromZipFile(ModelPath);
            var resultprediction = modelScorer.PredictSingle(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Nice")} sentiment | Probability: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");
            //
        }
Esempio n. 15
0
        public void should_verify_prediction(string issue, bool expected)
        {
            // Act
            var sampleStatement = new SentimentIssue
            {
                SentimentText = issue
            };

            //  Arrange
            var result = Sut.Predict(sampleStatement);

            testOutputHelper.WriteLine(result.ToString());

            //  Assert
            result.Prediction.ShouldBe(expected);
        }
Esempio n. 16
0
        // (OPTIONAL) Try/test a single prediction by loding the model from the file, first.
        private static void TestSinglePrediction(MLContext mlContext)
        {
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out var modelInputSchema);

            // Create prediction engine related to the loaded trained model
            var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            //Score
            var resultprediction = predEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");
        }
Esempio n. 17
0
        public void should_verify_prediction_from_serialized_model(string issue, bool expected)
        {
            // Act
            var context         = new MLContext();
            var sampleStatement = new SentimentIssue
            {
                SentimentText = issue
            };

            //  Arrange
            Sut.SaveModel(ModelFilePath);
            var transformer      = context.Model.Load(ModelFilePath, out _);
            var predictionEngine = context.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(transformer);
            var result           = predictionEngine.Predict(sampleStatement);

            testOutputHelper.WriteLine(result.ToString());

            //  Assert
            result.Prediction.ShouldBe(expected);
        }
        public IModelCreationBuilder <TInput, TOutput, TResult> LoadDefaultData()
        {
            var inputs = LoadFromEmbededResource.GetRecords <InputSentimentIssueRow>("Content.wikiDetoxAnnotated40kRows.tsv", delimiter: "\t", hasHeaderRecord: true);

            // convert int to boolean values
            var result = new List <SentimentIssue>();

            foreach (var item in inputs)
            {
                var newItem = new SentimentIssue
                {
                    Label = item.Label == 0 ? false : true,
                    Text  = item.comment
                };

                result.Add(newItem);
            }

            Records.AddRange(result as List <TInput>);
            return(this);
        }
        //---------------------------------------

        public static SentimentPrediction predict(String txt)
        {
            MLContext      mlContext = new MLContext();
            DataViewSchema modelSchema;
            ITransformer   trainedModel = mlContext.Model.Load(ModelPath, out modelSchema);
            //string[] lines = { txt };
            SentimentIssue sampleStatement;

            //foreach (string line in lines)
            //{
            //sampleStatement = new SentimentIssue { Text = line };
            sampleStatement = new SentimentIssue {
                Text = txt
            };
            var predEngine       = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);
            var resultprediction = predEngine.Predict(sampleStatement);

            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
            //}
            return(resultprediction);
        }
Esempio n. 20
0
        // (OPTIONAL) Try/test a single prediction by loading the model from the file, first.
        private static void TestSinglePrediction(MLContext mlContext)
        {
            ConsoleHelper.ConsoleWriteHeader("=============== Testing prediction engine ===============");
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out var modelInputSchema);

            Console.WriteLine($"=============== Loaded Model OK  ===============");

            // Create prediction engine related to the loaded trained model
            var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            Console.WriteLine($"=============== Created Prediction Engine OK  ===============");
            // Score
            var predictedResult = predEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(predictedResult.Prediction) ? "Toxic" : "Non Toxic")} sentiment");
            Console.WriteLine($"==================================================");
        }
        private static Task ClassifyAsync(
            IModelDefinitionBuilder <SentimentIssue, BinaryClassificationMetricsResult> modelBuilder,
            string text,
            bool expectedResult,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            return(Task.Run(
                       () =>
            {
                var predictor = modelBuilder.MLContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(modelBuilder.Model);

                var input = new SentimentIssue {
                    Text = text
                };

                var prediction = predictor.Predict(input);

                var result = prediction.Prediction ? "Toxic" : "Non Toxic";

                if (prediction.Prediction == expectedResult)
                {
                    logger.LogInformation(
                        "[ClassifyAsync][Predict] result: '{0}' is {1} Probability of being toxic: {2}",
                        input.Text,
                        result,
                        prediction.Probability);
                }
                else
                {
                    logger.LogWarning(
                        "[ClassifyAsync][Predict] result: '{0}' is {1} Probability of being toxic: {2}",
                        input.Text,
                        result,
                        prediction.Probability);
                }
            },
                       cancellationToken));
        }
Esempio n. 22
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            [Blob("models/SentimentModel.zip", FileAccess.Read)] Stream serializedModel,
            Binder binder,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);
            string  text        = data?.Text;

            if (text == null)
            {
                return(new BadRequestObjectResult("Please pass sentiment text [Text] in the request body"));
            }

            //Create MLContext to be shared across the model creation workflow objects
            //Set a random seed for repeatable/deterministic results across multiple trainings.
            var mlContext = new MLContext(seed: 1);

            SentimentIssue sampleStatement = new SentimentIssue {
                Text = text
            };

            ITransformer trainedModel = mlContext.Model.Load(serializedModel);

            // Create prediction engine related to the loaded trained model
            var predFunction = trainedModel.MakePredictionFunction <SentimentIssue, SentimentPrediction>(mlContext);

            //Score
            var resultprediction = predFunction.Predict(sampleStatement);

            log.LogInformation($"Text: {sampleStatement.Text} | Prediction: {resultprediction.Prediction} | Probability: {resultprediction.Probability} | Score: {resultprediction.Score}  ");

            return(text != null
                ? (ActionResult) new OkObjectResult(resultprediction.Prediction)
                : new BadRequestObjectResult("Please pass a Text in the request body"));
        }
        public void Track(SentimentPrediction output, SentimentIssue input, ILogger logger)
        {
            try
            {
                string sentimentText = input.SentimentText;

                var props = new Dictionary <string, string>
                {
                    { "model_uri", _modelUri?.ToString() },
                    { "prediction_response", output.Prediction ? "Positive" : "Negative" },
                    { "prediction_text", sentimentText },
                };

                _telemetryClient.TrackMetric("Prediction.Probability", output.Probability, props);

                _telemetryClient.TrackMetric("Prediction.Score", output.Score, props);
            }
            catch (Exception ex)
            {
                // avoid fail prediction due to telemetry record saving issues
                logger?.LogError(ex, nameof(Track));
            }
        }
Esempio n. 24
0
        // (OPTIONAL) Try/test a single prediction by loding the model from the file, first.
        private static void TestSinglePrediction(MLContext mlContext)
        {
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel;

            using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                trainedModel = mlContext.Model.Load(stream);
            }

            // Create prediction engine related to the loaded trained model
            var predEngine = trainedModel.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(mlContext);

            //Score
            var resultprediction = predEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");
        }
        private static void TestPredict()
        {
            // Create MLContext
            MLContext mlContext = new MLContext();

            // Define data preparation and trained model schemas
            DataViewSchema modelSchema;

            // Load data preparation pipeline and trained model
            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out modelSchema);

            Console.WriteLine($"=============== Prediction(s)  ===============");

            string[] lines = System.IO.File.ReadAllLines($"{BaseDatasetsRelativePath}/test_predict.txt");

            // TRY IT: Make a single test prediction, loading the model from .ZIP file
            SentimentIssue sampleStatement;

            string[] reviewSentences = { "This is a must place to visit if you are Gundam Lover.", "The rates suck but when you finally get an ssr its the happiest moment ever.", "Also, there is no PvP, making it totally irrelevant whether you have top tier Servants or not.", "Because I like the game, it's that simple.", "I have to go slowly and be patient, and if there's an enemy I can't beat the numbers are so strange and large that I don't normally understand why I'm losing, and I rarely have the time to try again or do something different.",
                                         "This is shitty game." };

            foreach (string line in lines)
            {
                //assignment to variable
                sampleStatement = new SentimentIssue {
                    Text = line
                };

                // Create prediction engine related to the loaded trained model
                var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

                // Score
                var resultprediction = predEngine.Predict(sampleStatement);

                Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
            }
        }
        static void Main(string[] args)
        {
            #region try
            // Create MLContext to be shared across the model creation workflow objects
            // Set a random seed for repeatable/deterministic results across multiple trainings.
            var mlContext = new MLContext(seed: 1);

            //Note: 程式控制 需要訓練模型 / 載入並使用在本機的模型
            // train : 訓練模型
            // using : 使用模型
            string Status = "using";

            if (Status == "train")
            {
                #region step1to3
                // STEP 1: 載入要訓練的資料, 有表頭
                IDataView dataView = mlContext.Data.LoadFromTextFile <SentimentIssue>(DataPath, hasHeader: true);
                // 切割資料 分離出 訓練資料 / 測試資料
                TrainTestData trainTestSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
                // 訓練資料
                IDataView trainingData = trainTestSplit.TrainSet;
                // 測試資料
                IDataView testData = trainTestSplit.TestSet;

                // STEP 2: 設置通用數據流程 配置器
                var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentIssue.Text));

                // STEP 3: 設置訓練算法(BinaryClassification),然後創建並配置 modelBuilder
                var trainer          = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features");
                var trainingPipeline = dataProcessPipeline.Append(trainer);
                #endregion

                #region step4
                // STEP 4: 訓練模型並擬合到DataSet
                ITransformer trainedModel = trainingPipeline.Fit(trainingData);
                #endregion

                #region step5
                // STEP 5: 評估模型並顯示準確性統計數據
                var predictions = trainedModel.Transform(testData);
                var metrics     = mlContext.BinaryClassification.Evaluate(data: predictions, labelColumnName: "Label", scoreColumnName: "Score");
                #endregion
                // 在主控台列印相關數據
                ConsoleHelper.PrintBinaryClassificationMetrics(trainer.ToString(), metrics);

                // STEP 6: 將訓練的模型保存/保存到.ZIP文件(MLModels資料夾下)
                mlContext.Model.Save(trainedModel, trainingData.Schema, ModelPath);

                //顯示訓練完成 model 的保存路徑
                Console.WriteLine("The model is saved to {0}", ModelPath);
            }
            else if (Status == "using")
            {
                // !!嘗試:從.ZIP文件加載模型進行 單個測試預測=================================
                // 創建一句話,先放入model
                SentimentIssue sampleStatement = new SentimentIssue {
                    Text = "This is a very rude movie"
                };
                //SentimentIssue sampleStatement = new SentimentIssue { Text = "Machine learning is not fun" };
                //SentimentIssue sampleStatement = new SentimentIssue { Text = "Ancient sources for this, please." };
                //SentimentIssue sampleStatement = new SentimentIssue { Text = "This is a very f**k movie" };

                #region consume
                // 載入儲存在本機的模型
                DataViewSchema outSchema;
                ITransformer   loadModel = mlContext.Model.Load(ModelPath, out outSchema);

                // 創建與加載的訓練模型相關的預測引擎 loadModel / trainedModel
                var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(loadModel);

                // 將需要分析的資料丟入預測引擎
                var resultprediction = predEngine.Predict(sampleStatement);
                #endregion

                Console.WriteLine($"=============== Single Prediction  ===============");
                Console.WriteLine($"Text: {sampleStatement.Text} ");
                Console.WriteLine($"Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
                Console.WriteLine($"Score: {resultprediction.Score} ");
                Console.WriteLine($"================End of Process.Hit any key to exit==================================");
                Console.ReadLine();
            }
            #endregion
        }
Esempio n. 27
0
        static void Main(string[] args)
        {
            try
            {
                // Create MLContext
                MLContext mlContext = new MLContext();

                // Load Trained Model
                DataViewSchema predictionPipelineSchema;
                ITransformer   predictionPipeline = mlContext.Model.Load("../../../MLModels/SentimentModel.zip", out predictionPipelineSchema);

                // Create PredictionEngines
                PredictionEngine <SentimentIssue, SentimentPrediction> predictionEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(predictionPipeline);

                // Input Data (single)
                SentimentIssue inputData = new SentimentIssue
                {
                    Text = "I love this movie!"
                };

                // Get Prediction
                SentimentPrediction prediction = predictionEngine.Predict(inputData);

                Console.WriteLine($"=============== Single Prediction  ===============");
                Console.WriteLine($"Text: {inputData.Text} | Prediction: {(Convert.ToBoolean(prediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {prediction.Probability} ");
                Console.WriteLine($"================End of Process.Hit any key to exit==================================");

                // input data multiple
                SentimentIssue[] inputArray = new SentimentIssue[]
                {
                    new SentimentIssue
                    {
                        Text = "I love this movie!"
                    },
                    new SentimentIssue
                    {
                        Text = "F*****g good!"
                    },
                };

                //Load Data
                IDataView data = mlContext.Data.LoadFromEnumerable <SentimentIssue>(inputArray);

                // Predicted Data
                IDataView predictions = predictionPipeline.Transform(data);

                // Create an IEnumerable of prediction objects from IDataView
                IEnumerable <SentimentPrediction> dataEnumerable =
                    mlContext.Data.CreateEnumerable <SentimentPrediction>(predictions, reuseRowObject: true);

                // Iterate over each row
                for (int i = 0; i < inputArray.Length; i++)
                {
                    string input             = inputArray[i].Text;
                    SentimentPrediction item = dataEnumerable.ElementAt(i);
                    // display
                    Console.WriteLine($"Text: {input} | Prediction: {(Convert.ToBoolean(item.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {item.Probability} ");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
Esempio n. 28
0
        static void Main(string[] args)
        {
            // Create MLContext to be shared across the model creation work flow objects
            // Set a random seed for repeatable/deterministic results across multiple trainings.
            try
            {
                if (mlModel == MLModel.MLSentiment)
                {
                    var mlContext = new MLContext();

                    DataRelativePath  = $"{BaseDatasetsRelativePath}/wikiDetoxAnnotated40kRows.tsv";
                    DataPath          = GetAbsolutePath(DataRelativePath);
                    ModelRelativePath = $"{BaseModelsRelativePath}/SentimentModel.zip";
                    ModelPath         = GetAbsolutePath(ModelRelativePath);

                    // STEP 1: Common data loading configuration
                    IDataView dataView = mlContext.Data.LoadFromTextFile <SentimentIssue>(DataPath, hasHeader: true);

                    DataOperationsCatalog.TrainTestData trainTestSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
                    IDataView trainingData = trainTestSplit.TrainSet;
                    IDataView testData     = trainTestSplit.TestSet;

                    // STEP 2: Common data process configuration with pipeline data transformations
                    var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentIssue.Text));

                    // STEP 3: Set the training algorithm, then create and config the modelBuilder
                    var trainer          = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features");
                    var trainingPipeline = dataProcessPipeline.Append(trainer);

                    // STEP 4: Train the model fitting to the DataSet
                    ITransformer trainedModel = trainingPipeline.Fit(trainingData);

                    // STEP 5: Evaluate the model and show accuracy stats
                    var predictions = trainedModel.Transform(testData);
                    var metrics     = mlContext.BinaryClassification.Evaluate(data: predictions, labelColumnName: "Label", scoreColumnName: "Score");

                    ConsoleHelper.PrintBinaryClassificationMetrics(trainer.ToString(), metrics);

                    // STEP 6: Save/persist the trained model to a .ZIP file
                    mlContext.Model.Save(trainedModel, trainingData.Schema, ModelPath);

                    Console.WriteLine("The model is saved to {0}", ModelPath);

                    // TRY IT: Make a single test prediction, loading the model from .ZIP file
                    SentimentIssue sampleStatement = new SentimentIssue {
                        Text = "I love this movie!"
                    };

                    // Create prediction engine related to the loaded trained model
                    var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

                    // Score
                    var resultprediction = predEngine.Predict(sampleStatement);

                    Console.WriteLine($"=============== Single Prediction  ===============");
                    Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ");
                    Console.WriteLine($"================End of Process.Hit any key to exit==================================");
                }
                else if (mlModel == MLModel.MLDiamond)
                {
                    DiamondML();
                }
            } catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
Esempio n. 29
0
        static void Main(string[] args)
        {
            #region try
            // Create MLContext to be shared across the model creation workflow objects
            // Set a random seed for repeatable/deterministic results across multiple trainings.
            var mlContext = new MLContext(seed: 1);

            #region step1to3
            // STEP 1: Common data loading configuration
            IDataView dataView = mlContext.Data.LoadFromTextFile <SentimentIssue>(DataPath, hasHeader: true);

            TrainTestData trainTestSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
            IDataView     trainingData   = trainTestSplit.TrainSet;
            IDataView     testData       = trainTestSplit.TestSet;

            // STEP 2: Common data process configuration with pipeline data transformations
            var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentIssue.Text));

            // STEP 3: Set the training algorithm, then create and config the modelBuilder
            var trainer          = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features");
            var trainingPipeline = dataProcessPipeline.Append(trainer);
            #endregion

            #region step4
            // STEP 4: Train the model fitting to the DataSet
            ITransformer trainedModel = trainingPipeline.Fit(trainingData);
            #endregion

            #region step5
            // STEP 5: Evaluate the model and show accuracy stats
            var predictions = trainedModel.Transform(testData);
            var metrics     = mlContext.BinaryClassification.Evaluate(data: predictions, labelColumnName: "Label", scoreColumnName: "Score");
            #endregion

            ConsoleHelper.PrintBinaryClassificationMetrics(trainer.ToString(), metrics);

            // STEP 6: Save/persist the trained model to a .ZIP file
            mlContext.Model.Save(trainedModel, trainingData.Schema, ModelPath);

            Console.WriteLine("The model is saved to {0}", ModelPath);

            // TRY IT: Make a single test prediction loding the model from .ZIP file
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "Centerpartiet lockar fler väljare och förväntas bli större än Kalle Anka partiet till hösten"
            };
            SentimentIssue sampleStatement2 = new SentimentIssue {
                Text = "Och Kristdemokraterna agerar nästan ännu mer obegripligt"
            };

            #region consume
            // Create prediction engine related to the loaded trained model
            var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            // Score
            var resultprediction  = predEngine.Predict(sampleStatement);
            var resultprediction2 = predEngine.Predict(sampleStatement2);
            #endregion

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Förutsägelse: {(Convert.ToBoolean(resultprediction.Prediction) ? "Negativt" : "Positivt")} känsla | Sannolikheten att denna mening är negativt laddad: {resultprediction.Probability} ");
            Console.WriteLine($"Text: {sampleStatement2.Text} | Förutsägelse: {(Convert.ToBoolean(resultprediction2.Prediction) ? "Negativt" : "Positivt")} känsla | Sannolikheten att denna mening är negativt laddad: {resultprediction2.Probability} ");
            #endregion
        }
Esempio n. 30
0
 public ActionResult <SentimentPrediction> GetNewSentiment([FromQuery] SentimentIssue input)
 {
     return(_predictionEnginePool.Predict("newModel", input));
 }