swift 创建ML文本分类器概率

sshcrbum  于 2023-02-03  发布在  Swift
关注(0)|答案(4)|浏览(175)

我正在用Create ML创建模型。我正在使用JSON文件。

let data = try MLDataTable(contentsOf: URL(fileURLWithPath: "poems.json"))
let (trainingData , testingData) = data.randomSplit(by: 0.8, seed: 0)

let classifier = try MLRegressor(trainingData: data, targetColumn: "author", featureColumns: ["text"])
let metadata = MLModelMetadata(author: "ffffff", shortDescription: "sdhkgfjhsgdjfhgs", license: nil, version: "1.0", additional: nil)

try classifier.write(to: URL(fileURLWithPath: "poems.mlmodel"), metadata: metadata)

JSON文件如下所示

{"title":"When You Are Old",
 "author":"William Butler Yeats",
 "text":"When you are old and grey and full of sleep,\nAnd nodding by the   fire, take down this book,\nAnd slowly read, and dream of the soft look\nYour eyes had once, and of their shadows deep;\nHow many loved your moments of glad grace,\nAnd loved your beauty with love false or true,\nBut one man loved the pilgrim Soul in you,\nAnd loved the sorrows of your changing face;\nAnd bending down beside the glowing bars,\nMurmur, a little sadly, how Love fled\nAnd paced upon the mountains overhead\nAnd hid his face amid a crowd of stars."}

遵循教程,我试图做一个文本检测的“文本”,并返回可能的“作者”
我可以做到,但我也想知道概率。
创建模型与创建ML,作为一个文本分类器我只得到输出标签:作者.有没有一种方法可以在创建ML的同时,在文本分类中也具有概率?
谢谢

pbwdgjma

pbwdgjma1#

这似乎不可能与当前的MLTextClassifier API。
如果在Netron中打开mlmodel文件https://github.com/lutzroeder/Netron/,它会显示模型产生的输出,我猜它只给出了类,而不是概率。

e5njpo68

e5njpo682#

我想MLTextClassifier API还不支持它,但是您可以在创建模型时使用更通用(总体上不太准确)的MLClassifier从模型中获得概率。

3qpi33ja

3qpi33ja3#

在XCode平台中创建的MLModel是一个文本分类器,它只返回一个标签(String),而Apple在Github(https://apple.github.io/turicreate/docs/userguide/text_classifier/)上的例子是一个Pipeline分类器,它使用turicreate python库来创建模型,它返回一个标签及其概率(String -〉Double)。

dgiusagp

dgiusagp4#

是的,使用NatrualLanguage框架将MLModel转换为NLModel,然后获得信心。

import CoreML
import NaturalLanguage

let model = try my_model(configuration: MLModelConfiguration()).model
let text = "Hello, World"
let nlmodel = try NLModel(mlModel: model)
let prediction = nlmodel.predictedLabel(for: text)
let predictionSet = nlmodel.predictedLabelHypotheses(for: text, maximumCount: 1)
let confidence = predictionSet["1"] ?? 0.0
print(text + "," + "\(confidence)")

相关问题