[英]Can we train CreateML on iOS device using user's data?
I understand that we can train ML model on macOS with Xcode CreateML GUI, as well as in macOS Playground.我知道我们可以在 macOS 上使用 Xcode CreateML GUI 以及 macOS Playground 训练 ML model。 The problem I had, is to train a similar model on user's device, using their own data.我遇到的问题是使用用户自己的数据在用户设备上训练类似的 model。 I'm wondering if it's possible?我想知道这是否可能?
Can we train CreateML text classifier on user's device?我们可以在用户设备上训练 CreateML 文本分类器吗? I had did some research but could not find an answer.我做了一些研究,但找不到答案。 Mostly people are talking about deploy a trained model to iOS. But I wanna to train on iOS.大多数人都在谈论将训练有素的 model 部署到 iOS。但我想在 iOS 上训练。
PsI also had a look at the updatable CoreML model. Which does not seems to support text classifier. PsI 还查看了可更新的 CoreML model。它似乎不支持文本分类器。 They only supports KNN model as well as shallow neural.network.它们仅支持 KNN model 以及浅层神经网络。
More Specifically.进一步来说。 Can we even use MLTextClassifier
this to create Model on iOS?我们甚至可以使用MLTextClassifier
在 iOS 上创建 Model 吗? The conflict information is that, on Apple's CreateML main page, it says you need to train on Mac.冲突信息是,在Apple的CreateML主页上,它说你需要在Mac上训练。 But this API seems to indicate that it supports iOS, which really confuses me.但是这个API好像表示支持iOS,真是让我很疑惑。
init(trainingData: [String : [String]], parameters: MLTextClassifier.ModelParameters)
The CreateML
module does work on iOS (since iOS 15). CreateML
模块确实适用于 iOS(自 iOS 15 起)。 It just doesn't work on iOS simulator.它只是不适用于 iOS 模拟器。
You can surround all your training code with您可以将所有训练代码包围起来
#if canImport(CreateML)
...
#endif
so that it only runs when you are on a real device.以便它仅在您使用真实设备时运行。 Admittedly, this is rather inconvenient...不可否认,这很不方便......
As for how to use the CreateML API, you can follow the guide here .至于如何使用CreateML API,可以参考这里的指南。 The code would look something like this.代码看起来像这样。 Note that I've updated some of the deprecated (since iOS 16) code in the guide to use the newest APIs.请注意,我已经更新了指南中一些已弃用(自 iOS 16 起)的代码,以使用最新的 API。
import CoreML
import CreateML
import NaturalLanguage
import TabularData
// training...
let sentimentClassifier = try MLTextClassifier(trainingData: [
"positive": [...],
"negative": [...],
"neutral": [...],
])
// write to file for later use...
let metadata = MLModelMetadata(author: "John Appleseed",
shortDescription: "A model trained to classify movie review sentiment",
version: "1.0")
try sentimentClassifier.write(to: URL(fileURLWithPath: "/path/to/save/SentimentClassifier.mlmodel"),
metadata: metadata)
// or use it immediately:
print(sentimentClassifier.prediction(from: "foo bar baz"))
//... at some later point
let model = try MLModel(contentsOf: URL(fileURLWithPath: "/path/to/save/SentimentClassifier.mlmodel"))
let nlModel = try NLModel(mlModel: model)
print(nlModel.predictedLabel(for: "foo bar baz") ?? "no label")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.