swift coreML:不带“的预测函数;选项“;参数

swift coreML:不带“的预测函数;选项“;参数,swift,lstm,coreml,Swift,Lstm,Coreml,在swift中,MLM模型具有两个预测功能 func预测(来自:MLFeatureProvider)->MLFeatureProvider。根据给定的输入特征值预测输出特征值 func预测(来自:MLFeatureProvider,选项:MLPredictionOptions)->MLFeatureProvider。根据给定的输入特征值预测输出特征值 但是,在我的自动生成的MLModel类中,没有生成带有options参数的函数。下面的代码是我自动生成的预测函数 func prediction(

在swift中,MLM模型具有两个预测功能

  • func预测(来自:MLFeatureProvider)->MLFeatureProvider。根据给定的输入特征值预测输出特征值
  • func预测(来自:MLFeatureProvider,选项:MLPredictionOptions)->MLFeatureProvider。根据给定的输入特征值预测输出特征值
  • 但是,在我的自动生成的MLModel类中,没有生成带有options参数的函数。下面的代码是我自动生成的预测函数

    func prediction(input: coreML_1denses_80iters_k213_2Input) throws -> coreML_1denses_80iters_k213_2Output {
        let outFeatures = try model.prediction(from: input)
        let result = coreML_1denses_80iters_k213_2Output(output1: outFeatures.featureValue(for: "output1")!.multiArrayValue!, lstm_1_h_out: outFeatures.featureValue(for: "lstm_1_h_out")!.multiArrayValue!, lstm_1_c_out: outFeatures.featureValue(for: "lstm_1_c_out")!.multiArrayValue!)
        return result
    }
    
    func prediction(input1: MLMultiArray, input2: MLMultiArray, lstm_1_h_in: MLMultiArray?, lstm_1_c_in: MLMultiArray?) throws -> coreML_1denses_80iters_k213_2Output {
        let input_ = coreML_1denses_80iters_k213_2Input(input1: input1, input2: input2, lstm_1_h_in: lstm_1_h_in, lstm_1_c_in: lstm_1_c_in)
        return try self.prediction(input: input_)
    }
    
    注: 顺便说一句,我想找到带有“options”参数的预测函数的原因是以下错误消息:

    [coreml] Cannot evaluate a sequence of length 600, which is longer than maximum of 400.
    

    我发现了一个,它在预测函数中添加了一个forceCPU标志。该选项可在名为“usesCPUOnly”的中找到。但是,我找不到放置选项的位置。

    一种方法是在自动生成类的
    扩展名中添加您自己的预测方法(在不同的源文件中)。

    谢谢@Matthijs Hollemans。我找到了解决办法。只需编写我自己的扩展并像这样重写预测函数

    func prediction(input: model_1denses_50iters_k213Input) throws -> model_1denses_50iters_k213Output {
        let options = MLPredictionOptions()
        options.usesCPUOnly = true
        let outFeatures = try model.prediction(from: input, options:options)
        let result = model_1denses_50iters_k213Output(output1: outFeatures.featureValue(for: "output1")!.multiArrayValue!, lstm_85_h_out: outFeatures.featureValue(for: "lstm_85_h_out")!.multiArrayValue!, lstm_85_c_out: outFeatures.featureValue(for: "lstm_85_c_out")!.multiArrayValue!)
        return result
    }