使用 CreateML 创建文本分类器模型时收到“引发错误但未捕获:提供的验证数据必须包含...”
Posted
技术标签:
【中文标题】使用 CreateML 创建文本分类器模型时收到“引发错误但未捕获:提供的验证数据必须包含...”【英文标题】:Receiving, "An error was thrown and was not caught: The validation data provided must contain ..." when creating a Text Classifier Model with CreateML 【发布时间】:2020-04-25 11:16:03 【问题描述】:我正在使用 Playground 使用 CreateML 创建文本分类器模型并不断收到错误:
Playground execution terminated: An error was thrown and was not caught:
▿ The validation data provided must contain class.
▿ type : 1 element
- reason : "The validation data provided must contain class."
我的代码比较简单,使用数据表中的两列。 textColumn 标记为“text”,labelColumn 标记为“class”:
import Cocoa
import CreateML
let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/ ... .csv"))
let(trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "class")
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")
let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100
我能发现这与 Apple 开发者文档中提供的代码之间的唯一区别是,而不是
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class")
他们的文档是:
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData)
如果我尝试使用 Apple Developer Documentation 中的行,Xcode 的 11.2.1 版会失败。
提前感谢您提供的任何帮助。
【问题讨论】:
你找到解决办法了吗? 【参考方案1】:试试这个!它对我有用
let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/justinmacbook/Desktop/twitter-sanders-apple3.csv"))
let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5 )
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "class", labelColumn: "text")
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "class", labelColumn: "text")
let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100
【讨论】:
请正确格式化代码,以便其他人(包括未来用户)实际上可读且易于理解 那行得通@PrinceRana。唯一的问题是我交换了 textColumn 和 labelColumn 标签。谢谢。【参考方案2】:这是对我有用的解决方案。我相信这个原始代码只能在 OSX 10.15+ 上运行
import Cocoa
import CreateML
import NaturalLanguage
let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/m0a04y6/Desktop/ios/ML/twitter-sanders-apple3.csv"))
let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
let parameters = MLTextClassifier.ModelParameters.init(validationData: trainingData, algorithm: MLTextClassifier.ModelAlgorithmType.maxEnt(revision: 1), language: NLLanguage.english, textColumnValidationData: "text", labelColumnValidationData: "class")
let sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "class", parameters: parameters)
let evaluationMetrics = sentimentClassifier.evaluation(on: testingData, textColumn: "text", labelColumn: "class") //Training accuracy as a percentage
let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100
print(evaluationAccuracy)
let metadata = MLModelMetadata(author: "Madhur Ahuja", shortDescription: "A model trained to classify movie review sentiment", version: "1.0")
try sentimentClassifier.write(to: URL(fileURLWithPath: "/Users/m0a04y6/Desktop/iOS/ML/sentiment.mlmodel"), metadata: metadata)
【讨论】:
以上是关于使用 CreateML 创建文本分类器模型时收到“引发错误但未捕获:提供的验证数据必须包含...”的主要内容,如果未能解决你的问题,请参考以下文章
使用 CreateML 训练图像分类器的最佳图像分辨率是多少?