我想利用 google 的 AI 平台来部署我的 keras 模型,这要求模型采用 tensorflow SavedModel 格式。我将 keras 模型保存到 tensorflow 估计器模型,然后导出这个估计器模型。我在定义我的serving_input_receiver_fn
.
这是我的模型的摘要:
Model: "model_49"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_49 (InputLayer) [(None, 400, 254)] 0
_________________________________________________________________
gru_121 (GRU) (None, 400, 64) 61248
_________________________________________________________________
gru_122 (GRU) (None, 64) 24768
_________________________________________________________________
dropout_73 (Dropout) (None, 64) 0
_________________________________________________________________
1M (Dense) (None, 1) 65
=================================================================
Total params: 86,081
Trainable params: 86,081
Non-trainable params: 0
_________________________________________________________________
这是我遇到的错误:
KeyError: "The dictionary passed into features does not have the expected
inputs keys defined in the keras model.\n\tExpected keys:
{'input_49'}\n\tfeatures keys: {'col1','col2', ..., 'col254'}
下面是我的代码。
def serving_input_receiver_fn():
feature_placeholders = {
column.name: tf.placeholder(tf.float64, [None]) for column in INPUT_COLUMNS
}
# feature_placeholders = {
# 'input_49': tf.placeholder(tf.float64, [None])
# }
features = {
key: tf.expand_dims(tensor, -1)
for key, tensor in feature_placeholders.items()
}
return tf.estimator.export.ServingInputReceiver(features, feature_placeholders)
def run():
h5_model_file = '../models/model2.h5'
json_model_file = '../models/model2.json'
model = get_keras_model(h5_model_file, json_model_file)
print(model.summary())
estimator_model = tf.keras.estimator.model_to_estimator(keras_model=model, model_dir='estimator_model')
export_path = estimator_model.export_saved_model('export',
serving_input_receiver_fn=serving_input_receiver_fn)
似乎我的模型需要一个单一的特征键:(input_49
我的神经网络的第一层),但是,例如,从我看到的代码示例中,将serving_receiver_input_fn
所有特征的字典输入到我的模型中。
我该如何解决这个问题?
我正在使用 tensorflow==2.0.0-beta1。