调用trainingPipeline.Fit时遇‘Method not found: Void Module.train()’错误求助
解决ML.NET文本分类训练时“Method not found: 'Void Module.train()'”错误
这个错误几乎都是TorchSharp与Microsoft.ML.TorchSharp版本不兼容导致的,结合你的代码细节,给出以下修复方案:
1. 强制统一NuGet包版本
卸载当前项目中的Microsoft.ML.TorchSharp和TorchSharp包,重新安装完全相同版本的两个包。ML.NET的TorchSharp绑定依赖特定版本的TorchSharp核心库,版本不匹配会直接引发方法找不到的反射错误。
2. 移除冗余的文本特征处理步骤
你的ProcessData方法中手动做了FeaturizeText和Concatenate,但TextClassification Trainer(基于BERT)会自动处理原始文本列的特征提取,手动预处理会和Trainer的内置逻辑冲突。修改后的ProcessData方法如下:
IEstimator<ITransformer> ProcessData() { var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(inputColumnName: "Sentiment", outputColumnName: "Label") .AppendCacheCheckpoint(_mlContext); return pipeline; }
3. 补充足够的训练数据
当前你只使用了1条训练数据,经过TrainTestSplit后训练集数据量极端不足,会导致训练流程异常。至少准备5条以上带标注的样本,比如:
var reviews = new List<ExpenseNote> { new ExpenseNote() { Note = "报销办公用品费用", Sentiment = "Approved"}, new ExpenseNote() { Note = "超出预算的招待费", Sentiment = "Rejected"}, new ExpenseNote() { Note = "差旅住宿发票齐全", Sentiment = "Approved"}, new ExpenseNote() { Note = "无对应合同的采购支出", Sentiment = "Rejected"}, new ExpenseNote() { Note = "员工加班餐费报销", Sentiment = "Approved"} };
4. 清理项目缓存并重建
删除项目目录下的bin和obj文件夹,然后重新生成项目,避免旧版本的dll缓存干扰。
修改后的完整代码示例
using System; using System.Collections.Generic; using Microsoft.ML; using Microsoft.ML.Data; using Microsoft.ML.TorchSharp; using TorchSharp; namespace ConsoleApp5 { internal class TextNLPClassifier { MLContext _mlContext; PredictionEngine<ExpenseNote, IssuePrediction> _predEngine; ITransformer _trainedModel; public void FitData() { _mlContext = new MLContext(seed: 0); // 补充足够的训练数据 var reviews = new List<ExpenseNote> { new ExpenseNote() { Note = "报销办公用品费用", Sentiment = "Approved"}, new ExpenseNote() { Note = "超出预算的招待费", Sentiment = "Rejected"}, new ExpenseNote() { Note = "差旅住宿发票齐全", Sentiment = "Approved"}, new ExpenseNote() { Note = "无对应合同的采购支出", Sentiment = "Rejected"}, new ExpenseNote() { Note = "员工加班餐费报销", Sentiment = "Approved"} }; var reviewsDV = _mlContext.Data.LoadFromEnumerable<ExpenseNote>(reviews); var pipeline = ProcessData(); BuildAndTrainModel(reviewsDV, pipeline); foreach(var rev in reviews) { var review = new ExpenseNote { Note = rev.Note }; // 创建无标注的预测样本 var prediction = _predEngine.Predict(review); Console.WriteLine($"备注: {review.Note} | 预测结果: {prediction.Sentiment}"); } } IEstimator<ITransformer> ProcessData() { var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(inputColumnName: "Sentiment", outputColumnName: "Label") .AppendCacheCheckpoint(_mlContext); return pipeline; } void BuildAndTrainModel(IDataView trainingDataView, IEstimator<ITransformer> pipeline) { var trainTestSplit = _mlContext.Data.TrainTestSplit(trainingDataView, testFraction:0.2); var trainingPipeline = pipeline.Append(_mlContext.MulticlassClassification.Trainers.TextClassification(numberOfClasses: 2, sentence1ColumnName: "Note")) .Append(_mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel")); _trainedModel = trainingPipeline.Fit(trainTestSplit.TrainSet); _predEngine = _mlContext.Model.CreatePredictionEngine<ExpenseNote, IssuePrediction>(_trainedModel); } } public class ExpenseNote { [LoadColumn(0)] public string Note { get; set; } [LoadColumn(1)] public string Sentiment { get; set; } } public class IssuePrediction { [ColumnName("PredictedLabel")] public string Sentiment; } }
内容的提问来源于stack exchange,提问作者Josh Reed
相关产品推荐
相关产品推荐

