如何在 Swift 5 中从 MLModel(机器学习模型)获得预测的置信度
How to get the confidence of a prediction from a MLModel (Machine Learning Model) in Swift 5
尽管我的代码在尝试做的事情上非常简单,但基本上我想让分类器模型 return 一个 accuracy/confidence 值来表示它认为它的预测有多准确,请根据实际 Xcode 申请(不是 playground)。
我正在使用虚构的狗品种分类器,它是文本分类器作为示例:
代码:
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")
returned 是一个 DogClassiferModelOutput
对象。如果你的模型的输出命名为confidence
,你可以这样写:
if let output = try DogClassiferModel().prediction(text: "value") {
print(output.confidence)
}
然而,classCore ML 中的 ifier 模型通常以特殊方式处理。他们可以 return 得分最高的标签 class,或者包含所有标签概率的字典。
了解它如何适用于您的模型的最佳方法是查看 Xcode 中的 mlmodel 文件,单击箭头转到自动生成的源代码文件,然后查找 "Output" class。这将具有一个或多个您可以访问的属性(如上例所示)。
她的也是另一种方法。
您可以通过以下代码获得置信度百分比
let probs = output.classLabelProbs[output.classLabel]
self.confidence = probs ?? 0.0 //Coalesce using '??' to provide a default when the optional value contains 'nil'
尽管我的代码在尝试做的事情上非常简单,但基本上我想让分类器模型 return 一个 accuracy/confidence 值来表示它认为它的预测有多准确,请根据实际 Xcode 申请(不是 playground)。
我正在使用虚构的狗品种分类器,它是文本分类器作为示例:
代码:
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")
returned 是一个 DogClassiferModelOutput
对象。如果你的模型的输出命名为confidence
,你可以这样写:
if let output = try DogClassiferModel().prediction(text: "value") {
print(output.confidence)
}
然而,classCore ML 中的 ifier 模型通常以特殊方式处理。他们可以 return 得分最高的标签 class,或者包含所有标签概率的字典。
了解它如何适用于您的模型的最佳方法是查看 Xcode 中的 mlmodel 文件,单击箭头转到自动生成的源代码文件,然后查找 "Output" class。这将具有一个或多个您可以访问的属性(如上例所示)。
她的也是另一种方法。 您可以通过以下代码获得置信度百分比
let probs = output.classLabelProbs[output.classLabel]
self.confidence = probs ?? 0.0 //Coalesce using '??' to provide a default when the optional value contains 'nil'