Microsoft.ML sentiment-analysis results issue with different versions of Microsoft.ML
Seems to be an issue with this sample
The sample works with Nuget Package Microsoft.ML 0.3.0 When running 0.4.0 you get different results.
That is, both results in the console are positive for 0.4.0. The first result should be negative if it is supposed to be the same as 0.3.0, which I'd assume it would.
Document details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: c8e22825-c4ee-5a36-f911-8ad456970ecc
- Version Independent ID: ad0bf640-7dfc-1184-077c-358bb14277a7
- Content: Use ML.NET in a sentiment analysis binary classification scenario
- Content Source: docs/machine-learning/tutorials/sentiment-analysis.md
- Product: dotnet-ml
- GitHub Login: @JRAlexander
- Microsoft Alias: johalex
Same issue, get both as positive even if I use exact wording from sample, i.e. 'RUDE' Also noted that if use 2.0 in csproj file get lowere percentages than if use 2.1. Tried bringing in the csproj, project.cs and SentimentData.cs files from samples on GitHub but got the same, thats how I saw the test results differ based on .net core version
Indeed, output with 0.3.0 version:
PredictionModel quality metrics evaluation
------------------------------------------
Accuracy: 66.67%
Auc: 94.44%
F1Score: 75.00%
Sentiment Predictions
---------------------
Sentiment: Please refrain from adding nonsense to Wikipedia. | Prediction: Negative
Sentiment: He is the best, and the article should say that. | Prediction: Positive
Output with 0.4.0 version:
PredictionModel quality metrics evaluation
------------------------------------------
Accuracy: 61.11%
Auc: 85.19%
F1Score: 72.00%
Sentiment Predictions
---------------------
Sentiment: Please refrain from adding nonsense to Wikipedia. | Prediction: Positive
Sentiment: He is the best, and the article should say that. | Prediction: Positive
Note, evaluation metrics values have also changed.
(I've run the code that is in the dotnet/samples repo)
It's not clear from the release notes what might have caused the observed change.
The used trainer is FastTreeBinaryClassifier. Its history shows mostly doc changes with some API update changes.
One more guess is that this commit might have introduced different behavior.
@OliaG do you know who might explain the observed difference in the tutorial output?
@GalOshri, or @sfilipi, any insight on this?
This is likely due to the datasets being very small so random fluctuations can cause large differences in metrics (the 5% difference in accuracy just means that 1 out of the 18 examples in the test data flipped).
The other factor is that the tree parameters ({ NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 }) are very small which limits the performance of the trees. When I switch to default parameters, the AUC matches between 0.3 and 0.4. The accuracy is still different but only corresponds to one example's prediction being flipped.
I tried the same pipeline on a much larger dataset and the AUC/accuracy differed by around 0.3% (which likely is still due to the relatively small tree parameters and current inability to set a random seed).
@GalOshri
This is likely due to the datasets being very small so random fluctuations can cause large differences in metrics
Is it possible to produce such random fluctuations running against the fixed version of the Microsoft.ML? What causes the results be different in versions 0.3 and 0.4, but stay stable within one version?
@GalOshri the code uses TextFeaturizer that seems to have been influenced by dotnet/machinelearning#548 (am I correct here?). So, two sets of numerical features produced by 0.3 and 0.4 versions might be different. Thus, trained models differ. Is my understanding correct?
Good point. It seems like the implementation changed which likely affects the predictions. @zeahmed, can you please verify this?
Due the change in https://github.com/dotnet/machinelearning/pull/548, one of the prediction changed.
Previously, it was 12/18 = 0.666666
Now, it is 11/18 = 0.61111
The dataset is so small to make any conclusion. However, @justinormont may help us on this. @justinormont, Do we have benchmarks for it? Can you confirm if the change in TextFeaturizer has adversely effected the benchmarks?
@zeahmed is this file considered as benchmarks? That file was changed as part of dotnet/machinelearning#548 as well.
@pkulikov, no these are just tests. Benchmarks are different.
I get more accurate results with the previous version (0.3.0) ? nothing else changed in my solution (sentiment analysis)
I tried running the solution today, it didn't work initially for me with given hyperparameters, but when I changed them to NumLeaves = 50, NumTrees = 50, MinDocumentsInLeafs = 20 , it gave me correct results.
Thanks, @mangeshw. @GalOshri - should I update the tutorial accordingly?
Running the sample code provided on GitHub on ML.net 0.5.0 causes predictions to be positive regardless of the sentiment text being provided. I've even used negative texts from the training set, still returns a positive prediction.
However, adding data from the training set to the testing set causes the accuracy to go up in the metrics output, so my theory is there's something wrong within the Predict method only.
Looks like it's just the output displaying "Positive" for negative sentiments and vice versa.. https://github.com/dotnet/samples/pull/329
Microsoft.ML 0.6.0
Accuracy: 94.44% Auc: 98.77% F1Score: 94.74%
Sentiment: Please refrain from adding nonsense to Wikipedia. | Prediction: Positive Sentiment: He is the best, and the article should say that. | Prediction: Negative
The last code snippet has error: foreach (var item in sentimentsAndPredictions) { Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Negative" : "Positive")}"); } Console.WriteLine();
Should be
foreach (var item in sentimentsAndPredictions) { Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}"); } Console.WriteLine();
PredictionModel quality metrics evaluation
Accuracy: 94.44% Auc: 98.77% F1Score: 94.74%
@bolajiniy Try taking a look at the training set, it should be pretty obvious that 0 = positive and 1 = negative. Please reference https://github.com/dotnet/samples/pull/329 Let me know if you still believe it to be an error.
Perhaps we should increase the size of the sample. The dataset is currently sized for a unit test; I don't think its small size is valuable for example code. Perhaps we should increase the size of the sample for use in the example. If the same dataset is used in unit tests, we should ensure the runtime of the unit tests are not adversely impacted. The only point of the unit tests are to see if something changed; they are not expected to make useful models.
Similar issues were mentioned in the main repo: https://github.com/dotnet/machinelearning/issues/708#issuecomment-425706329:
Try running on the full-sized dataset: https://aka.ms/tlc-resources/benchmarks/WikiDetoxAnnotated160kRows.tsv
The
wikipedia-detox-250-line-data.tsvdataset is a 250 row sample of the original 160k rows. Training on this small of a sample won't create a useful model.
If someone wants to try a run on the full dataset, this would give the upper bound for how well a specific pipeline (transforms+learner) will perform on the dataset. Running on any small sample of the dataset will do worse (plus noise). We could find an optimal trade-off between sample size (which determines runtime) and output accuracy. This would let us pick a more optimal size dataset for the example.
tried the full dataset on v0.6.0
Accuracy: 50.00 % Auc: 50.00 % F1Score: NaN
Sentiment Predictions
Sentiment: Please refrain from adding nonsense to Wikipedia. | Prediction: Positive Sentiment: He is the best, and the article should say that. | Prediction: Positive