创建ML文本分类器概率

时间:2018-09-07 14:03:15

标签: swift text-classification coreml createml

我正在使用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作为文本分类器创建模型,我仅输出标签:作者。使用Create ML是否有办法在文本分类中也具有概率?

谢谢

3 个答案:

答案 0 :(得分:2)

当前的MLTextClassifier API似乎无法实现。

如果您在Netron https://github.com/lutzroeder/Netron/中打开mlmodel文件,它将显示该模型产生的输出。我的猜测是,它只是给阶级,而不是概率。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在XCode游乐场中创建的MLModel是仅返回标签(字符串)的文本分类器。 苹果公司在Github(https://apple.github.io/turicreate/docs/userguide/text_classifier/)上使用turicreate python库创建模型的示例是Pipeline分类器,它返回标签及其概率(字符串-> Double)。