即使我的代码在尝试执行时非常简单,但基本上我还是希望分类器模型在实际的Xcode应用程序(而非运动场)上返回其认为预测的准确性的准确性/置信度值。
我使用了一个伪造的“狗品种”分类器,例如“文本分类器”:
码:
do {
if try DogClassiferModel().prediction(text: "value").confidence >= 90 {
print("We have a high enough accuracy that this is the name of a dog breed")
}
} catch let error {
print(error)
}
我知道在Xcode的操场上,您可以使用测试数据执行此操作:
码:
let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "Path"))
let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
let testClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "recognized")
// Getting the testing evaluation.
let evaluationMetrics = testClassifier.evaluation(on: testingData)
let evaluationAccuracy = (1.0 - evaluationMetrics.classificationError) * 100
// We can print the accuracy with print(evaluationAccuracy).
想到:也许CoreML不能像我不知道的那样工作?
当你写的时候
try DogClassiferModel().prediction(text: "value")
返回的是一个DogClassiferModelOutput
对象。如果模型的输出名为confidence
,则可以通过编写以下内容获取它:
if let output = try DogClassiferModel().prediction(text: "value") {
print(output.confidence)
}
但是,Core ML中的分类器模型通常以特殊方式处理。他们可以返回得分最高的类别的标签,或者返回包含所有标签的概率的字典。
找出这对您的模型如何工作的最佳方法是,查看Xcode中的mlmodel文件,单击箭头转到自动生成的源代码文件,然后查找“ Output”类。这将具有一个或多个您可以访问的属性(如上例所示)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句