我有一个简单的Tensorflow支持的Keras模型,如下所示:

x_test = np.zeros((len(data) - max_samples, max_len, max_features), dtype=np.bool)
y_test = np.zeros((len(data) - max_samples, max_len, max_features), dtype=np.bool)

#... Populate x_test and y_test

model = Sequential()
model.add(LSTM(32, batch_input_shape = (1, max_len, max_features), return_sequences=True, stateful=True))
model.add(Dense(max_features, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.01),  metrics=['accuracy'])

在使用形状输入 (1, max_len, max_features) 进行预测时,它在python中工作正常

但是,当使用 coremltools 导出到CoreML模型时,输入完全不同,如下所示:

enter image description here

它要求3个双打阵列 . 不知道为什么以及我的初始形状的映射是什么 .

Update:

我尝试按照以下方式为每个迭代输入 lstm_1_h_inlstm_1_c_in 的模型权重,但是在这种情况下,模型返回 nan .

Model* model = [Model new];
MLMultiArray* h = [[MLMultiArray alloc] initWithShape:@[@(neurons)]
                                             dataType:MLMultiArrayDataTypeDouble
                                                error:&error];
MLMultiArray* c = [[MLMultiArray alloc] initWithShape:@[@(neurons)]
                                         dataType:MLMultiArrayDataTypeDouble
                                            error:&error];
NSAssert(error == nil, assertMessage);

for (NSUInteger i = 0; i < neurons; i++) {
    [h setObject:@(0) forKeyedSubscript:@[@(i)]];
    [c setObject:@(0) forKeyedSubscript:@[@(i)]];
}

ModelOutput* modelOutput = nil;
for (NSUInteger i = 0; i < data.count; i++) {

    MLMultiArray* input = [[MLMultiArray alloc] initWithShape:@[@(modelZCount)]
                                                         dataType:MLMultiArrayDataTypeDouble
                                                            error:&error];
    NSAssert(error == nil, assertMessage);

    NSArray* key = @[data[i]];
    [input setObject:@(1) forKeyedSubscript:key];

    ModelInput* modelInput = [[ModelInput alloc] initWithInput1:input
                                                    lstm_1_h_in:h
                                                    lstm_1_c_in:c];

    modelOutput = [model predictionFromFeatures:modelInput error:&error];
    assertMessage = @"Failed to predict based on settings";
    NSAssert(error == nil, assertMessage);
    NSAssert(modelOutput != nil, assertMessage);

    h = modelOutput.lstm_1_h_out;
    c = modelOutput.lstm_1_c_out;
}

//modelOutput.output1 is @[nan, nan, nan, ...]
//same for lstm_1_h_in and lstm_1_c_in