我有一个简单的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)
但是,使用coremltools
导出到CoreML模型时,输入完全不同,如下所示:
它要求3个双打阵列。不知道为什么以及我的初始形状的映射是什么。
更新
我尝试在每次迭代中为lstm_1_h_in
和lstm_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