Sentinemt Analysis - ML prediction wrong
System information
- OS version/distro: Windows 10 Pro x64
- .NET Version (eg., dotnet --info): .NET version 4.7/ C# 7.2
Issue
I used the "sentiment analysis" tutorial and wrote the console application as described. It seems to be working, but the predictions are wrong. The model predicts "positive" every time. I used the training and testing data from github of course.
Source code / logs
`using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.ML; using Microsoft.ML.Data; using Microsoft.ML.Models; using Microsoft.ML.Runtime.Api; using Microsoft.ML.Trainers; using Microsoft.ML.Transforms;
namespace MyML { class Program{
//Variables for training and testing
static readonly string _dataPath = 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 _modelpath = Path.Combine(Environment.CurrentDirectory, "Data", "Model.zip");
//starting the program
static async Task Main(string[] args){
var model = await Train();
Evaluate(model);
Predict(model);
//keeps the console open until enter is pressed
Console.WriteLine("Done. Press 'Enter' to exit.");
Console.ReadLine();
}
//Creating and training the model. Writes learned data into a .zip file
public static async Task<PredictionModel<SentimentData, SentimentPrediction>> Train(){
var pipeline = new LearningPipeline();
pipeline.Add(new TextLoader(_dataPath).CreateFrom<SentimentData>());
pipeline.Add(new TextFeaturizer("Features", "SentimentText"));
pipeline.Add(new FastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 });
PredictionModel<SentimentData, SentimentPrediction> model =
pipeline.Train<SentimentData, SentimentPrediction>();
await model.WriteAsync(_modelpath);
return model;
}
//Evaluating data after model training
public static void Evaluate(PredictionModel<SentimentData, SentimentPrediction> model){
var testData = new TextLoader(_testDataPath).CreateFrom<SentimentData>();
var evaluator = new BinaryClassificationEvaluator();
BinaryClassificationMetrics metrics = evaluator.Evaluate(model, testData);
//Console user information
Console.WriteLine();
Console.WriteLine("PredictionModel quality metrics evaluation");
Console.WriteLine("------------------------------------------");
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"Auc: {metrics.Auc:P2}");
Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
}
//Method for sentiment prediction. Use after training & evaluation
public static void Predict(PredictionModel<SentimentData, SentimentPrediction> model){
//Small testdata for prediction
IEnumerable<SentimentData> sentiments = new[] {
new SentimentData{
SentimentText = "Please refrain from adding nonsense to Wikipedia."},
new SentimentData{
SentimentText = "He is the best, and the article should say that."}
};
IEnumerable<SentimentPrediction> predictions = model.Predict(sentiments);
//User information header
Console.WriteLine();
Console.WriteLine("Sentiment Predictions");
Console.WriteLine("---------------------");
var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));
//Write prediction to console for the user
//Error! The model predicts "Positive" every time!
foreach (var item in sentimentsAndPredictions)
{
Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}");
}
Console.WriteLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ML.Runtime.Api;
namespace MyML { public class SentimentData { [Column(ordinal: "0", name: "Label")] public float Sentiment; [Column(ordinal: "1")] public string SentimentText; }
public class SentimentPrediction
{
[ColumnName("PredictedLabel")]
public bool Sentiment;
}
} `
I have the same issue! I've just copied the code of Sentiment analysis, and the predictions are always "Positive" independent of the SentimentText
I'd like to chime in on this, the predictions are always "Positive" even when I'm using negative sentiment texts from the training set itself.
I'm using ML.Net 0.5.0 to run the sample code provided on GitHub.
dotnet --info: .NET Core SDK (reflecting any global.json): Version: 2.1.401 Commit: 91b1c13032
Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64
I am running on exact same platform and specifications and I can confirm all my predictions are return "Positive" as well, even the negative ones from the training set.
dotnet --info .NET Core SDK (reflecting any global.json): Version: 2.1.402 Commit: 3599f217f4
Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.402\
Try running on the full-sized dataset: http://aka.ms/tlc-resources/benchmarks/WikiDetoxAnnotated160kRows.tsv
The wikipedia-detox-250-line-data.tsv dataset is a 250 row sample of the original 160k rows. Training on this small of a sample won't create a useful model.
If there aren't any more questions related to this topic I will assume that training on the larger dataset worked, and close the issue in the next couple of days.
I am closing this issue in favor of the above.