|
| 1 | +module BinaryClassification_SentimentAnalysis |
| 2 | + |
| 3 | +open System |
| 4 | +open System.IO |
| 5 | +open Microsoft.ML |
| 6 | +open Microsoft.ML.Data |
| 7 | +open Microsoft.ML.Models |
| 8 | +open Microsoft.ML.Runtime.Api |
| 9 | +open Microsoft.ML.Trainers |
| 10 | +open Microsoft.ML.Transforms |
| 11 | + |
| 12 | +type SentimentData() = |
| 13 | + [<Column("0")>] |
| 14 | + member val SentimentText: string = "" with get, set |
| 15 | + |
| 16 | + [<Column("1", name="Label")>] |
| 17 | + member val Sentiment : double = 0.0 with get, set |
| 18 | + |
| 19 | +type SentimentPrediction() = |
| 20 | + [<ColumnName("PredictedLabel")>] |
| 21 | + member val Sentiment : bool = false with get, set |
| 22 | + |
| 23 | +let sentiments = |
| 24 | + [| SentimentData(SentimentText = "Contoso's 11 is a wonderful experience", Sentiment = 1.0) |
| 25 | + SentimentData(SentimentText = "The acting in this movie is very bad", Sentiment = 0.0) |
| 26 | + SentimentData(SentimentText = "Joe versus the Volcano Coffee Company is a great film.", Sentiment = 1.0) |] |
| 27 | + |
| 28 | +let AppPath = Path.Combine(__SOURCE_DIRECTORY__, "../../../..") |
| 29 | +let TrainDataPath = Path.Combine(AppPath, "datasets", "sentiment-imdb-train.txt") |
| 30 | +let TestDataPath = Path.Combine(AppPath, "datasets", "sentiment-yelp-test.txt") |
| 31 | +let modelPath = Path.Combine(AppPath, "SentimentModel.zip") |
| 32 | + |
| 33 | +let TrainAsync() = |
| 34 | + // LearningPipeline holds all steps of the learning process: data, transforms, learners. |
| 35 | + let pipeline = LearningPipeline() |
| 36 | + |
| 37 | + // The TextLoader loads a dataset. The schema of the dataset is specified by passing a class containing |
| 38 | + // all the column names and their types. |
| 39 | + pipeline.Add(TextLoader(TrainDataPath).CreateFrom<SentimentData>()) |
| 40 | + |
| 41 | + // TextFeaturizer is a transform that will be used to featurize an input column to format and clean the data. |
| 42 | + pipeline.Add(TextFeaturizer("Features", "SentimentText")) |
| 43 | + |
| 44 | + // FastTreeBinaryClassifier is an algorithm that will be used to train the model. |
| 45 | + // It has three hyperparameters for tuning decision tree performance. |
| 46 | + pipeline.Add(FastTreeBinaryClassifier(NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2)) |
| 47 | + |
| 48 | + Console.WriteLine("=============== Training model ===============") |
| 49 | + // The pipeline is trained on the dataset that has been loaded and transformed. |
| 50 | + let model = pipeline.Train<SentimentData, SentimentPrediction>() |
| 51 | + |
| 52 | + // Saving the model as a .zip file. |
| 53 | + model.WriteAsync(modelPath) |> Async.AwaitTask |> Async.RunSynchronously |
| 54 | + |
| 55 | + Console.WriteLine("=============== End training ===============") |
| 56 | + Console.WriteLine(sprintf "The model is saved to %s" modelPath) |
| 57 | + |
| 58 | + model |
| 59 | + |
| 60 | +let Evaluate(model: PredictionModel<SentimentData, SentimentPrediction> ) = |
| 61 | + // To evaluate how good the model predicts values, the model is ran against new set |
| 62 | + // of data (test data) that was not involved in training. |
| 63 | + let testData = TextLoader(TestDataPath).CreateFrom<SentimentData>() |
| 64 | + |
| 65 | + // BinaryClassificationEvaluator performs evaluation for Binary Classification type of ML problems. |
| 66 | + let evaluator = BinaryClassificationEvaluator() |
| 67 | + |
| 68 | + Console.WriteLine("=============== Evaluating model ===============") |
| 69 | + |
| 70 | + let metrics = evaluator.Evaluate(model, testData) |
| 71 | + // BinaryClassificationMetrics contains the overall metrics computed by binary classification evaluators |
| 72 | + // The Accuracy metric gets the accuracy of a classifier which is the proportion |
| 73 | + //of correct predictions in the test set. |
| 74 | + |
| 75 | + // The Auc metric gets the area under the ROC curve. |
| 76 | + // The area under the ROC curve is equal to the probability that the classifier ranks |
| 77 | + // a randomly chosen positive instance higher than a randomly chosen negative one |
| 78 | + // (assuming 'positive' ranks higher than 'negative'). |
| 79 | + |
| 80 | + // The F1Score metric gets the classifier's F1 score. |
| 81 | + // The F1 score is the harmonic mean of precision and recall: |
| 82 | + // 2 * precision * recall / (precision + recall). |
| 83 | + |
| 84 | + Console.WriteLine(sprintf "Accuracy: %0.2f" metrics.Accuracy) |
| 85 | + Console.WriteLine(sprintf "Auc: %0.2f" metrics.Auc) |
| 86 | + Console.WriteLine(sprintf "F1Score: %0.2f" metrics.F1Score) |
| 87 | + Console.WriteLine("=============== End evaluating ===============") |
| 88 | + Console.WriteLine() |
| 89 | + |
| 90 | +// STEP 1: Create a model |
| 91 | +let model = TrainAsync() |
| 92 | + |
| 93 | +// STEP2: Test accuracy |
| 94 | +Evaluate(model) |
| 95 | + |
| 96 | +// STEP 3: Make a prediction |
| 97 | +let predictions = model.Predict(sentiments) |
| 98 | + |
| 99 | +for (sentiment, prediction) in Seq.zip sentiments predictions do |
| 100 | + Console.WriteLine( sprintf "Sentiment: %s | Prediction: %s sentiment" sentiment.SentimentText (if prediction.Sentiment then "Positive" else "Negative")) |
| 101 | + |
| 102 | +Console.ReadLine() |> ignore |
| 103 | + |
0 commit comments