Microsoft ML 情绪分析打印不正确的预测结果?
Microsoft ML Sentiment Analysis printing incorrect prediction results?
我使用 C# 和 Microsoft ML 库构建了一个文本分析模型。 Microsoft 提供的数据集擅长预测某些评论字符串的值,例如 Batteries not included
,它会打印负值,No batteries
也会打印负值预测值。但是,我已经针对 Not bad
和 This is really bad
等值对其进行了测试,它为两者打印了 Positive
的预测值,这是不正确的。是否有更大的数据集文本文件可用于提高模型的准确性。
我从 Microsoft 文档中实现了情绪分析教程。
用于训练文本分析模型的数据集非常小60kb
。数据集名称是 yelp_labelled.txt
。它包含示例语句,每个语句的值为 0(负)或 1(正)。在哪里可以找到更大的数据集来训练我的文本分析预测?
我使用的代码如下
using AnalysisSentiment;
using Microsoft.ML;
using Microsoft.ML.Data;
using static Microsoft.ML.DataOperationsCatalog;
//create a field to hold the data file
string _dataPath = "yelp_labelled.txt";
//initialize the context
MLContext mlContext = new MLContext();
TrainTestData splitDataView = LoadData(mlContext);
ITransformer model = BuildAndTrainModel(mlContext, splitDataView.TrainSet);
Evaluate(mlContext, model, splitDataView.TestSet);
UseModelWithSingleItem(mlContext, model);
TrainTestData LoadData(MLContext mlContext)
{
IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(_dataPath, hasHeader: false);
TrainTestData splitDataView = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
return splitDataView;
}
ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet)
{
var estimator = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText))
.Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"));
Console.WriteLine("=============== Create and Train the Model ===============");
var model = estimator.Fit(splitTrainSet);
Console.WriteLine("=============== End of training ===============");
Console.WriteLine();
return model;
}
void Evaluate(MLContext mlContext, ITransformer model, IDataView splitTestSet)
{
Console.WriteLine("=============== Evaluating Model accuracy with Test data===============");
IDataView predictions = model.Transform(splitTestSet);
CalibratedBinaryClassificationMetrics metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");
Console.WriteLine();
Console.WriteLine("Model quality metrics evaluation");
Console.WriteLine("--------------------------------");
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"Auc: {metrics.AreaUnderRocCurve:P2}");
Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
Console.WriteLine("=============== End of model evaluation ===============");
}
void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
{
PredictionEngine<SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
SentimentData sampleStatement = new SentimentData
{
SentimentText = "not bad"
};
var resultPrediction = predictionFunction.Predict(sampleStatement);
Console.WriteLine();
Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");
Console.WriteLine();
Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");
Console.WriteLine("=============== End of Predictions ===============");
Console.WriteLine();
}
- 迁移学习: 因为你的数据集很低,最好的方法是做
pre-training 关于 IMBD 电影评论等情感数据集,然后
fine-tune 在你的数据集上。
- 但是,您使用的模型是
不支持 pre-training 和
fine-tuning。所以你将不得不改变你的下划线 ML 模型
到深度学习模型。
- 添加更多相似数据:如果无法更改下划线
逻辑回归模型,那么你可以尝试添加 IMDB 数据集到
您的数据集并从头开始训练,看看您的模型是否测试性能
改善。它可能有效,因为 IMDB 是一个 two-class(正面和负面)数据集,它看起来与您的数据集非常相似。
我使用 C# 和 Microsoft ML 库构建了一个文本分析模型。 Microsoft 提供的数据集擅长预测某些评论字符串的值,例如 Batteries not included
,它会打印负值,No batteries
也会打印负值预测值。但是,我已经针对 Not bad
和 This is really bad
等值对其进行了测试,它为两者打印了 Positive
的预测值,这是不正确的。是否有更大的数据集文本文件可用于提高模型的准确性。
我从 Microsoft 文档中实现了情绪分析教程。
用于训练文本分析模型的数据集非常小60kb
。数据集名称是 yelp_labelled.txt
。它包含示例语句,每个语句的值为 0(负)或 1(正)。在哪里可以找到更大的数据集来训练我的文本分析预测?
我使用的代码如下
using AnalysisSentiment;
using Microsoft.ML;
using Microsoft.ML.Data;
using static Microsoft.ML.DataOperationsCatalog;
//create a field to hold the data file
string _dataPath = "yelp_labelled.txt";
//initialize the context
MLContext mlContext = new MLContext();
TrainTestData splitDataView = LoadData(mlContext);
ITransformer model = BuildAndTrainModel(mlContext, splitDataView.TrainSet);
Evaluate(mlContext, model, splitDataView.TestSet);
UseModelWithSingleItem(mlContext, model);
TrainTestData LoadData(MLContext mlContext)
{
IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(_dataPath, hasHeader: false);
TrainTestData splitDataView = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
return splitDataView;
}
ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet)
{
var estimator = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText))
.Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"));
Console.WriteLine("=============== Create and Train the Model ===============");
var model = estimator.Fit(splitTrainSet);
Console.WriteLine("=============== End of training ===============");
Console.WriteLine();
return model;
}
void Evaluate(MLContext mlContext, ITransformer model, IDataView splitTestSet)
{
Console.WriteLine("=============== Evaluating Model accuracy with Test data===============");
IDataView predictions = model.Transform(splitTestSet);
CalibratedBinaryClassificationMetrics metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");
Console.WriteLine();
Console.WriteLine("Model quality metrics evaluation");
Console.WriteLine("--------------------------------");
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"Auc: {metrics.AreaUnderRocCurve:P2}");
Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
Console.WriteLine("=============== End of model evaluation ===============");
}
void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
{
PredictionEngine<SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
SentimentData sampleStatement = new SentimentData
{
SentimentText = "not bad"
};
var resultPrediction = predictionFunction.Predict(sampleStatement);
Console.WriteLine();
Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");
Console.WriteLine();
Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");
Console.WriteLine("=============== End of Predictions ===============");
Console.WriteLine();
}
- 迁移学习: 因为你的数据集很低,最好的方法是做 pre-training 关于 IMBD 电影评论等情感数据集,然后 fine-tune 在你的数据集上。
- 但是,您使用的模型是 不支持 pre-training 和 fine-tuning。所以你将不得不改变你的下划线 ML 模型 到深度学习模型。
- 添加更多相似数据:如果无法更改下划线 逻辑回归模型,那么你可以尝试添加 IMDB 数据集到 您的数据集并从头开始训练,看看您的模型是否测试性能 改善。它可能有效,因为 IMDB 是一个 two-class(正面和负面)数据集,它看起来与您的数据集非常相似。