From 8cbe05a4152b979febfbf217c778f72ae43deea7 Mon Sep 17 00:00:00 2001 From: jralexander Date: Thu, 28 Feb 2019 09:58:01 -0800 Subject: [PATCH 1/5] Update dataset usage --- .../tutorials/SentimentAnalysis/Program.cs | 210 +++++++++--------- .../SentimentAnalysis.csproj | 16 +- .../SentimentAnalysis/SentimentData.cs | 16 +- 3 files changed, 118 insertions(+), 124 deletions(-) diff --git a/machine-learning/tutorials/SentimentAnalysis/Program.cs b/machine-learning/tutorials/SentimentAnalysis/Program.cs index 7cccf89a0b4..a6b422eda68 100644 --- a/machine-learning/tutorials/SentimentAnalysis/Program.cs +++ b/machine-learning/tutorials/SentimentAnalysis/Program.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; using System.IO; @@ -7,120 +7,118 @@ using Microsoft.ML; using Microsoft.ML.Core.Data; using Microsoft.ML.Data; +using Microsoft.ML.Trainers.Online; using Microsoft.ML.Transforms.Text; -// +// namespace SentimentAnalysis { class Program { - // - static readonly string _trainDataPath = Path.Combine(Environment.CurrentDirectory, "Data", "wikipedia-detox-250-line-data.tsv"); - static readonly string _testDataPath = Path.Combine(Environment.CurrentDirectory, "Data", "wikipedia-detox-250-line-test.tsv"); + // + static readonly string _dataPath = Path.Combine(Environment.CurrentDirectory, "Data", "yelp_labelled.txt"); static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "Data", "Model.zip"); - static TextLoader _textLoader; - // + // static void Main(string[] args) { // Create ML.NET context/local environment - allows you to add steps in order to keep everything together // during the learning process. //Create ML Context with seed for repeatable/deterministic results - // + // MLContext mlContext = new MLContext(seed: 0); - // + // // The TextLoader loads a dataset with comments and corresponding postive or negative sentiment. // When you create a loader, you specify the schema by passing a class to the loader containing // all the column names and their types. This is used to create the model, and train it. // Initialize our TextLoader - // - _textLoader = mlContext.Data.CreateTextLoader( - columns: new TextLoader.Column[] + // + TextLoader textLoader = mlContext.Data.CreateTextLoader( + columns: new TextLoader.Column[] { - new TextLoader.Column("Label", DataKind.Bool,0), - new TextLoader.Column("SentimentText", DataKind.Text,1) + new TextLoader.Column("SentimentText", DataKind.Text,0), + new TextLoader.Column("Label", DataKind.Bool,1) }, separatorChar: '\t', - hasHeader: true + hasHeader: false ); - // + // + //Note that this case, loading your training data from a file, + //is the easiest way to get started, but ML.NET also allows you + //to load data from databases or in-memory collections. + // + IDataView dataView = textLoader.Read(_dataPath); + // + + // + (IDataView trainSet, IDataView testSet) splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2); + // - // - var model = Train(mlContext, _trainDataPath); - // + // + ITransformer model = BuildAndTrainModel(mlContext, splitDataView.trainSet); + // - // - Evaluate(mlContext, model); - // - - // - Predict(mlContext, model); - // + // + Evaluate(mlContext, model, splitDataView.testSet); + // - // - PredictWithModelLoadedFromFile(mlContext); - // + // + PredictSentiment(mlContext, model); + // + + // + PredictSentimentWithModelLoadedFromFile(mlContext); + // Console.WriteLine(); Console.WriteLine("=============== End of process ==============="); } - public static ITransformer Train(MLContext mlContext, string dataPath) + public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet) { - //Note that this case, loading your training data from a file, - //is the easiest way to get started, but ML.NET also allows you - //to load data from databases or in-memory collections. - // - IDataView dataView =_textLoader.Read(dataPath); - // - // Create a flexible pipeline (composed by a chain of estimators) for creating/training the model. // This is used to format and clean the data. // Convert the text column to numeric vectors (Features column) - // - var pipeline = mlContext.Transforms.Text.FeaturizeText(inputColumnName: "SentimentText", outputColumnName: "Features") - // + // + EstimatorChain>> pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultColumnNames.Features, inputColumnName: nameof(SentimentData.SentimentText)) + // - // Adds a FastTreeBinaryClassificationTrainer, the decision tree learner for this project - // - .Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 20)); - // + // Adds a FastTreeBinaryClassificationTrainer, the decision tree learner for this project + // + .Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 20)); + // // Create and train the model based on the dataset that has been loaded, transformed. - // + // Console.WriteLine("=============== Create and Train the Model ==============="); - var model = pipeline.Fit(dataView); + TransformerChain>> model = pipeline.Fit(splitTrainSet); Console.WriteLine("=============== End of training ==============="); Console.WriteLine(); - // + // // Returns the model we trained to use for evaluation. - // + // return model; - // + // } - public static void Evaluate(MLContext mlContext, ITransformer model) + public static void Evaluate(MLContext mlContext, ITransformer model, IDataView splitTestSet) { // Evaluate the model and show accuracy stats - // Load evaluation/test data - // - var dataView = _textLoader.Read(_testDataPath); - // //Take the data in, make transformations, output the data. - // + // Console.WriteLine("=============== Evaluating Model accuracy with Test data==============="); - var predictions = model.Transform(dataView); - // + IDataView predictions = model.Transform(splitTestSet); + // // BinaryClassificationContext.Evaluate returns a BinaryClassificationEvaluator.CalibratedResult // that contains the computed overall metrics. - // - var metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label"); - // + // + CalibratedBinaryClassificationMetrics metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label"); + // // The Accuracy metric gets the accuracy of a classifier, which is the proportion // of correct predictions in the test set. @@ -134,7 +132,7 @@ public static void Evaluate(MLContext mlContext, ITransformer model) // The F1 score is the harmonic mean of precision and recall: // 2 * precision * recall / (precision + recall). - // + // Console.WriteLine(); Console.WriteLine("Model quality metrics evaluation"); Console.WriteLine("--------------------------------"); @@ -142,109 +140,111 @@ public static void Evaluate(MLContext mlContext, ITransformer model) Console.WriteLine($"Auc: {metrics.Auc:P2}"); Console.WriteLine($"F1Score: {metrics.F1Score:P2}"); Console.WriteLine("=============== End of model evaluation ==============="); - // + // // Save the new model to .ZIP file - // + // SaveModelAsFile(mlContext, model); - // + // } - private static void Predict(MLContext mlContext, ITransformer model) + private static void PredictSentiment(MLContext mlContext, ITransformer model) { - // - var predictionFunction = model.CreatePredictionEngine(mlContext); - // + // + PredictionEngine predictionFunction = model.CreatePredictionEngine(mlContext); + // - // + // SentimentData sampleStatement = new SentimentData { - SentimentText = "This is a very rude movie" + SentimentText = "This was a very bad steak" }; - // + // - // - var resultprediction = predictionFunction.Predict(sampleStatement); - // - // + // + SentimentPrediction resultprediction = predictionFunction.Predict(sampleStatement); + // + // Console.WriteLine(); Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ==============="); Console.WriteLine(); - Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Not Toxic")} | Probability: {resultprediction.Probability} "); + Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultprediction.Probability} "); + Console.WriteLine("=============== End of Predictions ==============="); Console.WriteLine(); - // + // } - public static void PredictWithModelLoadedFromFile(MLContext mlContext) + public static void PredictSentimentWithModelLoadedFromFile(MLContext mlContext) { // Adds some comments to test the trained model's predictions. - // + // IEnumerable sentiments = new[] { new SentimentData { - SentimentText = "This is a very rude movie" + SentimentText = "This was a horrible meal" }, new SentimentData { - SentimentText = "I love this article." + SentimentText = "I love this spaghetti." } }; - // + // - // + // ITransformer loadedModel; using (var stream = new FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = mlContext.Model.Load(stream); } - // + // - // // Create prediction engine - var sentimentStreamingDataView = mlContext.Data.ReadFromEnumerable(sentiments); - var predictions = loadedModel.Transform(sentimentStreamingDataView); - - // Use the model to predict whether comment data is toxic (1) or nice (0). - var predictedResults = mlContext.CreateEnumerable(predictions, reuseRowObject: false); - // - - // + // + IDataView sentimentStreamingDataView = mlContext.Data.ReadFromEnumerable(sentiments); + IDataView predictions = loadedModel.Transform(sentimentStreamingDataView); + + // Use the model to predict whether comment data is Positive (1) or Negative (0). + IEnumerable predictedResults = mlContext.CreateEnumerable(predictions, reuseRowObject: false); + // + + // Console.WriteLine(); Console.WriteLine("=============== Prediction Test of loaded model with a multiple samples ==============="); - // + // Console.WriteLine(); // Builds pairs of (sentiment, prediction) - // - var sentimentsAndPredictions = sentiments.Zip(predictedResults, (sentiment, prediction) => (sentiment, prediction)); - // + // + IEnumerable<(SentimentData sentiment, SentimentPrediction prediction)> sentimentsAndPredictions = sentiments.Zip(predictedResults, (sentiment, prediction) => (sentiment, prediction)); + // - // - foreach (var item in sentimentsAndPredictions) + // + foreach ((SentimentData sentiment, SentimentPrediction prediction) item in sentimentsAndPredictions) { - Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(Convert.ToBoolean(item.prediction.Prediction) ? "Toxic" : "Not Toxic")} | Probability: {item.prediction.Probability} "); + Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(Convert.ToBoolean(item.prediction.Prediction) ? "Positive" : "Negative")} | Probability: {item.prediction.Probability} "); + } Console.WriteLine("=============== End of predictions ==============="); - // + // } // Saves the model we trained to a zip file. private static void SaveModelAsFile(MLContext mlContext, ITransformer model) { - // + // using (var fs = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write)) - mlContext.Model.Save(model,fs); - // + mlContext.Model.Save(model, fs); + // Console.WriteLine("The model is saved to {0}", _modelPath); } - + } } diff --git a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj index 24644687c3d..329f60a0c04 100644 --- a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj +++ b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj @@ -12,21 +12,15 @@ - - - - - - PreserveNewest - - - PreserveNewest - - + PreserveNewest + + + + diff --git a/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs b/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs index e764b891327..1ec7eb82592 100644 --- a/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs +++ b/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs @@ -1,17 +1,17 @@ -// +// using Microsoft.ML.Data; -// +// namespace SentimentAnalysis { - // + // public class SentimentData { - [Column(ordinal: "0", name: "Label")] - public float Sentiment; - - [Column(ordinal: "1")] + [Column(ordinal: "0")] public string SentimentText; + + [Column(ordinal: "1", name: "Label")] + public float Sentiment; } public class SentimentPrediction @@ -25,5 +25,5 @@ public class SentimentPrediction [ColumnName("Score")] public float Score { get; set; } } - // + // } From 22e5276aeefbf6872edc11e0f5919fae19058746 Mon Sep 17 00:00:00 2001 From: jralexander Date: Fri, 1 Mar 2019 12:34:48 -0800 Subject: [PATCH 2/5] Updating methods --- .../tutorials/SentimentAnalysis/Program.cs | 73 +++++++++++-------- .../SentimentAnalysis.csproj | 4 - .../SentimentAnalysis/SentimentData.cs | 10 +-- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/machine-learning/tutorials/SentimentAnalysis/Program.cs b/machine-learning/tutorials/SentimentAnalysis/Program.cs index a6b422eda68..84549a10752 100644 --- a/machine-learning/tutorials/SentimentAnalysis/Program.cs +++ b/machine-learning/tutorials/SentimentAnalysis/Program.cs @@ -29,31 +29,7 @@ static void Main(string[] args) MLContext mlContext = new MLContext(seed: 0); // - // The TextLoader loads a dataset with comments and corresponding postive or negative sentiment. - // When you create a loader, you specify the schema by passing a class to the loader containing - // all the column names and their types. This is used to create the model, and train it. - // Initialize our TextLoader - // - TextLoader textLoader = mlContext.Data.CreateTextLoader( - columns: new TextLoader.Column[] - { - new TextLoader.Column("SentimentText", DataKind.Text,0), - new TextLoader.Column("Label", DataKind.Bool,1) - }, - separatorChar: '\t', - hasHeader: false - ); - // - //Note that this case, loading your training data from a file, - //is the easiest way to get started, but ML.NET also allows you - //to load data from databases or in-memory collections. - // - IDataView dataView = textLoader.Read(_dataPath); - // - - // - (IDataView trainSet, IDataView testSet) splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2); - // + (IDataView trainSet, IDataView testSet) splitDataView = LoadAndTransformData(mlContext); // ITransformer model = BuildAndTrainModel(mlContext, splitDataView.trainSet); @@ -64,25 +40,57 @@ static void Main(string[] args) // // - PredictSentiment(mlContext, model); + UseModelWithSingleItem(mlContext, model); // // - PredictSentimentWithModelLoadedFromFile(mlContext); + UseLoadedModelWithBatchItems(mlContext); // - Console.WriteLine(); Console.WriteLine("=============== End of process ==============="); } + private static (IDataView trainSet, IDataView testSet) LoadAndTransformData(MLContext mlContext) + { + // The TextLoader loads a dataset with comments and corresponding postive or negative sentiment. + // When you create a loader, you specify the schema by passing a class to the loader containing + // all the column names and their types. This is used to create the model, and train it. + // Initialize our TextLoader + // TODO: Change to ReadFromTextFile + // + //return mlContext.Data.CreateTextLoader( + // columns: new TextLoader.Column[] + // { + // new TextLoader.Column("SentimentText", DataKind.Text,0), + // new TextLoader.Column("Label", DataKind.Bool,1) + // }, + // separatorChar: '\t', + // hasHeader: false + //); + // + + //Note that this case, loading your training data from a file, + //is the easiest way to get started, but ML.NET also allows you + //to load data from databases or in-memory collections. + // + IDataView dataView = mlContext.Data.ReadFromTextFile(_dataPath,hasHeader:false); + // + + // + (IDataView trainSet, IDataView testSet) splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2); + // + + return splitDataView; + } + public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet) { // Create a flexible pipeline (composed by a chain of estimators) for creating/training the model. // This is used to format and clean the data. // Convert the text column to numeric vectors (Features column) // - EstimatorChain>> pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultColumnNames.Features, inputColumnName: nameof(SentimentData.SentimentText)) + var pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultColumnNames.Features, inputColumnName: nameof(SentimentData.SentimentText)) // // Adds a FastTreeBinaryClassificationTrainer, the decision tree learner for this project @@ -148,7 +156,7 @@ public static void Evaluate(MLContext mlContext, ITransformer model, IDataView s // } - private static void PredictSentiment(MLContext mlContext, ITransformer model) + private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model) { // PredictionEngine predictionFunction = model.CreatePredictionEngine(mlContext); @@ -162,7 +170,7 @@ private static void PredictSentiment(MLContext mlContext, ITransformer model) // // - SentimentPrediction resultprediction = predictionFunction.Predict(sampleStatement); + var resultprediction = predictionFunction.Predict(sampleStatement); // // Console.WriteLine(); @@ -176,7 +184,7 @@ private static void PredictSentiment(MLContext mlContext, ITransformer model) // } - public static void PredictSentimentWithModelLoadedFromFile(MLContext mlContext) + public static void UseLoadedModelWithBatchItems(MLContext mlContext) { // Adds some comments to test the trained model's predictions. // @@ -204,6 +212,7 @@ public static void PredictSentimentWithModelLoadedFromFile(MLContext mlContext) // Create prediction engine // IDataView sentimentStreamingDataView = mlContext.Data.ReadFromEnumerable(sentiments); + //TODO: check exp in tutorial IDataView predictions = loadedModel.Transform(sentimentStreamingDataView); // Use the model to predict whether comment data is Positive (1) or Negative (0). diff --git a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj index 329f60a0c04..86366b9381d 100644 --- a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj +++ b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj @@ -18,9 +18,5 @@ PreserveNewest - - - - diff --git a/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs b/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs index 1ec7eb82592..9131f2262da 100644 --- a/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs +++ b/machine-learning/tutorials/SentimentAnalysis/SentimentData.cs @@ -7,11 +7,11 @@ namespace SentimentAnalysis // public class SentimentData { - [Column(ordinal: "0")] + [LoadColumn(0)] public string SentimentText; - [Column(ordinal: "1", name: "Label")] - public float Sentiment; + [LoadColumn(1), ColumnName("Label")] + public bool Sentiment; } public class SentimentPrediction @@ -19,10 +19,10 @@ public class SentimentPrediction [ColumnName("PredictedLabel")] public bool Prediction { get; set; } - [ColumnName("Probability")] + // [ColumnName("Probability")] public float Probability { get; set; } - [ColumnName("Score")] + // [ColumnName("Score")] public float Score { get; set; } } // From 3a41d214b08ba0b4793310cf45722917758a5c68 Mon Sep 17 00:00:00 2001 From: jralexander Date: Fri, 1 Mar 2019 15:21:44 -0800 Subject: [PATCH 3/5] Revised based on feedback --- .../tutorials/SentimentAnalysis/Program.cs | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/machine-learning/tutorials/SentimentAnalysis/Program.cs b/machine-learning/tutorials/SentimentAnalysis/Program.cs index 84549a10752..d4b371d06c1 100644 --- a/machine-learning/tutorials/SentimentAnalysis/Program.cs +++ b/machine-learning/tutorials/SentimentAnalysis/Program.cs @@ -51,24 +51,8 @@ static void Main(string[] args) Console.WriteLine("=============== End of process ==============="); } - private static (IDataView trainSet, IDataView testSet) LoadAndTransformData(MLContext mlContext) + private static (IDataView trainSet, IDataView testSet) LoadData(MLContext mlContext) { - // The TextLoader loads a dataset with comments and corresponding postive or negative sentiment. - // When you create a loader, you specify the schema by passing a class to the loader containing - // all the column names and their types. This is used to create the model, and train it. - // Initialize our TextLoader - // TODO: Change to ReadFromTextFile - // - //return mlContext.Data.CreateTextLoader( - // columns: new TextLoader.Column[] - // { - // new TextLoader.Column("SentimentText", DataKind.Text,0), - // new TextLoader.Column("Label", DataKind.Bool,1) - // }, - // separatorChar: '\t', - // hasHeader: false - //); - // //Note that this case, loading your training data from a file, //is the easiest way to get started, but ML.NET also allows you @@ -86,13 +70,13 @@ private static (IDataView trainSet, IDataView testSet) LoadAndTransformData(MLCo public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet) { + // Create a flexible pipeline (composed by a chain of estimators) for creating/training the model. // This is used to format and clean the data. // Convert the text column to numeric vectors (Features column) // var pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultColumnNames.Features, inputColumnName: nameof(SentimentData.SentimentText)) // - // Adds a FastTreeBinaryClassificationTrainer, the decision tree learner for this project // .Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 20)); @@ -101,7 +85,7 @@ public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView spl // Create and train the model based on the dataset that has been loaded, transformed. // Console.WriteLine("=============== Create and Train the Model ==============="); - TransformerChain>> model = pipeline.Fit(splitTrainSet); + var model = pipeline.Fit(splitTrainSet); Console.WriteLine("=============== End of training ==============="); Console.WriteLine(); // @@ -209,15 +193,15 @@ public static void UseLoadedModelWithBatchItems(MLContext mlContext) } // - // Create prediction engine - // + // Load test data + // IDataView sentimentStreamingDataView = mlContext.Data.ReadFromEnumerable(sentiments); - //TODO: check exp in tutorial + IDataView predictions = loadedModel.Transform(sentimentStreamingDataView); - // Use the model to predict whether comment data is Positive (1) or Negative (0). + // Use model to predict whether comment data is Positive (1) or Negative (0). IEnumerable predictedResults = mlContext.CreateEnumerable(predictions, reuseRowObject: false); - // + // // Console.WriteLine(); From 4a0cabe82e63d28ed1729f59296fd47dfa600e0f Mon Sep 17 00:00:00 2001 From: jralexander Date: Wed, 6 Mar 2019 14:39:35 -0800 Subject: [PATCH 4/5] revised based on feedbeck --- .../tutorials/SentimentAnalysis/Program.cs | 23 +++++++++++-------- .../SentimentAnalysis.csproj | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/machine-learning/tutorials/SentimentAnalysis/Program.cs b/machine-learning/tutorials/SentimentAnalysis/Program.cs index d4b371d06c1..9f15892b088 100644 --- a/machine-learning/tutorials/SentimentAnalysis/Program.cs +++ b/machine-learning/tutorials/SentimentAnalysis/Program.cs @@ -26,10 +26,13 @@ static void Main(string[] args) // during the learning process. //Create ML Context with seed for repeatable/deterministic results // - MLContext mlContext = new MLContext(seed: 0); + MLContext mlContext = new MLContext(); // - (IDataView trainSet, IDataView testSet) splitDataView = LoadAndTransformData(mlContext); + // + (IDataView trainSet, IDataView testSet) splitDataView = LoadData(mlContext); + // + // ITransformer model = BuildAndTrainModel(mlContext, splitDataView.trainSet); @@ -39,33 +42,35 @@ static void Main(string[] args) Evaluate(mlContext, model, splitDataView.testSet); // - // + // UseModelWithSingleItem(mlContext, model); - // + // - // + // UseLoadedModelWithBatchItems(mlContext); - // + // Console.WriteLine(); Console.WriteLine("=============== End of process ==============="); } - private static (IDataView trainSet, IDataView testSet) LoadData(MLContext mlContext) + public static (IDataView trainSet, IDataView testSet) LoadData(MLContext mlContext) { //Note that this case, loading your training data from a file, //is the easiest way to get started, but ML.NET also allows you //to load data from databases or in-memory collections. - // + // IDataView dataView = mlContext.Data.ReadFromTextFile(_dataPath,hasHeader:false); - // + // // (IDataView trainSet, IDataView testSet) splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2); // + // return splitDataView; + // } public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet) diff --git a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj index 86366b9381d..9dd42a56de0 100644 --- a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj +++ b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj @@ -12,8 +12,8 @@ - - + + PreserveNewest From 886a6c292c247df64fb7fe26bb8664e5cb236c94 Mon Sep 17 00:00:00 2001 From: jralexander Date: Thu, 7 Mar 2019 14:07:23 -0800 Subject: [PATCH 5/5] Updated for v 0.11.0 --- .../tutorials/SentimentAnalysis/Program.cs | 19 +++++++++---------- .../SentimentAnalysis.csproj | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/machine-learning/tutorials/SentimentAnalysis/Program.cs b/machine-learning/tutorials/SentimentAnalysis/Program.cs index 9f15892b088..eb81c525ca0 100644 --- a/machine-learning/tutorials/SentimentAnalysis/Program.cs +++ b/machine-learning/tutorials/SentimentAnalysis/Program.cs @@ -5,9 +5,8 @@ using System.Linq; using Microsoft.Data.DataView; using Microsoft.ML; -using Microsoft.ML.Core.Data; using Microsoft.ML.Data; -using Microsoft.ML.Trainers.Online; +using Microsoft.ML.Trainers; using Microsoft.ML.Transforms.Text; // @@ -30,16 +29,16 @@ static void Main(string[] args) // // - (IDataView trainSet, IDataView testSet) splitDataView = LoadData(mlContext); + TrainCatalogBase.TrainTestData splitDataView = LoadData(mlContext); // // - ITransformer model = BuildAndTrainModel(mlContext, splitDataView.trainSet); + ITransformer model = BuildAndTrainModel(mlContext, splitDataView.TrainSet); // // - Evaluate(mlContext, model, splitDataView.testSet); + Evaluate(mlContext, model, splitDataView.TestSet); // // @@ -54,18 +53,18 @@ static void Main(string[] args) Console.WriteLine("=============== End of process ==============="); } - public static (IDataView trainSet, IDataView testSet) LoadData(MLContext mlContext) + public static TrainCatalogBase.TrainTestData LoadData(MLContext mlContext) { //Note that this case, loading your training data from a file, //is the easiest way to get started, but ML.NET also allows you //to load data from databases or in-memory collections. // - IDataView dataView = mlContext.Data.ReadFromTextFile(_dataPath,hasHeader:false); + IDataView dataView = mlContext.Data.LoadFromTextFile(_dataPath,hasHeader:false); // // - (IDataView trainSet, IDataView testSet) splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2); + TrainCatalogBase.TrainTestData splitDataView = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction: 0.2); // // @@ -200,12 +199,12 @@ public static void UseLoadedModelWithBatchItems(MLContext mlContext) // Load test data // - IDataView sentimentStreamingDataView = mlContext.Data.ReadFromEnumerable(sentiments); + IDataView sentimentStreamingDataView = mlContext.Data.LoadFromEnumerable(sentiments); IDataView predictions = loadedModel.Transform(sentimentStreamingDataView); // Use model to predict whether comment data is Positive (1) or Negative (0). - IEnumerable predictedResults = mlContext.CreateEnumerable(predictions, reuseRowObject: false); + IEnumerable predictedResults = mlContext.Data.CreateEnumerable(predictions, reuseRowObject: false); // // diff --git a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj index 9dd42a56de0..2c175268cab 100644 --- a/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj +++ b/machine-learning/tutorials/SentimentAnalysis/SentimentAnalysis.csproj @@ -10,7 +10,7 @@ - +