【问题标题】:Can we train CreateML on iOS device using user's data?我们可以使用用户数据在 iOS 设备上训练 CreateML 吗?
【发布时间】:2023-01-30 15:49:52
【问题描述】:

我知道我们可以在 macOS 上使用 Xcode CreateML GUI 以及在 macOS Playground 中训练 ML 模型。我遇到的问题是使用用户自己的数据在用户设备上训练类似的模型。我想知道这是否可能?

我们可以在用户设备上训练 CreateML 文本分类器吗?我做了一些研究,但找不到答案。大多数人都在谈论将经过训练的模型部署到 iOS。但我想在 iOS 上训练。

P.s.我还查看了可更新的 CoreML 模型。其中似乎不支持文本分类器。它们只支持 KNN 模型和浅层神经网络。

进一步来说。我们甚至可以使用MLTextClassifier 在 iOS 上创建模型吗?冲突信息是,在Apple的CreateML主页上,它说你需要在Mac上训练。但是这个API好像是说支持iOS的,这让我很疑惑。

init(trainingData: [String : [String]], parameters: MLTextClassifier.ModelParameters) 

【问题讨论】:

    标签: ios swift createml


    【解决方案1】:

    CreateML模块在 iOS 上工作。它只是在 iOS 模拟器上不起作用。

    您可以将所有训练代码包围起来

    #if canImport(CreateML)
    
    ...
    
    #endif
    

    以便它仅在您使用真实设备时运行。不可否认,这很不方便......

    至于如何使用CreateML API,可以参考here的指导。代码看起来像这样。请注意,我已经更新了指南中一些已弃用(自 iOS 16 起)的代码,以使用最新的 API。

    let data = try DataFrame(contentsOfJSONFile: URL(fileURLWithPath: "/path/to/read/data.json"))
    let (trainingData, _) = data.randomSplit(by: 0.8, seed: 5)
    
    // training...
    let sentimentClassifier = try MLTextClassifier(trainingData: DataFrame(trainingData),
                                                   textColumn: "text",
                                                   labelColumn: "label")
    
    // 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")
    

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 1970-01-01
      • 2011-06-17
      • 2021-02-16
      • 2017-02-24
      • 2017-09-07
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多