Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift/coreml实施中的LSTM维度问题_Swift_Tensorflow_Keras_Coreml_Coremltools - Fatal编程技术网

swift/coreml实施中的LSTM维度问题

swift/coreml实施中的LSTM维度问题,swift,tensorflow,keras,coreml,coremltools,Swift,Tensorflow,Keras,Coreml,Coremltools,我使用keras和tf作为后端生成了一个用于音频分类的LSTM模型。在使用coremltools转换为.mlmodel时,我遇到了您可以看到的问题。尺寸与预期的大不相同 我在swift的xcode中使用了我的base 尤其是这个剪子,我认为这给我带来了麻烦: do { let request = try SNClassifySoundRequest(mlModel: soundClassifier.model) try analyzer.add(request, withObse

我使用keras和tf作为后端生成了一个用于音频分类的LSTM模型。在使用coremltools转换为.mlmodel时,我遇到了您可以看到的问题。尺寸与预期的大不相同

我在swift的xcode中使用了我的base

尤其是这个剪子,我认为这给我带来了麻烦:

do {
    let request = try SNClassifySoundRequest(mlModel: soundClassifier.model)
    try analyzer.add(request, withObserver: resultsObserver)
        } catch {
                print("Unable to prepare request: \(error.localizedDescription)")
                return
                }
   }
运行此模型会导致以下错误:

Invalid model, inputDescriptions.count = 5

Unable to prepare request: Invalid model, inputDescriptions.count = 5
即使在我创建模型时,我看到了规范中预期的内容:

description {
  input {
    name: "audioSamples"
    shortDescription: "Audio from microphone"
    type {
      multiArrayType {
        shape: 13
        dataType: DOUBLE
      }
    }
  }
我正在尝试将post合并到我的代码中,但我不确定如何根据我的需要格式化它。非常感谢您的建议。我可以看出MLMultiArray是我问题的关键,但我不确定:如何将适当的数据放入其中,以及如何将其推入SNClassifySoundRequest类型

keras==2.3.1
coremltools==3.3

当您使用
SNClassifySoundRequest
时,您的模型需要具有特定的结构。我不知道我头脑中的确切细节,但我认为它需要一个管道,其中第一个模型是一个内置模型,可以将音频转换为频谱图

如果您使用Keras培训您的模型,它很可能与
SNClassifySoundRequest
的要求不兼容

好消息是运行模型不需要
SNClassifySoundRequest
。只需在模型上调用
soundClassifier.prediction(…)


请注意,您需要传入输入,但也需要传入LSTM层的隐藏状态。Core ML不会自动为您管理LSTM状态(与Keras不同)。

谢谢,您为我指明了正确的方向。我也在使用它来帮助用户,看起来很好,但是我在设置模型输入时遇到了一个问题。我知道我将输入“audioSamples”输入实际的音频数据,但我要传递什么到其他LSTM层?另外,我还收到了你的书,非常好的信息<代码>保护模式输出=尝试?self.model.prediction(音频样本:audioData,lstm_1_h_in:,lstm_1_c_in:,lstm_2_h_in:,lstm_2_c_in:)或者{fatalError(“错误调用predict”)}此外,我得到的确切错误是“[coreml]验证输入失败。”这也发生在modelInput阶段,比如:
让modelInput=classyInput(音频样本:audioData,lstm_1_h_in:,lstm_1_c_in:,lstm_2_h_in:,lstm_2_c_in:)
并且我的输出是:
保护让modelOutput=try?self.model.prediction(输入:modelInput)else{fatalError(“故障预测模型”)}
您需要创建形状正确的MLMultiArray对象(取决于LSTM中隐藏单元的数量),并用初始状态填充它。通常是零或随机数。但是请注意,一个新的MLMultiArray中会有垃圾,也就是说,它是未初始化的内存,所以您需要先用零或随机数覆盖它。你也可以零分通过。