在我的Keras模型中,我在调用GRU层时传递了initial_state参数。当我想预测新的输入时,我的应用程序会跟踪encoder_state并将其传递回模型。
encoder_output, encoder_state = GRU(latent_dim,return_state=True,name='gru')(encoder_input,initial_state=state)
使用Keras进行推理时,一切都变了,但是当我尝试转换为coreML模型时,问题就来了。
0 : input_56, <keras.engine.topology.InputLayer object at 0x7f5e21159668>
1 : input_57, <keras.engine.topology.InputLayer object at 0x7f5e21159630>
2 : gru, <keras.layers.recurrent.GRU object at 0x7f5e2116a9e8>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lbai/.local/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 752, in convert
custom_conversion_functions=custom_conversion_functions)
File "/home/lbai/.local/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 550, in convertToSpec
custom_objects=custom_objects)
File "/home/lbai/.local/lib/python3.6/site-packages/coremltools/converters/keras/_keras2_converter.py", line 316, in _convert
converter_func(builder, layer, input_names, output_names, keras_layer)
File "/home/lbai/.local/lib/python3.6/site-packages/coremltools/converters/keras/_layers2.py", line 1058, in convert_gru
reverse_input = reverse_input)
File "/home/lbai/.local/lib/python3.6/site-packages/coremltools/models/neural_network/builder.py", line 1530, in add_gru
spec_layer_params.inputVectorSize = input_size
TypeError: (None, 256) has type tuple, but expected one of: int, long
当我检查Keras层的形状时,我得到了期望的结果。第一个元组是encoder_input形状,第二个元组是initial_state形状。
print(encoder.get_layer('gru').input_shape)
>>> [(None, 1, 1536), (None, 256)]
查看convert_gru的coreML代码会告诉我,它只是将input_shape中的最后一个条目作为input_size,在我的情况下为(无,256)。
def convert_gru(builder, layer, input_names, output_names, keras_layer):
...
input_size = keras_layer.input_shape[-1]
有人知道我如何将initial_state传递给我的GRU层,并让coreml为我转换它吗?